diff --git a/TODO b/TODO index 0e024aea6..f5457d8dc 100644 --- a/TODO +++ b/TODO @@ -31,9 +31,8 @@ TODO • DONE: Implement `PlayerInventoryWidget` completely in Lua • DONE: Implement `PlayerCraftingWidget` completely in Lua -• TODO: Implement `FurnaceWidget` completely in Lua - ◦ TODO: Add a `ProgressBarWidget` and find how to make it communicate between client and server - ◦ TODO: Remove `BlockMetadata::get` which is only required by `FurnaceWidget` +• WIP: Implement `FurnaceWidget` completely in Lua + ◦ DONE: Add a `ProgressBarWidget` # Menus diff --git a/client/include/gui/FurnaceWidget.hpp b/client/include/gui/FurnaceWidget.hpp index 39ae59dce..e020fb5e7 100644 --- a/client/include/gui/FurnaceWidget.hpp +++ b/client/include/gui/FurnaceWidget.hpp @@ -25,6 +25,7 @@ #include "BlockData.hpp" #include "InventoryWidget.hpp" +#include "ProgressBarWidget.hpp" class FurnaceWidget : public Widget { public: @@ -51,10 +52,10 @@ class FurnaceWidget : public Widget { BlockData &m_blockData; - gk::Image m_burnImage{"texture-furnace"}; - gk::Image m_progressImage{"texture-furnace"}; - MouseItemWidget &m_mouseItemWidget; + + ProgressBarWidget m_progressBar; + ProgressBarWidget m_burnBar; }; #endif // FURNACEWIDGET_HPP_ diff --git a/client/include/gui/ProgressBarWidget.hpp b/client/include/gui/ProgressBarWidget.hpp new file mode 100644 index 000000000..f79b534a2 --- /dev/null +++ b/client/include/gui/ProgressBarWidget.hpp @@ -0,0 +1,62 @@ +/* + * ===================================================================================== + * + * 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 + * + * ===================================================================================== + */ +#ifndef PROGRESSBARWIDGET_HPP_ +#define PROGRESSBARWIDGET_HPP_ + +#include + +#include "BlockData.hpp" +#include "Widget.hpp" + +enum class ProgressBarType { + ItemProcess, + BurnProcess +}; + +class ProgressBarWidget : public Widget { + public: + ProgressBarWidget(const std::string &texture, BlockData &blockData, ProgressBarType type, Widget *parent = nullptr); + + void init(const gk::IntRect &clipRect, const gk::Vector2i &position, const std::string &meta, unsigned int maxMetaValue); + void init(const gk::IntRect &clipRect, const gk::Vector2i &position, const std::string &meta, const std::string &maxMeta); + + void update() override; + + private: + void draw(gk::RenderTarget &target, gk::RenderStates states) const override; + + BlockData &m_blockData; + + gk::IntRect m_clipRect; + gk::Vector2i m_position; + + std::string m_meta; + std::string m_maxMeta; + unsigned int m_maxMetaValue = 0; + + gk::Image m_image; + + ProgressBarType m_type; +}; + +#endif // PROGRESSBARWIDGET_HPP_ diff --git a/client/source/gui/FurnaceWidget.cpp b/client/source/gui/FurnaceWidget.cpp index aef661392..c037df1a9 100644 --- a/client/source/gui/FurnaceWidget.cpp +++ b/client/source/gui/FurnaceWidget.cpp @@ -23,7 +23,10 @@ #include "FurnaceWidget.hpp" FurnaceWidget::FurnaceWidget(ClientCommandHandler &client, MouseItemWidget &mouseItemWidget, Inventory &playerInventory, BlockData &blockData, Widget *parent) - : Widget(176, 166, parent), m_client(client), m_playerInventory(playerInventory), m_blockData(blockData), m_mouseItemWidget(mouseItemWidget) + : Widget(176, 166, parent), m_client(client), m_playerInventory(playerInventory), + m_blockData(blockData), m_mouseItemWidget(mouseItemWidget), + m_progressBar("texture-furnace", blockData, ProgressBarType::ItemProcess, this), + m_burnBar("texture-furnace", blockData, ProgressBarType::BurnProcess, this) { m_background.load("texture-furnace"); m_background.setClipRect(0, 0, 176, 166); @@ -38,11 +41,8 @@ FurnaceWidget::FurnaceWidget(ClientCommandHandler &client, MouseItemWidget &mous m_outputInventoryWidget.setPosition(115, 34, 0); m_fuelInventoryWidget.setPosition(55, 52, 0); - m_burnImage.setClipRect(176, 0, 14, 14); - m_burnImage.setPosition(57, 37, 0); - - m_progressImage.setClipRect(176, 14, 24, 17); - m_progressImage.setPosition(80, 35, 0); + m_progressBar.init(gk::IntRect{176, 14, 24, 17}, gk::Vector2i{80, 35}, "item_progress", 200); + m_burnBar.init(gk::IntRect{176, 0, 14, 14}, gk::Vector2i{57, 37}, "ticks_remaining", "current_burn_time"); } void FurnaceWidget::onEvent(const SDL_Event &event) { @@ -57,19 +57,8 @@ void FurnaceWidget::onEvent(const SDL_Event &event) { } void FurnaceWidget::update() { - u16 ticksRemaining = m_blockData.meta.get("ticks_remaining"); - u16 currentBurnTime = m_blockData.meta.get("current_burn_time"); - u16 itemProgress = m_blockData.meta.get("item_progress"); - - if (currentBurnTime) { - m_burnImage.setPosition(57, 37 + 14 - ticksRemaining * 14 / currentBurnTime, 0); - m_burnImage.setClipRect(176, 14 - ticksRemaining * 14 / currentBurnTime, 14, ticksRemaining * 14 / currentBurnTime); - } - else { - m_burnImage.setClipRect(0, 0, 0, 0); - } - - m_progressImage.setClipRect(176, 14, itemProgress * 24 / 200, 17); + m_progressBar.update(); + m_burnBar.update(); m_inputInventoryWidget.init(m_blockData.inventory, 0, 1); m_outputInventoryWidget.init(m_blockData.inventory, 1, 1); @@ -98,7 +87,10 @@ void FurnaceWidget::draw(gk::RenderTarget &target, gk::RenderStates states) cons target.draw(m_outputInventoryWidget, states); target.draw(m_fuelInventoryWidget, states); - target.draw(m_burnImage, states); - target.draw(m_progressImage, states); + // target.draw(m_burnImage, states); + // target.draw(m_progressImage, states); + + target.draw(m_progressBar, states); + target.draw(m_burnBar, states); } diff --git a/client/source/gui/ProgressBarWidget.cpp b/client/source/gui/ProgressBarWidget.cpp new file mode 100644 index 000000000..9d858aa05 --- /dev/null +++ b/client/source/gui/ProgressBarWidget.cpp @@ -0,0 +1,76 @@ +/* + * ===================================================================================== + * + * 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 "ProgressBarWidget.hpp" + +ProgressBarWidget::ProgressBarWidget(const std::string &texture, BlockData &blockData, ProgressBarType type, Widget *parent) + : Widget(parent), m_blockData(blockData), m_image(texture) +{ + m_type = type; +} + +void ProgressBarWidget::init(const gk::IntRect &clipRect, const gk::Vector2i &position, const std::string &meta, unsigned int maxMetaValue) { + m_clipRect = clipRect; + m_position = position; + + m_image.setClipRect(clipRect.x, clipRect.y, clipRect.sizeX, clipRect.sizeY); + m_image.setPosition(position.x, position.y); + + m_meta = meta; + m_maxMetaValue = maxMetaValue; +} + +void ProgressBarWidget::init(const gk::IntRect &clipRect, const gk::Vector2i &position, const std::string &meta, const std::string &maxMeta) { + m_clipRect = clipRect; + m_position = position; + + m_image.setClipRect(clipRect.x, clipRect.y, clipRect.sizeX, clipRect.sizeY); + m_image.setPosition(position.x, position.y); + + m_meta = meta; + m_maxMeta = maxMeta; +} + +void ProgressBarWidget::update() { + int metaValue = m_blockData.meta.get(m_meta); + if (!m_maxMeta.empty()) + m_maxMetaValue = m_blockData.meta.get(m_maxMeta); + + if (m_maxMetaValue == 0) { + m_image.setClipRect(0, 0, 0, 0); + } + else if (m_type == ProgressBarType::ItemProcess) { + m_image.setClipRect(m_clipRect.x, m_clipRect.y, (float)metaValue / m_maxMetaValue * m_clipRect.sizeX, m_clipRect.sizeY); + } + else if (m_type == ProgressBarType::BurnProcess) { + float height = ceil((float)metaValue / m_maxMetaValue * m_clipRect.sizeY); + m_image.setPosition(m_position.x, m_position.y + m_clipRect.sizeY - height); + m_image.setClipRect(m_clipRect.x, m_clipRect.y + m_clipRect.sizeY - height, m_clipRect.sizeX, height); + } +} + +void ProgressBarWidget::draw(gk::RenderTarget &target, gk::RenderStates states) const { + states.transform *= getTransform(); + + target.draw(m_image, states); +} +