FifeGUI 0.3.0
A C++ GUI library designed for games.
iconprogressbar.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later OR BSD-3-Clause
2// SPDX-FileCopyrightText: 2004 - 2008 Olof Naessén and Per Larsson
3// SPDX-FileCopyrightText: 2013 - 2026 Fifengine contributors
4
5// Corresponding header include
6#include <fifechan/widgets/iconprogressbar.hpp>
7
8// Standard library includes
9#include <cassert>
10#include <string>
11
12// Project headers (subdirs before local)
13#include "fifechan/exception.hpp"
14#include "fifechan/graphics.hpp"
15#include "fifechan/image.hpp"
16
17namespace fcn
18{
22
23 IconProgressBar::IconProgressBar(Image* image, int maxIcons) : mImage(image), mMaxIcons(maxIcons)
24 {
26 }
27
28 IconProgressBar::IconProgressBar(std::string const & filename, int maxIcons) :
29 mImage(Image::load(filename)), mInternalImage(true), mMaxIcons(maxIcons)
30 {
32 }
33
34 IconProgressBar::~IconProgressBar()
35 {
36 if (mInternalImage) {
37 delete mImage;
38 }
39 }
40
42 {
43 assert("graphics must not be null" && graphics != nullptr);
44 bool const active = isFocused();
45
46 if (isOpaque()) {
47 // Fill the background around the content
48 if (active &&
49 ((getSelectionMode() & Widget::SelectionMode::Background) == Widget::SelectionMode::Background)) {
50 graphics->setColor(getSelectionColor());
51 } else {
52 graphics->setColor(getBackgroundColor());
53 }
54 graphics->fillRectangle(
57 getWidth() - (2 * getBorderSize()),
58 getHeight() - (2 * getBorderSize()));
59 }
60 // draw border or frame
61 if (getBorderSize() > 0) {
62 if (active && (getSelectionMode() & Widget::SelectionMode::Border) == Widget::SelectionMode::Border) {
63 drawSelectionFrame(graphics);
64 } else {
65 drawBorder(graphics);
66 }
67 }
68 if (mImage == nullptr) {
69 return;
70 }
71 // draw "icons"
72 int x = getBorderSize() + getPaddingLeft();
73 int y = getBorderSize() + getPaddingTop();
74 if (mOrientation == Orientation::Horizontal) {
75 for (int i = 0; i < mIconCounter; i++) {
76 graphics->drawImage(mImage, x, y);
77 x += mImage->getWidth();
78 }
79 } else {
80 for (int i = 0; i < mIconCounter; i++) {
81 graphics->drawImage(mImage, x, y);
82 y += mImage->getHeight();
83 }
84 }
85 }
86
88 {
89 mOpaque = opaque;
90 }
91
93 {
94 return mOpaque;
95 }
96
98 {
99 if (mInternalImage) {
100 delete mImage;
101 }
102
103 mInternalImage = false;
104 mImage = image;
105
107 }
108
110 {
111 return mImage;
112 }
113
115 {
116 assert("max icons must be non-negative" && maxIcons >= 0);
117 mMaxIcons = maxIcons;
119 }
120
122 {
123 return mMaxIcons;
124 }
125
127 {
128 if (mOrientation != orientation) {
129 if (orientation != Orientation::Horizontal && orientation != Orientation::Vertical) {
130 throwException("Unknown orientation type in IconProgressBar object");
131 return;
132 }
133 mOrientation = orientation;
135 }
136 }
137
142
144 {
145 assert("max icons must be non-negative" && mMaxIcons >= 0);
146 mIconCounter = (mIconCounter + 1) % (mMaxIcons + 1);
147 }
148
150 {
151 mIconCounter = 0;
152 }
153
155 {
156 mIconCounter = icons % (mMaxIcons + 1);
157 }
158
160 {
161 return mIconCounter;
162 }
163
165 {
166 static_cast<void>(recursion);
168 }
169
174
176 {
177 int w = 0;
178 int h = 0;
179 if (mImage != nullptr) {
180 w = mImage->getWidth();
181 h = mImage->getHeight();
182 if (mOrientation == Orientation::Horizontal) {
183 w *= mMaxIcons;
184 } else {
185 h *= mMaxIcons;
186 }
187 }
188 w += (2 * getBorderSize()) + getPaddingLeft() + getPaddingRight();
189 h += (2 * getBorderSize()) + getPaddingTop() + getPaddingBottom();
190 setSize(w, h);
191 }
192}; // namespace fcn
Abstract interface providing primitive drawing functions (lines, rectangles, etc.).
Definition graphics.hpp:58
virtual void drawImage(Image const *image, int srcX, int srcY, int dstX, int dstY, int width, int height)=0
Draws a part of an image.
virtual void setColor(Color const &color)=0
Sets the color to use when drawing.
virtual void fillRectangle(Rectangle const &rectangle)=0
Draws a filled rectangle.
void adjustSizeImpl()
Adjusts the size of the IconProgressBar to fit the icons.
void setOpaque(bool opaque)
Sets the opacity of the IconProgressBar.
void setIconCount(int icons)
Sets count of icons.
IconProgressBar()
Default constructor.
void advance()
Advances the progress bar by one icon.
Orientation mOrientation
IconProgressBar's orientation.
void setMaxIcons(int maxIcons)
Sets count of icons when the progress bar is full.
Image const * mImage
Image used by the progress bar.
void setImage(Image *image)
Sets the IconProgressBar's image.
int mMaxIcons
Count of icons when progress bar is full.
void resizeToContent(bool recursion=true) override
Resizes the widget's size to fit the content exactly, calls recursively all childs.
int mIconCounter
Holds how many icons are currently displayed.
void setOrientation(Orientation orientation)
Sets the IconProgressBar's orientation.
void reset()
Resets the progress bar.
Orientation getOrientation() const
void draw(Graphics *graphics) override
Draws this IconProgressBar.
bool mOpaque
True if the widget is opaque, false otherwise.
void adjustSize() override
Resizes the widget's size to fit the content exactly.
Orientation
Orientation of the IconProgressBar (horizontal or vertical).
Image const * getImage() const
bool mInternalImage
True if the image has been loaded internally, false otherwise.
Abstract holder for image data.
Definition image.hpp:34
int getWidth() const
Gets the width of the widget.
Definition widget.cpp:252
virtual bool isFocused() const
Checks if the widget is focused.
Definition widget.cpp:624
unsigned int getPaddingLeft() const
Gets the left padding.
Definition widget.cpp:604
virtual void setSize(int width, int height)
Sets the size of the widget.
Definition widget.cpp:1065
Color const & getBackgroundColor() const
Gets the background color.
Definition widget.cpp:762
virtual void drawBorder(Graphics *graphics)
Called when a widget have a border.
Definition widget.cpp:156
unsigned int getPaddingTop() const
Gets the top padding.
Definition widget.cpp:574
unsigned int getBorderSize() const
Gets the size of the widget's border.
Definition widget.cpp:474
virtual void drawSelectionFrame(Graphics *graphics)
Called when a widget is "active" and the selection mode is Frame or FrameWithBackground.
Definition widget.cpp:217
unsigned int getPaddingBottom() const
Gets the bottom padding.
Definition widget.cpp:594
SelectionMode getSelectionMode() const
Gets the selection mode.
Definition widget.cpp:802
int getHeight() const
Gets the height of the widget.
Definition widget.cpp:265
unsigned int getPaddingRight() const
Gets the right padding.
Definition widget.cpp:584
Color const & getSelectionColor() const
Gets the selection color.
Definition widget.cpp:772
Used replacement tokens by configure_file():
void throwException(std::string const &message, std::source_location location=std::source_location::current())
Throw an Exception capturing the current source location.