TGUI  1.3-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
26#ifndef TGUI_SPIN_BUTTON_HPP
27#define TGUI_SPIN_BUTTON_HPP
28
29
30#include <TGUI/Renderers/SpinButtonRenderer.hpp>
31#include <TGUI/Widgets/ClickableWidget.hpp>
32#include <TGUI/Timer.hpp>
33
35
36TGUI_MODULE_EXPORT namespace 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
50
58 SpinButton(const char* typeName = StaticWidgetType, bool initRenderer = true);
59
60
70 TGUI_NODISCARD static SpinButton::Ptr create(float minimum = 0, float maximum = 10);
71
72
81 TGUI_NODISCARD static SpinButton::Ptr copy(const SpinButton::ConstPtr& spinButton);
82
83
88 TGUI_NODISCARD SpinButtonRenderer* getSharedRenderer() override;
89 TGUI_NODISCARD const SpinButtonRenderer* getSharedRenderer() const override;
90
96 TGUI_NODISCARD SpinButtonRenderer* getRenderer() override;
97
98
106 void setSize(const Layout2d& size) override;
107 using Widget::setSize;
108
109
119 void setMinimum(float minimum);
120
121
130 TGUI_NODISCARD float getMinimum() const;
131
132
142 void setMaximum(float maximum);
143
144
153 TGUI_NODISCARD float getMaximum() const;
154
155
165 void setValue(float value);
166
167
176 TGUI_NODISCARD float getValue() const;
177
178
184 void setStep(float step);
185
186
192 TGUI_NODISCARD float getStep() const;
193
194
201 void setVerticalScroll(bool vertical);
202
203
208 TGUI_NODISCARD bool getVerticalScroll() const;
209
210
214 bool leftMousePressed(Vector2f pos) override;
215
219 void leftMouseReleased(Vector2f pos) override;
220
224 void mouseMoved(Vector2f pos) override;
225
226
234 void draw(BackendRenderTarget& target, RenderStates states) const override;
235
236
238 protected:
239
249 TGUI_NODISCARD Signal& getSignal(String signalName) override;
250
251
258 void rendererChanged(const String& property) override;
259
260
264 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
265
266
270 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
271
272
274 // Returns the size of the arrows
276 TGUI_NODISCARD Vector2f getArrowSize() const;
277
278
280 // Makes a copy of the widget
282 TGUI_NODISCARD Widget::Ptr clone() const override;
283
284
286 private:
287
289 // Schedules a callback to regularly change the value of the spin button as long as the mouse remains pressed on an arrow
291 void callMousePressPeriodically(std::chrono::time_point<std::chrono::steady_clock> clicked);
292
293
295 public:
296
297 SignalFloat onValueChange = {"ValueChanged"};
298
299
301 protected:
302
303 // Is the spin button draw vertically (arrows on top of each other)?
304 bool m_verticalScroll = true;
305 std::chrono::time_point<std::chrono::steady_clock> m_PressedAt;
306
307 float m_minimum = 0;
308 float m_maximum = 10;
309 float m_value = 0;
310 float m_step = 1;
311
312 // On which arrow is the mouse?
313 bool m_mouseHoverOnTopArrow = false;
314 bool m_mouseDownOnTopArrow = false;
315
316 Sprite m_spriteArrowUp;
317 Sprite m_spriteArrowUpHover;
318 Sprite m_spriteArrowDown;
319 Sprite m_spriteArrowDownHover;
320
321 // Cached renderer properties
322 Borders m_bordersCached;
323 Color m_borderColorCached;
324 Color m_backgroundColorCached;
325 Color m_backgroundColorHoverCached;
326 Color m_arrowColorCached;
327 Color m_arrowColorHoverCached;
328 float m_borderBetweenArrowsCached = 0;
329 };
330
332}
333
335
336#endif // TGUI_SPIN_BUTTON_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:48
Clickable widget.
Definition ClickableWidget.hpp:40
Wrapper for colors.
Definition Color.hpp:72
Class to store the position or size of a widget.
Definition Layout.hpp:305
Definition Outline.hpp:39
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:62
Definition SpinButtonRenderer.hpp:37
Spin button widget.
Definition SpinButton.hpp:42
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:45
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.
void setVerticalScroll(bool vertical)
Changes whether the spin button lies horizontally or vertically.
std::shared_ptr< const SpinButton > ConstPtr
Shared constant widget pointer.
Definition SpinButton.hpp:46
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 bool getVerticalScroll() const
Returns whether the spin button lies horizontally or vertically.
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.
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 Sprite.hpp:48
Wrapper class to store strings.
Definition String.hpp:101
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:87
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:39
States used for drawing.
Definition RenderStates.hpp:39