summaryrefslogtreecommitdiff
path: root/Runtime/GUI
diff options
context:
space:
mode:
Diffstat (limited to 'Runtime/GUI')
-rw-r--r--Runtime/GUI/UITextMesh.cpp52
-rw-r--r--Runtime/GUI/UITextMesh.h1
2 files changed, 40 insertions, 13 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();
+
+ // 记录文本每行的长度
+ 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();
}
}
diff --git a/Runtime/GUI/UITextMesh.h b/Runtime/GUI/UITextMesh.h
index 487bccd..7778785 100644
--- a/Runtime/GUI/UITextMesh.h
+++ b/Runtime/GUI/UITextMesh.h
@@ -23,7 +23,6 @@ enum ETextAlignment {
TextAlignment_Left,
TextAlignment_Center,
TextAlignment_Right,
- TextAlignment_Auto,
};
typedef unsigned long long TextMeshHash;