diff --git a/res/modules/internal/gui_util.lua b/res/modules/internal/gui_util.lua index 294a7c79..c44e2fbe 100644 --- a/res/modules/internal/gui_util.lua +++ b/res/modules/internal/gui_util.lua @@ -102,6 +102,7 @@ end setmetatable(RadioGroup, RadioGroup) gui_util.Document = Document +gui_util.Element = Element gui_util.RadioGroup = RadioGroup return gui_util diff --git a/res/scripts/stdlib.lua b/res/scripts/stdlib.lua index 5fa9ca29..a0f18e6d 100644 --- a/res/scripts/stdlib.lua +++ b/res/scripts/stdlib.lua @@ -186,9 +186,14 @@ end gui_util = require "core:internal/gui_util" Document = gui_util.Document +Element = gui_util.Element RadioGroup = gui_util.RadioGroup __vc_page_loader = gui_util.load_page +function __vc_get_document_node(docname, nodeid) + return Element.new(docname, nodeid) +end + _GUI_ROOT = Document.new("core:root") _MENU = _GUI_ROOT.menu menu = _MENU diff --git a/src/logic/scripting/lua/libs/libgui.cpp b/src/logic/scripting/lua/libs/libgui.cpp index 2df7f322..a70e7136 100644 --- a/src/logic/scripting/lua/libs/libgui.cpp +++ b/src/logic/scripting/lua/libs/libgui.cpp @@ -341,6 +341,29 @@ static int p_get_data(UINode* node, lua::State* L) { return 0; } +static int p_get_parent(UINode* node, lua::State* L) { + auto parent = node->getParent(); + if (!parent) { + return 0; + } + auto id = parent->getId(); + if (id.empty()) { + id = "#" + std::to_string(reinterpret_cast(parent)); + } + parent->setId(id); + + auto docname = lua::require_string(L, 1); + auto element = lua::require_string(L, 2); + auto docnode = get_document_node_impl(L, docname, element); + UINode::getIndices( + parent->shared_from_this(), docnode.document->getMapWriteable() + ); + lua::requireglobal(L, "__vc_get_document_node"); + lua::pushvalue(L, 1); + lua::pushstring(L, id); + return lua::call(L, 2, 1); +} + static int p_get_add(UINode* node, lua::State* L) { if (dynamic_cast(node)) { return lua::pushcfunction(L, lua::wrap); @@ -492,6 +515,7 @@ static int l_gui_getattr(lua::State* L) { {"focused", p_get_focused}, {"cursor", p_get_cursor}, {"data", p_get_data}, + {"parent", p_get_parent}, }; auto func = getters.find(attr); if (func != getters.end()) {