TGUI 1.11
Loading...
Searching...
No Matches
SpinButton.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2025 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/Widgets/ClickableWidget.hpp>
30#include <TGUI/Timer.hpp>
31
32#include <cmath>
33
35
36namespace tgui
37{
41 class TGUI_API SpinButton : public ClickableWidget
42 {
43 public:
44
45 using Ptr = std::shared_ptr<SpinButton>;
46 using ConstPtr = std::shared_ptr<const SpinButton>;
47
48 static constexpr const char StaticWidgetType[] = "SpinButton";
49
57 SpinButton(const char* typeName = StaticWidgetType, bool initRenderer = true);
58
67 TGUI_NODISCARD static SpinButton::Ptr create(float minimum = 0, float maximum = 10);
68
76 TGUI_NODISCARD static SpinButton::Ptr copy(const SpinButton::ConstPtr& spinButton);
77
82 TGUI_NODISCARD SpinButtonRenderer* getSharedRenderer() override;
83 TGUI_NODISCARD const SpinButtonRenderer* getSharedRenderer() const override;
84
90 TGUI_NODISCARD SpinButtonRenderer* getRenderer() override;
91
100 void setSize(const Layout2d& size) override;
101 using Widget::setSize;
102
111 void setMinimum(float minimum);
112
120 TGUI_NODISCARD float getMinimum() const;
121
130 void setMaximum(float maximum);
131
139 TGUI_NODISCARD float getMaximum() const;
140
149 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic<T>::value, T>>
150 void setValue(T value)
151 {
152 // TGUI_NEXT: For backwards compatibility, this function needs to accept a float without conversion warnings.
153 // We however need the function to take a double as parameter to actually make use of the extra significant digits.
154 const double oldValue = m_value;
155
156 // Round to nearest allowed value
157 if (m_step != 0)
158 m_value = m_minimum + (std::round((static_cast<double>(value) - m_minimum) / m_step) * m_step);
159
160 // When the value is below the minimum or above the maximum then adjust it
161 if (m_value < m_minimum)
162 m_value = m_minimum;
163 else if (m_value > m_maximum)
164 m_value = m_maximum;
165
166 if (oldValue != m_value)
167 onValueChange.emit(this, static_cast<float>(m_value));
168 }
169
177 TGUI_NODISCARD float getValue() const;
178
184 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic<T>::value, T>>
185 void setStep(T step)
186 {
187 // TGUI_NEXT: For backwards compatibility, this function needs to accept a float without conversion warnings.
188 // We however need the function to take a double as parameter to actually make use of the extra significant digits in calculations.
189 m_step = static_cast<double>(step);
190 }
191
197 TGUI_NODISCARD float getStep() const;
198
205 TGUI_DEPRECATED("Use setOrientation instead")void setVerticalScroll(bool vertical);
206
211 TGUI_DEPRECATED("Use getOrientation instead")TGUI_NODISCARD bool getVerticalScroll() const;
212
219 void setOrientation(Orientation orientation);
220
226 TGUI_NODISCARD Orientation getOrientation() const;
227
231 bool leftMousePressed(Vector2f pos) override;
232
236 void leftMouseReleased(Vector2f pos) override;
237
241 void mouseMoved(Vector2f pos) override;
242
249 void draw(BackendRenderTarget& target, RenderStates states) const override;
250
252 protected:
253
263 TGUI_NODISCARD Signal& getSignal(String signalName) override;
264
270 void rendererChanged(const String& property) override;
271
275 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
276
280 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
281
283 // Returns the size of the arrows
285 TGUI_NODISCARD Vector2f getArrowSize() const;
286
288 // Makes a copy of the widget
290 TGUI_NODISCARD Widget::Ptr clone() const override;
291
293 private:
294
296 // Schedules a callback to regularly change the value of the spin button as long as the mouse remains pressed on an arrow
298 void callMousePressPeriodically(std::chrono::time_point<std::chrono::steady_clock> clickedTime, bool repeatedCall);
299
301 public:
302
303 SignalFloat onValueChange = {"ValueChanged"};
304
306 protected:
307
308 Orientation m_orientation = Orientation::Vertical; // Is the spin button draw horizontally (arrows next to each other) or vertically (arrows on top of each other)?
309 bool m_orientationLocked = false; // Will setSize change the orientation or not?
310 std::chrono::time_point<std::chrono::steady_clock> m_lastMousePressTime;
311
312 double m_minimum = 0;
313 double m_maximum = 10;
314 double m_value = 0;
315 double m_step = 1;
316
317 // On which arrow is the mouse?
318 bool m_mouseHoverOnTopArrow = false;
319 bool m_mouseDownOnTopArrow = false;
320
321 Sprite m_spriteArrowUp;
322 Sprite m_spriteArrowUpHover;
323 Sprite m_spriteArrowDown;
324 Sprite m_spriteArrowDownHover;
325
326 // Cached renderer properties
327 Borders m_bordersCached;
328 Color m_borderColorCached;
329 Color m_backgroundColorCached;
330 Color m_backgroundColorHoverCached;
331 Color m_arrowColorCached;
332 Color m_arrowColorHoverCached;
333 float m_borderBetweenArrowsCached = 0;
334 };
335
337}
338
340
341#endif // TGUI_SPIN_BUTTON_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:46
Wrapper for colors.
Definition Color.hpp:71
Class to store the position or size of a widget.
Definition Layout.hpp:321
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:45
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:150
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:46
static constexpr const char StaticWidgetType[]
Type name of the widget.
Definition SpinButton.hpp:48
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.
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:185
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:303
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:84
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:36
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:424
States used for drawing.
Definition RenderStates.hpp:38