TGUI  1.5
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#ifndef TGUI_SLIDER_HPP
26#define TGUI_SLIDER_HPP
27
28#include <TGUI/Widget.hpp>
29#include <TGUI/Renderers/SliderRenderer.hpp>
30
32
33TGUI_MODULE_EXPORT namespace tgui
34{
38 class TGUI_API Slider : public Widget
39 {
40 public:
41
42 using Ptr = std::shared_ptr<Slider>;
43 using ConstPtr = std::shared_ptr<const Slider>;
44
45 static constexpr const char StaticWidgetType[] = "Slider";
46
54 Slider(const char* typeName = StaticWidgetType, bool initRenderer = true);
55
64 TGUI_NODISCARD static Slider::Ptr create(float minimum = 0, float maximum = 10);
65
73 TGUI_NODISCARD static Slider::Ptr copy(const Slider::ConstPtr& slider);
74
79 TGUI_NODISCARD SliderRenderer* getSharedRenderer() override;
80 TGUI_NODISCARD const SliderRenderer* getSharedRenderer() const override;
81
87 TGUI_NODISCARD SliderRenderer* getRenderer() override;
88
96 void setSize(const Layout2d& size) override;
97 using Widget::setSize;
98
106 TGUI_NODISCARD Vector2f getFullSize() const override;
107
116 TGUI_NODISCARD Vector2f getWidgetOffset() const override;
117
127 void setMinimum(float minimum);
128
136 TGUI_NODISCARD float getMinimum() const;
137
147 void setMaximum(float maximum);
148
156 TGUI_NODISCARD float getMaximum() const;
157
166 void setValue(float value);
167
175 TGUI_NODISCARD float getValue() const;
176
185 void setStep(float step);
186
192 TGUI_NODISCARD float getStep() const;
193
194#ifndef TGUI_REMOVE_DEPRECATED_CODE
201 TGUI_DEPRECATED("Use setOrientation instead") void setVerticalScroll(bool vertical);
202
207 TGUI_DEPRECATED("Use getOrientation instead") TGUI_NODISCARD bool getVerticalScroll() const;
208#endif
209
218 void setOrientation(Orientation orientation);
219
225 TGUI_NODISCARD Orientation getOrientation() const;
226
233 void setInvertedDirection(bool invertedDirection);
234
241 TGUI_NODISCARD bool getInvertedDirection() const;
242
247 void setChangeValueOnScroll(bool changeValueOnScroll);
248
253 TGUI_NODISCARD bool getChangeValueOnScroll() const;
254
260 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
261
265 bool leftMousePressed(Vector2f pos) override;
266
270 void leftMouseReleased(Vector2f pos) override;
271
275 void mouseMoved(Vector2f pos) override;
276
280 bool scrolled(float delta, Vector2f pos, bool touch) override;
281
285 void leftMouseButtonNoLongerDown() override;
286
293 void draw(BackendRenderTarget& target, RenderStates states) const override;
294
296 protected:
297
307 TGUI_NODISCARD Signal& getSignal(String signalName) override;
308
314 void rendererChanged(const String& property) override;
315
319 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
320
324 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
325
327 // Returns the size without the borders
329 TGUI_NODISCARD Vector2f getInnerSize() const;
330
332 // Updates the position of the thumb based on the current value of the slider
334 void updateThumbPosition();
335
337 // Makes a copy of the widget
339 TGUI_NODISCARD Widget::Ptr clone() const override;
340
342 public:
343
344 SignalFloat onValueChange = {"ValueChanged"};
345
347 protected:
348
349 FloatRect m_thumb;
350
351 // When the mouse went down, did it go down on top of the thumb? If so, where?
352 bool m_mouseDownOnThumb = false;
353 Vector2f m_mouseDownOnThumbPos;
354
355 float m_minimum = 0;
356 float m_maximum = 10;
357 float m_value = 0;
358 float m_step = 1;
359
360 bool m_invertedDirection = false; // Are min and max swapped?
361 bool m_changeValueOnScroll = true; // Does mouseScroll event change slider value?
362
363 Orientation m_orientation = Orientation::Horizontal; // Is the slider drawn horizontally or vertically?
364 Orientation m_imageOrientation = Orientation::Horizontal; // Does the loaded image lie horizontally or vertically?
365
366 Sprite m_spriteTrack;
367 Sprite m_spriteTrackHover;
368 Sprite m_spriteThumb;
369 Sprite m_spriteThumbHover;
370
371 // Cached renderer properties
372 Borders m_bordersCached;
373 Color m_borderColorCached;
374 Color m_borderColorHoverCached;
375 Color m_thumbColorCached;
376 Color m_thumbColorHoverCached;
377 Color m_trackColorCached;
378 Color m_trackColorHoverCached;
379 bool m_thumbWithinTrackCached = false;
380 };
381
383}
384
386
387#endif // TGUI_SLIDER_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:46
Wrapper for colors.
Definition Color.hpp:73
Class to store the position or size of a widget.
Definition Layout.hpp:313
Definition Outline.hpp:38
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:61
Definition SliderRenderer.hpp:35
Slider widget.
Definition Slider.hpp:39
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.
TGUI_NODISCARD Orientation getOrientation() const
Returns whether the slider lies horizontally or vertically.
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 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:42
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 setOrientation(Orientation orientation)
Changes whether the slider lies horizontally or vertically.
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.
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:43
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:47
Wrapper class to store strings.
Definition String.hpp:96
The parent class for every widget.
Definition Widget.hpp:83
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:86
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38
Orientation
Orientation of the object.
Definition Layout.hpp:51
States used for drawing.
Definition RenderStates.hpp:38