/* * ===================================================================================== * * OpenMiner * Copyright (C) 2018-2020 Unarelith, Quentin Bazin * * 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. * * 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. * * 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 * * ===================================================================================== */ #include #include #include #include #include #include "ClientPlayer.hpp" #include "ClientWorld.hpp" #include "Config.hpp" #include "FurnaceWidget.hpp" #include "InventoryWidget.hpp" #include "LuaGUIState.hpp" #include "LuaWidget.hpp" #include "Network.hpp" #include "Player.hpp" #include "PlayerCraftingWidget.hpp" #include "TextButton.hpp" LuaGUIState::LuaGUIState(ClientCommandHandler &client, ClientPlayer &player, ClientWorld &world, sf::Packet &packet, gk::ApplicationState *parent) : InterfaceState(parent), m_client(client) { gk::Mouse::setCursorGrabbed(false); gk::Mouse::setCursorVisible(true); gk::Mouse::resetToWindowCenter(); m_mainWidget.setScale(Config::guiScale, Config::guiScale); while (!packet.endOfPacket()) loadGUI(player, world, packet); } void LuaGUIState::onEvent(const SDL_Event &event) { InterfaceState::onEvent(event); 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(); } 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); // FIXME: Temporary if (m_playerCraftingWidget) m_playerCraftingWidget->onMouseEvent(event, m_mouseItemWidget); m_mouseItemWidget.onEvent(event); } void LuaGUIState::update() { if (m_parent) m_parent->update(); for (auto &it : m_widgets) it->update(); for (auto &it : m_craftingWidgets) { it.update(); } // FIXME: Temporary if (m_playerCraftingWidget) m_playerCraftingWidget->update(); const ItemWidget *currentItemWidget = nullptr; for (auto &it : m_inventoryWidgets) { if (!currentItemWidget) currentItemWidget = it.currentItemWidget(); } for (auto &it : m_craftingWidgets) { if (!currentItemWidget) currentItemWidget = it.currentItemWidget(); } // FIXME: Temporary if (!currentItemWidget && m_playerCraftingWidget) currentItemWidget = m_playerCraftingWidget->currentItemWidget(); if (m_inventoryWidgets.size() != 0) // FIXME m_mouseItemWidget.updateCurrentItem(currentItemWidget); } void LuaGUIState::draw(gk::RenderTarget &target, gk::RenderStates states) const { if (m_parent) target.draw(*m_parent, states); prepareDraw(target, states); states.transform *= m_mainWidget.getTransform(); states.viewMatrix = gk::Transform::Identity; for (auto &it : m_drawables) target.draw(*it, states); for (auto &it : m_widgets) target.draw(*it, states); for (auto &it : m_inventoryWidgets) target.draw(it, states); for (auto &it : m_craftingWidgets) target.draw(it, states); // FIXME: Temporary if (m_playerCraftingWidget) target.draw(*m_playerCraftingWidget, states); target.draw(m_mouseItemWidget, states); } void LuaGUIState::loadGUI(ClientPlayer &player, ClientWorld &world, 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 playerName, inventory; float width, height; u16 offset, count; packet >> playerName >> inventory >> width >> height >> offset >> count; m_inventoryWidgets.emplace_back(m_client, &m_mainWidget); auto &inventoryWidget = m_inventoryWidgets.back(); inventoryWidget.setPosition(x, y); inventoryWidget.init(player.inventory(), offset, count); } else if (type == LuaWidget::CraftingWidget) { gk::Vector3i block; u16 offset, size; s32 resultX, resultY; packet >> block.x >> block.y >> block.z >> offset >> size >> resultX >> resultY; BlockData *data = world.getBlockData(block.x, block.y, block.z); if (data) { m_craftingWidgets.emplace_back(m_client, data->inventory, &m_mainWidget); auto &craftingWidget = m_craftingWidgets.back(); craftingWidget.init(offset, size); craftingWidget.craftingInventoryWidget().setPosition(x, y); craftingWidget.craftingResultInventoryWidget().setPosition(resultX, resultY); } else { DEBUG("ERROR: No inventory found at", block.x, block.y, block.z); } } 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); if (data) { auto *furnaceWidget = new FurnaceWidget(m_client, m_mouseItemWidget, player.inventory(), *data, &m_mainWidget); furnaceWidget->setPosition(x, y); m_widgets.emplace_back(furnaceWidget); } else { DEBUG("ERROR: No inventory found at", block.x, block.y, block.z); } } // FIXME: Temporary else if (type == LuaWidget::PlayerCraftingWidget) { s32 resultX, resultY; packet >> resultX >> resultY; m_playerCraftingWidget.reset(new PlayerCraftingWidget(m_client, &m_mainWidget)); m_playerCraftingWidget->craftingInventoryWidget().setPosition(x, y); m_playerCraftingWidget->craftingResultInventoryWidget().setPosition(resultX, resultY); } }