From d8b3920d6549fd6b466bd59bf35e433f8c7ab145 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sat, 17 Feb 2024 13:58:12 +0300 Subject: [PATCH] ui: more xml support + hud.close function --- src/frontend/gui/gui_xml.cpp | 14 ++++++++++++++ src/logic/scripting/lua/libhud.cpp | 15 +++++++++++++-- src/logic/scripting/lua/libhud.h | 2 ++ src/logic/scripting/scripting_functional.cpp | 15 +++++++++++++++ src/logic/scripting/scripting_functional.h | 6 ++++++ 5 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/frontend/gui/gui_xml.cpp b/src/frontend/gui/gui_xml.cpp index 8f395dfa..13065104 100644 --- a/src/frontend/gui/gui_xml.cpp +++ b/src/frontend/gui/gui_xml.cpp @@ -40,6 +40,12 @@ static void _readUINode(UiXmlReader& reader, xml::xmlelement element, UINode& no if (element->has("z-index")) { node.setZIndex(element->attr("z-index").asInt()); } + if (element->has("interactive")) { + node.setInteractive(element->attr("interactive").asBool()); + } + if (element->has("visible")) { + node.setVisible(element->attr("visible").asBool()); + } if (element->has("position-func")) { auto supplier = scripting::create_vec2_supplier( reader.getEnvironment().getId(), @@ -202,6 +208,14 @@ static std::shared_ptr readTrackBar(UiXmlReader& reader, xml::xmlelement ); bar->setConsumer(consumer); } + if (element->has("supplier")) { + auto supplier = scripting::create_number_supplier( + reader.getEnvironment().getId(), + element->attr("supplier").getText(), + reader.getFilename()+".lua" + ); + bar->setSupplier(supplier); + } return bar; } diff --git a/src/logic/scripting/lua/libhud.cpp b/src/logic/scripting/lua/libhud.cpp index 0a834fc2..c058f73f 100644 --- a/src/logic/scripting/lua/libhud.cpp +++ b/src/logic/scripting/lua/libhud.cpp @@ -15,6 +15,7 @@ #include "../../../logic/BlocksController.h" #include "../../../items/Inventories.h" #include "../../../engine.h" +#include "../../../frontend/UiDocument.h" namespace scripting { extern Hud* hud; @@ -64,13 +65,23 @@ int l_hud_open_block(lua_State* L) { return 2; } -int l_hud_open_permanent(lua_State* L) { +UiDocument* require_layout(lua_State* L, const char* name) { auto assets = scripting::engine->getAssets(); - auto name = lua_tostring(L, 1); auto layout = assets->getLayout(name); if (layout == nullptr) { luaL_error(L, "layout '%s' is not found", name); } + return layout; +} + +int l_hud_open_permanent(lua_State* L) { + auto layout = require_layout(L, lua_tostring(L, 1)); scripting::hud->openPermanent(layout); return 0; } + +int l_hud_close(lua_State* L) { + auto layout = require_layout(L, lua_tostring(L, 1)); + scripting::hud->remove(layout->getRoot()); + return 0; +} diff --git a/src/logic/scripting/lua/libhud.h b/src/logic/scripting/lua/libhud.h index 259f0653..167b5f03 100644 --- a/src/logic/scripting/lua/libhud.h +++ b/src/logic/scripting/lua/libhud.h @@ -7,12 +7,14 @@ extern int l_hud_open_inventory(lua_State* L); extern int l_hud_close_inventory(lua_State* L); extern int l_hud_open_block(lua_State* L); extern int l_hud_open_permanent(lua_State* L); +extern int l_hud_close(lua_State* L); static const luaL_Reg hudlib [] = { {"open_inventory", l_hud_open_inventory}, {"close_inventory", l_hud_close_inventory}, {"open_block", l_hud_open_block}, {"open_permanent", l_hud_open_permanent}, + {"close", l_hud_close}, {NULL, NULL} }; diff --git a/src/logic/scripting/scripting_functional.cpp b/src/logic/scripting/scripting_functional.cpp index 5fc497b8..520de0a0 100644 --- a/src/logic/scripting/scripting_functional.cpp +++ b/src/logic/scripting/scripting_functional.cpp @@ -60,6 +60,21 @@ doubleconsumer scripting::create_number_consumer( }; } +doublesupplier scripting::create_number_supplier( + int env, + const std::string& src, + const std::string& file +) { + return [=](){ + if (processCallback(env, src, file)) { + state->callNoThrow(0); + lua::luanumber x = state->tonumber(-1); state->pop(); + return x; + } + return 0.0; + }; +} + int_array_consumer scripting::create_int_array_consumer( int env, const std::string& src, diff --git a/src/logic/scripting/scripting_functional.h b/src/logic/scripting/scripting_functional.h index 919e541a..d608eded 100644 --- a/src/logic/scripting/scripting_functional.h +++ b/src/logic/scripting/scripting_functional.h @@ -24,6 +24,12 @@ namespace scripting { const std::string& file="" ); + doublesupplier create_number_supplier( + int env, + const std::string& src, + const std::string& file="" + ); + int_array_consumer create_int_array_consumer( int env, const std::string& src,