diff --git a/res/scripts/stdlib.lua b/res/scripts/stdlib.lua index d5cd34bd..79482c3e 100644 --- a/res/scripts/stdlib.lua +++ b/res/scripts/stdlib.lua @@ -229,7 +229,7 @@ function gui.template(name, params) text = text:gsub("%%{([^}]+)}", function(n) local s = params[n] if type(s) ~= "string" then - return s + return tostring(s) end if #s == 0 then return diff --git a/src/graphics/ui/elements/Container.cpp b/src/graphics/ui/elements/Container.cpp index eddde7e2..b930d76a 100644 --- a/src/graphics/ui/elements/Container.cpp +++ b/src/graphics/ui/elements/Container.cpp @@ -71,10 +71,7 @@ void Container::mouseRelease(int x, int y) { } void Container::act(float delta) { - if (mustRefresh) { - refresh(); - mustRefresh = false; - } + UINode::act(delta); for (const auto& node : nodes) { if (node->isVisible()) { node->act(delta); @@ -167,6 +164,12 @@ void Container::add(const std::shared_ptr& node) { node->setParent(this); node->reposition(); mustRefresh = true; + + auto parent = getParent(); + while (parent) { + parent->setMustRefresh(); + parent = parent->getParent(); + } } void Container::add(const std::shared_ptr& node, glm::vec2 pos) { diff --git a/src/graphics/ui/elements/Container.hpp b/src/graphics/ui/elements/Container.hpp index 459d5b07..d3dbbfc7 100644 --- a/src/graphics/ui/elements/Container.hpp +++ b/src/graphics/ui/elements/Container.hpp @@ -9,7 +9,6 @@ namespace gui { class Container : public UINode, public ::util::ObjectsKeeper { int prevScrollY = -1; - bool mustRefresh = true; protected: std::vector> nodes; std::vector intervalEvents; diff --git a/src/graphics/ui/elements/UINode.hpp b/src/graphics/ui/elements/UINode.hpp index e03eb91e..53854db5 100644 --- a/src/graphics/ui/elements/UINode.hpp +++ b/src/graphics/ui/elements/UINode.hpp @@ -66,6 +66,7 @@ namespace gui { class UINode : public std::enable_shared_from_this { protected: GUI& gui; + bool mustRefresh = true; private: /// @brief element identifier used for direct access in UiDocument std::string id = ""; @@ -131,7 +132,12 @@ namespace gui { /// @brief Called every frame for all visible elements /// @param delta delta timУ - virtual void act(float delta) {}; + virtual void act(float delta) { + if (mustRefresh) { + mustRefresh = false; + refresh(); + } + }; virtual void draw(const DrawContext& pctx, const Assets& assets) = 0; virtual void setVisible(bool flag); @@ -275,5 +281,9 @@ namespace gui { const std::shared_ptr& node, const std::string& id ); + + void setMustRefresh() { + mustRefresh = true; + } }; }