TGUI 1.13
Loading...
Searching...
No Matches
SpinButton.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2026 Bruno Van de Velde (vdv_b@tgui.eu)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25#ifndef TGUI_SPIN_BUTTON_HPP
26#define TGUI_SPIN_BUTTON_HPP
27
28#include <TGUI/Renderers/SpinButtonRenderer.hpp>
29#include <TGUI/Timer.hpp>
30#include <TGUI/Widgets/ClickableWidget.hpp>
31
32#include <cmath>
33
35
36namespace tgui
37{
41 class TGUI_API SpinButton : public ClickableWidget
42 {
43 public:
44 using Ptr = std::shared_ptr<SpinButton>;
45 using ConstPtr = std::shared_ptr<const SpinButton>;
46
47 static constexpr char StaticWidgetType[] = "SpinButton";
48
56 explicit SpinButton(const char* typeName = StaticWidgetType, bool initRenderer = true);
57
66 [[nodiscard]] static SpinButton::Ptr create(float minimum = 0, float maximum = 10);
67
75 [[nodiscard]] static SpinButton::Ptr copy(const SpinButton::ConstPtr& spinButton);
76
81 [[nodiscard]] SpinButtonRenderer* getSharedRenderer() override;
82 [[nodiscard]] const SpinButtonRenderer* getSharedRenderer() const override;
83
89 [[nodiscard]] SpinButtonRenderer* getRenderer() override;
90
99 void setSize(const Layout2d& size) override;
100 using Widget::setSize;
101
110 void setMinimum(float minimum);
111
119 [[nodiscard]] float getMinimum() const;
120
129 void setMaximum(float maximum);
130
138 [[nodiscard]] float getMaximum() const;
139
148 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic_v<T>, T>>
149 void setValue(T value)
150 {
151 // TGUI_NEXT: For backwards compatibility, this function needs to accept a float without conversion warnings.
152 // We however need the function to take a double as parameter to actually make use of the extra significant digits.
153 const double oldValue = m_value;
154
155 // Round to nearest allowed value
156 if (m_step != 0)
157 m_value = m_minimum + (std::round((static_cast<double>(value) - m_minimum) / m_step) * m_step);
158
159 // When the value is below the minimum or above the maximum then adjust it
160 if (m_value < m_minimum)
161 m_value = m_minimum;
162 else if (m_value > m_maximum)
163 m_value = m_maximum;
164
165 if (oldValue != m_value)
166 onValueChange.emit(this, static_cast<float>(m_value));
167 }
168
176 [[nodiscard]] float getValue() const;
177
183 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic_v<T>, T>>
184 void setStep(T step)
185 {
186 // TGUI_NEXT: For backwards compatibility, this function needs to accept a float without conversion warnings.
187 // We however need the function to take a double as parameter to actually make use of the extra significant digits in calculations.
188 m_step = static_cast<double>(step);
189 }
190
196 [[nodiscard]] float getStep() const;
197
204 TGUI_DEPRECATED("Use setOrientation instead") void setVerticalScroll(bool vertical);
205
210 TGUI_DEPRECATED("Use getOrientation instead") [[nodiscard]] bool getVerticalScroll() const;
211
218 void setOrientation(Orientation orientation);
219
225 [[nodiscard]] Orientation getOrientation() const;
226
230 bool leftMousePressed(Vector2f pos) override;
231
235 void leftMouseReleased(Vector2f pos) override;
236
240 void mouseMoved(Vector2f pos) override;
241
248 void draw(BackendRenderTarget& target, RenderStates states) const override;
249
251
252 protected:
262 [[nodiscard]] Signal& getSignal(String signalName) override;
263
269 void rendererChanged(const String& property) override;
270
274 [[nodiscard]] std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
275
279 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
280
282 // Returns the size of the arrows
284 [[nodiscard]] Vector2f getArrowSize() const;
285
287 // Makes a copy of the widget
289 [[nodiscard]] Widget::Ptr clone() const override;
290
292
293 private:
295 // Schedules a callback to regularly change the value of the spin button as long as the mouse remains pressed on an arrow
297 void callMousePressPeriodically(std::chrono::time_point<std::chrono::steady_clock> clickedTime, bool repeatedCall);
298
300
301 public:
302 SignalFloat onValueChange = {"ValueChanged"};
303
305
306 protected:
307 Orientation m_orientation = Orientation::Vertical; // Is the spin button draw horizontally (arrows next to each other) or vertically (arrows on top of each other)?
308 bool m_orientationLocked = false; // Will setSize change the orientation or not?
309 std::chrono::time_point<std::chrono::steady_clock> m_lastMousePressTime;
310
311 double m_minimum = 0;
312 double m_maximum = 10;
313 double m_value = 0;
314 double m_step = 1;
315
316 // On which arrow is the mouse?
317 bool m_mouseHoverOnTopArrow = false;
318 bool m_mouseDownOnTopArrow = false;
319
320 Sprite m_spriteArrowUp;
321 Sprite m_spriteArrowUpHover;
322 Sprite m_spriteArrowDown;
323 Sprite m_spriteArrowDownHover;
324
325 // Cached renderer properties
326 Borders m_bordersCached;
327 Color m_borderColorCached;
328 Color m_backgroundColorCached;
329 Color m_backgroundColorHoverCached;
330 Color m_arrowColorCached;
331 Color m_arrowColorHoverCached;
332 float m_borderBetweenArrowsCached = 0;
333 };
334} // namespace tgui
335
336#endif // TGUI_SPIN_BUTTON_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:46
Wrapper for colors.
Definition Color.hpp:63
Class to store the position or size of a widget.
Definition Layout.hpp:320
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:59
Definition SpinButtonRenderer.hpp:35
static SpinButton::Ptr create(float minimum=0, float maximum=10)
Creates a new spin button widget.
std::shared_ptr< SpinButton > Ptr
Shared widget pointer.
Definition SpinButton.hpp:44
void setOrientation(Orientation orientation)
Changes whether the spin button lies horizontally or vertically.
bool leftMousePressed(Vector2f pos) override
Called by the parent when the left mouse button goes down on top of the widget.
float getMaximum() const
Returns the maximum value.
SpinButtonRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
bool getVerticalScroll() const
Returns whether the spin button lies horizontally or vertically.
float getStep() const
Returns the number of positions the thumb advances with each move.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
void setValue(T value)
Changes the current value.
Definition SpinButton.hpp:149
float getValue() const
Returns the current value.
std::unique_ptr< DataIO::Node > save(SavingRenderersMap &renderers) const override
Saves the widget as a tree node in order to save it to a file.
void setVerticalScroll(bool vertical)
Changes whether the spin button lies horizontally or vertically.
float getMinimum() const
Returns the minimum value.
std::shared_ptr< const SpinButton > ConstPtr
Shared constant widget pointer.
Definition SpinButton.hpp:45
void setSize(const Layout2d &size) override
Changes the size of the spin button.
Orientation getOrientation() const
Returns whether the spin button lies horizontally or vertically.
Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
static constexpr char StaticWidgetType[]
Type name of the widget.
Definition SpinButton.hpp:47
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
void setStep(T step)
Changes how much the value changes on each arrow press.
Definition SpinButton.hpp:184
void setMaximum(float maximum)
Sets a maximum value.
void setMinimum(float minimum)
Sets a minimum value.
SignalFloat onValueChange
Value of the spin button changed. Optional parameter: new value.
Definition SpinButton.hpp:302
SpinButtonRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
static SpinButton::Ptr copy(const SpinButton::ConstPtr &spinButton)
Makes a copy of another spin button.
Definition Sprite.hpp:45
Wrapper class to store strings.
Definition String.hpp:94
The parent class for every widget.
Definition Widget.hpp:83
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:37
Orientation
Orientation of the object.
Definition Layout.hpp:50
@ Vertical
Vertical orientation.
Definition Layout.hpp:51
SignalTyped< float > SignalFloat
Signal with one "float" as optional unbound parameter.
Definition Signal.hpp:411
States used for drawing.
Definition RenderStates.hpp:38