Free memory from unused GUI elements, and make colorpicker not glitch on first appearance

This commit is contained in:
Yousef Khadadeh 2025-10-14 12:51:24 +03:00
parent 1ec7c717b2
commit 9ceb4e1b8a
4 changed files with 15 additions and 3 deletions

View file

@ -23,6 +23,7 @@ class GUIManager {
struct ElementContainer {
std::unique_ptr<Element> elem;
std::any extra;
bool isUsedThisFrame;
};
GUIManager();
@ -90,15 +91,19 @@ class GUIManager {
template <typename NewElement> NewElement* insert_element() {
auto [it, inserted] = elements.emplace(idStack, ElementContainer());
auto& container = it->second;
if(inserted)
it->second = {std::make_unique<NewElement>()};
container = {std::make_unique<NewElement>()};
container.isUsedThisFrame = true;
return static_cast<NewElement*>(it->second.elem.get());
}
template <typename T> T& insert_any(const T& def) {
auto [it, inserted] = elements.emplace(idStack, ElementContainer());
auto& container = it->second;
if(inserted)
it->second.extra = def;
container.isUsedThisFrame = true;
return std::any_cast<T&>(it->second.extra);
}