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

@ -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
);
}

View File

@ -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();
};
}