-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
157 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|