diff --git a/docs/lua-api-gui.md b/docs/lua-api-gui.md index fa32db1fb..f89f78993 100644 --- a/docs/lua-api-gui.md +++ b/docs/lua-api-gui.md @@ -6,6 +6,11 @@ You can create a new GUI with: ```lua local gui = LuaGUI.new() ``` +or +```lua +local gui = LuaGUI.new(keyID) +``` +where `keyID` is the ID of the key to use for closing the GUI along with `Esc` key. ## Set the size of the gui diff --git a/docs/lua-api-key.md b/docs/lua-api-key.md index e7ec8beeb..2af0c86d0 100644 --- a/docs/lua-api-key.md +++ b/docs/lua-api-key.md @@ -8,8 +8,8 @@ mod:key { name = "Inventory", default_key = "E", - callback = function(client, screen_width, screen_height, gui_scale) - show_inventory(client, screen_width, screen_height, gui_scale) + callback = function(keyID, client, screen_width, screen_height, gui_scale) + show_inventory(keyID, client, screen_width, screen_height, gui_scale) end } ``` @@ -22,8 +22,8 @@ Function called when the key is pressed. Example: ```lua -callback = function(client, screen_width, screen_height, gui_scale) - show_inventory(client, screen_width, screen_height, gui_scale) +callback = function(keyID, client, screen_width, screen_height, gui_scale) + show_inventory(keyID, client, screen_width, screen_height, gui_scale) end ``` diff --git a/mods/creative_inventory/init.lua b/mods/creative_inventory/init.lua index fc8463ff7..269afc33e 100644 --- a/mods/creative_inventory/init.lua +++ b/mods/creative_inventory/init.lua @@ -36,7 +36,7 @@ mod:key { name = "Creative window", default_key = "H", - callback = function(client, screen_width, screen_height, gui_scale) + callback = function(keyID, client, screen_width, screen_height, gui_scale) items = {} for k, v in pairs(openminer.registry:items()) do if k ~= 1 and not v:has_group("group:ci_ignore") then @@ -44,7 +44,7 @@ mod:key { end end - local gui = LuaGUI.new() + local gui = LuaGUI.new(keyID) gui:set_size(195, 136) gui:set_centered(true) diff --git a/mods/default/inventory.lua b/mods/default/inventory.lua index 5c54af435..943fae46e 100644 --- a/mods/default/inventory.lua +++ b/mods/default/inventory.lua @@ -31,8 +31,8 @@ mod:key { name = "Inventory", default_key = "E", - callback = function(client, screen_width, screen_height, gui_scale) - local gui = LuaGUI.new() + callback = function(keyID, client, screen_width, screen_height, gui_scale) + local gui = LuaGUI.new(keyID) gui:set_size(176, 166) gui:set_centered(true) diff --git a/source/client/states/LuaGUIState.cpp b/source/client/states/LuaGUIState.cpp index 4feacefdd..bdff70c7c 100644 --- a/source/client/states/LuaGUIState.cpp +++ b/source/client/states/LuaGUIState.cpp @@ -43,6 +43,7 @@ #include "NetworkUtils.hpp" #include "Player.hpp" #include "ProgressBarWidget.hpp" +#include "Registry.hpp" #include "ScrollBarWidget.hpp" #include "TextButton.hpp" @@ -57,7 +58,7 @@ LuaGUIState::LuaGUIState(ClientCommandHandler &client, ClientPlayer &player, Cli m_mainWidget.setScale(Config::guiScale, Config::guiScale); - packet >> m_width >> m_height >> m_isCentered; + packet >> m_width >> m_height >> m_isCentered >> m_keyID; if (m_isCentered) centerMainWidget(); @@ -74,7 +75,8 @@ void LuaGUIState::onEvent(const SDL_Event &event) { if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED && m_isCentered) centerMainWidget(); - if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) { + if (event.type == SDL_KEYDOWN && (event.key.keysym.sym == SDLK_ESCAPE + || (m_keyID >= 0 && event.key.keysym.sym == Registry::getInstance().getKey(m_keyID).keycode()))) { gk::Mouse::setCursorGrabbed(true); gk::Mouse::setCursorVisible(false); gk::Mouse::resetToWindowCenter(); diff --git a/source/client/states/LuaGUIState.hpp b/source/client/states/LuaGUIState.hpp index eefd94a90..8bc19b113 100644 --- a/source/client/states/LuaGUIState.hpp +++ b/source/client/states/LuaGUIState.hpp @@ -87,6 +87,8 @@ class LuaGUIState : public InterfaceState { u16 m_height = 0; bool m_isCentered = false; + + s16 m_keyID = -1; }; #endif // LUAGUISTATE_HPP_ diff --git a/source/server/lua/LuaGUI.cpp b/source/server/lua/LuaGUI.cpp index b2218ec0e..b34e7de11 100644 --- a/source/server/lua/LuaGUI.cpp +++ b/source/server/lua/LuaGUI.cpp @@ -95,7 +95,7 @@ void LuaGUI::show(ClientInfo &client) { Network::Packet packet; packet << Network::Command::BlockGUIData; - packet << m_width << m_height << m_isCentered; + packet << m_width << m_height << m_isCentered << m_keyID; for (auto &it : m_inventoryList) packet << u8(LuaWidget::Inventory) << it.name() << it; @@ -108,6 +108,8 @@ void LuaGUI::show(ClientInfo &client) { void LuaGUI::initUsertype(sol::state &lua) { lua.new_usertype("LuaGUI", + sol::constructors(), + "image", &LuaGUI::addImage, "button", &LuaGUI::addTextButton, "inventory", &LuaGUI::addInventoryWidget, diff --git a/source/server/lua/LuaGUI.hpp b/source/server/lua/LuaGUI.hpp index 5778152c5..f4c3b9f41 100644 --- a/source/server/lua/LuaGUI.hpp +++ b/source/server/lua/LuaGUI.hpp @@ -37,6 +37,9 @@ class ClientInfo; // This class is meant to be used ONLY in Lua class LuaGUI { public: + LuaGUI() = default; + LuaGUI(s16 keyID) : m_keyID(keyID) {} + void addImage(const sol::table &table); void addTextButton(const sol::table &table); void addInventoryWidget(const sol::table &table); @@ -60,6 +63,8 @@ class LuaGUI { std::list m_inventoryList; std::list> m_widgetDefinitions; + + s16 m_keyID = -1; }; #endif // LUAGUI_HPP_ diff --git a/source/server/network/ServerCommandHandler.cpp b/source/server/network/ServerCommandHandler.cpp index 448fa1a0d..8781c26fa 100644 --- a/source/server/network/ServerCommandHandler.cpp +++ b/source/server/network/ServerCommandHandler.cpp @@ -406,7 +406,7 @@ void ServerCommandHandler::setupCallbacks() { u8 guiScale; packet >> keyID >> screenWidth >> screenHeight >> guiScale; - m_registry.getKey(keyID).callback()(client, screenWidth, screenHeight, guiScale); + m_registry.getKey(keyID).callback()(keyID, client, screenWidth, screenHeight, guiScale); }); }