TGUI  1.2.0
Loading...
Searching...
No Matches
Slider.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_SLIDER_HPP
27#define TGUI_SLIDER_HPP
28
29
30#include <TGUI/Widget.hpp>
31#include <TGUI/Renderers/SliderRenderer.hpp>
32
34
35TGUI_MODULE_EXPORT namespace 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 TGUI_NODISCARD static Slider::Ptr create(float minimum = 0, float maximum = 10);
70
71
80 TGUI_NODISCARD static Slider::Ptr copy(const Slider::ConstPtr& slider);
81
82
87 TGUI_NODISCARD SliderRenderer* getSharedRenderer() override;
88 TGUI_NODISCARD const SliderRenderer* getSharedRenderer() const override;
89
95 TGUI_NODISCARD SliderRenderer* getRenderer() override;
96
97
105 void setSize(const Layout2d& size) override;
106 using Widget::setSize;
107
108
117 TGUI_NODISCARD Vector2f getFullSize() const override;
118
119
129 TGUI_NODISCARD Vector2f getWidgetOffset() const override;
130
131
142 void setMinimum(float minimum);
143
144
153 TGUI_NODISCARD float getMinimum() const;
154
155
166 void setMaximum(float maximum);
167
168
177 TGUI_NODISCARD float getMaximum() const;
178
179
189 void setValue(float value);
190
191
200 TGUI_NODISCARD float getValue() const;
201
202
211 void setStep(float step);
212
213
219 TGUI_NODISCARD float getStep() const;
220
221
228 void setVerticalScroll(bool vertical);
229
230
235 TGUI_NODISCARD bool getVerticalScroll() const;
236
237
244 void setInvertedDirection(bool invertedDirection);
245
246
253 TGUI_NODISCARD bool getInvertedDirection() const;
254
255
260 void setChangeValueOnScroll(bool changeValueOnScroll);
261
262
267 TGUI_NODISCARD bool getChangeValueOnScroll() const;
268
269
276 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
277
281 bool leftMousePressed(Vector2f pos) override;
282
286 void leftMouseReleased(Vector2f pos) override;
287
291 void mouseMoved(Vector2f pos) override;
292
296 bool scrolled(float delta, Vector2f pos, bool touch) override;
297
301 void leftMouseButtonNoLongerDown() override;
302
303
311 void draw(BackendRenderTarget& target, RenderStates states) const override;
312
313
315 protected:
316
326 TGUI_NODISCARD Signal& getSignal(String signalName) override;
327
328
335 void rendererChanged(const String& property) override;
336
337
341 TGUI_NODISCARD 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 TGUI_NODISCARD 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 TGUI_NODISCARD 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: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 SliderRenderer.hpp:37
Slider widget.
Definition Slider.hpp:41
bool scrolled(float delta, Vector2f pos, bool touch) override
Called by the parent on scroll event (either from mouse wheel of from two finger scrolling on a touch...
TGUI_NODISCARD Vector2f getWidgetOffset() const override
Returns the distance between the position where the widget is drawn and where the widget is placed.
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
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 bool isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
TGUI_NODISCARD float getStep() const
Returns the number of positions the thumb advances with each move.
TGUI_NODISCARD float getMaximum() const
Returns the maximum value.
TGUI_NODISCARD bool getInvertedDirection() const
Returns whether the side of the slider that is the minimum and maximum is inverted.
TGUI_NODISCARD float getMinimum() const
Returns the minimum value.
TGUI_NODISCARD SliderRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
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 setVerticalScroll(bool vertical)
Changes whether the slider lies horizontally or vertically.
void setMinimum(float minimum)
Sets a minimum value.
TGUI_NODISCARD 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
TGUI_NODISCARD bool getChangeValueOnScroll() const
Returns whether the mouse wheel can be used to change the value of the slider.
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.
TGUI_NODISCARD SliderRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
TGUI_NODISCARD float getValue() const
Returns the current value.
void setValue(float value)
Changes the current value.
TGUI_NODISCARD bool getVerticalScroll() const
Returns whether the slider lies horizontally or vertically.
static TGUI_NODISCARD Slider::Ptr copy(const Slider::ConstPtr &slider)
Makes a copy of another slider.
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.
static TGUI_NODISCARD Slider::Ptr create(float minimum=0, float maximum=10)
Creates a new slider widget.
void setMaximum(float maximum)
Sets a maximum value.
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.
Definition Sprite.hpp:48
Wrapper class to store strings.
Definition String.hpp:101
The parent class for every widget.
Definition Widget.hpp:84
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