diff options
Diffstat (limited to 'Client/Source/GUI/TextMeshGenerator.cpp')
-rw-r--r-- | Client/Source/GUI/TextMeshGenerator.cpp | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/Client/Source/GUI/TextMeshGenerator.cpp b/Client/Source/GUI/TextMeshGenerator.cpp new file mode 100644 index 0000000..766a9b0 --- /dev/null +++ b/Client/Source/GUI/TextMeshGenerator.cpp @@ -0,0 +1,89 @@ +#include "TextMeshGenerator.h" + +// ¼ÆËãÒ»¸ö¹þÏ£Öµ +static uint32_t GetStrHash(const UnicodeString& str) +{ + if (str.length == 0) + return 0; + + int len = str.length; + uint32_t hash = (uint32_t)len; // seed + size_t step = (len >> 5) + 1; + for (int l = len; l >= step; l -= step) + { + unsigned short unicode = str.str[l - 1]; + unsigned char hicode = *((unsigned char*)&unicode); + unsigned char locode = *(((unsigned char*)&unicode) + 1); + hash = hash ^ ((hash << 5) + (hash >> 2) + hicode + locode); + } + return hash; +} + +const UITextMesh* TextMeshGenerator::GetTextMesh( + const UnicodeString& str + , Font* font + , int pixelSize + , int lineHeight + , Color32 col32 + , ETextAnchor anchor + , ETextAlignment alignment + , bool wordwrap + , float preferred +){ + uint32_t hash = GetStrHash(str); + UITextMeshList* tm = NULL; + bool hasHash = m_TextMeshes.count(hash); + if (hasHash) + { + tm = m_TextMeshes[hash]; + while (tm) + { + UITextMesh* mesh = tm->mesh; + if (mesh->GetFont() != font + || mesh->GetPixelSize() != pixelSize + || mesh->GetLineHeight() != lineHeight + || mesh->GetAnchor() != anchor + || mesh->GetAlignment() != alignment + || mesh->GetWordwrap() != wordwrap + || mesh->GetPreferred() != preferred + || mesh->GetColor() != col32 + ){ + tm = tm->next; + continue; + } + const UnicodeString& content = mesh->GetContent(); + if (content.length != str.length) + { + tm = tm->next; + continue; + } + if (memcmp(str.str, content.str, sizeof(character::Unicode) * str.length) == 0) + { + return tm->mesh; + } + tm = tm->next; + } + } + tm = new UITextMeshList(); + try { + tm->mesh = new UITextMesh(str, font, pixelSize, lineHeight, col32, anchor, alignment, wordwrap, preferred); + log_info("Text", "New UITextMesh"); + } + catch(TextMeshException& e) + { + delete tm; + throw; + } + if (hasHash) + { + UITextMeshList* list = m_TextMeshes[hash]; + tm->next = list; + m_TextMeshes[hash] = tm; + } + else + { + tm->next = NULL; + m_TextMeshes.insert(std::pair<uint32_t, UITextMeshList*>(hash, tm)); + } + return tm->mesh; +} |