Skip to content

Commit

Permalink
[ProgressBarWidget] Added.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Feb 23, 2020
1 parent 47b7e3d commit 4eb3e37
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 27 deletions.
5 changes: 2 additions & 3 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 4 additions & 3 deletions client/include/gui/FurnaceWidget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include "BlockData.hpp"
#include "InventoryWidget.hpp"
#include "ProgressBarWidget.hpp"

class FurnaceWidget : public Widget {
public:
Expand All @@ -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_
62 changes: 62 additions & 0 deletions client/include/gui/ProgressBarWidget.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* =====================================================================================
*
* OpenMiner
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <[email protected]>
*
* 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 <gk/graphics/Image.hpp>

#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_
34 changes: 13 additions & 21 deletions client/source/gui/FurnaceWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand All @@ -57,19 +57,8 @@ void FurnaceWidget::onEvent(const SDL_Event &event) {
}

void FurnaceWidget::update() {
u16 ticksRemaining = m_blockData.meta.get<int>("ticks_remaining");
u16 currentBurnTime = m_blockData.meta.get<int>("current_burn_time");
u16 itemProgress = m_blockData.meta.get<int>("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);
Expand Down Expand Up @@ -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);
}

76 changes: 76 additions & 0 deletions client/source/gui/ProgressBarWidget.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* =====================================================================================
*
* OpenMiner
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <[email protected]>
*
* 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<int>(m_meta);
if (!m_maxMeta.empty())
m_maxMetaValue = m_blockData.meta.get<int>(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);
}

0 comments on commit 4eb3e37

Please sign in to comment.