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
+6 -15
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);
+1 -2
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);
+17
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);
}
}
}
+7
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
);
};
}
+3 -1
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());
}