FifeGUI 0.2.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#include <fifechan/widgets/imageprogressbar.hpp>
6
7#include <fifechan/exception.hpp>
8#include <fifechan/graphics.hpp>
9#include <fifechan/image.hpp>
10#include <fifechan/rectangle.hpp>
11
12#include <string>
13
14namespace fcn
15{
16 ImageProgressBar::ImageProgressBar() :
17 mBarImage(nullptr),
18 mForegroundImage(nullptr),
19 mInternalImage(false),
20 mMaxValue(100),
21 mValue(0),
22 mOrientation(Orientation::Horizontal),
23 mOpaque(true)
24 {
25 }
26
27 ImageProgressBar::ImageProgressBar(Image* image, int maxValue) :
28 mBarImage(image),
29 mForegroundImage(nullptr),
30 mInternalImage(false),
31 mMaxValue(maxValue),
32 mValue(0),
33 mOrientation(Orientation::Horizontal),
34 mOpaque(true)
35 {
37 }
38
39 ImageProgressBar::ImageProgressBar(std::string const & filename, int maxValue) :
40 mBarImage(Image::load(filename)),
41 mForegroundImage(nullptr),
42 mInternalImage(true),
43 mMaxValue(maxValue),
44 mValue(0),
45 mOrientation(Orientation::Horizontal),
46 mOpaque(true)
47 {
48
50 }
51
52 ImageProgressBar::~ImageProgressBar()
53 {
54 if (mInternalImage) {
55 delete mBarImage;
56 }
57 }
58
60 {
61 bool const active = isFocused();
62
63 if (isOpaque()) {
64 // Fill the background around the content
65 if (active &&
66 ((getSelectionMode() & Widget::SelectionMode::Background) == Widget::SelectionMode::Background)) {
67 graphics->setColor(getSelectionColor());
68 } else {
69 graphics->setColor(getBackgroundColor());
70 }
71 graphics->fillRectangle(
74 getWidth() - (2 * getBorderSize()),
75 getHeight() - (2 * getBorderSize()));
76 }
77 // draw border or frame
78 if (getBorderSize() > 0) {
79 if (active && (getSelectionMode() & Widget::SelectionMode::Border) == Widget::SelectionMode::Border) {
80 drawSelectionFrame(graphics);
81 } else {
82 drawBorder(graphics);
83 }
84 }
85 if (mBarImage != nullptr) {
86 if (getOrientation() == Orientation::Horizontal) {
87 Rectangle const rec = Rectangle(
90 mBarImage->getWidth() * mValue / mMaxValue,
91 mBarImage->getHeight());
92 graphics->pushClipArea(rec);
93 graphics->drawImage(mBarImage, 0, 0);
94 graphics->popClipArea();
95 } else {
96 Rectangle const rec = Rectangle(
99 (mBarImage->getHeight() - mBarImage->getHeight() * mValue / mMaxValue),
100 mBarImage->getWidth(),
101 mBarImage->getHeight() * mValue / mMaxValue);
102 graphics->pushClipArea(rec);
103 graphics->drawImage(mBarImage, 0, 0);
104 graphics->popClipArea();
105 }
106 }
107
108 if (mForegroundImage != nullptr) {
109 int const x = getBorderSize() + getPaddingLeft();
110 int const y = getBorderSize() + getPaddingTop();
111 graphics->drawImage(mForegroundImage, x, y);
112 }
113 }
114
116 {
117 mOpaque = opaque;
118 }
119
121 {
122 return mOpaque;
123 }
124
126 {
127 if (mInternalImage) {
128 delete mBarImage;
129 }
130
131 mInternalImage = false;
132 mBarImage = image;
133
135 }
136
138 {
139 return mBarImage;
140 }
141
143 {
144 mForegroundImage = image;
146 }
147
149 {
150 return mForegroundImage;
151 }
152
154 {
155 if (mOrientation != orientation) {
156 if (orientation != Orientation::Horizontal && orientation != Orientation::Vertical) {
157 throwException("Unknown orientation type in ImageProgressBar object");
158 return;
159 }
160 mOrientation = orientation;
161 }
162 }
163
168
170 {
171 mMaxValue = value;
172 }
173
175 {
176 return mMaxValue;
177 }
178
180 {
181 if (value > mMaxValue) {
183 } else if (value < 0) {
184 mValue = 0;
185 } else {
186 mValue = value;
187 }
188 }
189
191 {
192 return mValue;
193 }
194
196 {
197 static_cast<void>(recursion);
199 }
200
205
207 {
208 int w = 0;
209 int h = 0;
210 if (mBarImage != nullptr) {
211 w = mBarImage->getWidth();
212 h = mBarImage->getHeight();
213 }
216 setSize(w, h);
217 }
218}; // namespace fcn
Abstract interface providing primitive drawing functions (lines, rectangles, etc.).
Definition graphics.hpp:57
virtual void popClipArea()
Removes the top most clip area from the stack.
Definition graphics.cpp:55
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:18
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 resizeToContent()
Resizes the widget's size to fit the content exactly, calls recursively all childs.
Definition widget.hpp:1417
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
int getValue() const
Gets the value of the bar.
Abstract holder for image data.
Definition image.hpp:32
Represents a rectangular area (X, Y, Width, Height).
Definition rectangle.hpp:20
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