TGUI 1.13
Loading...
Searching...
No Matches
Slider.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2026 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_SLIDER_HPP
26#define TGUI_SLIDER_HPP
27
28#include <TGUI/Renderers/SliderRenderer.hpp>
29#include <TGUI/Widget.hpp>
30
32
33namespace tgui
34{
38 class TGUI_API Slider : public Widget
39 {
40 public:
41 using Ptr = std::shared_ptr<Slider>;
42 using ConstPtr = std::shared_ptr<const Slider>;
43
44 static constexpr char StaticWidgetType[] = "Slider";
45
53 explicit Slider(const char* typeName = StaticWidgetType, bool initRenderer = true);
54
63 [[nodiscard]] static Slider::Ptr create(float minimum = 0, float maximum = 10);
64
72 [[nodiscard]] static Slider::Ptr copy(const Slider::ConstPtr& slider);
73
78 [[nodiscard]] SliderRenderer* getSharedRenderer() override;
79 [[nodiscard]] const SliderRenderer* getSharedRenderer() const override;
80
86 [[nodiscard]] SliderRenderer* getRenderer() override;
87
96 void setSize(const Layout2d& size) override;
97 using Widget::setSize;
98
106 [[nodiscard]] Vector2f getFullSize() const override;
107
116 [[nodiscard]] Vector2f getWidgetOffset() const override;
117
127 void setMinimum(float minimum);
128
136 [[nodiscard]] float getMinimum() const;
137
147 void setMaximum(float maximum);
148
156 [[nodiscard]] float getMaximum() const;
157
166 void setValue(float value);
167
175 [[nodiscard]] float getValue() const;
176
185 void setStep(float step);
186
192 [[nodiscard]] float getStep() const;
193
200 TGUI_DEPRECATED("Use setOrientation instead") void setVerticalScroll(bool vertical);
201
206 TGUI_DEPRECATED("Use getOrientation instead") [[nodiscard]] bool getVerticalScroll() const;
207
214 void setOrientation(Orientation orientation);
215
221 [[nodiscard]] Orientation getOrientation() const;
222
229 void setInvertedDirection(bool invertedDirection);
230
237 [[nodiscard]] bool getInvertedDirection() const;
238
243 void setChangeValueOnScroll(bool changeValueOnScroll);
244
249 [[nodiscard]] bool getChangeValueOnScroll() const;
250
256 [[nodiscard]] bool isMouseOnWidget(Vector2f pos) const override;
257
261 bool leftMousePressed(Vector2f pos) override;
262
266 void leftMouseReleased(Vector2f pos) override;
267
271 void mouseMoved(Vector2f pos) override;
272
276 bool scrolled(float delta, Vector2f pos, bool touch) override;
277
281 void leftMouseButtonNoLongerDown() override;
282
289 void draw(BackendRenderTarget& target, RenderStates states) const override;
290
292
293 protected:
303 [[nodiscard]] Signal& getSignal(String signalName) override;
304
310 void rendererChanged(const String& property) override;
311
315 [[nodiscard]] std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
316
320 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
321
323 // Returns the size without the borders
325 [[nodiscard]] Vector2f getInnerSize() const;
326
328 // Updates the position of the thumb based on the current value of the slider
330 void updateThumbPosition();
331
333 // Makes a copy of the widget
335 [[nodiscard]] Widget::Ptr clone() const override;
336
338
339 public:
340 SignalFloat onValueChange = {"ValueChanged"};
341
343
344 protected:
345 FloatRect m_thumb;
346
347 // When the mouse went down, did it go down on top of the thumb? If so, where?
348 bool m_mouseDownOnThumb = false;
349 Vector2f m_mouseDownOnThumbPos;
350
351 float m_minimum = 0;
352 float m_maximum = 10;
353 float m_value = 0;
354 float m_step = 1;
355
356 bool m_invertedDirection = false; // Are min and max swapped?
357 bool m_changeValueOnScroll = true; // Does mouseScroll event change slider value?
358
359 Orientation m_orientation = Orientation::Horizontal; // Is the slider drawn horizontally or vertically?
360 Orientation m_imageOrientation = Orientation::Horizontal; // Does the loaded image lie horizontally or vertically?
361 bool m_orientationLocked = false; // Will setSize change the orientation or not?
362
363 Sprite m_spriteTrack;
364 Sprite m_spriteTrackHover;
365 Sprite m_spriteThumb;
366 Sprite m_spriteThumbHover;
367
368 // Cached renderer properties
369 Borders m_bordersCached;
370 Color m_borderColorCached;
371 Color m_borderColorHoverCached;
372 Color m_thumbColorCached;
373 Color m_thumbColorHoverCached;
374 Color m_trackColorCached;
375 Color m_trackColorHoverCached;
376 bool m_thumbWithinTrackCached = false;
377 };
378} // namespace tgui
379
380#endif // TGUI_SLIDER_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:46
Wrapper for colors.
Definition Color.hpp:63
Class to store the position or size of a widget.
Definition Layout.hpp:320
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:59
Definition SliderRenderer.hpp:35
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...
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.
bool leftMousePressed(Vector2f pos) override
Called by the parent when the left mouse button goes down on top of the widget.
Vector2f getWidgetOffset() const override
Returns the distance between the position where the widget is drawn and where the widget is placed.
Orientation getOrientation() const
Returns whether the slider lies horizontally or vertically.
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.
static constexpr char StaticWidgetType[]
Type name of the widget.
Definition Slider.hpp:44
SliderRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
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:41
SignalFloat onValueChange
Value of the slider changed. Optional parameter: new value.
Definition Slider.hpp:340
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.
SliderRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void setOrientation(Orientation orientation)
Changes whether the slider lies horizontally or vertically.
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.
void setValue(float value)
Changes the current value.
std::shared_ptr< const Slider > ConstPtr
Shared constant widget pointer.
Definition Slider.hpp:42
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:94
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:37
Orientation
Orientation of the object.
Definition Layout.hpp:50
@ Horizontal
Horizontal orientation.
Definition Layout.hpp:52
SignalTyped< float > SignalFloat
Signal with one "float" as optional unbound parameter.
Definition Signal.hpp:411
States used for drawing.
Definition RenderStates.hpp:38