Skip to content

Commit

Permalink
[LuaGUI] Now allows defining a closing key.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Jul 8, 2020
1 parent 0d7b1fa commit 70f58e7
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 12 deletions.
5 changes: 5 additions & 0 deletions docs/lua-api-gui.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions docs/lua-api-key.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
```
Expand All @@ -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
```

Expand Down
4 changes: 2 additions & 2 deletions mods/creative_inventory/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ 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
items[#items + 1] = {v:string_id()}
end
end

local gui = LuaGUI.new()
local gui = LuaGUI.new(keyID)

gui:set_size(195, 136)
gui:set_centered(true)
Expand Down
4 changes: 2 additions & 2 deletions mods/default/inventory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions source/client/states/LuaGUIState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "NetworkUtils.hpp"
#include "Player.hpp"
#include "ProgressBarWidget.hpp"
#include "Registry.hpp"
#include "ScrollBarWidget.hpp"
#include "TextButton.hpp"

Expand All @@ -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();
Expand All @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions source/client/states/LuaGUIState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ class LuaGUIState : public InterfaceState {
u16 m_height = 0;

bool m_isCentered = false;

s16 m_keyID = -1;
};

#endif // LUAGUISTATE_HPP_
4 changes: 3 additions & 1 deletion source/server/lua/LuaGUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -108,6 +108,8 @@ void LuaGUI::show(ClientInfo &client) {

void LuaGUI::initUsertype(sol::state &lua) {
lua.new_usertype<LuaGUI>("LuaGUI",
sol::constructors<LuaGUI(), LuaGUI(s16)>(),

"image", &LuaGUI::addImage,
"button", &LuaGUI::addTextButton,
"inventory", &LuaGUI::addInventoryWidget,
Expand Down
5 changes: 5 additions & 0 deletions source/server/lua/LuaGUI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -60,6 +63,8 @@ class LuaGUI {

std::list<Inventory> m_inventoryList;
std::list<std::unique_ptr<WidgetDef>> m_widgetDefinitions;

s16 m_keyID = -1;
};

#endif // LUAGUI_HPP_
2 changes: 1 addition & 1 deletion source/server/network/ServerCommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

Expand Down

0 comments on commit 70f58e7

Please sign in to comment.