TGUI  1.6.1
Loading...
Searching...
No Matches
SpinButton.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2024 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
33
34TGUI_MODULE_EXPORT namespace tgui
35{
39 class TGUI_API SpinButton : public ClickableWidget
40 {
41 public:
42
43 using Ptr = std::shared_ptr<SpinButton>;
44 using ConstPtr = std::shared_ptr<const SpinButton>;
45
46 static constexpr const char StaticWidgetType[] = "SpinButton";
47
55 SpinButton(const char* typeName = StaticWidgetType, bool initRenderer = true);
56
65 TGUI_NODISCARD static SpinButton::Ptr create(float minimum = 0, float maximum = 10);
66
74 TGUI_NODISCARD static SpinButton::Ptr copy(const SpinButton::ConstPtr& spinButton);
75
80 TGUI_NODISCARD SpinButtonRenderer* getSharedRenderer() override;
81 TGUI_NODISCARD const SpinButtonRenderer* getSharedRenderer() const override;
82
88 TGUI_NODISCARD SpinButtonRenderer* getRenderer() override;
89
98 void setSize(const Layout2d& size) override;
99 using Widget::setSize;
100
109 void setMinimum(float minimum);
110
118 TGUI_NODISCARD float getMinimum() const;
119
128 void setMaximum(float maximum);
129
137 TGUI_NODISCARD float getMaximum() const;
138
147 void setValue(float value);
148
156 TGUI_NODISCARD float getValue() const;
157
163 void setStep(float step);
164
170 TGUI_NODISCARD float getStep() const;
171
178 TGUI_DEPRECATED("Use setOrientation instead")void setVerticalScroll(bool vertical);
179
184 TGUI_DEPRECATED("Use getOrientation instead")TGUI_NODISCARD bool getVerticalScroll() const;
185
192 void setOrientation(Orientation orientation);
193
199 TGUI_NODISCARD Orientation getOrientation() const;
200
204 bool leftMousePressed(Vector2f pos) override;
205
209 void leftMouseReleased(Vector2f pos) override;
210
214 void mouseMoved(Vector2f pos) override;
215
222 void draw(BackendRenderTarget& target, RenderStates states) const override;
223
225 protected:
226
236 TGUI_NODISCARD Signal& getSignal(String signalName) override;
237
243 void rendererChanged(const String& property) override;
244
248 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
249
253 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
254
256 // Returns the size of the arrows
258 TGUI_NODISCARD Vector2f getArrowSize() const;
259
261 // Makes a copy of the widget
263 TGUI_NODISCARD Widget::Ptr clone() const override;
264
266 private:
267
269 // Schedules a callback to regularly change the value of the spin button as long as the mouse remains pressed on an arrow
271 void callMousePressPeriodically(std::chrono::time_point<std::chrono::steady_clock> clicked);
272
274 public:
275
276 SignalFloat onValueChange = {"ValueChanged"};
277
279 protected:
280
281 Orientation m_orientation = Orientation::Vertical; // Is the spin button draw horizontally (arrows next to each other) or vertically (arrows on top of each other)?
282 bool m_orientationLocked = false; // Will setSize change the orientation or not?
283 std::chrono::time_point<std::chrono::steady_clock> m_PressedAt;
284
285 float m_minimum = 0;
286 float m_maximum = 10;
287 float m_value = 0;
288 float m_step = 1;
289
290 // On which arrow is the mouse?
291 bool m_mouseHoverOnTopArrow = false;
292 bool m_mouseDownOnTopArrow = false;
293
294 Sprite m_spriteArrowUp;
295 Sprite m_spriteArrowUpHover;
296 Sprite m_spriteArrowDown;
297 Sprite m_spriteArrowDownHover;
298
299 // Cached renderer properties
300 Borders m_bordersCached;
301 Color m_borderColorCached;
302 Color m_backgroundColorCached;
303 Color m_backgroundColorHoverCached;
304 Color m_arrowColorCached;
305 Color m_arrowColorHoverCached;
306 float m_borderBetweenArrowsCached = 0;
307 };
308
310}
311
313
314#endif // TGUI_SPIN_BUTTON_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:46
Clickable widget.
Definition ClickableWidget.hpp:38
Wrapper for colors.
Definition Color.hpp:73
Class to store the position or size of a widget.
Definition Layout.hpp:323
Definition Outline.hpp:38
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:61
Definition SpinButtonRenderer.hpp:35
Spin button widget.
Definition SpinButton.hpp:40
static SpinButton::Ptr create(float minimum=0, float maximum=10)
Creates a new spin button widget.
void setStep(float step)
Changes how much the value changes on each arrow press.
std::shared_ptr< SpinButton > Ptr
Shared widget pointer.
Definition SpinButton.hpp:43
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.
float getStep() const
Returns the number of positions the thumb advances with each move.
float getValue() const
Returns the current value.
void setValue(float value)
Changes the current value.
float getMinimum() const
Returns the minimum value.
std::shared_ptr< const SpinButton > ConstPtr
Shared constant widget pointer.
Definition SpinButton.hpp:44
void setSize(const Layout2d &size) override
Changes the size of the spin button.
void setMaximum(float maximum)
Sets a maximum value.
void setMinimum(float minimum)
Sets a minimum value.
SpinButtonRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
static SpinButton::Ptr copy(const SpinButton::ConstPtr &spinButton)
Makes a copy of another spin button.
Definition Sprite.hpp:47
Wrapper class to store strings.
Definition String.hpp:96
The parent class for every widget.
Definition Widget.hpp:83
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38
Orientation
Orientation of the object.
Definition Layout.hpp:52
States used for drawing.
Definition RenderStates.hpp:38