2019-01-07 01:26:02 +01:00
|
|
|
/*
|
|
|
|
* =====================================================================================
|
|
|
|
*
|
2020-02-08 18:34:26 +09:00
|
|
|
* OpenMiner
|
|
|
|
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <openminer@unarelith.net>
|
2019-01-07 01:26:02 +01:00
|
|
|
*
|
2020-02-08 18:34:26 +09:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2019-01-07 01:26:02 +01:00
|
|
|
*
|
2020-02-08 18:34:26 +09:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2019-01-07 01:26:02 +01:00
|
|
|
*
|
2020-02-08 18:34:26 +09:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2019-01-07 01:26:02 +01:00
|
|
|
*
|
|
|
|
* =====================================================================================
|
|
|
|
*/
|
2019-01-20 01:31:44 +01:00
|
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
|
|
|
2019-01-07 01:26:02 +01:00
|
|
|
#include <gk/core/ApplicationStateStack.hpp>
|
2019-01-07 03:55:37 +01:00
|
|
|
#include <gk/core/Debug.hpp>
|
2019-01-07 01:26:02 +01:00
|
|
|
#include <gk/core/Mouse.hpp>
|
|
|
|
#include <gk/graphics/Color.hpp>
|
|
|
|
|
2019-01-25 13:45:43 +01:00
|
|
|
#include "ClientPlayer.hpp"
|
|
|
|
#include "ClientWorld.hpp"
|
2019-01-07 01:26:02 +01:00
|
|
|
#include "Config.hpp"
|
2019-01-07 02:05:23 +01:00
|
|
|
#include "InventoryWidget.hpp"
|
2019-01-07 01:26:02 +01:00
|
|
|
#include "LuaGUIState.hpp"
|
2020-02-08 02:48:39 +09:00
|
|
|
#include "LuaWidget.hpp"
|
2019-01-25 13:45:43 +01:00
|
|
|
#include "Network.hpp"
|
2019-01-07 02:05:23 +01:00
|
|
|
#include "Player.hpp"
|
2020-02-23 22:38:51 +09:00
|
|
|
#include "ProgressBarWidget.hpp"
|
2019-01-07 01:26:02 +01:00
|
|
|
#include "TextButton.hpp"
|
|
|
|
|
2019-04-08 12:59:02 +02:00
|
|
|
LuaGUIState::LuaGUIState(ClientCommandHandler &client, ClientPlayer &player, ClientWorld &world, sf::Packet &packet, gk::ApplicationState *parent)
|
2019-04-08 18:44:05 +02:00
|
|
|
: InterfaceState(parent), m_client(client)
|
2019-01-26 20:29:13 +01:00
|
|
|
{
|
2019-01-07 01:26:02 +01:00
|
|
|
gk::Mouse::setCursorGrabbed(false);
|
|
|
|
gk::Mouse::setCursorVisible(true);
|
|
|
|
gk::Mouse::resetToWindowCenter();
|
|
|
|
|
2020-02-15 22:48:56 +09:00
|
|
|
m_mainWidget.setScale(Config::guiScale, Config::guiScale);
|
2019-01-07 01:26:02 +01:00
|
|
|
|
2019-01-25 13:45:43 +01:00
|
|
|
while (!packet.endOfPacket())
|
|
|
|
loadGUI(player, world, packet);
|
2019-01-07 01:26:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void LuaGUIState::onEvent(const SDL_Event &event) {
|
2020-01-27 15:33:06 +09:00
|
|
|
InterfaceState::onEvent(event);
|
|
|
|
|
2019-01-07 01:26:02 +01:00
|
|
|
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();
|
|
|
|
}
|
2019-01-07 03:55:37 +01:00
|
|
|
|
|
|
|
for (auto &it : m_widgets)
|
|
|
|
it->onEvent(event);
|
|
|
|
|
|
|
|
for (auto &it : m_inventoryWidgets)
|
|
|
|
it.onMouseEvent(event, m_mouseItemWidget, false);
|
|
|
|
|
|
|
|
for (auto &it : m_craftingWidgets)
|
|
|
|
it.onMouseEvent(event, m_mouseItemWidget);
|
|
|
|
|
|
|
|
m_mouseItemWidget.onEvent(event);
|
2019-01-07 01:26:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void LuaGUIState::update() {
|
|
|
|
if (m_parent)
|
|
|
|
m_parent->update();
|
|
|
|
|
|
|
|
for (auto &it : m_widgets)
|
|
|
|
it->update();
|
2019-01-07 03:55:37 +01:00
|
|
|
|
|
|
|
for (auto &it : m_craftingWidgets) {
|
|
|
|
it.update();
|
|
|
|
}
|
|
|
|
|
2020-02-23 22:38:51 +09:00
|
|
|
for (auto &it : m_inventoryWidgets) {
|
|
|
|
it.update();
|
|
|
|
}
|
|
|
|
|
2019-01-07 03:55:37 +01:00
|
|
|
const ItemWidget *currentItemWidget = nullptr;
|
|
|
|
for (auto &it : m_inventoryWidgets) {
|
|
|
|
if (!currentItemWidget)
|
|
|
|
currentItemWidget = it.currentItemWidget();
|
|
|
|
}
|
|
|
|
for (auto &it : m_craftingWidgets) {
|
|
|
|
if (!currentItemWidget)
|
|
|
|
currentItemWidget = it.currentItemWidget();
|
|
|
|
}
|
|
|
|
|
2020-02-08 14:36:22 +09:00
|
|
|
if (m_inventoryWidgets.size() != 0) // FIXME
|
2020-02-15 20:22:10 +09:00
|
|
|
m_mouseItemWidget.updateCurrentItem(currentItemWidget);
|
2019-01-07 01:26:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void LuaGUIState::draw(gk::RenderTarget &target, gk::RenderStates states) const {
|
|
|
|
if (m_parent)
|
|
|
|
target.draw(*m_parent, states);
|
|
|
|
|
2019-04-08 18:44:05 +02:00
|
|
|
prepareDraw(target, states);
|
|
|
|
|
2019-01-07 01:26:02 +01:00
|
|
|
states.transform *= m_mainWidget.getTransform();
|
|
|
|
states.viewMatrix = gk::Transform::Identity;
|
|
|
|
|
2019-01-07 02:39:39 +01:00
|
|
|
for (auto &it : m_drawables)
|
|
|
|
target.draw(*it, states);
|
|
|
|
|
2019-01-07 01:26:02 +01:00
|
|
|
for (auto &it : m_widgets)
|
|
|
|
target.draw(*it, states);
|
2019-01-07 02:39:39 +01:00
|
|
|
|
|
|
|
for (auto &it : m_inventoryWidgets)
|
|
|
|
target.draw(it, states);
|
|
|
|
|
2019-01-07 03:55:37 +01:00
|
|
|
for (auto &it : m_craftingWidgets)
|
|
|
|
target.draw(it, states);
|
|
|
|
|
2019-01-07 02:39:39 +01:00
|
|
|
target.draw(m_mouseItemWidget, states);
|
2019-01-07 01:26:02 +01:00
|
|
|
}
|
|
|
|
|
2019-01-25 13:45:43 +01:00
|
|
|
void LuaGUIState::loadGUI(ClientPlayer &player, ClientWorld &world, sf::Packet &packet) {
|
|
|
|
u8 type;
|
|
|
|
std::string name;
|
2020-02-22 22:49:48 +09:00
|
|
|
s32 x, y;
|
2019-01-25 13:45:43 +01:00
|
|
|
packet >> type >> name >> x >> y;
|
2020-02-08 02:48:39 +09:00
|
|
|
if (type == LuaWidget::Image) {
|
2019-01-25 13:45:43 +01:00
|
|
|
std::string texture;
|
|
|
|
gk::FloatRect clipRect;
|
2020-02-19 20:11:30 +01:00
|
|
|
packet >> texture >> clipRect.x >> clipRect.y >> clipRect.sizeX >> clipRect.sizeY;
|
2019-01-25 13:45:43 +01:00
|
|
|
|
|
|
|
auto *image = new gk::Image(texture);
|
|
|
|
image->setPosition(x, y);
|
2020-02-19 20:11:30 +01:00
|
|
|
image->setClipRect(clipRect.x, clipRect.y, clipRect.sizeX, clipRect.sizeY);
|
2019-01-25 13:45:43 +01:00
|
|
|
m_drawables.emplace_back(image);
|
|
|
|
}
|
2020-02-08 02:48:39 +09:00
|
|
|
else if (type == LuaWidget::TextButton) {
|
2019-01-25 13:45:43 +01:00
|
|
|
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);
|
|
|
|
}
|
2020-02-08 02:48:39 +09:00
|
|
|
else if (type == LuaWidget::InventoryWidget) {
|
2020-02-23 22:38:51 +09:00
|
|
|
std::string inventory, playerName, inventory_name;
|
|
|
|
gk::Vector3i block;
|
2019-01-25 13:45:43 +01:00
|
|
|
float width, height;
|
|
|
|
u16 offset, count;
|
2020-02-23 22:38:51 +09:00
|
|
|
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);
|
|
|
|
|
|
|
|
auto &inventoryWidget = m_inventoryWidgets.back();
|
|
|
|
inventoryWidget.setPosition(x, y);
|
|
|
|
inventoryWidget.init(*widgetInventory, offset, count);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
DEBUG("ERROR: Widget inventory is invalid");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
2019-01-25 13:45:43 +01:00
|
|
|
|
2020-02-23 22:38:51 +09:00
|
|
|
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;
|
|
|
|
}
|
2019-02-26 15:44:32 +01:00
|
|
|
|
2020-02-23 22:38:51 +09:00
|
|
|
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);
|
2019-01-25 13:45:43 +01:00
|
|
|
}
|
2020-02-23 22:38:51 +09:00
|
|
|
else if (type == LuaWidget::CraftingWidget) { // FIXME
|
2020-02-23 18:24:19 +09:00
|
|
|
std::string inventory;
|
2019-01-25 13:45:43 +01:00
|
|
|
gk::Vector3i block;
|
2020-02-03 14:11:18 +09:00
|
|
|
u16 offset, size;
|
2020-02-23 18:00:20 +09:00
|
|
|
s32 resultX, resultY;
|
2020-02-23 18:24:19 +09:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (craftingInventory) {
|
|
|
|
m_craftingWidgets.emplace_back(m_client, *craftingInventory, &m_mainWidget);
|
2019-02-26 15:44:32 +01:00
|
|
|
|
|
|
|
auto &craftingWidget = m_craftingWidgets.back();
|
2020-02-03 14:11:18 +09:00
|
|
|
craftingWidget.init(offset, size);
|
2020-02-23 18:00:20 +09:00
|
|
|
craftingWidget.craftingInventoryWidget().setPosition(x, y);
|
|
|
|
craftingWidget.craftingResultInventoryWidget().setPosition(resultX, resultY);
|
2019-01-25 13:45:43 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-02-23 18:24:19 +09:00
|
|
|
DEBUG("ERROR: Crafting inventory is invalid");
|
2019-01-25 13:45:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-07 02:05:23 +01:00
|
|
|
|