TGUI  0.8.8
Slider.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2020 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 
35 namespace 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 
49  // Default constructor
51  Slider();
52 
53 
63  static Slider::Ptr create(float minimum = 0, float maximum = 10);
64 
65 
75 
76 
82  const SliderRenderer* getSharedRenderer() const;
83 
90  const SliderRenderer* getRenderer() const;
91 
92 
100  void setSize(const Layout2d& size) override;
101  using Widget::setSize;
102 
103 
112  Vector2f getFullSize() const override;
113 
114 
124  Vector2f getWidgetOffset() const override;
125 
126 
137  void setMinimum(float minimum);
138 
139 
148  float getMinimum() const;
149 
150 
161  void setMaximum(float maximum);
162 
163 
172  float getMaximum() const;
173 
174 
184  void setValue(float value);
185 
186 
195  float getValue() const;
196 
197 
206  void setStep(float step);
207 
208 
214  float getStep() const;
215 
216 
223  void setVerticalScroll(bool vertical);
224 
225 
230  bool getVerticalScroll() const;
231 
232 
239  void setInvertedDirection(bool invertedDirection);
240 
241 
248  bool getInvertedDirection() const;
249 
250 
255  void setChangeValueOnScroll(bool changeValueOnScroll);
256 
257 
263 
264 
271  bool mouseOnWidget(Vector2f pos) const override;
272 
276  void leftMousePressed(Vector2f pos) override;
277 
281  void leftMouseReleased(Vector2f pos) override;
282 
286  void mouseMoved(Vector2f pos) override;
287 
291  bool mouseWheelScrolled(float delta, Vector2f pos) override;
292 
296  void leftMouseButtonNoLongerDown() override;
297 
298 
306  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
307 
308 
310  protected:
311 
321  Signal& getSignal(std::string signalName) override;
322 
323 
330  void rendererChanged(const std::string& property) override;
331 
332 
336  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
337 
338 
342  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
343 
344 
346  // Returns the size without the borders
348  Vector2f getInnerSize() const;
349 
350 
352  // Updates the position of the thumb based on the current value of the slider
354  void updateThumbPosition();
355 
356 
358  // Makes a copy of the widget
360  Widget::Ptr clone() const override
361  {
362  return std::make_shared<Slider>(*this);
363  }
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  };
405 
407 }
408 
410 
411 #endif // TGUI_SLIDER_HPP
Slider widget.
Definition: Slider.hpp:41
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:224
Vector2f getFullSize() const override
Returns the full size of the slider.
static Slider::Ptr copy(Slider::ConstPtr slider)
Makes a copy of another slider.
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:77
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:37
void setInvertedDirection(bool invertedDirection)
Changes whether the side of the slider that is the minimum and maximum should be inverted.
void setMinimum(float minimum)
Sets a minimum value.
float getMinimum() const
Returns the minimum value.
Vector2f getWidgetOffset() const override
Returns the distance between the position where the widget is drawn and where the widget is placed.
Class to store the position or size of a widget.
Definition: Layout.hpp:260
Wrapper for colors.
Definition: Color.hpp:49
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: Slider.hpp:360
void setVerticalScroll(bool vertical)
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.
bool mouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
Definition: Sprite.hpp:46
bool getVerticalScroll() const
Returns whether the slider lies horizontally or vertically.
bool getChangeValueOnScroll() const
Returns whether the mouse wheel can be used to change the value of the slider.
float getStep() const
Returns the number of positions the thumb advances with each move.
Definition: FloatRect.hpp:37
void draw(sf::RenderTarget &target, sf::RenderStates states) const override
Draw the widget to a render target.
float getMaximum() const
Returns the maximum value.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
Signal & getSignal(std::string signalName) override
Retrieves a signal based on its name.
Definition: Vector2f.hpp:39
void setMaximum(float maximum)
Sets a maximum value.
void setChangeValueOnScroll(bool changeValueOnScroll)
Changes 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.
Definition: Outline.hpp:39
void rendererChanged(const std::string &property) override
Function called when one of the properties of the renderer is changed.
The parent class for every widget.
Definition: Widget.hpp:74
SliderRenderer * getSharedRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
std::shared_ptr< Slider > Ptr
Shared widget pointer.
Definition: Slider.hpp:44
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
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.
float getValue() const
Returns the current value.
SliderRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void setValue(float value)
Changes the current value.
Definition: SliderRenderer.hpp:37
std::shared_ptr< const Slider > ConstPtr
Shared constant widget pointer.
Definition: Slider.hpp:45
void setStep(float step)
Changes the number of positions the thumb advances with each move.