diff options
author | chai <chaifix@163.com> | 2021-11-04 13:49:05 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2021-11-04 13:49:05 +0800 |
commit | d7c051cecf0db9056e94d5e80146aa0b066e606b (patch) | |
tree | da7a96046faa3323d1171d782164d3aae50ce322 /Runtime/GUI/UITextMesh.cpp | |
parent | d24f17b88d901b779c81c7434995675eb2a17429 (diff) |
*text alignment
Diffstat (limited to 'Runtime/GUI/UITextMesh.cpp')
-rw-r--r-- | Runtime/GUI/UITextMesh.cpp | 52 |
1 files changed, 40 insertions, 12 deletions
diff --git a/Runtime/GUI/UITextMesh.cpp b/Runtime/GUI/UITextMesh.cpp index 94e61c4..7fc2614 100644 --- a/Runtime/GUI/UITextMesh.cpp +++ b/Runtime/GUI/UITextMesh.cpp @@ -65,10 +65,21 @@ InitializeStaticVariables([]() { // preferredSize �Զ����������С UITextMesh::UITextMesh(const UnicodeString& str, Font* font, int pixelSize, int lineHeight, ETextAnchor anchor, ETextAlignment alignment, bool wordwrap, float preferred) { - m_Font = font; s_TextInfos.clear(); + + // ��¼�ı�ÿ�еij��� + static unordered_map<int, int> s_LineWidths; + s_LineWidths.clear(); + + // ��¼ÿ�е�ƫ�Ƴ��� + static unordered_map<int, int> s_LineOffsets; + s_LineOffsets.clear(); + + m_Font = font; const Vector2 atlasSize = font->GetAtlasSize(); + //----------------------------------------------------------------- + // �����˻��к��Զ�����֮����ı������С Vector2 textRegion; // ���ղ�ͬ��atlas���ൽs_TextInfos float offset = 0; @@ -84,6 +95,7 @@ UITextMesh::UITextMesh(const UnicodeString& str, Font* font, int pixelSize, int { if (offset + ch->bearing.x + ch->position.width > preferred) { + ++line; offset = 0; } @@ -127,6 +139,10 @@ UITextMesh::UITextMesh(const UnicodeString& str, Font* font, int pixelSize, int offset += ch->advance; textRegion.x = max(offset, textRegion.x); + + if (s_LineWidths.count(line) == 0) + s_LineWidths.insert(std::pair<int, int>(line, 0)); + s_LineWidths[line] = max(offset, s_LineWidths[line]); } textRegion.y = (line + 1) * lineHeight; @@ -149,6 +165,21 @@ UITextMesh::UITextMesh(const UnicodeString& str, Font* font, int pixelSize, int case TextAnchor_LowerRight: textOffset.Set(-textRegion.x, -textRegion.y); break; } + for (int i = 0; i < line; ++i) + { + int lineLen = s_LineWidths.count(i) != 0 ? s_LineWidths[i] : 0; + int lineOffset = 0; + switch (alignment) + { + case TextAlignment_Left: lineOffset = 0; break; + case TextAlignment_Center: lineOffset = (textRegion.x - lineLen)/2.f; break; + case TextAlignment_Right: lineOffset = textRegion.x - lineLen; break; + } + s_LineOffsets.insert(std::pair<int, int>(i, lineOffset)); + } + + //----------------------------------------------------------------- + // ���VBO��IBO for (auto iter : s_TextInfos) { unsigned int atlasIndex = iter.first; // atlas atlasIndex @@ -167,13 +198,14 @@ UITextMesh::UITextMesh(const UnicodeString& str, Font* font, int pixelSize, int TextInfo& text = texts[i]; int vOff = i * s_VertexPerText; - int lineOff = text.line * lineHeight; + int lineXOff = s_LineOffsets.count(text.line) ? s_LineOffsets[text.line] : 0; + int lineYOff = text.line * lineHeight; // ���Ͻ���ԭ�� float pos[] = { - textOffset.x + text.offset + text.ch->bearing.x, textOffset.y + lineOff + pixelSize - text.ch->bearing.y + text.ch->position.height, // bottom-left - textOffset.x + text.offset + text.ch->bearing.x + text.ch->position.width, textOffset.y + lineOff + pixelSize - text.ch->bearing.y + text.ch->position.height, // bottom-right - textOffset.x + text.offset + text.ch->bearing.x + text.ch->position.width, textOffset.y + lineOff + pixelSize - text.ch->bearing.y, // top-right - textOffset.x + text.offset + text.ch->bearing.x, textOffset.y + lineOff + pixelSize - text.ch->bearing.y, // top-left + textOffset.x + lineXOff + text.offset + text.ch->bearing.x, textOffset.y + lineYOff + pixelSize - text.ch->bearing.y + text.ch->position.height, // bottom-left + textOffset.x + lineXOff + text.offset + text.ch->bearing.x + text.ch->position.width, textOffset.y + lineYOff + pixelSize - text.ch->bearing.y + text.ch->position.height, // bottom-right + textOffset.x + lineXOff + text.offset + text.ch->bearing.x + text.ch->position.width, textOffset.y + lineYOff + pixelSize - text.ch->bearing.y, // top-right + textOffset.x + lineXOff + text.offset + text.ch->bearing.x, textOffset.y + lineYOff + pixelSize - text.ch->bearing.y, // top-left }; Vector4 uvQuad = Vector4(text.ch->position.x / atlasSize.x, text.ch->position.y / atlasSize.y, text.ch->position.width / atlasSize.x, text.ch->position.height / atlasSize.y); float uv[] = { @@ -222,14 +254,10 @@ void UITextMesh::Draw() g_GfxDevice.SetUniformTexture("gamelab_main_tex", atlas->altas); - CheckGLError( - throw GLException(error); - ); + WipeGLError(); vbo->Draw(s_TextMeshVBOLayout); - CheckGLError( - throw GLException(error); - ); + WipeGLError(); g_GfxDevice.ResetUniformsState(); } } |