Added text alignment support.
This commit is contained in:
		@@ -22,12 +22,65 @@ void UILabel::onStart() {
 | 
			
		||||
  this->shaderBuffer.init();
 | 
			
		||||
 | 
			
		||||
  useEvent([&]{
 | 
			
		||||
    // TODO: I believe I only need to rebuffer here if maxWidth is not -1
 | 
			
		||||
    this->rebufferQuads(this->texts);
 | 
			
		||||
  }, eventAlignmentUpdated);
 | 
			
		||||
 | 
			
		||||
  useEffect([&]{
 | 
			
		||||
    this->rebufferQuads(this->texts);
 | 
			
		||||
  }, lineHeight);
 | 
			
		||||
  
 | 
			
		||||
  useEvent([&]{
 | 
			
		||||
    this->updateTextAlignments();
 | 
			
		||||
  }, eventTextChanged);
 | 
			
		||||
 | 
			
		||||
  useEffect([&]{
 | 
			
		||||
    this->updateTextAlignments();
 | 
			
		||||
  }, this->textAlign);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void UILabel::updateTextAlignments() {
 | 
			
		||||
  struct FontShaderBufferData data;
 | 
			
		||||
 | 
			
		||||
  switch(this->textAlign) {
 | 
			
		||||
    case UI_LABEL_TEXT_ALIGN_LEFT: {
 | 
			
		||||
      auto itLine = lines.begin();
 | 
			
		||||
      int32_t i = 0;
 | 
			
		||||
      while(itLine != lines.end()) {
 | 
			
		||||
        data.linePositions[i] = glm::vec2(0, 0);
 | 
			
		||||
        ++itLine;
 | 
			
		||||
      }
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    case UI_LABEL_TEXT_ALIGN_RIGHT: {
 | 
			
		||||
      float_t widthBased = this->getWidth();
 | 
			
		||||
      auto itLine = lines.begin();
 | 
			
		||||
      int32_t i = 0;
 | 
			
		||||
      while(itLine != lines.end()) {
 | 
			
		||||
        data.linePositions[i] = glm::vec2(widthBased - itLine->width, 0);
 | 
			
		||||
        ++itLine;
 | 
			
		||||
      }
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    case UI_LABEL_TEXT_ALIGN_CENTER: {
 | 
			
		||||
      float_t widthBased = this->getWidth();
 | 
			
		||||
      auto itLine = lines.begin();
 | 
			
		||||
      int32_t i = 0;
 | 
			
		||||
      while(itLine != lines.end()) {
 | 
			
		||||
        float_t x = (widthBased - itLine->width) / 2.0f;
 | 
			
		||||
        data.linePositions[i] = glm::vec2(x, 0);
 | 
			
		||||
        ++itLine;
 | 
			
		||||
      }
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    default:
 | 
			
		||||
      assertUnreachable("UILabel::updateTextAlignments: TextAlign invalid");
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  this->shaderBuffer.buffer(&data, &data.linePositions);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
std::vector<struct ShaderPassItem> UILabel::getUIRenderPasses() {
 | 
			
		||||
 
 | 
			
		||||
@@ -59,6 +59,8 @@ namespace Dawn {
 | 
			
		||||
      bool_t ignoreAlignmentUpdate = false;
 | 
			
		||||
      bool_t hasDecorations = false;
 | 
			
		||||
 | 
			
		||||
      void updateTextAlignments();
 | 
			
		||||
 | 
			
		||||
    public:
 | 
			
		||||
      int32_t quadStart = 0;
 | 
			
		||||
      int32_t quadCount = -1;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user