TGUI  1.0-alpha
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 typedef std::shared_ptr<Slider> Ptr;
45 typedef std::shared_ptr<const Slider> ConstPtr;
46
47
55 Slider(const char* typeName = "Slider", bool initRenderer = true);
56
57
67 static Slider::Ptr create(float minimum = 0, float maximum = 10);
68
69
79
80
86 const SliderRenderer* getSharedRenderer() const;
87
94
95
103 void setSize(const Layout2d& size) override;
104 using Widget::setSize;
105
106
115 Vector2f getFullSize() const override;
116
117
127 Vector2f getWidgetOffset() const override;
128
129
140 void setMinimum(float minimum);
141
142
151 float getMinimum() const;
152
153
164 void setMaximum(float maximum);
165
166
175 float getMaximum() const;
176
177
187 void setValue(float value);
188
189
198 float getValue() const;
199
200
209 void setStep(float step);
210
211
217 float getStep() const;
218
219
226 void setVerticalScroll(bool vertical);
227
228
233 bool getVerticalScroll() const;
234
235
242 void setInvertedDirection(bool invertedDirection);
243
244
252
253
258 void setChangeValueOnScroll(bool changeValueOnScroll);
259
260
266
267
274 bool isMouseOnWidget(Vector2f pos) const override;
275
279 void leftMousePressed(Vector2f pos) override;
280
284 void leftMouseReleased(Vector2f pos) override;
285
289 void mouseMoved(Vector2f pos) override;
290
294 bool mouseWheelScrolled(float delta, Vector2f pos) override;
295
299 void leftMouseButtonNoLongerDown() override;
300
301
309 void draw(BackendRenderTarget& target, RenderStates states) const override;
310
311
313 protected:
314
324 Signal& getSignal(String signalName) override;
325
326
333 void rendererChanged(const String& property) override;
334
335
339 std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
340
341
345 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
346
347
349 // Returns the size without the borders
351 Vector2f getInnerSize() const;
352
353
355 // Updates the position of the thumb based on the current value of the slider
357 void updateThumbPosition();
358
359
361 // Makes a copy of the widget
363 Widget::Ptr clone() const override;
364
365
367 public:
368
369 SignalFloat onValueChange = {"ValueChanged"};
370
371
373 protected:
374
375 FloatRect m_thumb;
376
377 // When the mouse went down, did it go down on top of the thumb? If so, where?
378 bool m_mouseDownOnThumb = false;
379 Vector2f m_mouseDownOnThumbPos;
380
381 float m_minimum = 0;
382 float m_maximum = 10;
383 float m_value = 0;
384 float m_step = 1;
385
386 bool m_invertedDirection = false; // Are min and max swapped?
387 bool m_verticalScroll = false; // Is the slider drawn vertically?
388 bool m_verticalImage = false; // Does the image lie vertically?
389 bool m_changeValueOnScroll = true; // Does mouseScroll event change slider value?
390
391 Sprite m_spriteTrack;
392 Sprite m_spriteTrackHover;
393 Sprite m_spriteThumb;
394 Sprite m_spriteThumbHover;
395
396 // Cached renderer properties
397 Borders m_bordersCached;
398 Color m_borderColorCached;
399 Color m_borderColorHoverCached;
400 Color m_thumbColorCached;
401 Color m_thumbColorHoverCached;
402 Color m_trackColorCached;
403 Color m_trackColorHoverCached;
404 bool m_thumbWithinTrackCached = false;
405 };
406
408}
409
411
412#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.
std::shared_ptr< Slider > Ptr
Shared widget pointer.
Definition: Slider.hpp:44
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.
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.
std::shared_ptr< const Slider > ConstPtr
Shared constant widget pointer.
Definition: Slider.hpp:45
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.
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.
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.
static Slider::Ptr copy(Slider::ConstPtr slider)
Makes a copy of another slider.
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
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