Dawn/src/dawn/ui/UILabel.cpp

120 lines
2.8 KiB
C++

// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// 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() {
UIComponent::updatePositions();
this->updateMesh();
}
void UILabel::updateMesh() {
if(!this->needsRebuffering || !this->hasText) return;
if(this->font == nullptr || !this->font->isReady()) return;
float_t width = this->width;
if(width == 0) width = -1;
std::string text = this->getGame()->localeManager.getString(key);
this->font->buffer(
text,
this->fontSize,
width,
&this->mesh,
&this->measure
);
this->needsRebuffering = false;
}
void UILabel::setFont(Font *font) {
this->font = font;
this->needsRebuffering = true;
}
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;
}
void UILabel::setFontSize(float_t fontSize) {
this->fontSize = fontSize;
this->needsRebuffering = true;
}
float_t UILabel::getFontSize() {
return this->fontSize;
}
float_t UILabel::getContentWidth() {
this->updateMesh();
return this->measure.getWidth();
}
float_t UILabel::getContentHeight() {
this->updateMesh();
return this->measure.getHeight();
}
std::vector<struct ShaderPassItem> UILabel::getSelfPassItems(
glm::mat4 projection,
glm::mat4 view,
glm::mat4 transform
) {
std::vector<struct ShaderPassItem> items;
if(this->font == nullptr) return items;
// this has to go eventually
this->updateMesh();
auto item = this->getGame()->renderManager.uiShaderProgram.getUIPassItem(
projection,
view,
transform,
this->font->getTexture(),
this->textColor,
&this->mesh,
this->z
);
item.start = this->startQuad * QUAD_INDICE_COUNT;
item.count = this->quadCount * QUAD_INDICE_COUNT;
items.push_back(item);
return items;
}
void UILabel::setTransform(
UIComponentAlign xAlign,
UIComponentAlign yAlign,
glm::vec4 alignment,
float_t z
) {
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
);
}