Switched UI Label to use localized strings.

This commit is contained in:
2022-12-16 08:45:14 -08:00
parent b17592f4fd
commit 71dbf6e646
10 changed files with 70 additions and 43 deletions

View File

@ -50,6 +50,17 @@ static inline void assertTrue(bool_t x) {}
* Asserts a function as being deprecated. * Asserts a function as being deprecated.
*/ */
void assertDeprecated(); void assertDeprecated();
/**
* Asserts that a given map has a key.
*
* @param map Map to check.
* @param key Key to try and assert exists.
*/
template<typename K, typename V>
void assertMapHasKey(std::map<K,V> map, K key) {
assertTrue(map.find(key) != map.end());
}
#else #else
#define assertTrue assert #define assertTrue assert

View File

@ -72,6 +72,8 @@ void LanguageAsset::updateAsync() {
std::string LanguageAsset::getValue(std::string key) { std::string LanguageAsset::getValue(std::string key) {
assertTrue(this->state == 0x07); assertTrue(this->state == 0x07);
assertMapHasKey(this->values, key);
assertTrue(this->values[key].begin >= 0 && this->values[key].length > 0);
// Get the positions // Get the positions
struct AssetLanguageValue value = this->values[key]; struct AssetLanguageValue value = this->values[key];

View File

@ -4,11 +4,14 @@
// https://opensource.org/licenses/MIT // https://opensource.org/licenses/MIT
#include "UILabel.hpp" #include "UILabel.hpp"
#include "game/DawnGame.hpp"
using namespace Dawn; using namespace Dawn;
UILabel::UILabel(UICanvas *canvas) : UIComponent(canvas) { UILabel::UILabel(UICanvas *canvas) : UIComponent(canvas) {
getGame()->localeManager.eventLanguageUpdated.addListener(
this, &UILabel::onLanguageUpdated
);
} }
void UILabel::updatePositions() { void UILabel::updatePositions() {
@ -17,15 +20,15 @@ void UILabel::updatePositions() {
} }
void UILabel::updateMesh() { void UILabel::updateMesh() {
if(!this->needsRebuffering) return; if(!this->needsRebuffering || !this->hasText) return;
if(this->font == nullptr || !this->font->isReady()) return; if(this->font == nullptr || !this->font->isReady()) return;
if(this->text.size() == 0) return;
float_t width = this->width; float_t width = this->width;
if(width == 0) width = -1; if(width == 0) width = -1;
std::string text = this->getGame()->localeManager.getString(key);
this->font->buffer( this->font->buffer(
this->text, text,
this->fontSize, this->fontSize,
width, width,
&this->mesh, &this->mesh,
@ -35,17 +38,17 @@ void UILabel::updateMesh() {
this->needsRebuffering = false; this->needsRebuffering = false;
} }
std::string UILabel::getText() {
return this->text;
}
void UILabel::setFont(Font *font) { void UILabel::setFont(Font *font) {
this->font = font; this->font = font;
this->needsRebuffering = true; this->needsRebuffering = true;
} }
void UILabel::setText(std::string text) { void UILabel::setText(std::string key) {
this->text = text; this->key = key;
this->hasText = false;
if(key.size() > 0 && this->getGame()->localeManager.getString(key).size()>0){
this->hasText = true;
}
this->needsRebuffering = true; this->needsRebuffering = true;
} }
@ -65,7 +68,7 @@ float_t UILabel::getContentHeight() {
} }
void UILabel::drawSelf(UIShader *shader, glm::mat4 selfTransform) { void UILabel::drawSelf(UIShader *shader, glm::mat4 selfTransform) {
if(this->font == nullptr || this->text.size() == 0) return; if(this->font == nullptr || !this->hasText) return;
this->updateMesh(); this->updateMesh();
shader->setUIColor(this->textColor); shader->setUIColor(this->textColor);
@ -83,4 +86,17 @@ void UILabel::setTransform(
) { ) {
this->needsRebuffering = true; this->needsRebuffering = true;
UIComponent::setTransform(xAlign, yAlign, alignment, z); UIComponent::setTransform(xAlign, yAlign, alignment, z);
}
void UILabel::onLanguageUpdated() {
this->needsRebuffering = true;
if(key.size() > 0 && this->getGame()->localeManager.getString(key).size()>0){
this->hasText = true;
}
}
UILabel::~UILabel() {
getGame()->localeManager.eventLanguageUpdated.removeListener(
this, &UILabel::onLanguageUpdated
);
} }

View File

@ -14,11 +14,15 @@ namespace Dawn {
Mesh mesh; Mesh mesh;
bool_t needsRebuffering = true; bool_t needsRebuffering = true;
Font *font = nullptr; Font *font = nullptr;
std::string text = ""; std::string key = "";
float_t fontSize = 10.0f; float_t fontSize = 10.0f;
bool_t hasText = false;
void updatePositions() override; void updatePositions() override;
/** Event for when the language strings are updated */
void onLanguageUpdated();
public: public:
struct FontMeasure measure; struct FontMeasure measure;
int32_t startQuad = 0; int32_t startQuad = 0;
@ -43,13 +47,6 @@ namespace Dawn {
*/ */
void updateMesh(); void updateMesh();
/**
* Returns the current text that the label has.
*
* @return The current label string.
*/
std::string getText();
/** /**
* Set the font to use for the label. * Set the font to use for the label.
* *
@ -60,9 +57,9 @@ namespace Dawn {
/** /**
* Sets the text for the label to use. * Sets the text for the label to use.
* *
* @param text Text for the label to use. * @param key Localzied string key for the label to use.
*/ */
void setText(std::string text); void setText(std::string key);
/** /**
* Sets / Updates the font size for the label. * Sets / Updates the font size for the label.
@ -71,5 +68,6 @@ namespace Dawn {
*/ */
void setFontSize(float_t fontSize); void setFontSize(float_t fontSize);
~UILabel();
}; };
} }

View File

@ -9,15 +9,15 @@ using namespace Dawn;
VisualNovelTextboxEvent::VisualNovelTextboxEvent( VisualNovelTextboxEvent::VisualNovelTextboxEvent(
VisualNovelManager *manager, VisualNovelManager *manager,
std::string text std::string key
) : IVisualNovelEvent(manager) { ) : IVisualNovelEvent(manager) {
this->text = text; this->key = key;
} }
void VisualNovelTextboxEvent::onStart(IVisualNovelEvent *previous) { void VisualNovelTextboxEvent::onStart(IVisualNovelEvent *previous) {
if(this->manager->textBox == nullptr) return; if(this->manager->textBox == nullptr) return;
this->manager->textBox->setText(this->text); this->manager->textBox->setText(this->key);
this->manager->textBox->show(); this->manager->textBox->show();
this->hasSetText = true; this->hasSetText = true;
} }
@ -26,7 +26,7 @@ bool_t VisualNovelTextboxEvent::onUpdate() {
if(this->manager->textBox == nullptr) return true; if(this->manager->textBox == nullptr) return true;
if(!this->hasSetText) { if(!this->hasSetText) {
this->manager->textBox->setText(this->text); this->manager->textBox->setText(this->key);
this->manager->textBox->show(); this->manager->textBox->show();
this->hasSetText = true; this->hasSetText = true;
} }

View File

@ -9,7 +9,7 @@
namespace Dawn { namespace Dawn {
class VisualNovelTextboxEvent : public IVisualNovelEvent { class VisualNovelTextboxEvent : public IVisualNovelEvent {
protected: protected:
std::string text; std::string key;
bool_t hasSetText = false; bool_t hasSetText = false;
void onStart(IVisualNovelEvent *previous) override; void onStart(IVisualNovelEvent *previous) override;

View File

@ -147,14 +147,14 @@ void VisualNovelTextbox::setFont(Font *font) {
this->label.updateMesh(); this->label.updateMesh();
} }
void VisualNovelTextbox::setText(std::string text, float_t fontSize) { void VisualNovelTextbox::setText(std::string key, float_t fontSize) {
this->label.setText(text); this->label.setText(key);
this->label.setFontSize(fontSize); this->label.setFontSize(fontSize);
this->label.updateMesh(); this->label.updateMesh();
} }
void VisualNovelTextbox::setText(std::string text) { void VisualNovelTextbox::setText(std::string key) {
this->label.setText(text); this->label.setText(key);
this->label.updateMesh(); this->label.updateMesh();
} }

View File

@ -70,17 +70,17 @@ namespace Dawn {
/** /**
* Sets the string (label) for this textbox. * Sets the string (label) for this textbox.
* *
* @param text Text to set. * @param text Localized string key to set.
* @param fontSize Font size of the string. * @param fontSize Font size of the string.
*/ */
void setText(std::string text, float_t fontSize); void setText(std::string key, float_t fontSize);
/** /**
* Sets the string (label) for this textbox. * Sets the string (label) for this textbox.
* *
* @param text Text to set. * @param text Localized string key to set.
*/ */
void setText(std::string text); void setText(std::string key);
/** /**
* Sets the font size to use. * Sets the font size to use.

View File

@ -41,9 +41,9 @@ namespace Dawn {
); );
start start
->then(new VisualNovelTextboxEvent(vnManager, "Starting Game")) ->then(new VisualNovelTextboxEvent(vnManager, "test"))
->then(new PokerNewGameEvent(vnManager)) ->then(new PokerNewGameEvent(vnManager))
->then(new VisualNovelTextboxEvent(vnManager, "Game Started")) ->then(new VisualNovelTextboxEvent(vnManager, "test"))
->then(new PokerInitialEvent(vnManager)) ->then(new PokerInitialEvent(vnManager))
; ;

View File

@ -37,7 +37,7 @@ PokerPlayerDisplay::PokerPlayerDisplay(UICanvas *canvas) :
// Player Name // Player Name
this->borderInner.addChild(&this->labelName); this->borderInner.addChild(&this->labelName);
this->labelName.setText("Player Name"); this->labelName.setText("undefined");
this->labelName.setFont(&this->font->font); this->labelName.setFont(&this->font->font);
this->labelName.setFontSize(40); this->labelName.setFontSize(40);
this->labelName.setTransform( this->labelName.setTransform(
@ -69,7 +69,7 @@ void PokerPlayerDisplay::setPlayer(PokerPlayer *player) {
player->eventChipsChanged.addListener(this, &PokerPlayerDisplay::onPlayerChipsChanged); player->eventChipsChanged.addListener(this, &PokerPlayerDisplay::onPlayerChipsChanged);
this->labelName.setText("Name"); this->labelName.setText("undefined");
this->animChips.clear(); this->animChips.clear();
this->animChips.restart(); this->animChips.restart();
this->animChipsValue = player->chips; this->animChipsValue = player->chips;
@ -80,11 +80,11 @@ void PokerPlayerDisplay::setPlayer(PokerPlayer *player) {
void PokerPlayerDisplay::onSceneUpdate() { void PokerPlayerDisplay::onSceneUpdate() {
this->animChips.tick(getGame()->timeManager.delta); this->animChips.tick(getGame()->timeManager.delta);
std::stringstream stream; // std::stringstream stream;
stream.precision(0); // stream.precision(0);
stream << "$"; // stream << "$";
stream << this->animChipsValue; // stream << this->animChipsValue;
this->labelChips.setText(stream.str()); this->labelChips.setText("undefined");
} }
void PokerPlayerDisplay::onPlayerChipsChanged() { void PokerPlayerDisplay::onPlayerChipsChanged() {