From 7c12906aacdd31423f9391c83759ab122f50e3af Mon Sep 17 00:00:00 2001
From: Dominic Masters <dominic@domsplace.com>
Date: Mon, 10 Jul 2023 10:29:59 -0700
Subject: [PATCH] Fixed alignment on UI Label

---
 .../liminal/prefabs/VNTextboxMonologue.xml    |  3 ++-
 .../scenes/prologue/ScenePrologue0.xml        |  6 ++---
 src/dawn/scene/components/ui/text/UILabel.cpp | 25 ++++++++++++++-----
 src/dawn/scene/components/ui/text/UILabel.hpp | 10 ++++++++
 4 files changed, 34 insertions(+), 10 deletions(-)

diff --git a/assets/games/liminal/prefabs/VNTextboxMonologue.xml b/assets/games/liminal/prefabs/VNTextboxMonologue.xml
index 6fa6db8e..a7560480 100644
--- a/assets/games/liminal/prefabs/VNTextboxMonologue.xml
+++ b/assets/games/liminal/prefabs/VNTextboxMonologue.xml
@@ -16,8 +16,9 @@
     <UIRichTextLabel
       alignment="0, 0, 0, 0"
       alignX="UI_COMPONENT_ALIGN_STRETCH"
-      alignY="UI_COMPONENT_ALIGN_STRETCH"
+      alignY="UI_COMPONENT_ALIGN_MIDDLE"
       ref="uiLabel"
+      lineHeight="2"
     >
     </UIRichTextLabel>
   </child>
diff --git a/assets/games/liminal/scenes/prologue/ScenePrologue0.xml b/assets/games/liminal/scenes/prologue/ScenePrologue0.xml
index 6d2a1737..effedd4d 100644
--- a/assets/games/liminal/scenes/prologue/ScenePrologue0.xml
+++ b/assets/games/liminal/scenes/prologue/ScenePrologue0.xml
@@ -166,11 +166,11 @@
     </text>
     
     <text>
-      <string lang="en">"I wouldn't expect any less from our daughter,” Mother says. She smiles at me: it creases her mouth in an unnatural way. "Doesn't Ethereality look like Angelwood's Queen already?”</string>
+      <string lang="en">"I wouldn't expect any less from our daughter," Mother says. She smiles at me: it creases her mouth in an unnatural way. "Doesn't Ethereality look like Angelwood's Queen already?"</string>
     </text>
     
     <text>
-      <string lang="en">My father nods his agreement. "Yes,” he tells me, "An Estridge in her finest.”</string>
+      <string lang="en">My father nods his agreement. "Yes," he tells me, "An Estridge in her finest."</string>
     </text>
     
     <text>
@@ -198,7 +198,7 @@
     </text>
     
     <text>
-      <string lang="en">"Thank you,” I say. "I'm really happy you think that.”</string>
+      <string lang="en">"Thank you," I say. "I'm really happy you think that."</string>
     </text>
     
     <text>
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<struct ShaderPassItem> UILabel::getUIRenderPasses() {
@@ -94,10 +99,15 @@ float_t UILabel::getContentHeight() {
     h += it->height;
     ++it;
   }
-  return 1;
+  return h;
 }
 
 void UILabel::rebufferQuads(const std::vector<struct UILabelText> 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<struct UILabelText> newTexts) {
   // Determine font dimensions.
   auto itText = newTexts.begin();
   while(itText != newTexts.end()) {
-    position.y = mathMax<float_t>(position.y, itText->style.size);
+    position.y = mathMax<float_t>(position.y, itText->style.size * this->lineHeight);
     ++itText;
   }
 
@@ -189,7 +199,7 @@ void UILabel::rebufferQuads(const std::vector<struct UILabelText> 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<struct UILabelText> 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<struct UILabelText> 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<TrueTypeFaceTexture*, int32_t> textureMap;
+      bool_t ignoreAlignmentUpdate = false;
 
     public:
       int32_t quadStart = 0;
@@ -55,6 +62,9 @@ namespace Dawn {
       
       StateEvent<> eventTextChanged;
 
+      // @optional
+      StateProperty<float_t> lineHeight;
+
       UILabel(SceneItem *item);
 
       void onStart() override;