From 835d5e5892487255f4891bddda5d0d962dbe02a2 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Mon, 10 Jul 2023 10:29:59 -0700 Subject: [PATCH] Fixed alignment on UI Label --- src/dawn/scene/components/ui/text/UILabel.cpp | 25 ++++++++++++++----- src/dawn/scene/components/ui/text/UILabel.hpp | 10 ++++++++ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/dawn/scene/components/ui/text/UILabel.cpp b/src/dawn/scene/components/ui/text/UILabel.cpp index 4f896f01..214b4533 100644 --- a/src/dawn/scene/components/ui/text/UILabel.cpp +++ b/src/dawn/scene/components/ui/text/UILabel.cpp @@ -9,7 +9,8 @@ using namespace Dawn; UILabel::UILabel(SceneItem *item) : - UIComponentRenderable(item) + UIComponentRenderable(item), + lineHeight(1.0f) { } @@ -22,6 +23,10 @@ void UILabel::onStart() { useEvent([&]{ this->rebufferQuads(this->texts); }, eventAlignmentUpdated); + + useEffect([&]{ + this->rebufferQuads(this->texts); + }, lineHeight); } std::vector UILabel::getUIRenderPasses() { @@ -94,10 +99,15 @@ float_t UILabel::getContentHeight() { h += it->height; ++it; } - return 1; + return h; } void UILabel::rebufferQuads(const std::vector newTexts) { + if(this->ignoreAlignmentUpdate) { + this->ignoreAlignmentUpdate = false; + return; + } + assertTrue(newTexts.size() <= FONT_SHADER_PARTS_MAX); int32_t nextTexture = 0; @@ -116,7 +126,7 @@ void UILabel::rebufferQuads(const std::vector newTexts) { // Determine font dimensions. auto itText = newTexts.begin(); while(itText != newTexts.end()) { - position.y = mathMax(position.y, itText->style.size); + position.y = mathMax(position.y, itText->style.size * this->lineHeight); ++itText; } @@ -189,7 +199,7 @@ void UILabel::rebufferQuads(const std::vector newTexts) { // Move to next line if(i != len) { position.x = 0; - position.y += realText.style.size; + position.y += realText.style.size * this->lineHeight; lines.push_back(currentLine); } @@ -203,11 +213,11 @@ void UILabel::rebufferQuads(const std::vector newTexts) { currentLine = UILabelLine(); currentLine.quadStart = quadCountTotal; currentLine.position = position; - currentLine.height = realText.style.size; + currentLine.height = realText.style.size * this->lineHeight; // Here I subtract line height from the line position because we start // by moving the text down by the initial line height, so we need to // compensate for that. - currentLine.position.y -= realText.style.size; + currentLine.position.y -= realText.style.size * this->lineHeight; } }; @@ -342,6 +352,9 @@ void UILabel::rebufferQuads(const std::vector newTexts) { textsBuffered = realNewTexts; texts = newTexts; + this->ignoreAlignmentUpdate = true; + this->alignmentNeedsUpdating = true; + // Event this->eventTextChanged.invoke(); } \ No newline at end of file diff --git a/src/dawn/scene/components/ui/text/UILabel.hpp b/src/dawn/scene/components/ui/text/UILabel.hpp index c720e0eb..1d937099 100644 --- a/src/dawn/scene/components/ui/text/UILabel.hpp +++ b/src/dawn/scene/components/ui/text/UILabel.hpp @@ -36,11 +36,18 @@ namespace Dawn { int32_t quadCount = 0; }; + enum UILabelVerticalAlign { + UI_LABEL_VERTICAL_ALIGN_TOP, + UI_LABEL_VERTICAL_ALIGN_MIDDLE, + UI_LABEL_VERTICAL_ALIGN_BOTTOM + }; + class UILabel : public UIComponentRenderable { private: Mesh mesh; FontShaderBuffer shaderBuffer; std::map textureMap; + bool_t ignoreAlignmentUpdate = false; public: int32_t quadStart = 0; @@ -55,6 +62,9 @@ namespace Dawn { StateEvent<> eventTextChanged; + // @optional + StateProperty lineHeight; + UILabel(SceneItem *item); void onStart() override;