From b66bee5f825ea2c4d0c592328e195e45ac8c0e4b Mon Sep 17 00:00:00 2001 From: Federico Cecchetto Date: Wed, 15 Sep 2021 03:34:43 +0200 Subject: [PATCH] UI element attributes --- src/client/game/ui_scripting/element.hpp | 1 + src/client/game/ui_scripting/lua/context.cpp | 99 ++++++++++++++++++++ 2 files changed, 100 insertions(+) diff --git a/src/client/game/ui_scripting/element.hpp b/src/client/game/ui_scripting/element.hpp index 952daf74..8b167380 100644 --- a/src/client/game/ui_scripting/element.hpp +++ b/src/client/game/ui_scripting/element.hpp @@ -61,6 +61,7 @@ namespace ui_scripting alignment horzalign = alignment::start; alignment vertalign = alignment::start; + std::unordered_map attributes = {}; std::string font = "default"; std::string material = "white"; std::string border_material = "white"; diff --git a/src/client/game/ui_scripting/lua/context.cpp b/src/client/game/ui_scripting/lua/context.cpp index a700ad6b..212c354f 100644 --- a/src/client/game/ui_scripting/lua/context.cpp +++ b/src/client/game/ui_scripting/lua/context.cpp @@ -426,6 +426,21 @@ namespace ui_scripting::lua handler.dispatch(event); }; + element_type[sol::meta_function::new_index] = [](element& element, const std::string& attribute, const std::string& value) + { + element.attributes[attribute] = value; + }; + + element_type[sol::meta_function::index] = [](element& element, const sol::this_state s, const std::string& attribute) + { + if (element.attributes.find(attribute) == element.attributes.end()) + { + return sol::lua_value{s, sol::lua_nil}; + } + + return sol::lua_value{s, element.attributes[attribute]}; + }; + auto menu_type = state.new_usertype("menu"); menu_type["onnotify"] = [&handler](menu& menu, const std::string& event, @@ -516,12 +531,96 @@ namespace ui_scripting::lua menu.close(); }; + menu_type["getelement"] = [](menu& menu, const sol::this_state s, const std::string& value, const std::string& attribute) + { + for (const auto& element : menu.children) + { + if (element->attributes.find(attribute) != element->attributes.end() && element->attributes[attribute] == value) + { + return sol::lua_value{s, element}; + } + } + + return sol::lua_value{s, sol::lua_nil}; + }; + + menu_type["getelements"] = sol::overload + ( + [](menu& menu, const sol::this_state s, const std::string& value, const std::string& attribute) + { + auto result = sol::table::create(s.lua_state()); + + for (const auto& element : menu.children) + { + if (element->attributes.find(attribute) != element->attributes.end() && element->attributes[attribute] == value) + { + result.add(element); + } + } + + return result; + }, + [](menu& menu, const sol::this_state s) + { + auto result = sol::table::create(s.lua_state()); + + for (const auto& element : menu.children) + { + result.add(element); + } + + return result; + } + ); + struct game { }; auto game_type = state.new_usertype("game_"); state["game"] = game(); + game_type["getelement"] = [](const game&, const sol::this_state s, const std::string& value, const std::string& attribute) + { + for (const auto& element : elements) + { + if (element->attributes.find(attribute) != element->attributes.end() && element->attributes[attribute] == value) + { + return sol::lua_value{s, element}; + } + } + + return sol::lua_value{s, sol::lua_nil}; + }; + + game_type["getelements"] = sol::overload + ( + [](const game&, const sol::this_state s, const std::string& value, const std::string& attribute) + { + auto result = sol::table::create(s.lua_state()); + + for (const auto& element : elements) + { + if (element->attributes.find(attribute) != element->attributes.end() && element->attributes[attribute] == value) + { + result.add(element); + } + } + + return result; + }, + [](const game&, const sol::this_state s) + { + auto result = sol::table::create(s.lua_state()); + + for (const auto& element : elements) + { + result.add(element); + } + + return result; + } + ); + game_type["time"] = []() { const auto now = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch());