Skip to content

Commit

Permalink
[InventoryState] Replaced by 'show_inventory' function in init.lua.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Feb 7, 2020
1 parent 260d4cb commit 442bafc
Show file tree
Hide file tree
Showing 15 changed files with 114 additions and 149 deletions.
6 changes: 3 additions & 3 deletions client/include/gui/PlayerInventoryWidget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -29,6 +29,8 @@ class PlayerInventoryWidget : public Widget {

ClientCommandHandler &m_client;

MouseItemWidget &m_mouseItemWidget;

gk::Image m_background;

Inventory m_craftingInventory{2, 2};
Expand All @@ -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_
1 change: 1 addition & 0 deletions client/include/network/ClientCommandHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
49 changes: 0 additions & 49 deletions client/include/states/InventoryState.hpp

This file was deleted.

9 changes: 3 additions & 6 deletions client/source/gui/PlayerInventoryWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}

Expand All @@ -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() {
Expand All @@ -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);
}

6 changes: 6 additions & 0 deletions client/source/network/ClientCommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions client/source/states/GameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

#include "GameKey.hpp"
#include "GameState.hpp"
#include "InventoryState.hpp"
#include "LuaGUIState.hpp"
#include "PauseMenuState.hpp"
#include "PlayerInventoryWidget.hpp"
Expand Down Expand Up @@ -108,8 +107,7 @@ void GameState::update() {
m_player.processInputs();

if (gk::GamePad::isKeyPressedOnce(GameKey::Inventory)) {
auto &inventoryState = m_stateStack->push<InventoryState>(this);
inventoryState.setupWidget<PlayerInventoryWidget>(m_clientCommandHandler, m_player.inventory());
m_clientCommandHandler.sendPlayerInventoryRequest();
}
}

Expand Down
66 changes: 0 additions & 66 deletions client/source/states/InventoryState.cpp

This file was deleted.

20 changes: 14 additions & 6 deletions client/source/states/LuaGUIState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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);
}
}

30 changes: 30 additions & 0 deletions common/include/core/LuaWidget.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* =====================================================================================
*
* Filename: LuaWidget.hpp
*
* Description:
*
* Created: 08/02/2020 02:32:09
*
* Author: Quentin Bazin, <[email protected]>
*
* =====================================================================================
*/
#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_
1 change: 1 addition & 0 deletions common/include/network/Network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace Network {
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
PlayerSpawn, // <TCP> [NetworkCommand][u16 client id][s32 x, y, z] (from Server only)
PlayerInventory, // <TCP> [NetworkCommand] (from Client only)

// Block commands
BlockUpdate, // <TCP> [NetworkCommand][s32 x, y, z][u32 block] (from Server only)
Expand Down
1 change: 1 addition & 0 deletions common/source/network/Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
17 changes: 17 additions & 0 deletions mods/default/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

10 changes: 2 additions & 8 deletions server/include/lua/LuaGUI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,12 @@
#include "ServerInfo.hpp"

struct LuaGUIData {
enum {
Image,
TextButton,
InventoryWidget,
CraftingWidget,
FurnaceWidget,
};

std::list<LuaWidgetDef::Image> imageList;
std::list<LuaWidgetDef::TextButton> textButtonList;
std::list<LuaWidgetDef::InventoryWidget> inventoryWidgetList;
std::list<LuaWidgetDef::CraftingWidget> craftingWidgetList;
std::list<LuaWidgetDef::FurnaceWidget> furnaceWidgetList;
std::list<LuaWidgetDef::Widget> playerInventoryWidgetList;
};

// This class is meant to be used ONLY in Lua
Expand All @@ -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);

Expand Down
Loading

0 comments on commit 442bafc

Please sign in to comment.