FifeGUI 0.3.0
A C++ GUI library designed for games.
slider.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/slider.hpp"
7
8// Standard library includes
9#include <algorithm>
10
11// Project headers (subdirs before local)
12#include "fifechan/graphics.hpp"
13#include "fifechan/key.hpp"
14#include "fifechan/mouseinput.hpp"
15
16namespace fcn
17{
18 Slider::Slider(double scaleEnd) : Slider(0, scaleEnd)
19 {
20 }
21
22 Slider::Slider(double scaleStart, double scaleEnd) : mScaleStart(scaleStart), mScaleEnd(scaleEnd)
23 {
24 setFocusable(true);
26 setOrientation(Orientation::Horizontal);
27 setBackgroundColor(Color(220, 226, 237, 255));
28 setBaseColor(Color(154, 169, 196, 255));
29 setForegroundColor(Color(58, 72, 100, 255));
30 setValue(scaleStart);
31 setStepLength((scaleEnd - scaleStart) / 10);
33
34 addMouseListener(this);
35 addKeyListener(this);
36 }
37
38 void Slider::setScale(double scaleStart, double scaleEnd)
39 {
40 mScaleStart = scaleStart;
41 mScaleEnd = scaleEnd;
42 mValue = std::max(mScaleStart, mValue);
43 mValue = std::min(mScaleEnd, mValue);
44 }
45
46 double Slider::getScaleStart() const
47 {
48 return mScaleStart;
49 }
50
51 void Slider::setScaleStart(double scaleStart)
52 {
53 mScaleStart = scaleStart;
54 mValue = std::max(mScaleStart, mValue);
55 }
56
57 double Slider::getScaleEnd() const
58 {
59 return mScaleEnd;
60 }
61
62 void Slider::setScaleEnd(double scaleEnd)
63 {
64 mScaleEnd = scaleEnd;
65 mValue = std::min(mScaleEnd, mValue);
66 }
67
69 {
70 Color trackColor = getBackgroundColor() - Color(0x101010);
71 int const alpha = getBackgroundColor().a;
72 trackColor.a = alpha;
73
74 graphics->setColor(trackColor);
75 graphics->fillRectangle(0, 0, getWidth(), getHeight());
76
77 drawMarker(graphics);
78 }
79
81 {
82 fcn::Color const faceColor = getBaseColor();
83 fcn::Color highlightColor;
84 fcn::Color shadowColor;
85 int const alpha = getBaseColor().a;
86 highlightColor = faceColor + Color(0x303030);
87 highlightColor.a = alpha;
88 shadowColor = faceColor - Color(0x303030);
89 shadowColor.a = alpha;
90
91 graphics->setColor(faceColor);
92
93 if (getOrientation() == Orientation::Horizontal) {
94 int const v = getMarkerPosition();
95 graphics->fillRectangle(v + 1, 1, getMarkerLength() - 2, getHeight() - 2);
96 graphics->setColor(highlightColor);
97 graphics->drawLine(v, 0, v + getMarkerLength() - 1, 0);
98 graphics->drawLine(v, 0, v, getHeight() - 1);
99 graphics->setColor(shadowColor);
100 graphics->drawLine(v + getMarkerLength() - 1, 1, v + getMarkerLength() - 1, getHeight() - 1);
101 graphics->drawLine(v + 1, getHeight() - 1, v + getMarkerLength() - 1, getHeight() - 1);
102
103 if (isFocused()) {
104 graphics->setColor(getForegroundColor());
105 graphics->drawRectangle(v + 2, 2, getMarkerLength() - 4, getHeight() - 4);
106 }
107 } else {
108 int const v = (getHeight() - getMarkerLength()) - getMarkerPosition();
109 graphics->fillRectangle(1, v + 1, getWidth() - 2, getMarkerLength() - 2);
110 graphics->setColor(highlightColor);
111 graphics->drawLine(0, v, 0, v + getMarkerLength() - 1);
112 graphics->drawLine(0, v, getWidth() - 1, v);
113 graphics->setColor(shadowColor);
114 graphics->drawLine(1, v + getMarkerLength() - 1, getWidth() - 1, v + getMarkerLength() - 1);
115 graphics->drawLine(getWidth() - 1, v + 1, getWidth() - 1, v + getMarkerLength() - 1);
116
117 if (isFocused()) {
118 graphics->setColor(getForegroundColor());
119 graphics->drawRectangle(2, v + 2, getWidth() - 4, getMarkerLength() - 4);
120 }
121 }
122 }
123
125 {
126 if (mouseEvent.getButton() == fcn::MouseEvent::Button::Left && mouseEvent.getX() >= 0 &&
127 mouseEvent.getX() <= getWidth() && mouseEvent.getY() >= 0 && mouseEvent.getY() <= getHeight()) {
128 if (getOrientation() == Orientation::Horizontal) {
129 setValue(markerPositionToValue(mouseEvent.getX() - (getMarkerLength() / 2)));
130 } else {
131 setValue(markerPositionToValue(getHeight() - mouseEvent.getY() - (getMarkerLength() / 2)));
132 }
133
135 }
136 }
137
139 {
140 if (getOrientation() == Orientation::Horizontal) {
141 setValue(markerPositionToValue(mouseEvent.getX() - (getMarkerLength() / 2)));
142 } else {
143 setValue(markerPositionToValue(getHeight() - mouseEvent.getY() - (getMarkerLength() / 2)));
144 }
145
147
148 mouseEvent.consume();
149 }
150
151 void Slider::setValue(double value)
152 {
153 if (value > getScaleEnd()) {
155 return;
156 }
157
158 if (value < getScaleStart()) {
160 return;
161 }
162
163 mValue = value;
164 }
165
166 double Slider::getValue() const
167 {
168 return mValue;
169 }
170
172 {
173 return mMarkerLength;
174 }
175
176 void Slider::setMarkerLength(int length)
177 {
178 mMarkerLength = length;
179 }
180
182 {
183 Key const key = keyEvent.getKey();
184
185 if (getOrientation() == Orientation::Horizontal) {
186 if (key.getValue() == fcn::Key::RIGHT) {
189 keyEvent.consume();
190 } else if (key.getValue() == fcn::Key::LEFT) {
193 keyEvent.consume();
194 }
195 } else {
196 if (key.getValue() == fcn::Key::UP) {
199 keyEvent.consume();
200 } else if (key.getValue() == fcn::Key::DOWN) {
203 keyEvent.consume();
204 }
205 }
206 }
207
209 {
210 mOrientation = orientation;
211 }
212
217
218 double Slider::markerPositionToValue(int position) const
219 {
220 int w = 0;
221 if (getOrientation() == Orientation::Horizontal) {
222 w = getWidth();
223 } else {
224 w = getHeight();
225 }
226
227 double const pos = position / (static_cast<double>(w) - getMarkerLength());
228 return ((1.0 - pos) * getScaleStart()) + (pos * getScaleEnd());
229 }
230
231 int Slider::valueToMarkerPosition(double value) const
232 {
233 int v = 0;
234 if (getOrientation() == Orientation::Horizontal) {
235 v = getWidth();
236 } else {
237 v = getHeight();
238 }
239
240 int const w =
241 static_cast<int>(((v - getMarkerLength()) * (value - getScaleStart()) / (getScaleEnd() - getScaleStart())));
242
243 if (w < 0) {
244 return 0;
245 }
246
247 if (w > v - getMarkerLength()) {
248 return v - getMarkerLength();
249 }
250
251 return w;
252 }
253
254 void Slider::setStepLength(double length)
255 {
256 mStepLength = length;
257 }
258
260 {
261 return mStepLength;
262 }
263
265 {
267 }
268
270 {
271 if (getOrientation() == Orientation::Vertical) {
274
275 mouseEvent.consume();
276 }
277 }
278
280 {
281 if (getOrientation() == Orientation::Vertical) {
284
285 mouseEvent.consume();
286 }
287 }
288
290 {
291 if (getOrientation() == Orientation::Horizontal) {
294
295 mouseEvent.consume();
296 }
297 }
298
300 {
301 if (getOrientation() == Orientation::Horizontal) {
304
305 mouseEvent.consume();
306 }
307 }
308} // namespace fcn
Color.
Definition color.hpp:58
uint8_t a
Alpha color component (0-255).
Definition color.hpp:350
Abstract interface providing primitive drawing functions (lines, rectangles, etc.).
Definition graphics.hpp:58
virtual void drawLine(int x1, int y1, int x2, int y2)=0
Draws a line.
virtual void setColor(Color const &color)=0
Sets the color to use when drawing.
virtual void drawRectangle(Rectangle const &rectangle)=0
Draws a simple, non-filled rectangle with a one pixel width.
virtual void fillRectangle(Rectangle const &rectangle)=0
Draws a filled rectangle.
void consume()
Marks this event as consumed.
Represents a key event.
Definition keyevent.hpp:26
Key const & getKey() const
Gets the key of the event.
Definition keyevent.cpp:48
Represents a mouse event.
int getX() const
Gets the x coordinate of the mouse event.
int getY() const
Gets the y coordinate of the mouse event.
MouseEvent::Button getButton() const
Gets the button of the mouse event.
void setScale(double scaleStart, double scaleEnd)
Sets the scale of the slider.
Definition slider.cpp:38
double mValue
Holds the current selected value.
Definition slider.hpp:242
Slider(double scaleEnd=1.0)
Constructor.
Definition slider.cpp:18
virtual int getMarkerPosition() const
Gets the marker position of the current selected value.
Definition slider.cpp:264
void mousePressed(MouseEvent &mouseEvent) override
Called when a mouse button has been pressed down on the widget area.
Definition slider.cpp:124
double mStepLength
Holds the step length.
Definition slider.hpp:250
double getStepLength() const
Gets the step length.
Definition slider.cpp:259
Orientation
Draw orientations for the slider.
Definition slider.hpp:40
double mScaleEnd
Holds the end value of the scale.
Definition slider.hpp:265
void mouseWheelMovedLeft(MouseEvent &mouseEvent) override
Called when the mouse wheel has moved left on the widget area.
Definition slider.cpp:299
virtual double markerPositionToValue(int position) const
Converts a marker position to a value in the scale.
Definition slider.cpp:218
void setStepLength(double length)
Sets the step length.
Definition slider.cpp:254
Orientation mOrientation
Holds the orientation of the slider.
Definition slider.hpp:272
void setScaleStart(double scaleStart)
Sets the start value of the scale.
Definition slider.cpp:51
void keyPressed(KeyEvent &keyEvent) override
Called if a key is pressed when the widget has keyboard focus.
Definition slider.cpp:181
void setOrientation(Orientation orientation)
Sets the orientation of the slider.
Definition slider.cpp:208
double getScaleStart() const
Gets the start value of the scale.
Definition slider.cpp:46
double mScaleStart
Holds the start value of the scale.
Definition slider.hpp:260
void mouseWheelMovedUp(MouseEvent &mouseEvent) override
Called when the mouse wheel has moved up on the widget area.
Definition slider.cpp:269
virtual void drawMarker(Graphics *graphics)
Draws the marker.
Definition slider.cpp:80
Orientation getOrientation() const
Gets the orientation of the slider.
Definition slider.cpp:213
void setScaleEnd(double scaleEnd)
Sets the end value of the scale.
Definition slider.cpp:62
int getMarkerLength() const
Gets the length of the marker.
Definition slider.cpp:171
void mouseWheelMovedDown(MouseEvent &mouseEvent) override
Called when the mouse wheel has moved down on the widget area.
Definition slider.cpp:279
int mMarkerLength
Holds the length of the marker.
Definition slider.hpp:255
void setMarkerLength(int length)
Sets the length of the marker.
Definition slider.cpp:176
void setValue(double value)
Sets the current selected value.
Definition slider.cpp:151
double getValue() const
Gets the current selected value.
Definition slider.cpp:166
void mouseDragged(MouseEvent &mouseEvent) override
Called when the mouse has moved and the mouse has previously been pressed on the widget.
Definition slider.cpp:138
void draw(Graphics *graphics) override
Draws the widget.
Definition slider.cpp:68
virtual int valueToMarkerPosition(double value) const
Converts a value to a marker position.
Definition slider.cpp:231
void mouseWheelMovedRight(MouseEvent &mouseEvent) override
Called when the mouse wheel has moved right on the widget area.
Definition slider.cpp:289
double getScaleEnd() const
Gets the end value of the scale.
Definition slider.cpp:57
Color const & getBaseColor() const
Gets the base color.
Definition widget.cpp:742
void setFocusable(bool focusable)
Sets the widget to be focusable, or not.
Definition widget.cpp:646
int getWidth() const
Gets the width of the widget.
Definition widget.cpp:252
void setBorderSize(unsigned int size)
Sets the size of the widget's border.
Definition widget.cpp:469
virtual bool isFocused() const
Checks if the widget is focused.
Definition widget.cpp:624
Color const & getBackgroundColor() const
Gets the background color.
Definition widget.cpp:762
virtual void setForegroundColor(Color const &color)
Sets the foreground color.
Definition widget.cpp:747
void addMouseListener(MouseListener *mouseListener)
Adds a mouse listener to the widget.
Definition widget.cpp:907
void addKeyListener(KeyListener *keyListener)
Adds a key listener to the widget.
Definition widget.cpp:885
virtual void setBaseColor(Color const &color)
Sets the base color of the widget.
Definition widget.cpp:737
Color const & getForegroundColor() const
Gets the foreground color.
Definition widget.cpp:752
void distributeActionEvent()
Distributes an action event to all action listeners of the widget.
Definition widget.cpp:1291
int getHeight() const
Gets the height of the widget.
Definition widget.cpp:265
virtual void setBackgroundColor(Color const &color)
Sets the background color.
Definition widget.cpp:757
Used replacement tokens by configure_file():