TGUI  1.0-beta
Loading...
Searching...
No Matches
Slider.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_SLIDER_HPP
27#define TGUI_SLIDER_HPP
28
29
30#include <TGUI/Widget.hpp>
31#include <TGUI/Renderers/SliderRenderer.hpp>
32
34
35namespace tgui
36{
40 class TGUI_API Slider : public Widget
41 {
42 public:
43
44 using Ptr = std::shared_ptr<Slider>;
45 using ConstPtr = std::shared_ptr<const Slider>;
46
47 static constexpr const char StaticWidgetType[] = "Slider";
48
49
57 Slider(const char* typeName = StaticWidgetType, bool initRenderer = true);
58
59
69 static Slider::Ptr create(float minimum = 0, float maximum = 10);
70
71
80 static Slider::Ptr copy(const Slider::ConstPtr& slider);
81
82
88 const SliderRenderer* getSharedRenderer() const;
89
96
97
105 void setSize(const Layout2d& size) override;
106 using Widget::setSize;
107
108
117 Vector2f getFullSize() const override;
118
119
129 Vector2f getWidgetOffset() const override;
130
131
142 void setMinimum(float minimum);
143
144
153 float getMinimum() const;
154
155
166 void setMaximum(float maximum);
167
168
177 float getMaximum() const;
178
179
189 void setValue(float value);
190
191
200 float getValue() const;
201
202
211 void setStep(float step);
212
213
219 float getStep() const;
220
221
228 void setVerticalScroll(bool vertical);
229
230
235 bool getVerticalScroll() const;
236
237
244 void setInvertedDirection(bool invertedDirection);
245
246
254
255
260 void setChangeValueOnScroll(bool changeValueOnScroll);
261
262
268
269
276 bool isMouseOnWidget(Vector2f pos) const override;
277
281 void leftMousePressed(Vector2f pos) override;
282
286 void leftMouseReleased(Vector2f pos) override;
287
291 void mouseMoved(Vector2f pos) override;
292
296 bool mouseWheelScrolled(float delta, Vector2f pos) override;
297
301 void leftMouseButtonNoLongerDown() override;
302
303
311 void draw(BackendRenderTarget& target, RenderStates states) const override;
312
313
315 protected:
316
326 Signal& getSignal(String signalName) override;
327
328
335 void rendererChanged(const String& property) override;
336
337
341 std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
342
343
347 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
348
349
351 // Returns the size without the borders
353 Vector2f getInnerSize() const;
354
355
357 // Updates the position of the thumb based on the current value of the slider
359 void updateThumbPosition();
360
361
363 // Makes a copy of the widget
365 Widget::Ptr clone() const override;
366
367
369 public:
370
371 SignalFloat onValueChange = {"ValueChanged"};
372
373
375 protected:
376
377 FloatRect m_thumb;
378
379 // When the mouse went down, did it go down on top of the thumb? If so, where?
380 bool m_mouseDownOnThumb = false;
381 Vector2f m_mouseDownOnThumbPos;
382
383 float m_minimum = 0;
384 float m_maximum = 10;
385 float m_value = 0;
386 float m_step = 1;
387
388 bool m_invertedDirection = false; // Are min and max swapped?
389 bool m_verticalScroll = false; // Is the slider drawn vertically?
390 bool m_verticalImage = false; // Does the image lie vertically?
391 bool m_changeValueOnScroll = true; // Does mouseScroll event change slider value?
392
393 Sprite m_spriteTrack;
394 Sprite m_spriteTrackHover;
395 Sprite m_spriteThumb;
396 Sprite m_spriteThumbHover;
397
398 // Cached renderer properties
399 Borders m_bordersCached;
400 Color m_borderColorCached;
401 Color m_borderColorHoverCached;
402 Color m_thumbColorCached;
403 Color m_thumbColorHoverCached;
404 Color m_trackColorCached;
405 Color m_trackColorHoverCached;
406 bool m_thumbWithinTrackCached = false;
407 };
408
410}
411
413
414#endif // TGUI_SLIDER_HPP
Base class for render targets.
Definition: BackendRenderTarget.hpp:48
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: SliderRenderer.hpp:37
Slider widget.
Definition: Slider.hpp:41
SliderRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
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 draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
bool getVerticalScroll() const
Returns whether the slider lies horizontally or vertically.
Vector2f getWidgetOffset() const override
Returns the distance between the position where the widget is drawn and where the widget is placed.
static Slider::Ptr copy(const Slider::ConstPtr &slider)
Makes a copy of another slider.
bool getChangeValueOnScroll() const
Returns whether the mouse wheel can be used to change the value of the slider.
bool getInvertedDirection() const
Returns whether the side of the slider that is the minimum and maximum is inverted.
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
float getStep() const
Returns the number of positions the thumb advances with each move.
bool isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
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 setVerticalScroll(bool vertical)
Changes whether the slider lies horizontally or vertically.
void setMinimum(float minimum)
Sets a minimum value.
Vector2f getFullSize() const override
Returns the full size of the slider.
void setStep(float step)
Changes the number of positions the thumb advances with each move.
std::shared_ptr< Slider > Ptr
Shared widget pointer.
Definition: Slider.hpp:44
void setChangeValueOnScroll(bool changeValueOnScroll)
Changes whether the mouse wheel can be used to change the value of the slider.
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
void setSize(const Layout2d &size) override
Changes the size of the slider.
static Slider::Ptr create(float minimum=0, float maximum=10)
Creates a new slider widget.
float getValue() const
Returns the current value.
float getMinimum() const
Returns the minimum value.
float getMaximum() const
Returns the maximum value.
SliderRenderer * getSharedRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void setValue(float value)
Changes the current value.
std::shared_ptr< const Slider > ConstPtr
Shared constant widget pointer.
Definition: Slider.hpp:45
void setInvertedDirection(bool invertedDirection)
Changes whether the side of the slider that is the minimum and maximum should be inverted.
void setMaximum(float maximum)
Sets a maximum value.
Definition: Sprite.hpp:45
Wrapper class to store strings.
Definition: String.hpp:79
The parent class for every widget.
Definition: Widget.hpp:70
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:73
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
States used for drawing.
Definition: RenderStates.hpp:39