TGUI  0.8-alpha
Scrollbar.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2018 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  // Default constructor
51  Scrollbar();
52 
53 
60  static Scrollbar::Ptr create();
61 
62 
71  static Scrollbar::Ptr copy(Scrollbar::ConstPtr scrollbar);
72 
73 
78  ScrollbarRenderer* getSharedRenderer();
79  const ScrollbarRenderer* getSharedRenderer() const;
80 
86  ScrollbarRenderer* getRenderer();
87  const ScrollbarRenderer* getRenderer() const;
88 
89 
96  void setSize(const Layout2d& size) override;
97  using Widget::setSize;
98 
99 
108  void setMaximum(unsigned int maximum);
109 
110 
119  unsigned int getMaximum() const;
120 
121 
129  void setValue(unsigned int value);
130 
131 
140  unsigned int getValue() const;
141 
142 
155  void setViewportSize(unsigned int viewport);
156 
157 
162  unsigned int getViewportSize() const;
163 
164 
171  void setScrollAmount(unsigned int scrollAmount);
172 
173 
180  unsigned int getScrollAmount() const;
181 
182 
190  void setAutoHide(bool autoHide);
191 
192 
199  bool getAutoHide() const;
200 
201 
208  bool mouseOnWidget(Vector2f pos) const override;
209 
213  void leftMousePressed(Vector2f pos) override;
214 
218  void leftMouseReleased(Vector2f pos) override;
219 
223  void mouseMoved(Vector2f pos) override;
224 
228  void mouseWheelScrolled(float delta, Vector2f pos) override;
229 
233  void mouseNoLongerDown() override;
234 
235 
243  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
244 
245 
247  protected:
248 
258  Signal& getSignal(std::string signalName) override;
259 
260 
267  void rendererChanged(const std::string& property) override;
268 
269 
273  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
274 
275 
279  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
280 
281 
283  // Updates the position of the thumb based on the current value of the slider
285  void updateThumbPosition();
286 
287 
289  // Makes a copy of the widget
291  Widget::Ptr clone() const override
292  {
293  return std::make_shared<Scrollbar>(*this);
294  }
295 
296 
298  public:
299 
300  SignalUInt onValueChange = {"ValueChanged"};
301 
302 
304  protected:
305 
306  enum class Part
307  {
308  Track,
309  Thumb,
310  ArrowUp,
311  ArrowDown
312  };
313 
314  // Keep track on which part of the scrollbar the mouse is standing
315  Part m_mouseHoverOverPart = Part::Thumb;
316 
317  // When the mouse went down, did it go down on top of the thumb? If so, where?
318  bool m_mouseDownOnThumb = false;
319  Vector2f m_mouseDownOnThumbPos;
320 
321  unsigned int m_maximum = 10;
322  unsigned int m_value = 0;
323 
324  // Maximum should be above this value before the scrollbar is needed
325  unsigned int m_viewportSize = 1;
326 
327  // Is the scrollbar draw vertically?
328  bool m_verticalScroll = true;
329 
330  // Does the image lie vertically?
331  bool m_verticalImage = true;
332 
333  // How far should the value change when pressing one of the arrows?
334  unsigned int m_scrollAmount = 1;
335 
336  // When no scrollbar is needed, should the scrollbar be drawn or stay hidden?
337  bool m_autoHide = true;
338 
339  // Did the mouse went down on one of the arrows?
340  bool m_mouseDownOnArrow = false;
341 
342  FloatRect m_track;
343  FloatRect m_thumb;
344  FloatRect m_arrowUp;
345  FloatRect m_arrowDown;
346 
347  Sprite m_spriteTrack;
348  Sprite m_spriteTrackHover;
349  Sprite m_spriteThumb;
350  Sprite m_spriteThumbHover;
351  Sprite m_spriteArrowUp;
352  Sprite m_spriteArrowUpHover;
353  Sprite m_spriteArrowDown;
354  Sprite m_spriteArrowDownHover;
355 
356  // Cached renderer properties
357  Color m_thumbColorCached;
358  Color m_thumbColorHoverCached;
359  Color m_trackColorCached;
360  Color m_trackColorHoverCached;
361  Color m_arrowColorCached;
362  Color m_arrowColorHoverCached;
363  Color m_arrowBackgroundColorCached;
364  Color m_arrowBackgroundColorHoverCached;
365 
367  };
368 
369 
374  class TGUI_API ScrollbarChildWidget : public Scrollbar
375  {
376  public:
377 
382  bool isMouseDown() const;
383 
384 
389  bool isMouseDownOnThumb() const;
390 
391 
398  bool isShown() const;
399 
400 
402  };
403 
405 }
406 
408 
409 #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:243
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:374
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
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:58
Definition: ScrollbarRenderer.hpp:36
Widget::Ptr clone() const override
Makes a copy of the widget if you don&#39;t know its exact type.
Definition: Scrollbar.hpp:291
Scrollbar widget.
Definition: Scrollbar.hpp:40
std::shared_ptr< const Scrollbar > ConstPtr
Shared constant widget pointer.
Definition: Scrollbar.hpp:45