[InventoryState] Replaced by 'show_inventory' function in init.lua.

This commit is contained in:
Quentin Bazin 2020-02-08 02:48:39 +09:00
parent 260d4cb607
commit 442bafca68
15 changed files with 114 additions and 149 deletions

View File

@ -18,7 +18,7 @@
class PlayerInventoryWidget : public Widget { class PlayerInventoryWidget : public Widget {
public: 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; void onEvent(const SDL_Event &event) override;
@ -29,6 +29,8 @@ class PlayerInventoryWidget : public Widget {
ClientCommandHandler &m_client; ClientCommandHandler &m_client;
MouseItemWidget &m_mouseItemWidget;
gk::Image m_background; gk::Image m_background;
Inventory m_craftingInventory{2, 2}; Inventory m_craftingInventory{2, 2};
@ -37,8 +39,6 @@ class PlayerInventoryWidget : public Widget {
Inventory &m_playerInventory; Inventory &m_playerInventory;
InventoryWidget m_playerInventoryWidget{m_client, this}; InventoryWidget m_playerInventoryWidget{m_client, this};
InventoryWidget m_hotbarInventoryWidget{m_client, this}; InventoryWidget m_hotbarInventoryWidget{m_client, this};
MouseItemWidget m_mouseItemWidget{this};
}; };
#endif // PLAYERINVENTORYWIDGET_HPP_ #endif // PLAYERINVENTORYWIDGET_HPP_

View File

@ -35,6 +35,7 @@ class ClientCommandHandler {
void sendPlayerPosUpdate(); void sendPlayerPosUpdate();
void sendPlayerDigBlock(const glm::vec4 &selectedBlock); void sendPlayerDigBlock(const glm::vec4 &selectedBlock);
void sendPlayerPlaceBlock(s32 x, s32 y, s32 z, u32 block); void sendPlayerPlaceBlock(s32 x, s32 y, s32 z, u32 block);
void sendPlayerInventoryRequest();
void sendBlockActivated(const glm::vec4 &selectedBlock); void sendBlockActivated(const glm::vec4 &selectedBlock);
void sendBlockInvUpdate(Inventory &inventory); void sendBlockInvUpdate(Inventory &inventory);
void sendChunkRequest(s32 chunkX, s32 chunkY, s32 chunkZ); void sendChunkRequest(s32 chunkX, s32 chunkY, s32 chunkZ);

View File

@ -1,49 +0,0 @@
/*
* =====================================================================================
*
* Filename: InventoryState.hpp
*
* Description:
*
* Created: 20/06/2018 23:11:44
*
* Author: Quentin Bazin, <quent42340@gmail.com>
*
* =====================================================================================
*/
#ifndef INVENTORYSTATE_HPP_
#define INVENTORYSTATE_HPP_
#include <memory>
#include <gk/graphics/RectangleShape.hpp>
#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<typename T, typename... Args>
void setupWidget(Args &&...args) {
m_widget.reset(new T(std::forward<Args>(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<Widget> m_widget;
};
#endif // INVENTORYSTATE_HPP_

View File

@ -13,8 +13,8 @@
*/ */
#include "PlayerInventoryWidget.hpp" #include "PlayerInventoryWidget.hpp"
PlayerInventoryWidget::PlayerInventoryWidget(ClientCommandHandler &client, Inventory &playerInventory, Widget *parent) PlayerInventoryWidget::PlayerInventoryWidget(ClientCommandHandler &client, MouseItemWidget &mouseItemWidget, Inventory &playerInventory, Widget *parent)
: Widget(176, 166, parent), m_client(client), m_playerInventory(playerInventory) : Widget(176, 166, parent), m_client(client), m_mouseItemWidget(mouseItemWidget), m_playerInventory(playerInventory)
{ {
m_background.load("texture-inventory"); m_background.load("texture-inventory");
m_background.setClipRect(0, 0, 176, 166); m_background.setClipRect(0, 0, 176, 166);
@ -26,6 +26,7 @@ PlayerInventoryWidget::PlayerInventoryWidget(ClientCommandHandler &client, Inven
m_hotbarInventoryWidget.setPosition(7, 141, 0); m_hotbarInventoryWidget.setPosition(7, 141, 0);
m_craftingWidget.craftingInventoryWidget().setPosition(97, 17, 0); m_craftingWidget.craftingInventoryWidget().setPosition(97, 17, 0);
m_craftingWidget.craftingInventoryWidget().init(m_craftingInventory, 0, 4);
m_craftingWidget.craftingResultInventoryWidget().setPosition(153, 27, 0); 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_playerInventoryWidget.onMouseEvent(event, m_mouseItemWidget);
m_hotbarInventoryWidget.onMouseEvent(event, m_mouseItemWidget); m_hotbarInventoryWidget.onMouseEvent(event, m_mouseItemWidget);
m_mouseItemWidget.onEvent(event);
} }
void PlayerInventoryWidget::update() { void PlayerInventoryWidget::update() {
@ -59,7 +58,5 @@ void PlayerInventoryWidget::draw(gk::RenderTarget &target, gk::RenderStates stat
target.draw(m_playerInventoryWidget, states); target.draw(m_playerInventoryWidget, states);
target.draw(m_hotbarInventoryWidget, states); target.draw(m_hotbarInventoryWidget, states);
target.draw(m_mouseItemWidget, states);
} }

View File

@ -56,6 +56,12 @@ void ClientCommandHandler::sendPlayerPlaceBlock(s32 x, s32 y, s32 z, u32 block)
m_client.send(packet); 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) { void ClientCommandHandler::sendBlockActivated(const glm::vec4 &selectedBlock) {
sf::Packet packet; sf::Packet packet;
packet << Network::Command::BlockActivated packet << Network::Command::BlockActivated

View File

@ -25,7 +25,6 @@
#include "GameKey.hpp" #include "GameKey.hpp"
#include "GameState.hpp" #include "GameState.hpp"
#include "InventoryState.hpp"
#include "LuaGUIState.hpp" #include "LuaGUIState.hpp"
#include "PauseMenuState.hpp" #include "PauseMenuState.hpp"
#include "PlayerInventoryWidget.hpp" #include "PlayerInventoryWidget.hpp"
@ -108,8 +107,7 @@ void GameState::update() {
m_player.processInputs(); m_player.processInputs();
if (gk::GamePad::isKeyPressedOnce(GameKey::Inventory)) { if (gk::GamePad::isKeyPressedOnce(GameKey::Inventory)) {
auto &inventoryState = m_stateStack->push<InventoryState>(this); m_clientCommandHandler.sendPlayerInventoryRequest();
inventoryState.setupWidget<PlayerInventoryWidget>(m_clientCommandHandler, m_player.inventory());
} }
} }

View File

@ -1,66 +0,0 @@
/*
* =====================================================================================
*
* Filename: InventoryState.cpp
*
* Description:
*
* Created: 20/06/2018 23:13:50
*
* Author: Quentin Bazin, <quent42340@gmail.com>
*
* =====================================================================================
*/
#include <glm/gtc/matrix_transform.hpp>
#include <gk/core/ApplicationStateStack.hpp>
#include <gk/core/Mouse.hpp>
#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);
}

View File

@ -24,8 +24,10 @@
#include "FurnaceWidget.hpp" #include "FurnaceWidget.hpp"
#include "InventoryWidget.hpp" #include "InventoryWidget.hpp"
#include "LuaGUIState.hpp" #include "LuaGUIState.hpp"
#include "LuaWidget.hpp"
#include "Network.hpp" #include "Network.hpp"
#include "Player.hpp" #include "Player.hpp"
#include "PlayerInventoryWidget.hpp"
#include "TextButton.hpp" #include "TextButton.hpp"
LuaGUIState::LuaGUIState(ClientCommandHandler &client, ClientPlayer &player, ClientWorld &world, sf::Packet &packet, gk::ApplicationState *parent) LuaGUIState::LuaGUIState(ClientCommandHandler &client, ClientPlayer &player, ClientWorld &world, sf::Packet &packet, gk::ApplicationState *parent)
@ -85,7 +87,8 @@ void LuaGUIState::update() {
currentItemWidget = it.currentItemWidget(); 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 { 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; std::string name;
float x, y; float x, y;
packet >> type >> name >> x >> y; packet >> type >> name >> x >> y;
if (type == 0) { if (type == LuaWidget::Image) {
std::string texture; std::string texture;
gk::FloatRect clipRect; gk::FloatRect clipRect;
packet >> texture >> clipRect.x >> clipRect.y >> clipRect.width >> clipRect.height; 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); image->setClipRect(clipRect.x, clipRect.y, clipRect.width, clipRect.height);
m_drawables.emplace_back(image); m_drawables.emplace_back(image);
} }
else if (type == 1) { else if (type == LuaWidget::TextButton) {
std::string text; std::string text;
packet >> text; packet >> text;
@ -137,7 +140,7 @@ void LuaGUIState::loadGUI(ClientPlayer &player, ClientWorld &world, sf::Packet &
button->setText(text); button->setText(text);
m_widgets.emplace_back(button); m_widgets.emplace_back(button);
} }
else if (type == 2) { else if (type == LuaWidget::InventoryWidget) {
std::string playerName, inventory; std::string playerName, inventory;
float width, height; float width, height;
u16 offset, count; u16 offset, count;
@ -149,7 +152,7 @@ void LuaGUIState::loadGUI(ClientPlayer &player, ClientWorld &world, sf::Packet &
inventoryWidget.setPosition(x, y); inventoryWidget.setPosition(x, y);
inventoryWidget.init(player.inventory(), offset, count); inventoryWidget.init(player.inventory(), offset, count);
} }
else if (type == 3) { else if (type == LuaWidget::CraftingWidget) {
gk::Vector3i block; gk::Vector3i block;
u16 offset, size; u16 offset, size;
packet >> block.x >> block.y >> block.z >> 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); DEBUG("ERROR: No inventory found at", block.x, block.y, block.z);
} }
} }
else if (type == 4) { else if (type == LuaWidget::FurnaceWidget) {
gk::Vector3i block; gk::Vector3i block;
packet >> block.x >> block.y >> block.z; packet >> block.x >> block.y >> block.z;
BlockData *data = world.getBlockData(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); 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);
}
} }

View File

@ -0,0 +1,30 @@
/*
* =====================================================================================
*
* Filename: LuaWidget.hpp
*
* Description:
*
* Created: 08/02/2020 02:32:09
*
* Author: Quentin Bazin, <quent42340@gmail.com>
*
* =====================================================================================
*/
#ifndef LUAWIDGET_HPP_
#define LUAWIDGET_HPP_
#include <gk/core/IntTypes.hpp>
namespace LuaWidget {
enum : u8 {
Image = 0,
TextButton = 1,
InventoryWidget = 2,
CraftingWidget = 3,
FurnaceWidget = 4,
PlayerInventoryWidget = 5,
};
}
#endif // LUAWIDGET_HPP_

View File

@ -37,6 +37,7 @@ namespace Network {
PlayerInvUpdate, // <TCP> [NetworkCommand][u16 client id][[std::string item][u16 amount][u8 x, y]...] (both) [FIXME] PlayerInvUpdate, // <TCP> [NetworkCommand][u16 client id][[std::string item][u16 amount][u8 x, y]...] (both) [FIXME]
PlayerPosUpdate, // <TCP> [NetworkCommand][u16 client id][s32 x, y, z] (both) // FIXME PlayerPosUpdate, // <TCP> [NetworkCommand][u16 client id][s32 x, y, z] (both) // FIXME
PlayerSpawn, // <TCP> [NetworkCommand][u16 client id][s32 x, y, z] (from Server only) PlayerSpawn, // <TCP> [NetworkCommand][u16 client id][s32 x, y, z] (from Server only)
PlayerInventory, // <TCP> [NetworkCommand] (from Client only)
// Block commands // Block commands
BlockUpdate, // <TCP> [NetworkCommand][s32 x, y, z][u32 block] (from Server only) BlockUpdate, // <TCP> [NetworkCommand][s32 x, y, z][u32 block] (from Server only)

View File

@ -34,6 +34,7 @@ std::string Network::commandToString(Network::Command command) {
{Network::Command::PlayerInvUpdate, "PlayerInvUpdate"}, {Network::Command::PlayerInvUpdate, "PlayerInvUpdate"},
{Network::Command::PlayerPosUpdate, "PlayerPosUpdate"}, {Network::Command::PlayerPosUpdate, "PlayerPosUpdate"},
{Network::Command::PlayerSpawn, "PlayerSpawn"}, {Network::Command::PlayerSpawn, "PlayerSpawn"},
{Network::Command::PlayerInventory, "PlayerInventory"},
{Network::Command::BlockUpdate, "BlockUpdate"}, {Network::Command::BlockUpdate, "BlockUpdate"},
{Network::Command::BlockActivated, "BlockActivated"}, {Network::Command::BlockActivated, "BlockActivated"},

View File

@ -46,3 +46,20 @@ function init(player)
player_inv:add_stack("default:coal", 64); player_inv:add_stack("default:coal", 64);
end 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

View File

@ -20,19 +20,12 @@
#include "ServerInfo.hpp" #include "ServerInfo.hpp"
struct LuaGUIData { struct LuaGUIData {
enum {
Image,
TextButton,
InventoryWidget,
CraftingWidget,
FurnaceWidget,
};
std::list<LuaWidgetDef::Image> imageList; std::list<LuaWidgetDef::Image> imageList;
std::list<LuaWidgetDef::TextButton> textButtonList; std::list<LuaWidgetDef::TextButton> textButtonList;
std::list<LuaWidgetDef::InventoryWidget> inventoryWidgetList; std::list<LuaWidgetDef::InventoryWidget> inventoryWidgetList;
std::list<LuaWidgetDef::CraftingWidget> craftingWidgetList; std::list<LuaWidgetDef::CraftingWidget> craftingWidgetList;
std::list<LuaWidgetDef::FurnaceWidget> furnaceWidgetList; std::list<LuaWidgetDef::FurnaceWidget> furnaceWidgetList;
std::list<LuaWidgetDef::Widget> playerInventoryWidgetList;
}; };
// This class is meant to be used ONLY in Lua // This class is meant to be used ONLY in Lua
@ -43,6 +36,7 @@ class LuaGUI {
void addInventoryWidget(const sol::table &table); void addInventoryWidget(const sol::table &table);
void addCraftingWidget(const sol::table &table); void addCraftingWidget(const sol::table &table);
void addFurnaceWidget(const sol::table &table); void addFurnaceWidget(const sol::table &table);
void addPlayerInventoryWidget(const sol::table &table);
void show(Client &client); void show(Client &client);

View File

@ -14,8 +14,8 @@
#include <gk/core/ApplicationStateStack.hpp> #include <gk/core/ApplicationStateStack.hpp>
#include "LuaGUI.hpp" #include "LuaGUI.hpp"
#include "LuaWidget.hpp"
#include "Network.hpp" #include "Network.hpp"
// #include "LuaGUIState.hpp"
void LuaGUI::addImage(const sol::table &table) { void LuaGUI::addImage(const sol::table &table) {
// FIXME: Duplicated below // FIXME: Duplicated below
@ -164,21 +164,43 @@ void LuaGUI::addFurnaceWidget(const sol::table &table) {
furnaceWidget.block = block; furnaceWidget.block = block;
} }
void LuaGUI::addPlayerInventoryWidget(const sol::table &table) {
// FIXME: Duplicated above
float x = 0, y = 0;
sol::optional<sol::table> pos = table["pos"];
std::string name = table["name"].get<std::string>();
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) { void LuaGUI::show(Client &client) {
sf::Packet packet; sf::Packet packet;
packet << Network::Command::BlockGUIData; packet << Network::Command::BlockGUIData;
for (auto &it : m_data.imageList) 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) 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) for (auto &it : m_data.inventoryWidgetList)
packet << u8(2) << it.name << it.x << it.y << it.player << it.inventory << it.width << it.height packet << u8(LuaWidget::InventoryWidget) << it.name << it.x << it.y
<< it.offset << it.count; << it.player << it.inventory << it.width << it.height << it.offset << it.count;
for (auto &it : m_data.craftingWidgetList) for (auto &it : m_data.craftingWidgetList)
packet << u8(3) << it.name << it.x << it.y << it.block.x << it.block.y << it.block.z packet << u8(LuaWidget::CraftingWidget) << it.name << it.x << it.y
<< it.offset << it.size; << it.block.x << it.block.y << it.block.z << it.offset << it.size;
for (auto &it : m_data.furnaceWidgetList) 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); client.tcpSocket->send(packet);
} }
@ -189,6 +211,7 @@ void LuaGUI::initUsertype(sol::state &lua) {
"inventory", &LuaGUI::addInventoryWidget, "inventory", &LuaGUI::addInventoryWidget,
"crafting", &LuaGUI::addCraftingWidget, "crafting", &LuaGUI::addCraftingWidget,
"furnace", &LuaGUI::addFurnaceWidget, "furnace", &LuaGUI::addFurnaceWidget,
"player_inventory", &LuaGUI::addPlayerInventoryWidget,
"show", &LuaGUI::show "show", &LuaGUI::show
); );
} }

View File

@ -102,6 +102,10 @@ void ServerCommandHandler::setupCallbacks() {
m_server.sendToAllClients(answer); 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) { m_server.setCommandCallback(Network::Command::BlockActivated, [this](Client &client, sf::Packet &packet) {
s32 x, y, z; s32 x, y, z;
packet >> x >> y >> z; packet >> x >> y >> z;