--- --- TGUI: include/TGUI/Widgets/SpinButton.hpp Source File
TGUI  1.x-dev
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
97 void setSize(const Layout2d& size) override;
98 using Widget::setSize;
99
108 void setMinimum(float minimum);
109
117 TGUI_NODISCARD float getMinimum() const;
118
127 void setMaximum(float maximum);
128
136 TGUI_NODISCARD float getMaximum() const;
137
146 void setValue(float value);
147
155 TGUI_NODISCARD float getValue() const;
156
162 void setStep(float step);
163
169 TGUI_NODISCARD float getStep() const;
170
171#ifndef TGUI_REMOVE_DEPRECATED_CODE
178 TGUI_DEPRECATED("Use setOrientation instead")void setVerticalScroll(bool vertical);
179
184 TGUI_DEPRECATED("Use getOrientation instead")TGUI_NODISCARD bool getVerticalScroll() const;
185#endif
186
195 void setOrientation(Orientation orientation);
196
202 TGUI_NODISCARD Orientation getOrientation() const;
203
207 bool leftMousePressed(Vector2f pos) override;
208
212 void leftMouseReleased(Vector2f pos) override;
213
217 void mouseMoved(Vector2f pos) override;
218
225 void draw(BackendRenderTarget& target, RenderStates states) const override;
226
228 protected:
229
239 TGUI_NODISCARD Signal& getSignal(String signalName) override;
240
246 void rendererChanged(const String& property) override;
247
251 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
252
256 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
257
259 // Returns the size of the arrows
261 TGUI_NODISCARD Vector2f getArrowSize() const;
262
264 // Makes a copy of the widget
266 TGUI_NODISCARD Widget::Ptr clone() const override;
267
269 private:
270
272 // Schedules a callback to regularly change the value of the spin button as long as the mouse remains pressed on an arrow
274 void callMousePressPeriodically(std::chrono::time_point<std::chrono::steady_clock> clicked);
275
277 public:
278
279 SignalFloat onValueChange = {"ValueChanged"};
280
282 protected:
283
284 Orientation m_orientation = Orientation::Vertical; // Is the spin button draw horizontally (arrows next to each other) or vertically (arrows on top of each other)?
285 std::chrono::time_point<std::chrono::steady_clock> m_PressedAt;
286
287 float m_minimum = 0;
288 float m_maximum = 10;
289 float m_value = 0;
290 float m_step = 1;
291
292 // On which arrow is the mouse?
293 bool m_mouseHoverOnTopArrow = false;
294 bool m_mouseDownOnTopArrow = false;
295
296 Sprite m_spriteArrowUp;
297 Sprite m_spriteArrowUpHover;
298 Sprite m_spriteArrowDown;
299 Sprite m_spriteArrowDownHover;
300
301 // Cached renderer properties
302 Borders m_bordersCached;
303 Color m_borderColorCached;
304 Color m_backgroundColorCached;
305 Color m_backgroundColorHoverCached;
306 Color m_arrowColorCached;
307 Color m_arrowColorHoverCached;
308 float m_borderBetweenArrowsCached = 0;
309 };
310
312}
313
315
316#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:313
Definition Outline.hpp:38
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:61
Spin button widget.
Definition SpinButton.hpp:40
TGUI_NODISCARD 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 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
void setOrientation(Orientation orientation)
Changes whether the spin button lies horizontally or vertically.
TGUI_NODISCARD float getStep() const
Returns the number of positions the thumb advances with each move.
bool leftMousePressed(Vector2f pos) override
Called by the parent when the left mouse button goes down on top of the widget.
TGUI_NODISCARD Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
TGUI_NODISCARD float getMaximum() const
Returns the maximum value.
TGUI_NODISCARD Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
void setValue(float value)
Changes the current value.
TGUI_NODISCARD SpinButtonRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
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 draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
static TGUI_NODISCARD SpinButton::Ptr copy(const SpinButton::ConstPtr &spinButton)
Makes a copy of another spin button.
TGUI_NODISCARD float getValue() const
Returns the current value.
void setMaximum(float maximum)
Sets a maximum value.
void setMinimum(float minimum)
Sets a minimum value.
TGUI_NODISCARD SpinButtonRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
TGUI_NODISCARD float getMinimum() const
Returns the minimum value.
TGUI_NODISCARD Orientation getOrientation() const
Returns whether the spin button lies horizontally or vertically.
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
static TGUI_NODISCARD SpinButton::Ptr create(float minimum=0, float maximum=10)
Creates a new spin button widget.
Definition SpinButtonRenderer.hpp:35
Definition Sprite.hpp:47
Wrapper class to store strings.
Definition String.hpp:96
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:86
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38
Orientation
Orientation of the object.
Definition Layout.hpp:51
States used for drawing.
Definition RenderStates.hpp:38