Add TextMapSystem WIP
This commit is contained in:
@@ -96,6 +96,11 @@ class PD_LITHIUM_API Font {
|
||||
void CmdTextEx(std::vector<Command::Ref>& cmds, const fvec2& pos, u32 color,
|
||||
float scale, const std::string& text, LiTextFlags flags = 0,
|
||||
const fvec2& box = 0);
|
||||
|
||||
/**
|
||||
* Garbage collection for TextMapSystem
|
||||
*/
|
||||
void CleanupTMS();
|
||||
/**
|
||||
* Utility function to create a font atlas
|
||||
* During TTF loading (Internal and should not be called)
|
||||
@@ -116,6 +121,14 @@ class PD_LITHIUM_API Font {
|
||||
* **Now using unordered map**
|
||||
*/
|
||||
std::unordered_map<u32, Codepoint> CodeMap;
|
||||
/** TMS */
|
||||
struct TMELEM {
|
||||
PD::u32 ID;
|
||||
PD::fvec2 Size;
|
||||
std::string Text;
|
||||
u64 TimeStamp;
|
||||
};
|
||||
std::unordered_map<u32, TMELEM> pTMS;
|
||||
};
|
||||
} // namespace Li
|
||||
} // namespace PD
|
||||
@@ -38,6 +38,9 @@ PD_LITHIUM_API void DrawList::Clear() {
|
||||
pNumVertices = 0;
|
||||
pDrawList.clear();
|
||||
pPath.clear();
|
||||
if (pCurrentFont) {
|
||||
pCurrentFont->CleanupTMS();
|
||||
}
|
||||
while (!pClipRects.empty()) {
|
||||
pClipRects.pop();
|
||||
}
|
||||
|
||||
@@ -196,6 +196,11 @@ PD_LITHIUM_API Font::Codepoint &Font::GetCodepoint(u32 cp) {
|
||||
}
|
||||
|
||||
PD_LITHIUM_API fvec2 Font::GetTextBounds(const std::string &text, float scale) {
|
||||
u32 id = PD::FNV1A32(text);
|
||||
if (pTMS.find(id) != pTMS.end()) {
|
||||
pTMS[id].TimeStamp = PD::OS::GetTime();
|
||||
return pTMS[id].Size;
|
||||
}
|
||||
// Use wstring for exemple for german äöü
|
||||
auto wtext = Strings::MakeWstring(text);
|
||||
// Create a temp position and offset as [0, 0]
|
||||
@@ -237,6 +242,9 @@ PD_LITHIUM_API fvec2 Font::GetTextBounds(const std::string &text, float scale) {
|
||||
}
|
||||
res.x = std::max(res.x, x);
|
||||
res.y += lh;
|
||||
pTMS[id].ID = id;
|
||||
pTMS[id].Size = res;
|
||||
pTMS[id].TimeStamp = PD::OS::GetTime();
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -331,6 +339,14 @@ PD_LITHIUM_API void Font::CmdTextEx(std::vector<Command::Ref> &cmds,
|
||||
PD_LITHIUM_API std::string Font::pWrapText(const std::string &txt, float scale,
|
||||
const PD::fvec2 &max,
|
||||
PD::fvec2 &dim) {
|
||||
u32 id = PD::FNV1A32(txt);
|
||||
if (pTMS.find(id) != pTMS.end()) {
|
||||
if (pTMS[id].Text.size()) {
|
||||
dim = pTMS[id].Size;
|
||||
pTMS[id].TimeStamp = PD::OS::GetTime();
|
||||
return pTMS[id].Text;
|
||||
}
|
||||
}
|
||||
std::string ret;
|
||||
std::string line;
|
||||
int lx = 0;
|
||||
@@ -350,12 +366,24 @@ PD_LITHIUM_API std::string Font::pWrapText(const std::string &txt, float scale,
|
||||
}
|
||||
ret += line;
|
||||
dim = GetTextBounds(ret, scale);
|
||||
pTMS[id].ID = id;
|
||||
pTMS[id].Size = dim;
|
||||
pTMS[id].Text = ret;
|
||||
pTMS[id].TimeStamp = PD::OS::GetTime();
|
||||
return ret;
|
||||
}
|
||||
|
||||
PD_LITHIUM_API std::string Font::pShortText(const std::string &txt, float scale,
|
||||
const PD::fvec2 &max,
|
||||
PD::fvec2 &dim) {
|
||||
u32 id = PD::FNV1A32(txt);
|
||||
if (pTMS.find(id) != pTMS.end()) {
|
||||
if (pTMS[id].Text.size()) {
|
||||
dim = pTMS[id].Size;
|
||||
pTMS[id].TimeStamp = PD::OS::GetTime();
|
||||
return pTMS[id].Text;
|
||||
}
|
||||
}
|
||||
auto test = GetTextBounds(txt, scale);
|
||||
if (test.x < max.x) {
|
||||
return txt;
|
||||
@@ -382,8 +410,23 @@ PD_LITHIUM_API std::string Font::pShortText(const std::string &txt, float scale,
|
||||
}
|
||||
ret += it;
|
||||
}
|
||||
pTMS[id].ID = id;
|
||||
pTMS[id].Size = dim;
|
||||
pTMS[id].Text = ret;
|
||||
pTMS[id].TimeStamp = PD::OS::GetTime();
|
||||
return ret;
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void Font::CleanupTMS() {
|
||||
u64 t = PD::OS::GetTime();
|
||||
for (auto it = pTMS.begin(); it != pTMS.end();) {
|
||||
if (t - it->second.TimeStamp > 1000) {
|
||||
it = pTMS.erase(it);
|
||||
} else {
|
||||
it++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Li
|
||||
} // namespace PD
|
||||
Reference in New Issue
Block a user