generated ui nodes indexing

This commit is contained in:
MihailRis 2024-04-03 19:29:53 +03:00
parent e57b0eca15
commit 1a4c70d21a
6 changed files with 50 additions and 58 deletions

View File

@ -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("<label id='l_"..id.."'>-</label>")
-- value track-bar
document.tracks_panel:add(string.format(
"<trackbar id='t_%s' min='0' max='1' value='1' step='0.01' track-width='5' "..
" consumer='on_%s_change'/>"
, 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

View File

@ -13,17 +13,21 @@ UiDocument::UiDocument(
std::shared_ptr<gui::UINode> root,
std::unique_ptr<scripting::Environment> 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<gui::UINode> node) {
const std::string& id = node->getId();
if (!id.empty()) {
map[id] = node;
}
auto container = std::dynamic_pointer_cast<gui::Container>(node);
if (container) {
for (auto subnode : container->getNodes()) {
buildIndices(map, subnode);
}
}
}
std::unique_ptr<UiDocument> 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);

View File

@ -42,12 +42,11 @@ public:
const std::string& getId() const;
const uinodes_map& getMap() const;
uinodes_map& getMapWriteable();
const std::shared_ptr<gui::UINode> getRoot() const;
const std::shared_ptr<gui::UINode> 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<gui::UINode> node);
static std::unique_ptr<UiDocument> read(int env, std::string name, fs::path file);
static std::shared_ptr<gui::UINode> readElement(fs::path file);

View File

@ -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<UINode> node,
std::unordered_map<std::string, std::shared_ptr<UINode>>& map
) {
const std::string& id = node->getId();
if (!id.empty()) {
map[id] = node;
}
auto container = std::dynamic_pointer_cast<gui::Container>(node);
if (container) {
for (auto subnode : container->getNodes()) {
getIndices(subnode, map);
}
}
}

View File

@ -6,6 +6,7 @@
#include <memory>
#include <string>
#include <functional>
#include <unordered_map>
#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<UINode> node,
std::unordered_map<std::string, std::shared_ptr<UINode>>& map
);
};
}

View File

@ -219,7 +219,9 @@ static int container_add(lua_State* L) {
auto node = dynamic_cast<gui::Container*>(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());
}