TGUI  1.0-alpha
SpinButton.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2022 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
36namespace tgui
37{
41 class TGUI_API SpinButton : public ClickableWidget
42 {
43 public:
44
45 typedef std::shared_ptr<SpinButton> Ptr;
46 typedef std::shared_ptr<const SpinButton> ConstPtr;
47
48
56 SpinButton(const char* typeName = "SpinButton", bool initRenderer = true);
57
58
68 static SpinButton::Ptr create(float minimum = 0, float maximum = 10);
69
70
80
81
87 const SpinButtonRenderer* getSharedRenderer() const;
88
95
96
104 void setSize(const Layout2d& size) override;
105 using Widget::setSize;
106
107
117 void setMinimum(float minimum);
118
119
128 float getMinimum() const;
129
130
140 void setMaximum(float maximum);
141
142
151 float getMaximum() const;
152
153
163 void setValue(float value);
164
165
174 float getValue() const;
175
176
182 void setStep(float step);
183
184
190 float getStep() const;
191
192
199 void setVerticalScroll(bool vertical);
200
201
206 bool getVerticalScroll() const;
207
208
212 void leftMousePressed(Vector2f pos) override;
213
217 void leftMouseReleased(Vector2f pos) override;
218
222 void mouseMoved(Vector2f pos) override;
223
224
232 void draw(BackendRenderTarget& target, RenderStates states) const override;
233
234
236 protected:
237
247 Signal& getSignal(String signalName) override;
248
249
256 void rendererChanged(const String& property) override;
257
258
262 std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
263
264
268 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
269
270
272 // Returns the size of the arrows
274 Vector2f getArrowSize() const;
275
276
278 // Makes a copy of the widget
280 Widget::Ptr clone() const override;
281
282
284 private:
285
287 //
289 void callMousePressPeriodically(std::chrono::time_point<std::chrono::steady_clock> clicked);
290
291
293 public:
294
295 SignalFloat onValueChange = {"ValueChanged"};
296
297
299 protected:
300
301 // Is the spin button draw vertically (arrows on top of each other)?
302 bool m_verticalScroll = true;
303 std::chrono::time_point<std::chrono::steady_clock> m_PressedAt;
304
305 float m_minimum = 0;
306 float m_maximum = 10;
307 float m_value = 0;
308 float m_step = 1;
309
310 // On which arrow is the mouse?
311 bool m_mouseHoverOnTopArrow = false;
312 bool m_mouseDownOnTopArrow = false;
313
314 Sprite m_spriteArrowUp;
315 Sprite m_spriteArrowUpHover;
316 Sprite m_spriteArrowDown;
317 Sprite m_spriteArrowDownHover;
318
319 // Cached renderer properties
320 Borders m_bordersCached;
321 Color m_borderColorCached;
322 Color m_backgroundColorCached;
323 Color m_backgroundColorHoverCached;
324 Color m_arrowColorCached;
325 Color m_arrowColorHoverCached;
326 float m_borderBetweenArrowsCached = 0;
327 };
328
330}
331
333
334#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:63
Class to store the position or size of a widget.
Definition: Layout.hpp:284
Definition: Outline.hpp:39
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:58
Definition: SpinButtonRenderer.hpp:37
Spin button widget.
Definition: SpinButton.hpp:42
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.
SpinButtonRenderer * getSharedRenderer()
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:46
float getMaximum() const
Returns the maximum value.
std::shared_ptr< SpinButton > Ptr
Shared widget pointer.
Definition: SpinButton.hpp:45
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.
float getValue() const
Returns the current value.
void setValue(float value)
Changes 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.
void setSize(const Layout2d &size) override
Changes the size of the spin button.
static SpinButton::Ptr copy(SpinButton::ConstPtr spinButton)
Makes a copy of another spin button.
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.
SpinButtonRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void setMaximum(float maximum)
Sets a maximum value.
void setMinimum(float minimum)
Sets a minimum value.
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
Definition: Sprite.hpp:45
Wrapper class to store strings.
Definition: String.hpp:79
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:73
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
States used for drawing.
Definition: RenderStates.hpp:39