TGUI  0.8.4
Scrollbar.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2019 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 
69  static Scrollbar::Ptr create();
70 
71 
80  static Scrollbar::Ptr copy(Scrollbar::ConstPtr scrollbar);
81 
82 
87  ScrollbarRenderer* getSharedRenderer();
88  const ScrollbarRenderer* getSharedRenderer() const;
89 
95  ScrollbarRenderer* getRenderer();
96  const ScrollbarRenderer* getRenderer() const;
97 
98 
105  void setSize(const Layout2d& size) override;
106  using Widget::setSize;
107 
108 
117  void setMaximum(unsigned int maximum);
118 
119 
128  unsigned int getMaximum() const;
129 
130 
138  void setValue(unsigned int value);
139 
140 
149  unsigned int getValue() const;
150 
151 
164  void setViewportSize(unsigned int viewport);
165 
166 
171  unsigned int getViewportSize() const;
172 
173 
180  void setScrollAmount(unsigned int scrollAmount);
181 
182 
189  unsigned int getScrollAmount() const;
190 
191 
199  void setAutoHide(bool autoHide);
200 
201 
208  bool getAutoHide() const;
209 
210 
217  float getDefaultWidth();
218 
219 
226  bool mouseOnWidget(Vector2f pos) const override;
227 
231  void leftMousePressed(Vector2f pos) override;
232 
236  void leftMouseReleased(Vector2f pos) override;
237 
241  void mouseMoved(Vector2f pos) override;
242 
246  bool mouseWheelScrolled(float delta, Vector2f pos) override;
247 
251  void mouseNoLongerDown() override;
252 
253 
261  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
262 
263 
265  protected:
266 
267 
269  // Updates the scrollbar after a size change
271  void updateSize();
272 
273 
283  Signal& getSignal(std::string signalName) override;
284 
285 
292  void rendererChanged(const std::string& property) override;
293 
294 
298  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
299 
300 
304  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
305 
306 
308  // Updates the position of the thumb based on the current value of the slider
310  void updateThumbPosition();
311 
312 
314  // Makes a copy of the widget
316  Widget::Ptr clone() const override
317  {
318  return std::make_shared<Scrollbar>(*this);
319  }
320 
321 
323  public:
324 
325  SignalUInt onValueChange = {"ValueChanged"};
326 
327 
329  protected:
330 
331  enum class Part
332  {
333  Track,
334  Thumb,
335  ArrowUp,
336  ArrowDown
337  };
338 
339  // Keep track on which part of the scrollbar the mouse is standing
340  Part m_mouseHoverOverPart = Part::Thumb;
341 
342  // When the mouse went down, did it go down on top of the thumb? If so, where?
343  bool m_mouseDownOnThumb = false;
344  Vector2f m_mouseDownOnThumbPos;
345 
346  unsigned int m_maximum = 10;
347  unsigned int m_value = 0;
348 
349  // Maximum should be above this value before the scrollbar is needed
350  unsigned int m_viewportSize = 1;
351 
352  // Is the scrollbar draw vertically?
353  bool m_verticalScroll = true;
354 
355  // Does the image lie vertically?
356  bool m_verticalImage = true;
357 
358  // How far should the value change when pressing one of the arrows?
359  unsigned int m_scrollAmount = 1;
360 
361  // When no scrollbar is needed, should the scrollbar be drawn or stay hidden?
362  bool m_autoHide = true;
363 
364  // Did the mouse went down on one of the arrows?
365  bool m_mouseDownOnArrow = false;
366 
367  bool m_sizeSet = false; // Has setSize been called?
368 
369  FloatRect m_track;
370  FloatRect m_thumb;
371  FloatRect m_arrowUp;
372  FloatRect m_arrowDown;
373 
374  Sprite m_spriteTrack;
375  Sprite m_spriteTrackHover;
376  Sprite m_spriteThumb;
377  Sprite m_spriteThumbHover;
378  Sprite m_spriteArrowUp;
379  Sprite m_spriteArrowUpHover;
380  Sprite m_spriteArrowDown;
381  Sprite m_spriteArrowDownHover;
382 
383  // Cached renderer properties
384  Color m_thumbColorCached;
385  Color m_thumbColorHoverCached;
386  Color m_trackColorCached;
387  Color m_trackColorHoverCached;
388  Color m_arrowColorCached;
389  Color m_arrowColorHoverCached;
390  Color m_arrowBackgroundColorCached;
391  Color m_arrowBackgroundColorHoverCached;
392 
394  };
395 
396 
401  class TGUI_API ScrollbarChildWidget : public Scrollbar
402  {
403  public:
404 
409  bool isMouseDown() const;
410 
411 
416  bool isMouseDownOnThumb() const;
417 
418 
425  bool isShown() const;
426 
427 
429  };
430 
432 }
433 
435 
436 #endif // TGUI_SCROLLBAR_HPP
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Class to store the position or size of a widget.
Definition: Layout.hpp:247
The parent class for every widget.
Definition: Widget.hpp:72
std::shared_ptr< Scrollbar > Ptr
Shared widget pointer.
Definition: Scrollbar.hpp:44
Definition: Vector2f.hpp:37
Wrapper around scrollbar to be used inside widgets that need a scrollbar.
Definition: Scrollbar.hpp:401
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Policy
Defines when the scrollbar shows up.
Definition: Scrollbar.hpp:49
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:76
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:59
Definition: ScrollbarRenderer.hpp:36
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: Scrollbar.hpp:316
Scrollbar widget.
Definition: Scrollbar.hpp:40
std::shared_ptr< const Scrollbar > ConstPtr
Shared constant widget pointer.
Definition: Scrollbar.hpp:45