From 442bafca689811b09c5422965778189f93793b00 Mon Sep 17 00:00:00 2001 From: Quentin Bazin Date: Sat, 8 Feb 2020 02:48:39 +0900 Subject: [PATCH] [InventoryState] Replaced by 'show_inventory' function in init.lua. --- client/include/gui/PlayerInventoryWidget.hpp | 6 +- .../include/network/ClientCommandHandler.hpp | 1 + client/include/states/InventoryState.hpp | 49 -------------- client/source/gui/PlayerInventoryWidget.cpp | 9 +-- .../source/network/ClientCommandHandler.cpp | 6 ++ client/source/states/GameState.cpp | 4 +- client/source/states/InventoryState.cpp | 66 ------------------- client/source/states/LuaGUIState.cpp | 20 ++++-- common/include/core/LuaWidget.hpp | 30 +++++++++ common/include/network/Network.hpp | 1 + common/source/network/Network.cpp | 1 + mods/default/init.lua | 17 +++++ server/include/lua/LuaGUI.hpp | 10 +-- server/source/lua/LuaGUI.cpp | 39 ++++++++--- .../source/network/ServerCommandHandler.cpp | 4 ++ 15 files changed, 114 insertions(+), 149 deletions(-) delete mode 100644 client/include/states/InventoryState.hpp delete mode 100644 client/source/states/InventoryState.cpp create mode 100644 common/include/core/LuaWidget.hpp diff --git a/client/include/gui/PlayerInventoryWidget.hpp b/client/include/gui/PlayerInventoryWidget.hpp index a7bc7b888..d47dc5a7e 100644 --- a/client/include/gui/PlayerInventoryWidget.hpp +++ b/client/include/gui/PlayerInventoryWidget.hpp @@ -18,7 +18,7 @@ class PlayerInventoryWidget : public Widget { public: - PlayerInventoryWidget(ClientCommandHandler &client, Inventory &playerInventory, Widget *parent = nullptr); + PlayerInventoryWidget(ClientCommandHandler &client, MouseItemWidget &mouseItemWidget, Inventory &playerInventory, Widget *parent = nullptr); void onEvent(const SDL_Event &event) override; @@ -29,6 +29,8 @@ class PlayerInventoryWidget : public Widget { ClientCommandHandler &m_client; + MouseItemWidget &m_mouseItemWidget; + gk::Image m_background; Inventory m_craftingInventory{2, 2}; @@ -37,8 +39,6 @@ class PlayerInventoryWidget : public Widget { Inventory &m_playerInventory; InventoryWidget m_playerInventoryWidget{m_client, this}; InventoryWidget m_hotbarInventoryWidget{m_client, this}; - - MouseItemWidget m_mouseItemWidget{this}; }; #endif // PLAYERINVENTORYWIDGET_HPP_ diff --git a/client/include/network/ClientCommandHandler.hpp b/client/include/network/ClientCommandHandler.hpp index 9564943a2..cafea1196 100644 --- a/client/include/network/ClientCommandHandler.hpp +++ b/client/include/network/ClientCommandHandler.hpp @@ -35,6 +35,7 @@ class ClientCommandHandler { void sendPlayerPosUpdate(); void sendPlayerDigBlock(const glm::vec4 &selectedBlock); void sendPlayerPlaceBlock(s32 x, s32 y, s32 z, u32 block); + void sendPlayerInventoryRequest(); void sendBlockActivated(const glm::vec4 &selectedBlock); void sendBlockInvUpdate(Inventory &inventory); void sendChunkRequest(s32 chunkX, s32 chunkY, s32 chunkZ); diff --git a/client/include/states/InventoryState.hpp b/client/include/states/InventoryState.hpp deleted file mode 100644 index eded29142..000000000 --- a/client/include/states/InventoryState.hpp +++ /dev/null @@ -1,49 +0,0 @@ -/* - * ===================================================================================== - * - * Filename: InventoryState.hpp - * - * Description: - * - * Created: 20/06/2018 23:11:44 - * - * Author: Quentin Bazin, - * - * ===================================================================================== - */ -#ifndef INVENTORYSTATE_HPP_ -#define INVENTORYSTATE_HPP_ - -#include - -#include - -#include "Config.hpp" -#include "InterfaceState.hpp" -#include "Widget.hpp" - -// FIXME: Old class used when widgets were defined from C++ -class InventoryState : public InterfaceState { - public: - InventoryState(gk::ApplicationState *parent = nullptr); - - template - void setupWidget(Args &&...args) { - m_widget.reset(new T(std::forward(args)...)); - - m_widget->setScale(GUI_SCALE, GUI_SCALE, 1); - m_widget->setPosition(SCREEN_WIDTH / 2.0 - m_widget->getGlobalBounds().width / 2.0, - SCREEN_HEIGHT / 2.0 - m_widget->getGlobalBounds().height / 2.0, 0); - } - - void onEvent(const SDL_Event &event) override; - - void update() override; - - private: - void draw(gk::RenderTarget &target, gk::RenderStates states) const override; - - std::unique_ptr m_widget; -}; - -#endif // INVENTORYSTATE_HPP_ diff --git a/client/source/gui/PlayerInventoryWidget.cpp b/client/source/gui/PlayerInventoryWidget.cpp index 0346b408d..e26a9851a 100644 --- a/client/source/gui/PlayerInventoryWidget.cpp +++ b/client/source/gui/PlayerInventoryWidget.cpp @@ -13,8 +13,8 @@ */ #include "PlayerInventoryWidget.hpp" -PlayerInventoryWidget::PlayerInventoryWidget(ClientCommandHandler &client, Inventory &playerInventory, Widget *parent) - : Widget(176, 166, parent), m_client(client), m_playerInventory(playerInventory) +PlayerInventoryWidget::PlayerInventoryWidget(ClientCommandHandler &client, MouseItemWidget &mouseItemWidget, Inventory &playerInventory, Widget *parent) + : Widget(176, 166, parent), m_client(client), m_mouseItemWidget(mouseItemWidget), m_playerInventory(playerInventory) { m_background.load("texture-inventory"); m_background.setClipRect(0, 0, 176, 166); @@ -26,6 +26,7 @@ PlayerInventoryWidget::PlayerInventoryWidget(ClientCommandHandler &client, Inven m_hotbarInventoryWidget.setPosition(7, 141, 0); m_craftingWidget.craftingInventoryWidget().setPosition(97, 17, 0); + m_craftingWidget.craftingInventoryWidget().init(m_craftingInventory, 0, 4); m_craftingWidget.craftingResultInventoryWidget().setPosition(153, 27, 0); } @@ -34,8 +35,6 @@ void PlayerInventoryWidget::onEvent(const SDL_Event &event) { m_playerInventoryWidget.onMouseEvent(event, m_mouseItemWidget); m_hotbarInventoryWidget.onMouseEvent(event, m_mouseItemWidget); - - m_mouseItemWidget.onEvent(event); } void PlayerInventoryWidget::update() { @@ -59,7 +58,5 @@ void PlayerInventoryWidget::draw(gk::RenderTarget &target, gk::RenderStates stat target.draw(m_playerInventoryWidget, states); target.draw(m_hotbarInventoryWidget, states); - - target.draw(m_mouseItemWidget, states); } diff --git a/client/source/network/ClientCommandHandler.cpp b/client/source/network/ClientCommandHandler.cpp index 08dcbdaf8..f6dbbfb33 100644 --- a/client/source/network/ClientCommandHandler.cpp +++ b/client/source/network/ClientCommandHandler.cpp @@ -56,6 +56,12 @@ void ClientCommandHandler::sendPlayerPlaceBlock(s32 x, s32 y, s32 z, u32 block) m_client.send(packet); } +void ClientCommandHandler::sendPlayerInventoryRequest() { + sf::Packet packet; + packet << Network::Command::PlayerInventory; + m_client.send(packet); +} + void ClientCommandHandler::sendBlockActivated(const glm::vec4 &selectedBlock) { sf::Packet packet; packet << Network::Command::BlockActivated diff --git a/client/source/states/GameState.cpp b/client/source/states/GameState.cpp index d27f330d8..90897ca18 100644 --- a/client/source/states/GameState.cpp +++ b/client/source/states/GameState.cpp @@ -25,7 +25,6 @@ #include "GameKey.hpp" #include "GameState.hpp" -#include "InventoryState.hpp" #include "LuaGUIState.hpp" #include "PauseMenuState.hpp" #include "PlayerInventoryWidget.hpp" @@ -108,8 +107,7 @@ void GameState::update() { m_player.processInputs(); if (gk::GamePad::isKeyPressedOnce(GameKey::Inventory)) { - auto &inventoryState = m_stateStack->push(this); - inventoryState.setupWidget(m_clientCommandHandler, m_player.inventory()); + m_clientCommandHandler.sendPlayerInventoryRequest(); } } diff --git a/client/source/states/InventoryState.cpp b/client/source/states/InventoryState.cpp deleted file mode 100644 index 17fde38eb..000000000 --- a/client/source/states/InventoryState.cpp +++ /dev/null @@ -1,66 +0,0 @@ -/* - * ===================================================================================== - * - * Filename: InventoryState.cpp - * - * Description: - * - * Created: 20/06/2018 23:13:50 - * - * Author: Quentin Bazin, - * - * ===================================================================================== - */ -#include - -#include -#include - -#include "InventoryState.hpp" - -InventoryState::InventoryState(gk::ApplicationState *parent) : InterfaceState(parent) { - gk::Mouse::setCursorGrabbed(false); - gk::Mouse::setCursorVisible(true); - gk::Mouse::resetToWindowCenter(); -} - -void InventoryState::onEvent(const SDL_Event &event) { - InterfaceState::onEvent(event); - - if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) { - if (m_widget) { - m_widget->setPosition(SCREEN_WIDTH / 2.0 - m_widget->getGlobalBounds().width / 2.0, - SCREEN_HEIGHT / 2.0 - m_widget->getGlobalBounds().height / 2.0, 0); - } - } - - if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) { - gk::Mouse::setCursorGrabbed(true); - gk::Mouse::setCursorVisible(false); - gk::Mouse::resetToWindowCenter(); - - m_stateStack->pop(); - } - - if (m_widget) - m_widget->onEvent(event); -} - -void InventoryState::update() { - if (m_parent) - m_parent->update(); - - if (m_widget) - m_widget->update(); -} - -void InventoryState::draw(gk::RenderTarget &target, gk::RenderStates states) const { - if (m_parent) - target.draw(*m_parent, states); - - prepareDraw(target, states); - - if (m_widget) - target.draw(*m_widget, states); -} - diff --git a/client/source/states/LuaGUIState.cpp b/client/source/states/LuaGUIState.cpp index 2065069f7..1e0c38833 100644 --- a/client/source/states/LuaGUIState.cpp +++ b/client/source/states/LuaGUIState.cpp @@ -24,8 +24,10 @@ #include "FurnaceWidget.hpp" #include "InventoryWidget.hpp" #include "LuaGUIState.hpp" +#include "LuaWidget.hpp" #include "Network.hpp" #include "Player.hpp" +#include "PlayerInventoryWidget.hpp" #include "TextButton.hpp" LuaGUIState::LuaGUIState(ClientCommandHandler &client, ClientPlayer &player, ClientWorld &world, sf::Packet &packet, gk::ApplicationState *parent) @@ -85,7 +87,8 @@ void LuaGUIState::update() { currentItemWidget = it.currentItemWidget(); } - m_mouseItemWidget.update(currentItemWidget); + if (m_widgets.size() != 1) // FIXME + m_mouseItemWidget.update(currentItemWidget); } void LuaGUIState::draw(gk::RenderTarget &target, gk::RenderStates states) const { @@ -117,7 +120,7 @@ void LuaGUIState::loadGUI(ClientPlayer &player, ClientWorld &world, sf::Packet & std::string name; float x, y; packet >> type >> name >> x >> y; - if (type == 0) { + if (type == LuaWidget::Image) { std::string texture; gk::FloatRect clipRect; packet >> texture >> clipRect.x >> clipRect.y >> clipRect.width >> clipRect.height; @@ -127,7 +130,7 @@ void LuaGUIState::loadGUI(ClientPlayer &player, ClientWorld &world, sf::Packet & image->setClipRect(clipRect.x, clipRect.y, clipRect.width, clipRect.height); m_drawables.emplace_back(image); } - else if (type == 1) { + else if (type == LuaWidget::TextButton) { std::string text; packet >> text; @@ -137,7 +140,7 @@ void LuaGUIState::loadGUI(ClientPlayer &player, ClientWorld &world, sf::Packet & button->setText(text); m_widgets.emplace_back(button); } - else if (type == 2) { + else if (type == LuaWidget::InventoryWidget) { std::string playerName, inventory; float width, height; u16 offset, count; @@ -149,7 +152,7 @@ void LuaGUIState::loadGUI(ClientPlayer &player, ClientWorld &world, sf::Packet & inventoryWidget.setPosition(x, y); inventoryWidget.init(player.inventory(), offset, count); } - else if (type == 3) { + else if (type == LuaWidget::CraftingWidget) { gk::Vector3i block; u16 offset, size; packet >> block.x >> block.y >> block.z >> offset >> size; @@ -165,7 +168,7 @@ void LuaGUIState::loadGUI(ClientPlayer &player, ClientWorld &world, sf::Packet & DEBUG("ERROR: No inventory found at", block.x, block.y, block.z); } } - else if (type == 4) { + else if (type == LuaWidget::FurnaceWidget) { gk::Vector3i block; packet >> block.x >> block.y >> block.z; BlockData *data = world.getBlockData(block.x, block.y, block.z); @@ -178,5 +181,10 @@ void LuaGUIState::loadGUI(ClientPlayer &player, ClientWorld &world, sf::Packet & DEBUG("ERROR: No inventory found at", block.x, block.y, block.z); } } + else if (type == LuaWidget::PlayerInventoryWidget) { + auto *widget = new PlayerInventoryWidget(m_client, m_mouseItemWidget, player.inventory(), &m_mainWidget); + widget->setPosition(x, y); + m_widgets.emplace_back(widget); + } } diff --git a/common/include/core/LuaWidget.hpp b/common/include/core/LuaWidget.hpp new file mode 100644 index 000000000..d039103f4 --- /dev/null +++ b/common/include/core/LuaWidget.hpp @@ -0,0 +1,30 @@ +/* + * ===================================================================================== + * + * Filename: LuaWidget.hpp + * + * Description: + * + * Created: 08/02/2020 02:32:09 + * + * Author: Quentin Bazin, + * + * ===================================================================================== + */ +#ifndef LUAWIDGET_HPP_ +#define LUAWIDGET_HPP_ + +#include + +namespace LuaWidget { + enum : u8 { + Image = 0, + TextButton = 1, + InventoryWidget = 2, + CraftingWidget = 3, + FurnaceWidget = 4, + PlayerInventoryWidget = 5, + }; +} + +#endif // LUAWIDGET_HPP_ diff --git a/common/include/network/Network.hpp b/common/include/network/Network.hpp index 241546b9c..8789f4b3a 100644 --- a/common/include/network/Network.hpp +++ b/common/include/network/Network.hpp @@ -37,6 +37,7 @@ namespace Network { PlayerInvUpdate, // [NetworkCommand][u16 client id][[std::string item][u16 amount][u8 x, y]...] (both) [FIXME] PlayerPosUpdate, // [NetworkCommand][u16 client id][s32 x, y, z] (both) // FIXME PlayerSpawn, // [NetworkCommand][u16 client id][s32 x, y, z] (from Server only) + PlayerInventory, // [NetworkCommand] (from Client only) // Block commands BlockUpdate, // [NetworkCommand][s32 x, y, z][u32 block] (from Server only) diff --git a/common/source/network/Network.cpp b/common/source/network/Network.cpp index 352e5a98f..9d2a480c9 100644 --- a/common/source/network/Network.cpp +++ b/common/source/network/Network.cpp @@ -34,6 +34,7 @@ std::string Network::commandToString(Network::Command command) { {Network::Command::PlayerInvUpdate, "PlayerInvUpdate"}, {Network::Command::PlayerPosUpdate, "PlayerPosUpdate"}, {Network::Command::PlayerSpawn, "PlayerSpawn"}, + {Network::Command::PlayerInventory, "PlayerInventory"}, {Network::Command::BlockUpdate, "BlockUpdate"}, {Network::Command::BlockActivated, "BlockActivated"}, diff --git a/mods/default/init.lua b/mods/default/init.lua index 03309f169..679cffee8 100644 --- a/mods/default/init.lua +++ b/mods/default/init.lua @@ -46,3 +46,20 @@ function init(player) player_inv:add_stack("default:coal", 64); end +function show_inventory(client) + local gui = LuaGUI.new() + + -- FIXME: Replace this by gui:set_size() and gui:set_centered() + local gui_pos = { + x = SCREEN_WIDTH / GUI_SCALE / 2.0 - 176 / 2.0, + y = SCREEN_HEIGHT / GUI_SCALE / 2.0 - 166 / 2.0 + } + + gui:player_inventory { + name = "inventory", + pos = gui_pos, + } + + gui:show(client) +end + diff --git a/server/include/lua/LuaGUI.hpp b/server/include/lua/LuaGUI.hpp index c8cbfe89f..92baa229b 100644 --- a/server/include/lua/LuaGUI.hpp +++ b/server/include/lua/LuaGUI.hpp @@ -20,19 +20,12 @@ #include "ServerInfo.hpp" struct LuaGUIData { - enum { - Image, - TextButton, - InventoryWidget, - CraftingWidget, - FurnaceWidget, - }; - std::list imageList; std::list textButtonList; std::list inventoryWidgetList; std::list craftingWidgetList; std::list furnaceWidgetList; + std::list playerInventoryWidgetList; }; // This class is meant to be used ONLY in Lua @@ -43,6 +36,7 @@ class LuaGUI { void addInventoryWidget(const sol::table &table); void addCraftingWidget(const sol::table &table); void addFurnaceWidget(const sol::table &table); + void addPlayerInventoryWidget(const sol::table &table); void show(Client &client); diff --git a/server/source/lua/LuaGUI.cpp b/server/source/lua/LuaGUI.cpp index d6634628d..6ddf4ab65 100644 --- a/server/source/lua/LuaGUI.cpp +++ b/server/source/lua/LuaGUI.cpp @@ -14,8 +14,8 @@ #include #include "LuaGUI.hpp" +#include "LuaWidget.hpp" #include "Network.hpp" -// #include "LuaGUIState.hpp" void LuaGUI::addImage(const sol::table &table) { // FIXME: Duplicated below @@ -164,21 +164,43 @@ void LuaGUI::addFurnaceWidget(const sol::table &table) { furnaceWidget.block = block; } +void LuaGUI::addPlayerInventoryWidget(const sol::table &table) { + // FIXME: Duplicated above + float x = 0, y = 0; + sol::optional pos = table["pos"]; + std::string name = table["name"].get(); + if (pos != sol::nullopt) { + x = pos.value()["x"]; + y = pos.value()["y"]; + } + + m_data.playerInventoryWidgetList.emplace_back(); + + LuaWidgetDef::Widget &widget = m_data.playerInventoryWidgetList.back(); + widget.name = name; + widget.x = x; + widget.y = y; +} + void LuaGUI::show(Client &client) { sf::Packet packet; packet << Network::Command::BlockGUIData; for (auto &it : m_data.imageList) - packet << u8(0) << it.name << it.x << it.y << it.texture << it.clipRect.x << it.clipRect.y << it.clipRect.width << it.clipRect.height; + packet << u8(LuaWidget::Image) + << it.name << it.x << it.y << it.texture << it.clipRect.x << it.clipRect.y << it.clipRect.width << it.clipRect.height; for (auto &it : m_data.textButtonList) - packet << u8(1) << it.name << it.x << it.y << it.text; + packet << u8(LuaWidget::TextButton) << it.name << it.x << it.y << it.text; for (auto &it : m_data.inventoryWidgetList) - packet << u8(2) << it.name << it.x << it.y << it.player << it.inventory << it.width << it.height - << it.offset << it.count; + packet << u8(LuaWidget::InventoryWidget) << it.name << it.x << it.y + << it.player << it.inventory << it.width << it.height << it.offset << it.count; for (auto &it : m_data.craftingWidgetList) - packet << u8(3) << it.name << it.x << it.y << it.block.x << it.block.y << it.block.z - << it.offset << it.size; + packet << u8(LuaWidget::CraftingWidget) << it.name << it.x << it.y + << it.block.x << it.block.y << it.block.z << it.offset << it.size; for (auto &it : m_data.furnaceWidgetList) - packet << u8(4) << it.name << it.x << it.y << it.block.x << it.block.y << it.block.z; + packet << u8(LuaWidget::FurnaceWidget) << it.name << it.x << it.y + << it.block.x << it.block.y << it.block.z; + for (auto &it : m_data.playerInventoryWidgetList) + packet << u8(LuaWidget::PlayerInventoryWidget) << it.name << it.x << it.y; client.tcpSocket->send(packet); } @@ -189,6 +211,7 @@ void LuaGUI::initUsertype(sol::state &lua) { "inventory", &LuaGUI::addInventoryWidget, "crafting", &LuaGUI::addCraftingWidget, "furnace", &LuaGUI::addFurnaceWidget, + "player_inventory", &LuaGUI::addPlayerInventoryWidget, "show", &LuaGUI::show ); } diff --git a/server/source/network/ServerCommandHandler.cpp b/server/source/network/ServerCommandHandler.cpp index 3f48ca334..eaef06d35 100644 --- a/server/source/network/ServerCommandHandler.cpp +++ b/server/source/network/ServerCommandHandler.cpp @@ -102,6 +102,10 @@ void ServerCommandHandler::setupCallbacks() { m_server.sendToAllClients(answer); }); + m_server.setCommandCallback(Network::Command::PlayerInventory, [this](Client &client, sf::Packet &) { + m_scriptEngine.lua()["show_inventory"](client); + }); + m_server.setCommandCallback(Network::Command::BlockActivated, [this](Client &client, sf::Packet &packet) { s32 x, y, z; packet >> x >> y >> z;