FifeGUI 0.2.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#include <fifechan/widgets/iconprogressbar.hpp>
6
7#include <fifechan/exception.hpp>
8#include <fifechan/graphics.hpp>
9#include <fifechan/image.hpp>
10
11#include <string>
12
13namespace fcn
14{
16
17 IconProgressBar::IconProgressBar(Image* image, int maxIcons) : mImage(image), mMaxIcons(maxIcons)
18 {
20 }
21
22 IconProgressBar::IconProgressBar(std::string const & filename, int maxIcons) :
23 mImage(Image::load(filename)), mInternalImage(true), mMaxIcons(maxIcons)
24 {
26 }
27
28 IconProgressBar::~IconProgressBar()
29 {
30 if (mInternalImage) {
31 delete mImage;
32 }
33 }
34
36 {
37 bool const active = isFocused();
38
39 if (isOpaque()) {
40 // Fill the background around the content
41 if (active &&
42 ((getSelectionMode() & Widget::SelectionMode::Background) == Widget::SelectionMode::Background)) {
43 graphics->setColor(getSelectionColor());
44 } else {
45 graphics->setColor(getBackgroundColor());
46 }
47 graphics->fillRectangle(
50 getWidth() - (2 * getBorderSize()),
51 getHeight() - (2 * getBorderSize()));
52 }
53 // draw border or frame
54 if (getBorderSize() > 0) {
55 if (active && (getSelectionMode() & Widget::SelectionMode::Border) == Widget::SelectionMode::Border) {
56 drawSelectionFrame(graphics);
57 } else {
58 drawBorder(graphics);
59 }
60 }
61 if (mImage == nullptr) {
62 return;
63 }
64 // draw "icons"
65 int x = getBorderSize() + getPaddingLeft();
66 int y = getBorderSize() + getPaddingTop();
67 if (mOrientation == Orientation::Horizontal) {
68 for (int i = 0; i < mIconCounter; i++) {
69 graphics->drawImage(mImage, x, y);
70 x += mImage->getWidth();
71 }
72 } else {
73 for (int i = 0; i < mIconCounter; i++) {
74 graphics->drawImage(mImage, x, y);
75 y += mImage->getHeight();
76 }
77 }
78 }
79
81 {
82 mOpaque = opaque;
83 }
84
86 {
87 return mOpaque;
88 }
89
91 {
92 if (mInternalImage) {
93 delete mImage;
94 }
95
96 mInternalImage = false;
97 mImage = image;
98
100 }
101
103 {
104 return mImage;
105 }
106
108 {
109 mMaxIcons = maxIcons;
111 }
112
114 {
115 return mMaxIcons;
116 }
117
119 {
120 if (mOrientation != orientation) {
121 if (orientation != Orientation::Horizontal && orientation != Orientation::Vertical) {
122 throwException("Unknown orientation type in IconProgressBar object");
123 return;
124 }
125 mOrientation = orientation;
127 }
128 }
129
134
136 {
137 mIconCounter = (mIconCounter + 1) % (mMaxIcons + 1);
138 }
139
141 {
142 mIconCounter = 0;
143 }
144
146 {
147 mIconCounter = icons % (mMaxIcons + 1);
148 }
149
151 {
152 return mIconCounter;
153 }
154
156 {
157 static_cast<void>(recursion);
159 }
160
165
167 {
168 int w = 0;
169 int h = 0;
170 if (mImage != nullptr) {
171 w = mImage->getWidth();
172 h = mImage->getHeight();
173 if (mOrientation == Orientation::Horizontal) {
174 w *= mMaxIcons;
175 } else {
176 h *= mMaxIcons;
177 }
178 }
181 setSize(w, h);
182 }
183}; // namespace fcn
Abstract interface providing primitive drawing functions (lines, rectangles, etc.).
Definition graphics.hpp:57
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 to use one more 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.
void resizeToContent()
Resizes the widget's size to fit the content exactly, calls recursively all childs.
Definition widget.hpp:1417
int mMaxIcons
Count of icons when progress bar is full.
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:32
int getWidth() const
Gets the width of the widget.
Definition widget.cpp:170
virtual bool isFocused() const
Checks if the widget is focused.
Definition widget.cpp:497
unsigned int getPaddingLeft() const
Gets the left padding.
Definition widget.cpp:477
void setSize(int width, int height)
Sets the size of the widget.
Definition widget.cpp:855
Color const & getBackgroundColor() const
Gets the background color.
Definition widget.cpp:616
virtual void drawBorder(Graphics *graphics)
Called when a widget have a border.
Definition widget.cpp:111
unsigned int getPaddingTop() const
Gets the top padding.
Definition widget.cpp:447
unsigned int getBorderSize() const
Gets the size of the widget's border.
Definition widget.cpp:381
virtual void drawSelectionFrame(Graphics *graphics)
Called when a widget is "active" and the selection mode is Frame or FrameWithBackground.
Definition widget.cpp:135
unsigned int getPaddingBottom() const
Gets the bottom padding.
Definition widget.cpp:467
SelectionMode getSelectionMode() const
Gets the selection mode.
Definition widget.cpp:656
int getHeight() const
Gets the height of the widget.
Definition widget.cpp:183
unsigned int getPaddingRight() const
Gets the right padding.
Definition widget.cpp:457
Color const & getSelectionColor() const
Gets the selection color.
Definition widget.cpp:626