Fixed alignment on UI Label
This commit is contained in:
		@@ -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();
 | 
			
		||||
}
 | 
			
		||||
@@ -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;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user