From 1a4c70d21a7dcc8c735ca596442193c87d834aaf Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 3 Apr 2024 19:29:53 +0300 Subject: [PATCH] generated ui nodes indexing --- res/layouts/pages/settings-audio.xml.lua | 56 +++++++----------------- src/frontend/UiDocument.cpp | 21 +++------ src/frontend/UiDocument.h | 3 +- src/graphics/ui/elements/UINode.cpp | 17 +++++++ src/graphics/ui/elements/UINode.h | 7 +++ src/logic/scripting/lua/libgui.cpp | 4 +- 6 files changed, 50 insertions(+), 58 deletions(-) diff --git a/res/layouts/pages/settings-audio.xml.lua b/res/layouts/pages/settings-audio.xml.lua index f23426e8..efd925d6 100644 --- a/res/layouts/pages/settings-audio.xml.lua +++ b/res/layouts/pages/settings-audio.xml.lua @@ -1,28 +1,27 @@ function on_open() - new_volume_control("audio.volume-master", "master") - new_volume_control("audio.volume-regular", "regular") - new_volume_control("audio.volume-ui", "ui") - new_volume_control("audio.volume-ambient", "ambient") - new_volume_control("audio.volume-music", "music") - - gui.reindex("core:pages/settings-audio") - - on_master_change() - on_regular_change() - on_ui_change() - on_ambient_change() - on_music_change() + new_volume_control("audio.volume-master", "master", "Master Volume") + new_volume_control("audio.volume-regular", "regular", "Regular Sounds") + new_volume_control("audio.volume-ui", "ui", "UI Sounds") + new_volume_control("audio.volume-ambient", "ambient", "Ambient") + new_volume_control("audio.volume-music", "music", "Music") end - function new_volume_control(setting, id, name) -- value text label document.tracks_panel:add("") -- value track-bar document.tracks_panel:add(string.format( "" - , id, id)) + " consumer='function(x) on_volume_change(%q, %q, %q, x) end'/>" + , id, setting, id, name)) + refresh_label(setting, id, name) +end + +function refresh_label(setting, id, name) + document["l_"..id].text = ( + gui.str(name, "settings")..": ".. + core.str_setting(setting) + ) end function on_volume_change(setting, id, name, val) @@ -31,28 +30,5 @@ function on_volume_change(setting, id, name, val) else document["t_"..id].value = core.get_setting(setting, val) end - document["l_"..id].text = ( - gui.str(name, "settings")..": ".. - core.str_setting(setting) - ) -end - -function on_master_change(val) - on_volume_change("audio.volume-master", "master", "Master Volume", val) -end - -function on_regular_change(val) - on_volume_change("audio.volume-regular", "regular", "Regular Sounds", val) -end - -function on_ui_change(val) - on_volume_change("audio.volume-ui", "ui", "UI Sounds", val) -end - -function on_ambient_change(val) - on_volume_change("audio.volume-ambient", "ambient", "Ambient", val) -end - -function on_music_change(val) - on_volume_change("audio.volume-music", "music", "Music", val) + refresh_label(setting, id, name) end diff --git a/src/frontend/UiDocument.cpp b/src/frontend/UiDocument.cpp index 01d74aff..4ebfcbd2 100644 --- a/src/frontend/UiDocument.cpp +++ b/src/frontend/UiDocument.cpp @@ -13,17 +13,21 @@ UiDocument::UiDocument( std::shared_ptr root, std::unique_ptr env ) : id(id), script(script), root(root), env(std::move(env)) { - buildIndices(map, root); + gui::UINode::getIndices(root, map); } void UiDocument::rebuildIndices() { - buildIndices(map, root); + gui::UINode::getIndices(root, map); } const uinodes_map& UiDocument::getMap() const { return map; } +uinodes_map& UiDocument::getMapWriteable() { + return map; +} + const std::string& UiDocument::getId() const { return id; } @@ -48,19 +52,6 @@ int UiDocument::getEnvironment() const { return env->getId(); } -void UiDocument::buildIndices(uinodes_map& map, std::shared_ptr node) { - const std::string& id = node->getId(); - if (!id.empty()) { - map[id] = node; - } - auto container = std::dynamic_pointer_cast(node); - if (container) { - for (auto subnode : container->getNodes()) { - buildIndices(map, subnode); - } - } -} - std::unique_ptr UiDocument::read(int penv, std::string name, fs::path file) { const std::string text = files::read_string(file); auto xmldoc = xml::parse(file.u8string(), text); diff --git a/src/frontend/UiDocument.h b/src/frontend/UiDocument.h index 1de3c7ac..14cd4f9b 100644 --- a/src/frontend/UiDocument.h +++ b/src/frontend/UiDocument.h @@ -42,12 +42,11 @@ public: const std::string& getId() const; const uinodes_map& getMap() const; + uinodes_map& getMapWriteable(); const std::shared_ptr getRoot() const; const std::shared_ptr get(const std::string& id) const; const uidocscript& getScript() const; int getEnvironment() const; - // @brief Collect map of all uinodes having identifiers - static void buildIndices(uinodes_map& map, std::shared_ptr node); static std::unique_ptr read(int env, std::string name, fs::path file); static std::shared_ptr readElement(fs::path file); diff --git a/src/graphics/ui/elements/UINode.cpp b/src/graphics/ui/elements/UINode.cpp index 8bbd0ed3..485b96a9 100644 --- a/src/graphics/ui/elements/UINode.cpp +++ b/src/graphics/ui/elements/UINode.cpp @@ -1,5 +1,6 @@ #include "UINode.h" +#include "containers.h" #include "../../core/Batch2D.h" using gui::UINode; @@ -256,3 +257,19 @@ void UINode::setGravity(Gravity gravity) { reposition(); } } + +void UINode::getIndices( + std::shared_ptr node, + std::unordered_map>& map +) { + const std::string& id = node->getId(); + if (!id.empty()) { + map[id] = node; + } + auto container = std::dynamic_pointer_cast(node); + if (container) { + for (auto subnode : container->getNodes()) { + getIndices(subnode, map); + } + } +} diff --git a/src/graphics/ui/elements/UINode.h b/src/graphics/ui/elements/UINode.h index 5f53d0c4..7121c5f2 100644 --- a/src/graphics/ui/elements/UINode.h +++ b/src/graphics/ui/elements/UINode.h @@ -6,6 +6,7 @@ #include #include #include +#include #include "../../../delegates.h" #include "../../../window/input.h" @@ -193,6 +194,12 @@ namespace gui { void reposition(); virtual void setGravity(Gravity gravity); + + // @brief collect all nodes having id + static void getIndices( + std::shared_ptr node, + std::unordered_map>& map + ); }; } diff --git a/src/logic/scripting/lua/libgui.cpp b/src/logic/scripting/lua/libgui.cpp index ece5f19a..53c3efcb 100644 --- a/src/logic/scripting/lua/libgui.cpp +++ b/src/logic/scripting/lua/libgui.cpp @@ -219,7 +219,9 @@ static int container_add(lua_State* L) { auto node = dynamic_cast(docnode.node); auto xmlsrc = lua_tostring(L, 2); try { - node->add(guiutil::create(xmlsrc, docnode.document->getEnvironment())); + auto subnode = guiutil::create(xmlsrc, docnode.document->getEnvironment()); + node->add(subnode); + gui::UINode::getIndices(subnode, docnode.document->getMapWriteable()); } catch (const std::exception& err) { luaL_error(L, err.what()); }