[LuaGUI] 'set_size' and 'set_centered' added.

This commit is contained in:
Quentin Bazin 2020-03-07 15:41:19 +01:00
parent 5e0d6d7f6a
commit 5ee75e72f6
8 changed files with 80 additions and 44 deletions

@ -53,6 +53,11 @@ LuaGUIState::LuaGUIState(ClientCommandHandler &client, ClientPlayer &player, Cli
m_mainWidget.setScale(Config::guiScale, Config::guiScale);
packet >> m_width >> m_height >> m_isCentered;
if (m_isCentered)
centerMainWidget();
while (!packet.endOfPacket())
loadGUI(packet);
}
@ -60,6 +65,9 @@ LuaGUIState::LuaGUIState(ClientCommandHandler &client, ClientPlayer &player, Cli
void LuaGUIState::onEvent(const SDL_Event &event) {
InterfaceState::onEvent(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) {
gk::Mouse::setCursorGrabbed(true);
gk::Mouse::setCursorVisible(false);
@ -324,3 +332,9 @@ void LuaGUIState::loadInventory(const std::string &name, sf::Packet &packet) {
packet >> m_inventories.at(name);
}
void LuaGUIState::centerMainWidget() {
int x = floor(Config::screenWidth / 2.0f - m_width * Config::guiScale / 2.0f + 0.5f);
int y = floor(Config::screenHeight / 2.0f - m_height * Config::guiScale / 2.0f + 0.5f);
m_mainWidget.setPosition(x, y);
}

@ -59,6 +59,8 @@ class LuaGUIState : public InterfaceState {
void loadProgressBarWidget(const std::string &name, s32 x, s32 y, sf::Packet &packet);
void loadInventory(const std::string &name, sf::Packet &packet);
void centerMainWidget();
ClientCommandHandler &m_client;
Widget m_mainWidget;
@ -74,6 +76,11 @@ class LuaGUIState : public InterfaceState {
ClientPlayer &m_player;
ClientWorld &m_world;
u16 m_width = 0;
u16 m_height = 0;
bool m_isCentered = false;
};
#endif // LUAGUISTATE_HPP_

@ -39,15 +39,12 @@ mod:block {
on_block_activated = function(pos, player, world, client, screen_width, screen_height, gui_scale)
local gui = LuaGUI.new()
-- FIXME: Replace this by gui:set_size() and gui:set_centered()
local gui_pos = {
x = math.floor(screen_width / gui_scale / 2.0 - 176 / 2.0 + 0.5),
y = math.floor(screen_height / gui_scale / 2.0 - 166 / 2.0 + 0.5)
}
gui:set_size(176, 166)
gui:set_centered(true)
gui:progress_bar {
name = "bar_process",
pos = {x = gui_pos.x + 80, y = gui_pos.y + 35},
pos = {x = 80, y = 35},
type = 0, -- FIXME: Use string for that
@ -62,7 +59,7 @@ mod:block {
gui:progress_bar {
name = "bar_burn",
pos = {x = gui_pos.x + 57, y = gui_pos.y + 37},
pos = {x = 57, y = 37},
type = 1, -- FIXME: Use string for that
@ -77,7 +74,7 @@ mod:block {
gui:inventory {
name = "inv_input",
pos = {x = gui_pos.x + 55, y = gui_pos.y + 16},
pos = {x = 55, y = 16},
inventory = "block",
block = {x = pos.x, y = pos.y, z = pos.z},
@ -91,7 +88,7 @@ mod:block {
gui:inventory {
name = "inv_output",
pos = {x = gui_pos.x + 115, y = gui_pos.y + 34},
pos = {x = 115, y = 34},
inventory = "block",
block = {x = pos.x, y = pos.y, z = pos.z},
@ -105,7 +102,7 @@ mod:block {
gui:inventory {
name = "inv_fuel",
pos = {x = gui_pos.x + 55, y = gui_pos.y + 52},
pos = {x = 55, y = 52},
inventory = "block",
block = {x = pos.x, y = pos.y, z = pos.z},
@ -119,7 +116,7 @@ mod:block {
gui:inventory {
name = "inv_main",
pos = {x = gui_pos.x + 7, y = gui_pos.y + 83},
pos = {x = 7, y = 83},
inventory = "player",
player = "player",
@ -134,7 +131,7 @@ mod:block {
gui:inventory {
name = "inv_hotbar",
pos = {x = gui_pos.x + 7, y = gui_pos.y + 141},
pos = {x = 7, y = 141},
inventory = "player",
player = "player",
@ -149,7 +146,7 @@ mod:block {
gui:image {
name = "img_background",
pos = gui_pos,
pos = {x = 0, y = 0},
texture = "texture-furnace",
clip = {x = 0, y = 0, width = 176, height = 166},

@ -63,15 +63,12 @@ end
function show_inventory(client, screen_width, screen_height, gui_scale)
local gui = LuaGUI.new()
-- FIXME: Replace this by gui:set_size() and gui:set_centered()
local gui_pos = {
x = math.floor(screen_width / gui_scale / 2.0 - 176 / 2.0 + 0.5),
y = math.floor(screen_height / gui_scale / 2.0 - 166 / 2.0 + 0.5)
}
gui:set_size(176, 166)
gui:set_centered(true)
gui:image {
name = "img_background",
pos = gui_pos,
pos = {x = 0, y = 0},
texture = "texture-inventory",
clip = {x = 0, y = 0, width = 176, height = 166},
@ -79,7 +76,7 @@ function show_inventory(client, screen_width, screen_height, gui_scale)
gui:inventory {
name = "inv_main",
pos = {x = gui_pos.x + 7, y = gui_pos.y + 83},
pos = {x = 7, y = 83},
inventory = "player",
player = "player",
@ -93,7 +90,7 @@ function show_inventory(client, screen_width, screen_height, gui_scale)
gui:inventory {
name = "inv_hotbar",
pos = {x = gui_pos.x + 7, y = gui_pos.y + 141},
pos = {x = 7, y = 141},
inventory = "player",
player = "player",
@ -107,8 +104,8 @@ function show_inventory(client, screen_width, screen_height, gui_scale)
gui:crafting {
name = "inv_crafting",
pos = {x = gui_pos.x + 97, y = gui_pos.y + 17},
result_pos = {x = gui_pos.x + 97 + 56, y = gui_pos.y + 17 + 10},
pos = {x = 97, y = 17},
result_pos = {x = 97 + 56, y = 17 + 10},
inventory = "temp",
size = 2,
@ -122,15 +119,12 @@ end
function show_creative_window(client, screen_width, screen_height, gui_scale)
local gui = LuaGUI.new()
-- FIXME: Replace this by gui:set_size() and gui:set_centered()
local gui_pos = {
x = math.floor(screen_width / gui_scale / 2.0 - 195 / 2.0 + 0.5),
y = math.floor(screen_height / gui_scale / 2.0 - 136 / 2.0 + 0.5)
}
gui:set_size(195, 136)
gui:set_centered(true)
gui:image {
name = "img_background",
pos = gui_pos,
pos = {x = 0, y = 0},
texture = "texture-creative_window",
clip = {x = 0, y = 0, width = 195, height = 136},
@ -207,7 +201,7 @@ function show_creative_window(client, screen_width, screen_height, gui_scale)
gui:inventory {
name = "inv_creative_items",
pos = {x = gui_pos.x + 8, y = gui_pos.y + 17},
pos = {x = 8, y = 17},
inventory = "temp",
inventory_name = "inv_data",
@ -219,7 +213,7 @@ function show_creative_window(client, screen_width, screen_height, gui_scale)
gui:inventory {
name = "inv_hotbar",
pos = {x = gui_pos.x + 8, y = gui_pos.y + 111},
pos = {x = 8, y = 111},
inventory = "player",
player = "player",

@ -37,15 +37,12 @@ mod:block {
on_block_activated = function(pos, player, world, client, screen_width, screen_height, gui_scale)
local gui = LuaGUI.new()
-- FIXME: Replace this by gui:set_size() and gui:set_centered()
local gui_pos = {
x = math.floor(screen_width / gui_scale / 2.0 - 176 / 2.0 + 0.5),
y = math.floor(screen_height / gui_scale / 2.0 - 166 / 2.0 + 0.5)
}
gui:set_size(176, 166)
gui:set_centered(true)
gui:inventory {
name = "inv_main",
pos = {x = gui_pos.x + 7, y = gui_pos.y + 83},
pos = {x = 7, y = 83},
inventory = "player",
player = "player",
@ -59,7 +56,7 @@ mod:block {
gui:inventory {
name = "inv_hotbar",
pos = {x = gui_pos.x + 7, y = gui_pos.y + 141},
pos = {x = 7, y = 141},
inventory = "player",
player = "player",
@ -73,8 +70,8 @@ mod:block {
gui:crafting {
name = "inv_crafting",
pos = {x = gui_pos.x + 29, y = gui_pos.y + 16},
result_pos = {x = gui_pos.x + 123, y = gui_pos.y + 34},
pos = {x = 29, y = 16},
result_pos = {x = 123, y = 34},
inventory = "block",
block = {x = pos.x, y = pos.y, z = pos.z},
@ -84,7 +81,7 @@ mod:block {
gui:image {
name = "img_background",
pos = gui_pos,
pos = {x = 0, y = 0},
texture = "texture-workbench",
clip = {x = 0, y = 0, width = 176, height = 166},

@ -89,6 +89,8 @@ void LuaGUI::show(ClientInfo &client) {
sf::Packet packet;
packet << Network::Command::BlockGUIData;
packet << m_width << m_height << m_isCentered;
for (auto &it : m_inventoryList)
packet << u8(LuaWidget::Inventory) << it.name() << it;
@ -107,6 +109,9 @@ void LuaGUI::initUsertype(sol::state &lua) {
"progress_bar", &LuaGUI::addProgressBarWidget,
"inventory_data", &LuaGUI::addInventory,
"set_size", &LuaGUI::setSize,
"set_centered", &LuaGUI::setCentered,
"show", &LuaGUI::show
);
}

@ -44,13 +44,21 @@ class LuaGUI {
void addProgressBarWidget(const sol::table &table);
void addInventory(const sol::table &table);
void setSize(u16 width, u16 height) { m_width = width; m_height = height; }
void setCentered(bool isCentered) { m_isCentered = isCentered; }
void show(ClientInfo &client);
static void initUsertype(sol::state &lua);
private:
std::list<std::unique_ptr<WidgetDef>> m_widgetDefinitions;
u16 m_width = 0;
u16 m_height = 0;
bool m_isCentered = false;
std::list<Inventory> m_inventoryList;
std::list<std::unique_ptr<WidgetDef>> m_widgetDefinitions;
};
#endif // LUAGUI_HPP_

@ -173,7 +173,14 @@ void ServerCommandHandler::setupCallbacks() {
u8 guiScale;
packet >> screenWidth >> screenHeight >> guiScale;
m_scriptEngine.lua()["show_inventory"](client, screenWidth, screenHeight, guiScale);
sol::unsafe_function func = m_scriptEngine.lua()["show_inventory"];
try {
func(client, screenWidth, screenHeight, guiScale);
}
catch (const sol::error &error) {
DEBUG("Failed to send inventory GUI\n", error.what());
}
});
m_server.setCommandCallback(Network::Command::PlayerCreativeWindow, [this](ClientInfo &client, sf::Packet &packet) {
@ -181,7 +188,14 @@ void ServerCommandHandler::setupCallbacks() {
u8 guiScale;
packet >> screenWidth >> screenHeight >> guiScale;
m_scriptEngine.lua()["show_creative_window"](client, screenWidth, screenHeight, guiScale);
sol::unsafe_function func = m_scriptEngine.lua()["show_creative_window"];
try {
func(client, screenWidth, screenHeight, guiScale);
}
catch (const sol::error &error) {
DEBUG("Failed to send creative window GUI\n", error.what());
}
});
m_server.setCommandCallback(Network::Command::BlockActivated, [this](ClientInfo &client, sf::Packet &packet) {