diff options
author | chai <chaifix@163.com> | 2021-11-02 09:30:25 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2021-11-02 09:30:25 +0800 |
commit | b7511abf1a1f302b5c7ebf50faaf65b62d7bb6ed (patch) | |
tree | 7265398e248b55ede4b3550ec47660be334bf1ff /Runtime/GUI/TextMesh.cpp | |
parent | a11097e51fcaef4488218411f2d7b3ee34550000 (diff) |
* TextMesh
Diffstat (limited to 'Runtime/GUI/TextMesh.cpp')
-rw-r--r-- | Runtime/GUI/TextMesh.cpp | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/Runtime/GUI/TextMesh.cpp b/Runtime/GUI/TextMesh.cpp index e69de29..6f3b3d6 100644 --- a/Runtime/GUI/TextMesh.cpp +++ b/Runtime/GUI/TextMesh.cpp @@ -0,0 +1,90 @@ +#include "../Graphics/CustomVertexLayout.h" +#include "Runtime/Utilities/StaticInitiator.h" +#include "../Math/Math.h" +#include "../Graphics/Color.h" +#include "TextMesh.h" +#include <vector> +#include <unordered_map> + +using namespace std; + +struct TextMeshVBOLayout +{ + Vector2 position; + Vector2 uv; + Color32 color; +}; + +static CustomVertexLayout s_TextMeshVBOLayout; + +static unsigned int s_VertexPerText; +static unsigned int s_SizePerVertex; +static unsigned int s_SizePerText; +static unsigned int s_IndicesPerText; + +struct TextInfo { + const Character* ch; + float offset; +}; + +static unordered_map<unsigned int, vector<TextInfo>> s_TextInfos; + +InitializeStaticVariables([]() { + VertexAttributeDescriptor POSITION = VertexAttributeDescriptor(0, 2, VertexAttrFormat_Float, sizeof(TextMeshVBOLayout)); + VertexAttributeDescriptor UV = VertexAttributeDescriptor(sizeof(Vector2), 2, VertexAttrFormat_Float, sizeof(TextMeshVBOLayout)); + VertexAttributeDescriptor COLOR = VertexAttributeDescriptor(sizeof(Vector2)*2, 4, VertexAttrFormat_Unsigned_Byte, sizeof(TextMeshVBOLayout), true); + s_TextMeshVBOLayout.attributes.push_back(POSITION); + s_TextMeshVBOLayout.attributes.push_back(UV); + s_TextMeshVBOLayout.attributes.push_back(COLOR); + + s_VertexPerText = 4; + s_SizePerVertex = sizeof(TextMeshVBOLayout); + s_SizePerText = sizeof(TextMeshVBOLayout) * 4; + s_IndicesPerText = 6; +}); + +TextMesh::TextMesh(const UnicodeString& str, Font* font,int pixelSize, ETextAnchor anchor, ETextAlignment alignment) +{ + m_Font = font; + s_TextInfos.clear(); + + float offset = 0; + for (int i = 0; i < str.length; ++i) + { + character::Codepoint c = str.str[i]; + const Character* ch = font->GetCharacter(c, pixelSize); + unsigned int index = ch->atlas; + + TextInfo info; + info.ch = ch; + info.offset = offset; + + auto list = s_TextInfos.find(index); + if (list == s_TextInfos.end()) + { + s_TextInfos.insert(std::pair<unsigned int, vector<TextInfo>>(index, vector<TextInfo>())); + list = s_TextInfos.find(index); + } + vector<TextInfo>& v = list->second; + v.push_back(info); + + offset += ch->advance; + } + + if (s_TextInfos.size() == 0) + return; + + for (auto iter : s_TextInfos) { + unsigned int index = iter.first; + vector<TextInfo>& texts = iter.second; + int count = texts.size(); + + VertexBuffer* vb = new VertexBuffer(count * s_SizePerText, count * s_IndicesPerText, VertexBuffer::VertexBufferType_Static); + + } +} + +void TextMesh::Draw() +{ + +} |