TGUI  0.8.8
Scrollbar.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_SCROLLBAR_HPP
27 #define TGUI_SCROLLBAR_HPP
28 
29 
30 #include <TGUI/Widget.hpp>
31 #include <TGUI/Renderers/ScrollbarRenderer.hpp>
32 
34 
35 namespace tgui
36 {
40  class TGUI_API Scrollbar : public Widget
41  {
42  public:
43 
44  typedef std::shared_ptr<Scrollbar> Ptr;
45  typedef std::shared_ptr<const Scrollbar> ConstPtr;
46 
47 
49  enum class Policy
50  {
51  Automatic,
52  Always,
53  Never
54  };
55 
56 
58  // Default constructor
60  Scrollbar();
61 
62 
70 
71 
81 
82 
88  const ScrollbarRenderer* getSharedRenderer() const;
89 
96  const ScrollbarRenderer* getRenderer() const;
97 
98 
106  void setSize(const Layout2d& size) override;
107  using Widget::setSize;
108 
109 
118  void setMaximum(unsigned int maximum);
119 
120 
129  unsigned int getMaximum() const;
130 
131 
139  void setValue(unsigned int value);
140 
141 
150  unsigned int getValue() const;
151 
152 
165  void setViewportSize(unsigned int viewport);
166 
167 
172  unsigned int getViewportSize() const;
173 
174 
181  void setScrollAmount(unsigned int scrollAmount);
182 
183 
190  unsigned int getScrollAmount() const;
191 
192 
200  void setAutoHide(bool autoHide);
201 
202 
209  bool getAutoHide() const;
210 
211 
218  void setVerticalScroll(bool vertical);
219 
220 
225  bool getVerticalScroll() const;
226 
227 
235 
236 
243  bool mouseOnWidget(Vector2f pos) const override;
244 
248  void leftMousePressed(Vector2f pos) override;
249 
253  void leftMouseReleased(Vector2f pos) override;
254 
258  void mouseMoved(Vector2f pos) override;
259 
263  bool mouseWheelScrolled(float delta, Vector2f pos) override;
264 
268  void leftMouseButtonNoLongerDown() override;
269 
270 
278  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
279 
280 
282  protected:
283 
284 
286  // Updates the scrollbar after a size change
288  void updateSize();
289 
290 
300  Signal& getSignal(std::string signalName) override;
301 
302 
309  void rendererChanged(const std::string& property) override;
310 
311 
315  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
316 
317 
321  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
322 
323 
325  // Updates the position of the thumb based on the current value of the slider
327  void updateThumbPosition();
328 
329 
331  // Makes a copy of the widget
333  Widget::Ptr clone() const override
334  {
335  return std::make_shared<Scrollbar>(*this);
336  }
337 
338 
340  public:
341 
342  SignalUInt onValueChange = {"ValueChanged"};
343 
344 
346  protected:
347 
348  enum class Part
349  {
350  Track,
351  Thumb,
352  ArrowUp,
353  ArrowDown
354  };
355 
356  // Keep track on which part of the scrollbar the mouse is standing
357  Part m_mouseHoverOverPart = Part::Thumb;
358 
359  // When the mouse went down, did it go down on top of the thumb? If so, where?
360  bool m_mouseDownOnThumb = false;
361  Vector2f m_mouseDownOnThumbPos;
362 
363  unsigned int m_maximum = 10;
364  unsigned int m_value = 0;
365 
366  // Maximum should be above this value before the scrollbar is needed
367  unsigned int m_viewportSize = 1;
368 
369  // Is the scrollbar draw vertically?
370  bool m_verticalScroll = true;
371 
372  // Does the image lie vertically?
373  bool m_verticalImage = true;
374 
375  // How far should the value change when pressing one of the arrows?
376  unsigned int m_scrollAmount = 1;
377 
378  // When no scrollbar is needed, should the scrollbar be drawn or stay hidden?
379  bool m_autoHide = true;
380 
381  // Did the mouse went down on one of the arrows?
382  bool m_mouseDownOnArrow = false;
383 
384  bool m_sizeSet = false; // Has setSize been called?
385 
386  FloatRect m_track;
387  FloatRect m_thumb;
388  FloatRect m_arrowUp;
389  FloatRect m_arrowDown;
390 
391  Sprite m_spriteTrack;
392  Sprite m_spriteTrackHover;
393  Sprite m_spriteThumb;
394  Sprite m_spriteThumbHover;
395  Sprite m_spriteArrowUp;
396  Sprite m_spriteArrowUpHover;
397  Sprite m_spriteArrowDown;
398  Sprite m_spriteArrowDownHover;
399 
400  // Cached renderer properties
401  Color m_thumbColorCached;
402  Color m_thumbColorHoverCached;
403  Color m_trackColorCached;
404  Color m_trackColorHoverCached;
405  Color m_arrowColorCached;
406  Color m_arrowColorHoverCached;
407  Color m_arrowBackgroundColorCached;
408  Color m_arrowBackgroundColorHoverCached;
409 
411  };
412 
413 
418  class TGUI_API ScrollbarChildWidget : public Scrollbar
419  {
420  public:
421 
426  bool isMouseDown() const;
427 
428 
433  bool isMouseDownOnThumb() const;
434 
435 
442  bool isShown() const;
443 
444 
446  };
447 
449 }
450 
452 
453 #endif // TGUI_SCROLLBAR_HPP
static Scrollbar::Ptr create()
Creates a new scrollbar widget.
std::shared_ptr< Scrollbar > Ptr
Shared widget pointer.
Definition: Scrollbar.hpp:44
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:224
float getDefaultWidth()
Returns the default width of the scrollbar.
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: Scrollbar.hpp:333
unsigned int getViewportSize() const
Returns the viewport size.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
ScrollbarRenderer * getSharedRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
unsigned int getMaximum() const
Returns the maximum value.
void setScrollAmount(unsigned int scrollAmount)
Changes how much the value changes when scrolling or pressing one of the arrows of the scrollbar.
bool isMouseDownOnThumb() const
Returns whether the mouse is on top of the thumb of the scrollbar.
void rendererChanged(const std::string &property) override
Function called when one of the properties of the renderer is changed.
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 setViewportSize(unsigned int viewport)
Changes the viewport size.
bool isMouseDown() const
Returns whether the mouse is on top of the scrollbar.
ScrollbarRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
bool getAutoHide() const
Returns whether the scrollbar is hiding automatically or not.
Class to store the position or size of a widget.
Definition: Layout.hpp:260
unsigned int getValue() const
Returns the current value.
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.
Scrollbar widget.
Definition: Scrollbar.hpp:41
unsigned int getScrollAmount() const
Returns how much the value changes when scrolling or pressing one of the arrows of the scrollbar.
bool getVerticalScroll() const
Returns whether the scrollbar lies horizontally or vertically.
void setAutoHide(bool autoHide)
Changes whether the scrollbar should hide automatically or not.
bool mouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
void setSize(const Layout2d &size) override
Changes the size of the scrollbar.
static Scrollbar::Ptr copy(Scrollbar::ConstPtr scrollbar)
Makes a copy of another scrollbar.
bool isShown() const
Returns whether the scrollbar is currently visible.
Definition: ScrollbarRenderer.hpp:37
Policy
Defines when the scrollbar shows up.
Definition: Scrollbar.hpp:50
Definition: Vector2f.hpp:39
The parent class for every widget.
Definition: Widget.hpp:74
Signal & getSignal(std::string signalName) override
Retrieves a signal based on its name.
void setValue(unsigned int value)
Changes the current value.
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Wrapper around scrollbar to be used inside widgets that need a scrollbar.
Definition: Scrollbar.hpp:419
std::shared_ptr< const Scrollbar > ConstPtr
Shared constant widget pointer.
Definition: Scrollbar.hpp:45
void draw(sf::RenderTarget &target, sf::RenderStates states) const override
Draw the widget to a render target.
void setVerticalScroll(bool vertical)
Changes whether the scrollbar lies horizontally or vertically.
void setMaximum(unsigned int maximum)
Sets a maximum value.