Prepping label statistics.
This commit is contained in:
		@@ -89,7 +89,7 @@ void UILabel::rebufferQuads(const std::vector<struct UILabelText> newTexts) {
 | 
			
		||||
  struct FontShaderBufferData fontData;
 | 
			
		||||
  quadCountTotal = 0;
 | 
			
		||||
  std::vector<struct UILabelText> realNewTexts;
 | 
			
		||||
  
 | 
			
		||||
 | 
			
		||||
  // Determine font dimensions.
 | 
			
		||||
  auto itText = newTexts.begin();
 | 
			
		||||
  while(itText != newTexts.end()) {
 | 
			
		||||
@@ -131,24 +131,54 @@ void UILabel::rebufferQuads(const std::vector<struct UILabelText> newTexts) {
 | 
			
		||||
      realText.texture->texture.getHeight()
 | 
			
		||||
    );
 | 
			
		||||
 | 
			
		||||
    // Now, iterate each character
 | 
			
		||||
    // Prepare loop properties and shorthands
 | 
			
		||||
    auto len = text.text.length();
 | 
			
		||||
    float_t lineWidth = 0;
 | 
			
		||||
    float_t wordWidth = 0;
 | 
			
		||||
    int32_t lastSpaceCharacter = -1;
 | 
			
		||||
    for(int32_t i = 0; i < len; i++) {
 | 
			
		||||
      char ch = text.text[i];
 | 
			
		||||
 | 
			
		||||
      // Handle newline
 | 
			
		||||
      if(ch == '\n') {
 | 
			
		||||
        position.x = 0;
 | 
			
		||||
        position.y += realText.style.size;
 | 
			
		||||
    struct UILabelLine currentLine;
 | 
			
		||||
 | 
			
		||||
    std::function<void(int32_t)> fnInsertNewline = [&](int32_t i){
 | 
			
		||||
      if(i != len) {
 | 
			
		||||
        // Update text.
 | 
			
		||||
        realText.text += '\n';
 | 
			
		||||
 | 
			
		||||
        // Insert dummy quad
 | 
			
		||||
        glm::vec4 uvs(0, 0, 1, 1);
 | 
			
		||||
        glm::vec4 vert(0, 0, 0, 0);
 | 
			
		||||
        vertices.push_back(std::make_pair(vert, uvs));
 | 
			
		||||
        fontData.fontQuadMappings[quadCountTotal] = partIndex;
 | 
			
		||||
 | 
			
		||||
        currentLine.quadCount++;
 | 
			
		||||
        quadCountTotal++;
 | 
			
		||||
        realText.text += ch;
 | 
			
		||||
        lastSpaceCharacter = i;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      // Finalize current line
 | 
			
		||||
      lineWidth += wordWidth;
 | 
			
		||||
      currentLine.width = lineWidth;
 | 
			
		||||
 | 
			
		||||
      // Move to next line
 | 
			
		||||
      position.x = 0;
 | 
			
		||||
      position.y += realText.style.size;
 | 
			
		||||
      lines.push_back(currentLine);
 | 
			
		||||
      
 | 
			
		||||
      // Reset line
 | 
			
		||||
      lastSpaceCharacter = i;
 | 
			
		||||
      wordWidth = 0.0f;
 | 
			
		||||
      lineWidth = 0.0f;
 | 
			
		||||
 | 
			
		||||
      currentLine = UILabelLine();
 | 
			
		||||
      currentLine.quadStart = quadCountTotal;
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    // Now, iterate each character
 | 
			
		||||
    for(int32_t i = 0; i < len; i++) {
 | 
			
		||||
      char ch = text.text[i];
 | 
			
		||||
 | 
			
		||||
      // Handle special characters
 | 
			
		||||
      if(ch == '\n') {
 | 
			
		||||
        fnInsertNewline(i);
 | 
			
		||||
        continue;
 | 
			
		||||
      } else if(ch == ' ') {
 | 
			
		||||
        lastSpaceCharacter = i;
 | 
			
		||||
@@ -160,6 +190,7 @@ void UILabel::rebufferQuads(const std::vector<struct UILabelText> newTexts) {
 | 
			
		||||
      assertTrue(ch != '\t');
 | 
			
		||||
      assertTrue(ch != '\n');
 | 
			
		||||
 | 
			
		||||
      // Get font data.
 | 
			
		||||
      FT_ULong c = ch;
 | 
			
		||||
      auto charInfo = realText.texture->getCharacterData(c);
 | 
			
		||||
 | 
			
		||||
@@ -170,12 +201,17 @@ void UILabel::rebufferQuads(const std::vector<struct UILabelText> newTexts) {
 | 
			
		||||
        this->width > charInfo.bitmapSize.x &&
 | 
			
		||||
        (position.x + charInfo.advanceX) > this->width
 | 
			
		||||
      ) {
 | 
			
		||||
        // Basically this rewinds everything we've done to the last space char,
 | 
			
		||||
        // changes it to a newline, and then moves the position along.
 | 
			
		||||
        int32_t diff = i - lastSpaceCharacter;
 | 
			
		||||
        for(int32_t k = 0; k < diff; k++) vertices.pop_back();
 | 
			
		||||
        text.text[lastSpaceCharacter] = '\n';
 | 
			
		||||
        i = lastSpaceCharacter - 1;
 | 
			
		||||
        i = lastSpaceCharacter;
 | 
			
		||||
        lastSpaceCharacter = -1;
 | 
			
		||||
        quadCountTotal -= diff;
 | 
			
		||||
 | 
			
		||||
        // Now we've rewound to the space, treat it like a newline instead.
 | 
			
		||||
        fnInsertNewline(i);
 | 
			
		||||
        continue;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
@@ -192,19 +228,31 @@ void UILabel::rebufferQuads(const std::vector<struct UILabelText> newTexts) {
 | 
			
		||||
      vert.w = vert.x + charInfo.bitmapSize.x;
 | 
			
		||||
      vert.z = vert.y + charInfo.bitmapSize.y;
 | 
			
		||||
 | 
			
		||||
      // TODO:Check wordwrap here
 | 
			
		||||
      vertices.push_back(std::make_pair(vert, uvs));
 | 
			
		||||
 | 
			
		||||
      // Move the current position along.
 | 
			
		||||
      position.x += charInfo.advanceX;
 | 
			
		||||
      position.y += charInfo.advanceY;
 | 
			
		||||
 | 
			
		||||
      // Update the continuous dimensions
 | 
			
		||||
      if(ch == ' ') {
 | 
			
		||||
        lineWidth += wordWidth;
 | 
			
		||||
        lineWidth += charInfo.advanceX;
 | 
			
		||||
        wordWidth = 0.0f;
 | 
			
		||||
      } else {
 | 
			
		||||
        wordWidth += charInfo.advanceX;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      // Set the part index to the quad mappings
 | 
			
		||||
      fontData.fontQuadMappings[quadCountTotal] = partIndex;
 | 
			
		||||
      quadCountTotal++;
 | 
			
		||||
      realText.text += ch;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Now we insert a line. We do this because there is no newline at the end
 | 
			
		||||
    // of the text, so we need to insert the last line manually.
 | 
			
		||||
    fnInsertNewline(len);
 | 
			
		||||
 | 
			
		||||
    // Next
 | 
			
		||||
    ++partIndex;
 | 
			
		||||
    ++itText;
 | 
			
		||||
 
 | 
			
		||||
@@ -22,27 +22,27 @@ namespace Dawn {
 | 
			
		||||
  struct UILabelText {
 | 
			
		||||
    std::string text;
 | 
			
		||||
    struct UILabelStyle style;
 | 
			
		||||
    
 | 
			
		||||
    glm::vec2 position;
 | 
			
		||||
    glm::vec2 size;
 | 
			
		||||
 | 
			
		||||
    // Part index?
 | 
			
		||||
    // Quad start?
 | 
			
		||||
    // quad count?
 | 
			
		||||
    // position?
 | 
			
		||||
    // size?
 | 
			
		||||
    // some kind of custom data e.g. wobble or shake?
 | 
			
		||||
 | 
			
		||||
    int32_t lineStart = 0;
 | 
			
		||||
    int32_t lineCount = 0;
 | 
			
		||||
    usagelockid_t lockId = -1;
 | 
			
		||||
    struct NewTrueTypeFaceTexture *texture = nullptr;
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  struct UILabelLine {
 | 
			
		||||
    float_t width = 0.0f;
 | 
			
		||||
    int32_t quadStart = -1;
 | 
			
		||||
    int32_t quadCount = 0;
 | 
			
		||||
 | 
			
		||||
    std::vector<struct UILabelText*> texts;
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  class UILabel : public UIComponentRenderable {
 | 
			
		||||
    private:
 | 
			
		||||
      Mesh mesh;
 | 
			
		||||
      FontShaderBuffer shaderBuffer;
 | 
			
		||||
      std::vector<struct UILabelText> texts;
 | 
			
		||||
      std::vector<struct UILabelText> textsBuffered;
 | 
			
		||||
      std::vector<struct UILabelLine> lines;
 | 
			
		||||
      std::map<NewTrueTypeFaceTexture*, int32_t> textureMap;
 | 
			
		||||
 | 
			
		||||
    public:
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user