From 0c0d850d5d2980ec39b871c992553c4e3f957c50 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 20 Mar 2024 11:14:43 +0300 Subject: [PATCH] lua: hud.pause/resume + menu:back() method --- src/frontend/hud.cpp | 4 ++++ src/logic/scripting/lua/libgui.cpp | 36 +++++++++++++++++++++--------- src/logic/scripting/lua/libhud.cpp | 12 ++++++++++ 3 files changed, 42 insertions(+), 10 deletions(-) diff --git a/src/frontend/hud.cpp b/src/frontend/hud.cpp index c9fd8b77..b13de22c 100644 --- a/src/frontend/hud.cpp +++ b/src/frontend/hud.cpp @@ -545,6 +545,10 @@ void Hud::setPause(bool pause) { return; } this->pause = pause; + + if (inventoryOpen) { + closeInventory(); + } auto menu = gui->getMenu(); if (pause) { diff --git a/src/logic/scripting/lua/libgui.cpp b/src/logic/scripting/lua/libgui.cpp index cfbc1013..f19a778d 100644 --- a/src/logic/scripting/lua/libgui.cpp +++ b/src/logic/scripting/lua/libgui.cpp @@ -104,16 +104,6 @@ static bool getattr(lua_State* L, gui::FullCheckBox* box, const std::string& att return false; } -static bool getattr(lua_State* L, gui::PagesControl* menu, const std::string& attr) { - if (menu == nullptr) - return false; - if (attr == "page") { - lua_pushstring(L, menu->getCurrentName().c_str()); - return true; - } - return false; -} - static bool getattr(lua_State* L, gui::TextBox* box, const std::string& attr) { if (box == nullptr) return false; @@ -127,6 +117,32 @@ static bool getattr(lua_State* L, gui::TextBox* box, const std::string& attr) { return false; } +static int menu_back(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); + + auto menu = dynamic_cast(node); + menu->back(); + return 0; +} + +static bool getattr(lua_State* L, gui::PagesControl* menu, const std::string& attr) { + if (menu == nullptr) + return false; + if (attr == "page") { + lua_pushstring(L, menu->getCurrentName().c_str()); + return true; + } else if (attr == "back") { + lua_pushcfunction(L, menu_back); + return true; + } + return false; +} + static bool setattr(lua_State* L, gui::FullCheckBox* box, const std::string& attr) { if (box == nullptr) return false; diff --git a/src/logic/scripting/lua/libhud.cpp b/src/logic/scripting/lua/libhud.cpp index 971b4931..6afcfffc 100644 --- a/src/logic/scripting/lua/libhud.cpp +++ b/src/logic/scripting/lua/libhud.cpp @@ -99,6 +99,16 @@ static int l_hud_close(lua_State* L) { return 0; } +static int l_hud_pause(lua_State* L) { + scripting::hud->setPause(true); + return 0; +} + +static int l_hud_resume(lua_State* L) { + scripting::hud->setPause(false); + return 0; +} + const luaL_Reg hudlib [] = { {"open_inventory", lua_wrap_errors}, {"close_inventory", lua_wrap_errors}, @@ -106,5 +116,7 @@ const luaL_Reg hudlib [] = { {"open_permanent", lua_wrap_errors}, {"show_overlay", lua_wrap_errors}, {"close", lua_wrap_errors}, + {"pause", lua_wrap_errors}, + {"resume", lua_wrap_errors}, {NULL, NULL} };