From 124986199ec5a092ef6946dc961409753ab9d3df Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 20 Mar 2024 20:32:10 +0300 Subject: [PATCH] lua: container element add method --- src/logic/scripting/lua/libgui.cpp | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/logic/scripting/lua/libgui.cpp b/src/logic/scripting/lua/libgui.cpp index f19a778d..11349544 100644 --- a/src/logic/scripting/lua/libgui.cpp +++ b/src/logic/scripting/lua/libgui.cpp @@ -8,6 +8,7 @@ #include "../../../engine.h" #include "../../../assets/Assets.h" +#include "../../../graphics/ui/gui_util.h" #include "../../../graphics/ui/elements/UINode.h" #include "../../../graphics/ui/elements/controls.h" #include "../../../frontend/UiDocument.h" @@ -117,14 +118,18 @@ static bool getattr(lua_State* L, gui::TextBox* box, const std::string& attr) { return false; } -static int menu_back(lua_State* L) { +static gui::UINode* getDocumentNode(lua_State* L) { lua_getfield(L, 1, "docname"); lua_getfield(L, 1, "name"); auto docname = lua_tostring(L, -2); auto name = lua_tostring(L, -1); auto node = getDocumentNode(L, docname, name); lua_pop(L, 2); + return node; +} +static int menu_back(lua_State* L) { + auto node = getDocumentNode(L); auto menu = dynamic_cast(node); menu->back(); return 0; @@ -203,6 +208,24 @@ static bool setattr(lua_State* L, gui::PagesControl* menu, const std::string& at return false; } +static int container_add(lua_State* L) { + auto node = dynamic_cast(getDocumentNode(L)); + auto xmlsrc = lua_tostring(L, 2); + node->add(guiutil::create(xmlsrc)); + return 0; +} + +static bool getattr(lua_State* L, gui::Container* container, const std::string& attr) { + if (container == nullptr) + return false; + + if (attr == "add") { + lua_pushcfunction(L, container_add); + return true; + } + return false; +} + static int l_gui_getattr(lua_State* L) { auto docname = lua_tostring(L, 1); auto element = lua_tostring(L, 2); @@ -225,6 +248,8 @@ static int l_gui_getattr(lua_State* L) { return 1; } + if (getattr(L, dynamic_cast(node), attr)) + return 1; if (getattr(L, dynamic_cast(node), attr)) return 1; if (getattr(L, dynamic_cast(node), attr))