TGUI 1.13
Loading...
Searching...
No Matches
SpinControl.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_CONTROL_HPP
26#define TGUI_SPIN_CONTROL_HPP
27
28#include <TGUI/SubwidgetContainer.hpp>
29#include <TGUI/Widgets/EditBox.hpp>
30#include <TGUI/Widgets/SpinButton.hpp>
31
33
34namespace tgui
35{
39 class TGUI_API SpinControl : public SubwidgetContainer
40 {
41 public:
42 using Ptr = std::shared_ptr<SpinControl>;
43 using ConstPtr = std::shared_ptr<const SpinControl>;
44
45 static constexpr char StaticWidgetType[] = "SpinControl";
46
54 explicit SpinControl(const char* typeName = StaticWidgetType, bool initRenderer = true);
55
59 SpinControl(const SpinControl& other);
60
64 SpinControl(SpinControl&& other) noexcept;
65
69 SpinControl& operator=(const SpinControl& other);
70
74 SpinControl& operator=(SpinControl&& other) noexcept;
75
87 [[nodiscard]] static SpinControl::Ptr create(
88 float min = 0.0f,
89 float max = 10.0f,
90 float value = 0.0f,
91 unsigned int decimal = 0,
92 float step = 1.0f);
93
101 [[nodiscard]] static SpinControl::Ptr copy(const SpinControl::ConstPtr& SpinCtrl);
102
108 [[nodiscard]] const SpinButtonRenderer* getSpinButtonSharedRenderer() const;
109
116
122 [[nodiscard]] const EditBoxRenderer* getSpinTextSharedRenderer() const;
123
130
136 void setSize(const Layout2d& size) override;
138
147 void setMinimum(float minimum);
148
156 [[nodiscard]] float getMinimum() const;
157
166 void setMaximum(float maximum);
167
175 [[nodiscard]] float getMaximum() const;
176
186 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic_v<T>, T>>
187 bool setValue(T value)
188 {
189 // TGUI_NEXT: For backwards compatibility, this function needs to accept a float without conversion warnings.
190 // We however need the function to take a double as parameter to actually make use of the extra significant digits.
191 if (m_spinButton->getValue() != static_cast<float>(value) && inRange(static_cast<float>(value)))
192 {
193 m_spinButton->setValue(value);
194 setString(String::fromNumberRounded(value, m_decimalPlaces));
195 return true;
196 }
197 return false;
198 }
199
207 [[nodiscard]] float getValue() const;
208
214 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic_v<T>, T>>
215 void setStep(T step)
216 {
217 // TGUI_NEXT: For backwards compatibility, this function needs to accept a float without conversion warnings.
218 // We however need the function to take a double as parameter to actually make use of the extra significant digits in calculations.
219 m_spinButton->setStep(step);
220 }
221
229 [[nodiscard]] float getStep() const;
230
237 void setDecimalPlaces(unsigned int decimalPlaces);
238
246 [[nodiscard]] unsigned int getDecimalPlaces() const;
247
252 TGUI_DEPRECATED("Use setSpinButtonWidth(\"100%\") or setSpinButtonWidth(\"50%\") instead")
253 void setUseWideArrows(bool useWideArrows);
254
259 TGUI_DEPRECATED("Use getSpinButtonWidth() instead") [[nodiscard]] bool getUseWideArrows() const;
260
271
277 [[nodiscard]] float getSpinButtonWidth() const;
278
280
281 protected:
291 [[nodiscard]] Signal& getSignal(String signalName) override;
292
294 // Makes a copy of the widget
296 [[nodiscard]] Widget::Ptr clone() const override;
297
301 [[nodiscard]] std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
302
306 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
307
309
310 private:
312 // Helper function that initializes the widget when constructing a new widget or loading one from a file
314 void init();
315
317 // Checks whether a value lies between the minimum and maximum
319 bool inRange(float value) const;
320
322 // Updates the text in the edit box
324 void setString(const String& str);
325
327
328 public:
329 SignalFloat onValueChange = {"ValueChanged"};
330
332
333 protected:
334 unsigned int m_decimalPlaces = 0;
335 bool m_useWideArrows = false; // TGUI_NEXT: Remove
336 AbsoluteOrRelativeValue m_spinButtonWidth;
337
339
340 private:
341 SpinButton::Ptr m_spinButton = SpinButton::create();
342 EditBox::Ptr m_spinText = EditBox::create();
343 };
344} // namespace tgui
345
346#endif // TGUI_SPIN_CONTROL_HPP
Class to store the a value that is either a constant or a ratio.
Definition AbsoluteOrRelativeValue.hpp:44
Definition EditBoxRenderer.hpp:35
static EditBox::Ptr create()
Creates a new edit box widget.
std::shared_ptr< EditBox > Ptr
Shared widget pointer.
Definition EditBox.hpp:48
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
SpinButtonRenderer * getSpinButtonSharedRenderer()
Returns the renderer of spin buttons part of widget.
EditBoxRenderer * getSpinTextSharedRenderer()
Returns the renderer of edit box part of widget.
void setMaximum(float maximum)
Sets a maximum value.
static SpinControl::Ptr copy(const SpinControl::ConstPtr &SpinCtrl)
Makes a copy of another spin control.
float getMaximum() const
Returns the maximum value.
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
std::shared_ptr< const SpinControl > ConstPtr
Shared constant widget pointer.
Definition SpinControl.hpp:43
float getMinimum() const
Returns the minimum 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.
bool getUseWideArrows() const
Returns whether the spin button width equals the heigh or the widget or only half of the height (defa...
static constexpr char StaticWidgetType[]
Type name of the widget.
Definition SpinControl.hpp:45
SignalFloat onValueChange
Value of the spin control changed. Optional parameter: new value.
Definition SpinControl.hpp:329
SpinControl & operator=(const SpinControl &other)
Overload of copy assignment operator.
void setSpinButtonWidth(AbsoluteOrRelativeValue width)
Changes the width of the spin button (i.e. width of the arrows that are shown next to the edit box).
EditBoxRenderer * getSpinTextRenderer()
Returns the renderer of edit box part of widget.
unsigned int getDecimalPlaces() const
Returns the number of decimal places to display.
static SpinControl::Ptr create(float min=0.0f, float max=10.0f, float value=0.0f, unsigned int decimal=0, float step=1.0f)
Creates a new spin button widget.
SpinControl & operator=(SpinControl &&other) noexcept
Overload of move assignment operator.
void setUseWideArrows(bool useWideArrows)
Changes whether the spin button width equals the heigh or the widget or only half of the height (defa...
void setSize(const Layout2d &size) override
Changes the size of the spin control.
SpinControl(const SpinControl &other)
Copy constructor.
void setMinimum(float minimum)
Sets a minimum value.
bool setValue(T value)
Changes the current value.
Definition SpinControl.hpp:187
Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
std::shared_ptr< SpinControl > Ptr
Shared widget pointer.
Definition SpinControl.hpp:42
void setStep(T step)
Changes how much the value changes on each arrow press.
Definition SpinControl.hpp:215
SpinButtonRenderer * getSpinButtonRenderer()
Returns the renderer of spin buttons part of widget.
void setDecimalPlaces(unsigned int decimalPlaces)
Changes the number of decimal places to display.
float getSpinButtonWidth() const
Returns the width of the spin button (i.e. width of the arrows that are shown next to the edit box).
SpinControl(SpinControl &&other) noexcept
Move constructor.
float getValue() const
Returns the current value.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
float getStep() const
Returns the number of positions the thumb advances with each move.
Wrapper class to store strings.
Definition String.hpp:94
static String fromNumberRounded(T value, unsigned int decimals)
Construct the string from a floating point number, keeping only a certain amount of decimals behind t...
Definition String.hpp:326
void setSize(const Layout2d &size) override
Changes the size of the widget.
The parent class for every widget.
Definition Widget.hpp:83
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:37
SignalTyped< float > SignalFloat
Signal with one "float" as optional unbound parameter.
Definition Signal.hpp:411