Skip to content

Commit

Permalink
[LuaGUIState] Small refactorization.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Feb 25, 2020
1 parent 1d4fe0a commit 7a28b8b
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 107 deletions.
10 changes: 9 additions & 1 deletion client/include/states/LuaGUIState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ class LuaGUIState : public InterfaceState {
private:
void draw(gk::RenderTarget &target, gk::RenderStates states) const override;

void loadGUI(ClientPlayer &player, ClientWorld &world, sf::Packet &packet);
void loadGUI(sf::Packet &packet);
void loadImage(const std::string &name, s32 x, s32 y, sf::Packet &packet);
void loadTextButton(const std::string &name, s32 x, s32 y, sf::Packet &packet);
void loadInventoryWidget(const std::string &name, s32 x, s32 y, sf::Packet &packet);
void loadCraftingWidget(const std::string &name, s32 x, s32 y, sf::Packet &packet);
void loadProgressBarWidget(const std::string &name, s32 x, s32 y, sf::Packet &packet);

ClientCommandHandler &m_client;

Expand All @@ -65,6 +70,9 @@ class LuaGUIState : public InterfaceState {
std::vector<std::unique_ptr<gk::Drawable>> m_drawables;

Inventory m_inventory;

ClientPlayer &m_player;
ClientWorld &m_world;
};

#endif // LUAGUISTATE_HPP_
221 changes: 118 additions & 103 deletions client/source/states/LuaGUIState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
#include "TextButton.hpp"

LuaGUIState::LuaGUIState(ClientCommandHandler &client, ClientPlayer &player, ClientWorld &world, sf::Packet &packet, gk::ApplicationState *parent)
: InterfaceState(parent), m_client(client)
: InterfaceState(parent), m_client(client), m_player(player), m_world(world)
{
gk::Mouse::setCursorGrabbed(false);
gk::Mouse::setCursorVisible(true);
Expand All @@ -52,7 +52,7 @@ LuaGUIState::LuaGUIState(ClientCommandHandler &client, ClientPlayer &player, Cli
m_mainWidget.setScale(Config::guiScale, Config::guiScale);

while (!packet.endOfPacket())
loadGUI(player, world, packet);
loadGUI(packet);
}

void LuaGUIState::onEvent(const SDL_Event &event) {
Expand Down Expand Up @@ -103,8 +103,7 @@ void LuaGUIState::update() {
currentItemWidget = it.currentItemWidget();
}

if (m_inventoryWidgets.size() != 0) // FIXME
m_mouseItemWidget.updateCurrentItem(currentItemWidget);
m_mouseItemWidget.updateCurrentItem(currentItemWidget);
}

void LuaGUIState::draw(gk::RenderTarget &target, gk::RenderStates states) const {
Expand All @@ -131,122 +130,138 @@ void LuaGUIState::draw(gk::RenderTarget &target, gk::RenderStates states) const
target.draw(m_mouseItemWidget, states);
}

void LuaGUIState::loadGUI(ClientPlayer &player, ClientWorld &world, sf::Packet &packet) {
void LuaGUIState::loadGUI(sf::Packet &packet) {
u8 type;
std::string name;
s32 x, y;
packet >> type >> name >> x >> y;
if (type == LuaWidget::Image) {
std::string texture;
gk::FloatRect clipRect;
packet >> texture >> clipRect.x >> clipRect.y >> clipRect.sizeX >> clipRect.sizeY;

auto *image = new gk::Image(texture);
image->setPosition(x, y);
image->setClipRect(clipRect.x, clipRect.y, clipRect.sizeX, clipRect.sizeY);
m_drawables.emplace_back(image);
}
else if (type == LuaWidget::TextButton) {
std::string text;
packet >> text;

auto *button = new TextButton(&m_mainWidget);
button->setPosition(x, y);
// button->setCallback(it.on_click);
button->setText(text);
m_widgets.emplace_back(button);
}
else if (type == LuaWidget::InventoryWidget) {
std::string inventory, playerName, inventory_name;
gk::Vector3i block;
float width, height;
u16 offset, count;
packet >> inventory >> playerName >> inventory_name
>> block.x >> block.y >> block.z
>> width >> height >> offset >> count;

Inventory *widgetInventory = nullptr;
if (inventory == "player") {
widgetInventory = &player.inventory();
}
else if (inventory == "block") {
BlockData *data = world.getBlockData(block.x, block.y, block.z);
if (!data) {
DEBUG("ERROR: No inventory found at", block.x, block.y, block.z);
return;
}

widgetInventory = &data->inventory;
}

if (widgetInventory) {
m_inventoryWidgets.emplace_back(m_client, &m_mainWidget);
if (type == LuaWidget::Image)
loadImage(name, x, y, packet);
else if (type == LuaWidget::TextButton)
loadTextButton(name, x, y, packet);
else if (type == LuaWidget::InventoryWidget)
loadInventoryWidget(name, x, y, packet);
else if (type == LuaWidget::ProgressBarWidget)
loadProgressBarWidget(name, x, y, packet);
else if (type == LuaWidget::CraftingWidget)
loadCraftingWidget(name, x, y, packet);
}

auto &inventoryWidget = m_inventoryWidgets.back();
inventoryWidget.setPosition(x, y);
inventoryWidget.init(*widgetInventory, offset, count);
}
else {
DEBUG("ERROR: Widget inventory is invalid");
}
void LuaGUIState::loadImage(const std::string &, s32 x, s32 y, sf::Packet &packet) {
std::string texture;
gk::FloatRect clipRect;
packet >> texture >> clipRect.x >> clipRect.y >> clipRect.sizeX >> clipRect.sizeY;

auto *image = new gk::Image(texture);
image->setPosition(x, y);
image->setClipRect(clipRect.x, clipRect.y, clipRect.sizeX, clipRect.sizeY);
m_drawables.emplace_back(image);
}

void LuaGUIState::loadTextButton(const std::string &, s32 x, s32 y, sf::Packet &packet) {
std::string text;
packet >> text;

auto *button = new TextButton(&m_mainWidget);
button->setPosition(x, y);
// button->setCallback(it.on_click);
button->setText(text);
m_widgets.emplace_back(button);
}

void LuaGUIState::loadInventoryWidget(const std::string &, s32 x, s32 y, sf::Packet &packet) {
std::string inventory, playerName, inventory_name;
gk::Vector3i block;
float width, height;
u16 offset, count;
packet >> inventory >> playerName >> inventory_name
>> block.x >> block.y >> block.z
>> width >> height >> offset >> count;

Inventory *widgetInventory = nullptr;
if (inventory == "player") {
widgetInventory = &m_player.inventory();
}
else if (type == LuaWidget::ProgressBarWidget) {
u8 type;
gk::Vector3i block;
std::string meta, maxMeta;
u32 maxValue;
std::string texture;
gk::FloatRect clipRect;
packet >> type >> block.x >> block.y >> block.z >> meta >> maxMeta >> maxValue >> texture
>> clipRect.x >> clipRect.y >> clipRect.sizeX >> clipRect.sizeY;

BlockData *data = world.getBlockData(block.x, block.y, block.z);
else if (inventory == "block") {
BlockData *data = m_world.getBlockData(block.x, block.y, block.z);
if (!data) {
DEBUG("ERROR: No inventory found at", block.x, block.y, block.z);
return;
}

ProgressBarWidget *widget = new ProgressBarWidget(texture, *data, ProgressBarType(type));
if (!maxMeta.empty())
widget->init(clipRect, gk::Vector2i{x, y}, meta, maxMeta);
else
widget->init(clipRect, gk::Vector2i{x, y}, meta, maxValue);
widgetInventory = &data->inventory;
}

m_widgets.emplace_back(widget);
if (widgetInventory) {
m_inventoryWidgets.emplace_back(m_client, &m_mainWidget);

auto &inventoryWidget = m_inventoryWidgets.back();
inventoryWidget.setPosition(x, y);
inventoryWidget.init(*widgetInventory, offset, count);
}
else if (type == LuaWidget::CraftingWidget) { // FIXME
std::string inventory;
gk::Vector3i block;
u16 offset, size;
s32 resultX, resultY;
packet >> inventory >> block.x >> block.y >> block.z >> offset >> size >> resultX >> resultY;

Inventory *craftingInventory = nullptr;
if (inventory == "block") {
BlockData *data = world.getBlockData(block.x, block.y, block.z);
if (!data) {
DEBUG("ERROR: No inventory found at", block.x, block.y, block.z);
return;
}

craftingInventory = &data->inventory;
}
else if (inventory == "temp") {
craftingInventory = &m_inventory;
m_inventory.resize(size, size);
}
else {
DEBUG("ERROR: Widget inventory is invalid");
}
}

if (craftingInventory) {
m_craftingWidgets.emplace_back(m_client, *craftingInventory, &m_mainWidget);
void LuaGUIState::loadCraftingWidget(const std::string &, s32 x, s32 y, sf::Packet &packet) {
std::string inventory;
gk::Vector3i block;
u16 offset, size;
s32 resultX, resultY;
packet >> inventory >> block.x >> block.y >> block.z >> offset >> size >> resultX >> resultY;

auto &craftingWidget = m_craftingWidgets.back();
craftingWidget.init(offset, size);
craftingWidget.craftingInventoryWidget().setPosition(x, y);
craftingWidget.craftingResultInventoryWidget().setPosition(resultX, resultY);
}
else {
DEBUG("ERROR: Crafting inventory is invalid");
Inventory *craftingInventory = nullptr;
if (inventory == "block") {
BlockData *data = m_world.getBlockData(block.x, block.y, block.z);
if (!data) {
DEBUG("ERROR: No inventory found at", block.x, block.y, block.z);
return;
}

craftingInventory = &data->inventory;
}
else if (inventory == "temp") {
craftingInventory = &m_inventory;
m_inventory.resize(size, size);
}

if (craftingInventory) {
m_craftingWidgets.emplace_back(m_client, *craftingInventory, &m_mainWidget);

auto &craftingWidget = m_craftingWidgets.back();
craftingWidget.init(offset, size);
craftingWidget.craftingInventoryWidget().setPosition(x, y);
craftingWidget.craftingResultInventoryWidget().setPosition(resultX, resultY);
}
else {
DEBUG("ERROR: Crafting inventory is invalid");
}
}

void LuaGUIState::loadProgressBarWidget(const std::string &, s32 x, s32 y, sf::Packet &packet) {
u8 type;
gk::Vector3i block;
std::string meta, maxMeta;
u32 maxValue;
std::string texture;
gk::FloatRect clipRect;
packet >> type >> block.x >> block.y >> block.z >> meta >> maxMeta >> maxValue >> texture
>> clipRect.x >> clipRect.y >> clipRect.sizeX >> clipRect.sizeY;

BlockData *data = m_world.getBlockData(block.x, block.y, block.z);
if (!data) {
DEBUG("ERROR: No inventory found at", block.x, block.y, block.z);
return;
}

ProgressBarWidget *widget = new ProgressBarWidget(texture, *data, ProgressBarType(type));
if (!maxMeta.empty())
widget->init(clipRect, gk::Vector2i{x, y}, meta, maxMeta);
else
widget->init(clipRect, gk::Vector2i{x, y}, meta, maxValue);

m_widgets.emplace_back(widget);
}

9 changes: 6 additions & 3 deletions server/source/lua/LuaGUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ void LuaGUI::show(Client &client) {

for (auto &it : m_data.imageList)
packet << u8(LuaWidget::Image)
<< it.name << it.x << it.y << it.texture << it.clipRect.x << it.clipRect.y << it.clipRect.sizeX << it.clipRect.sizeY;
<< it.name << it.x << it.y << it.texture
<< it.clipRect.x << it.clipRect.y << it.clipRect.sizeX << it.clipRect.sizeY;

for (auto &it : m_data.textButtonList)
packet << u8(LuaWidget::TextButton) << it.name << it.x << it.y << it.text;
Expand All @@ -265,12 +266,14 @@ void LuaGUI::show(Client &client) {

for (auto &it : m_data.craftingWidgetList)
packet << u8(LuaWidget::CraftingWidget) << it.name << it.x << it.y << it.inventory
<< it.block.x << it.block.y << it.block.z << it.offset << it.size << it.resultX << it.resultY;
<< it.block.x << it.block.y << it.block.z << it.offset << it.size
<< it.resultX << it.resultY;

for (auto &it : m_data.progressBarWidgetList)
packet << u8(LuaWidget::ProgressBarWidget) << it.name << it.x << it.y << it.type
<< it.block.x << it.block.y << it.block.z << it.meta << it.maxMeta << it.maxValue
<< it.texture << it.clipRect.x << it.clipRect.y << it.clipRect.sizeX << it.clipRect.sizeY;
<< it.texture << it.clipRect.x << it.clipRect.y
<< it.clipRect.sizeX << it.clipRect.sizeY;

client.tcpSocket->send(packet);
}
Expand Down

0 comments on commit 7a28b8b

Please sign in to comment.