FifeGUI 0.3.0
A C++ GUI library designed for games.
imageprogressbar.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/imageprogressbar.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#include "fifechan/rectangle.hpp"
17
18namespace fcn
19{
20 ImageProgressBar::ImageProgressBar() :
21 mBarImage(nullptr),
22 mForegroundImage(nullptr),
23 mInternalImage(false),
24 mMaxValue(100),
25 mValue(0),
26 mOrientation(Orientation::Horizontal),
27 mOpaque(true)
28 {
29 }
30
31 ImageProgressBar::ImageProgressBar(Image* image, int maxValue) :
32 mBarImage(image),
33 mForegroundImage(nullptr),
34 mInternalImage(false),
35 mMaxValue(maxValue),
36 mValue(0),
37 mOrientation(Orientation::Horizontal),
38 mOpaque(true)
39 {
40 assert("max value must be positive" && mMaxValue > 0);
42 }
43
44 ImageProgressBar::ImageProgressBar(std::string const & filename, int maxValue) :
45 mBarImage(Image::load(filename)),
46 mForegroundImage(nullptr),
47 mInternalImage(true),
48 mMaxValue(maxValue),
49 mValue(0),
50 mOrientation(Orientation::Horizontal),
51 mOpaque(true)
52 {
53 assert("max value must be positive" && mMaxValue > 0);
55 }
56
57 ImageProgressBar::~ImageProgressBar()
58 {
59 if (mInternalImage) {
60 delete mBarImage;
61 }
62 }
63
65 {
66 assert("graphics must not be null" && graphics != nullptr);
67 assert("max value must be positive" && mMaxValue > 0);
68 bool const active = isFocused();
69
70 if (isOpaque()) {
71 // Fill the background around the content
72 if (active &&
73 ((getSelectionMode() & Widget::SelectionMode::Background) == Widget::SelectionMode::Background)) {
74 graphics->setColor(getSelectionColor());
75 } else {
76 graphics->setColor(getBackgroundColor());
77 }
78 graphics->fillRectangle(
81 getWidth() - (2 * getBorderSize()),
82 getHeight() - (2 * getBorderSize()));
83 }
84 // draw border or frame
85 if (getBorderSize() > 0) {
86 if (active && (getSelectionMode() & Widget::SelectionMode::Border) == Widget::SelectionMode::Border) {
87 drawSelectionFrame(graphics);
88 } else {
89 drawBorder(graphics);
90 }
91 }
92 if (mBarImage != nullptr) {
93 if (getOrientation() == Orientation::Horizontal) {
94 Rectangle const rec = Rectangle(
97 mBarImage->getWidth() * mValue / mMaxValue,
98 mBarImage->getHeight());
99 graphics->pushClipArea(rec);
100 graphics->drawImage(mBarImage, 0, 0);
101 graphics->popClipArea();
102 } else {
103 Rectangle const rec = Rectangle(
106 (mBarImage->getHeight() - (mBarImage->getHeight() * mValue / mMaxValue)),
107 mBarImage->getWidth(),
108 (mBarImage->getHeight() * mValue / mMaxValue));
109 graphics->pushClipArea(rec);
110 graphics->drawImage(mBarImage, 0, 0);
111 graphics->popClipArea();
112 }
113 }
114
115 if (mForegroundImage != nullptr) {
116 int const x = getBorderSize() + getPaddingLeft();
117 int const y = getBorderSize() + getPaddingTop();
118 graphics->drawImage(mForegroundImage, x, y);
119 }
120 }
121
123 {
124 mOpaque = opaque;
125 }
126
128 {
129 return mOpaque;
130 }
131
133 {
134 if (mInternalImage) {
135 delete mBarImage;
136 }
137
138 mInternalImage = false;
139 mBarImage = image;
140
142 }
143
145 {
146 return mBarImage;
147 }
148
150 {
151 mForegroundImage = image;
153 }
154
156 {
157 return mForegroundImage;
158 }
159
161 {
162 if (mOrientation != orientation) {
163 if (orientation != Orientation::Horizontal && orientation != Orientation::Vertical) {
164 throwException("Unknown orientation type in ImageProgressBar object");
165 return;
166 }
167 mOrientation = orientation;
168 }
169 }
170
175
177 {
178 assert("max value must be positive" && value > 0);
179 mMaxValue = value;
180 }
181
183 {
184 return mMaxValue;
185 }
186
188 {
189 if (value > mMaxValue) {
191 } else if (value < 0) {
192 mValue = 0;
193 } else {
194 mValue = value;
195 }
196 }
197
199 {
200 return mValue;
201 }
202
204 {
205 static_cast<void>(recursion);
207 }
208
213
215 {
216 int w = 0;
217 int h = 0;
218 if (mBarImage != nullptr) {
219 w = mBarImage->getWidth();
220 h = mBarImage->getHeight();
221 }
222 w += (2 * getBorderSize()) + getPaddingLeft() + getPaddingRight();
223 h += (2 * getBorderSize()) + getPaddingTop() + getPaddingBottom();
224 setSize(w, h);
225 }
226}; // namespace fcn
Abstract interface providing primitive drawing functions (lines, rectangles, etc.).
Definition graphics.hpp:58
virtual void popClipArea()
Removes the top most clip area from the stack.
Definition graphics.cpp:65
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 bool pushClipArea(Rectangle area)
Pushes a clip area onto the stack.
Definition graphics.cpp:25
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.
int mValue
Holds the current progress bar value.
void setOrientation(Orientation orientation)
Sets the ImageProgressBar's orientation.
void setValue(int value)
Sets the value of the bar.
int mMaxValue
Value when progress bar is full.
Image const * getBarImage() const
void draw(Graphics *graphics) override
Draws this ImageProgressBar.
void setMaxValue(int value)
Sets the max value of the bar.
void setOpaque(bool opaque)
Sets the opacity of the ImageProgressBar.
Orientation
Orientation of the progress bar (horizontal or vertical).
bool mInternalImage
True if the image has been loaded internally, false otherwise.
void adjustSize() override
Resizes the widget's size to fit the content exactly.
void setForegroundImage(Image *image)
Sets the foreground image.
Image const * getForegroundImage() const
void adjustSizeImpl()
Adjusts the size of the progress bar to fit the content.
Orientation mOrientation
ImageProgressBar's orientation.
void setBarImage(Image *image)
Sets the Bar image.
Image const * mForegroundImage
Foreground image.
int getMaxValue() const
Gets the max value of the bar.
Image const * mBarImage
Bar image.
bool mOpaque
True if the widget is opaque, false otherwise.
Orientation getOrientation() const
void resizeToContent(bool recursion=true) override
Resizes the widget's size to fit the content exactly, calls recursively all childs.
int getValue() const
Gets the value of the bar.
Abstract holder for image data.
Definition image.hpp:34
Represents a rectangular area (X, Y, Width, Height).
Definition rectangle.hpp:22
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.