Add TextMapSystem WIP

This commit is contained in:
2025-12-24 18:17:58 +01:00
parent c618c616fe
commit 03f65e069f
3 changed files with 59 additions and 0 deletions

View File

@@ -96,6 +96,11 @@ class PD_LITHIUM_API Font {
void CmdTextEx(std::vector<Command::Ref>& cmds, const fvec2& pos, u32 color, void CmdTextEx(std::vector<Command::Ref>& cmds, const fvec2& pos, u32 color,
float scale, const std::string& text, LiTextFlags flags = 0, float scale, const std::string& text, LiTextFlags flags = 0,
const fvec2& box = 0); const fvec2& box = 0);
/**
* Garbage collection for TextMapSystem
*/
void CleanupTMS();
/** /**
* Utility function to create a font atlas * Utility function to create a font atlas
* During TTF loading (Internal and should not be called) * During TTF loading (Internal and should not be called)
@@ -116,6 +121,14 @@ class PD_LITHIUM_API Font {
* **Now using unordered map** * **Now using unordered map**
*/ */
std::unordered_map<u32, Codepoint> CodeMap; 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 Li
} // namespace PD } // namespace PD

View File

@@ -38,6 +38,9 @@ PD_LITHIUM_API void DrawList::Clear() {
pNumVertices = 0; pNumVertices = 0;
pDrawList.clear(); pDrawList.clear();
pPath.clear(); pPath.clear();
if (pCurrentFont) {
pCurrentFont->CleanupTMS();
}
while (!pClipRects.empty()) { while (!pClipRects.empty()) {
pClipRects.pop(); pClipRects.pop();
} }

View File

@@ -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) { 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 äöü // Use wstring for exemple for german äöü
auto wtext = Strings::MakeWstring(text); auto wtext = Strings::MakeWstring(text);
// Create a temp position and offset as [0, 0] // 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.x = std::max(res.x, x);
res.y += lh; res.y += lh;
pTMS[id].ID = id;
pTMS[id].Size = res;
pTMS[id].TimeStamp = PD::OS::GetTime();
return res; 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, PD_LITHIUM_API std::string Font::pWrapText(const std::string &txt, float scale,
const PD::fvec2 &max, const PD::fvec2 &max,
PD::fvec2 &dim) { 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 ret;
std::string line; std::string line;
int lx = 0; int lx = 0;
@@ -350,12 +366,24 @@ PD_LITHIUM_API std::string Font::pWrapText(const std::string &txt, float scale,
} }
ret += line; ret += line;
dim = GetTextBounds(ret, scale); dim = GetTextBounds(ret, scale);
pTMS[id].ID = id;
pTMS[id].Size = dim;
pTMS[id].Text = ret;
pTMS[id].TimeStamp = PD::OS::GetTime();
return ret; return ret;
} }
PD_LITHIUM_API std::string Font::pShortText(const std::string &txt, float scale, PD_LITHIUM_API std::string Font::pShortText(const std::string &txt, float scale,
const PD::fvec2 &max, const PD::fvec2 &max,
PD::fvec2 &dim) { 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); auto test = GetTextBounds(txt, scale);
if (test.x < max.x) { if (test.x < max.x) {
return txt; return txt;
@@ -382,8 +410,23 @@ PD_LITHIUM_API std::string Font::pShortText(const std::string &txt, float scale,
} }
ret += it; ret += it;
} }
pTMS[id].ID = id;
pTMS[id].Size = dim;
pTMS[id].Text = ret;
pTMS[id].TimeStamp = PD::OS::GetTime();
return ret; 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 Li
} // namespace PD } // namespace PD