Switched UI Label to use localized strings.
This commit is contained in:
		@@ -50,6 +50,17 @@ static inline void assertTrue(bool_t x) {}
 | 
			
		||||
   * Asserts a function as being deprecated.
 | 
			
		||||
   */
 | 
			
		||||
  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
 | 
			
		||||
 | 
			
		||||
  #define assertTrue assert
 | 
			
		||||
 
 | 
			
		||||
@@ -72,6 +72,8 @@ void LanguageAsset::updateAsync() {
 | 
			
		||||
 | 
			
		||||
std::string LanguageAsset::getValue(std::string key) {
 | 
			
		||||
  assertTrue(this->state == 0x07);
 | 
			
		||||
  assertMapHasKey(this->values, key);
 | 
			
		||||
  assertTrue(this->values[key].begin >= 0 && this->values[key].length > 0);
 | 
			
		||||
 | 
			
		||||
  // Get the positions
 | 
			
		||||
  struct AssetLanguageValue value = this->values[key];
 | 
			
		||||
 
 | 
			
		||||
@@ -4,11 +4,14 @@
 | 
			
		||||
// https://opensource.org/licenses/MIT
 | 
			
		||||
 | 
			
		||||
#include "UILabel.hpp"
 | 
			
		||||
#include "game/DawnGame.hpp"
 | 
			
		||||
 | 
			
		||||
using namespace Dawn;
 | 
			
		||||
 | 
			
		||||
UILabel::UILabel(UICanvas *canvas) : UIComponent(canvas) {
 | 
			
		||||
 | 
			
		||||
  getGame()->localeManager.eventLanguageUpdated.addListener(
 | 
			
		||||
    this, &UILabel::onLanguageUpdated
 | 
			
		||||
  );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void UILabel::updatePositions() {
 | 
			
		||||
@@ -17,15 +20,15 @@ void UILabel::updatePositions() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void UILabel::updateMesh() {
 | 
			
		||||
  if(!this->needsRebuffering) return;
 | 
			
		||||
  if(!this->needsRebuffering || !this->hasText) return;
 | 
			
		||||
  if(this->font == nullptr || !this->font->isReady()) return;
 | 
			
		||||
  if(this->text.size() == 0) return;
 | 
			
		||||
 | 
			
		||||
  float_t width = this->width;
 | 
			
		||||
  if(width == 0) width = -1;
 | 
			
		||||
 | 
			
		||||
  std::string text = this->getGame()->localeManager.getString(key);
 | 
			
		||||
  this->font->buffer(
 | 
			
		||||
    this->text,
 | 
			
		||||
    text,
 | 
			
		||||
    this->fontSize,
 | 
			
		||||
    width,
 | 
			
		||||
    &this->mesh,
 | 
			
		||||
@@ -35,17 +38,17 @@ void UILabel::updateMesh() {
 | 
			
		||||
  this->needsRebuffering = false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
std::string UILabel::getText() {
 | 
			
		||||
  return this->text;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void UILabel::setFont(Font *font) {
 | 
			
		||||
  this->font = font;
 | 
			
		||||
  this->needsRebuffering = true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void UILabel::setText(std::string text) {
 | 
			
		||||
  this->text = text;
 | 
			
		||||
void UILabel::setText(std::string key) {
 | 
			
		||||
  this->key = key;
 | 
			
		||||
  this->hasText = false;
 | 
			
		||||
  if(key.size() > 0 && this->getGame()->localeManager.getString(key).size()>0){
 | 
			
		||||
    this->hasText = true;
 | 
			
		||||
  }
 | 
			
		||||
  this->needsRebuffering = true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -65,7 +68,7 @@ float_t UILabel::getContentHeight() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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();
 | 
			
		||||
  shader->setUIColor(this->textColor);
 | 
			
		||||
@@ -83,4 +86,17 @@ void UILabel::setTransform(
 | 
			
		||||
) {
 | 
			
		||||
  this->needsRebuffering = true;
 | 
			
		||||
  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
 | 
			
		||||
  );
 | 
			
		||||
}
 | 
			
		||||
@@ -14,11 +14,15 @@ namespace Dawn {
 | 
			
		||||
      Mesh mesh;
 | 
			
		||||
      bool_t needsRebuffering = true;
 | 
			
		||||
      Font *font = nullptr;
 | 
			
		||||
      std::string text = "";
 | 
			
		||||
      std::string key = "";
 | 
			
		||||
      float_t fontSize = 10.0f;
 | 
			
		||||
      bool_t hasText = false;
 | 
			
		||||
 | 
			
		||||
      void updatePositions() override;
 | 
			
		||||
 | 
			
		||||
      /** Event for when the language strings are updated */
 | 
			
		||||
      void onLanguageUpdated();
 | 
			
		||||
 | 
			
		||||
    public:
 | 
			
		||||
      struct FontMeasure measure;
 | 
			
		||||
      int32_t startQuad = 0;
 | 
			
		||||
@@ -43,13 +47,6 @@ namespace Dawn {
 | 
			
		||||
       */
 | 
			
		||||
      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.
 | 
			
		||||
       * 
 | 
			
		||||
@@ -60,9 +57,9 @@ namespace Dawn {
 | 
			
		||||
      /**
 | 
			
		||||
       * 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.
 | 
			
		||||
@@ -71,5 +68,6 @@ namespace Dawn {
 | 
			
		||||
       */
 | 
			
		||||
      void setFontSize(float_t fontSize);
 | 
			
		||||
 | 
			
		||||
      ~UILabel();
 | 
			
		||||
  };
 | 
			
		||||
}
 | 
			
		||||
@@ -9,15 +9,15 @@ using namespace Dawn;
 | 
			
		||||
 | 
			
		||||
VisualNovelTextboxEvent::VisualNovelTextboxEvent(
 | 
			
		||||
  VisualNovelManager *manager,
 | 
			
		||||
  std::string text
 | 
			
		||||
  std::string key
 | 
			
		||||
) : IVisualNovelEvent(manager) {
 | 
			
		||||
  this->text = text;
 | 
			
		||||
  this->key = key;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void VisualNovelTextboxEvent::onStart(IVisualNovelEvent *previous) {
 | 
			
		||||
  if(this->manager->textBox == nullptr) return;
 | 
			
		||||
 | 
			
		||||
  this->manager->textBox->setText(this->text);
 | 
			
		||||
  this->manager->textBox->setText(this->key);
 | 
			
		||||
  this->manager->textBox->show();
 | 
			
		||||
  this->hasSetText = true;
 | 
			
		||||
}
 | 
			
		||||
@@ -26,7 +26,7 @@ bool_t VisualNovelTextboxEvent::onUpdate() {
 | 
			
		||||
  if(this->manager->textBox == nullptr) return true;
 | 
			
		||||
 | 
			
		||||
  if(!this->hasSetText) {
 | 
			
		||||
    this->manager->textBox->setText(this->text);
 | 
			
		||||
    this->manager->textBox->setText(this->key);
 | 
			
		||||
    this->manager->textBox->show();
 | 
			
		||||
    this->hasSetText = true;
 | 
			
		||||
  }
 | 
			
		||||
 
 | 
			
		||||
@@ -9,7 +9,7 @@
 | 
			
		||||
namespace Dawn {
 | 
			
		||||
  class VisualNovelTextboxEvent : public IVisualNovelEvent {
 | 
			
		||||
    protected:
 | 
			
		||||
      std::string text;
 | 
			
		||||
      std::string key;
 | 
			
		||||
      bool_t hasSetText = false;
 | 
			
		||||
      
 | 
			
		||||
      void onStart(IVisualNovelEvent *previous) override;
 | 
			
		||||
 
 | 
			
		||||
@@ -147,14 +147,14 @@ void VisualNovelTextbox::setFont(Font *font) {
 | 
			
		||||
  this->label.updateMesh();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void VisualNovelTextbox::setText(std::string text, float_t fontSize) {
 | 
			
		||||
  this->label.setText(text);
 | 
			
		||||
void VisualNovelTextbox::setText(std::string key, float_t fontSize) {
 | 
			
		||||
  this->label.setText(key);
 | 
			
		||||
  this->label.setFontSize(fontSize);
 | 
			
		||||
  this->label.updateMesh();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void VisualNovelTextbox::setText(std::string text) {
 | 
			
		||||
  this->label.setText(text);
 | 
			
		||||
void VisualNovelTextbox::setText(std::string key) {
 | 
			
		||||
  this->label.setText(key);
 | 
			
		||||
  this->label.updateMesh();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -70,17 +70,17 @@ namespace Dawn {
 | 
			
		||||
      /**
 | 
			
		||||
       * 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.
 | 
			
		||||
       */
 | 
			
		||||
      void setText(std::string text, float_t fontSize);
 | 
			
		||||
      void setText(std::string key, float_t fontSize);
 | 
			
		||||
 | 
			
		||||
      /**
 | 
			
		||||
       * 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.
 | 
			
		||||
 
 | 
			
		||||
@@ -41,9 +41,9 @@ namespace Dawn {
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        start
 | 
			
		||||
          ->then(new VisualNovelTextboxEvent(vnManager, "Starting Game"))
 | 
			
		||||
          ->then(new VisualNovelTextboxEvent(vnManager, "test"))
 | 
			
		||||
          ->then(new PokerNewGameEvent(vnManager))
 | 
			
		||||
          ->then(new VisualNovelTextboxEvent(vnManager, "Game Started"))
 | 
			
		||||
          ->then(new VisualNovelTextboxEvent(vnManager, "test"))
 | 
			
		||||
          ->then(new PokerInitialEvent(vnManager))
 | 
			
		||||
        ;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -37,7 +37,7 @@ PokerPlayerDisplay::PokerPlayerDisplay(UICanvas *canvas) :
 | 
			
		||||
 | 
			
		||||
  // Player Name
 | 
			
		||||
  this->borderInner.addChild(&this->labelName); 
 | 
			
		||||
  this->labelName.setText("Player Name");
 | 
			
		||||
  this->labelName.setText("undefined");
 | 
			
		||||
  this->labelName.setFont(&this->font->font);
 | 
			
		||||
  this->labelName.setFontSize(40);
 | 
			
		||||
  this->labelName.setTransform(
 | 
			
		||||
@@ -69,7 +69,7 @@ void PokerPlayerDisplay::setPlayer(PokerPlayer *player) {
 | 
			
		||||
 | 
			
		||||
  player->eventChipsChanged.addListener(this, &PokerPlayerDisplay::onPlayerChipsChanged);
 | 
			
		||||
 | 
			
		||||
  this->labelName.setText("Name");
 | 
			
		||||
  this->labelName.setText("undefined");
 | 
			
		||||
  this->animChips.clear();
 | 
			
		||||
  this->animChips.restart();
 | 
			
		||||
  this->animChipsValue = player->chips;
 | 
			
		||||
@@ -80,11 +80,11 @@ void PokerPlayerDisplay::setPlayer(PokerPlayer *player) {
 | 
			
		||||
void PokerPlayerDisplay::onSceneUpdate() {
 | 
			
		||||
  this->animChips.tick(getGame()->timeManager.delta);
 | 
			
		||||
 | 
			
		||||
  std::stringstream stream;
 | 
			
		||||
  stream.precision(0);
 | 
			
		||||
  stream << "$";
 | 
			
		||||
  stream << this->animChipsValue;
 | 
			
		||||
  this->labelChips.setText(stream.str());
 | 
			
		||||
  // std::stringstream stream;
 | 
			
		||||
  // stream.precision(0);
 | 
			
		||||
  // stream << "$";
 | 
			
		||||
  // stream << this->animChipsValue;
 | 
			
		||||
  this->labelChips.setText("undefined");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void PokerPlayerDisplay::onPlayerChipsChanged() {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user