TGUI  0.8-dev
Scrollbar.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2017 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 {
48  class TGUI_API Scrollbar : public Widget
49  {
50  public:
51 
52  typedef std::shared_ptr<Scrollbar> Ptr;
53  typedef std::shared_ptr<const Scrollbar> ConstPtr;
54 
55 
57  // Default constructor
59  Scrollbar();
60 
61 
68  static Scrollbar::Ptr create();
69 
70 
79  static Scrollbar::Ptr copy(Scrollbar::ConstPtr scrollbar);
80 
81 
89  {
90  return aurora::downcast<ScrollbarRenderer*>(m_renderer.get());
91  }
92 
93 
100  void setSize(const Layout2d& size) override;
102 
103 
113  void setMaximum(unsigned int maximum);
114 
115 
124  unsigned int getMaximum() const;
125 
126 
135  void setValue(unsigned int value);
136 
137 
146  unsigned int getValue() const;
147 
148 
160  void setLowValue(unsigned int lowValue);
161 
162 
169  unsigned int getLowValue() const;
170 
171 
178  void setScrollAmount(unsigned int scrollAmount);
179 
180 
187  unsigned int getScrollAmount() const;
188 
189 
198  void setAutoHide(bool autoHide);
199 
200 
208  bool getAutoHide() const;
209 
210 
214  virtual bool mouseOnWidget(sf::Vector2f pos) const override;
215 
219  virtual void leftMousePressed(sf::Vector2f pos) override;
220 
224  virtual void leftMouseReleased(sf::Vector2f pos) override;
225 
229  virtual void mouseMoved(sf::Vector2f pos) override;
230 
234  virtual void mouseWheelScrolled(float delta, int x, int y) override;
235 
239  virtual void widgetFocused() override;
240 
244  virtual void mouseNoLongerDown() override;
245 
246 
254  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
255 
256 
258  protected:
259 
266  virtual void rendererChanged(const std::string& property) override;
267 
268 
270  // Updates the position of the thumb based on the current value of the slider
272  void updateThumbPosition();
273 
274 
276  // Makes a copy of the widget
278  virtual Widget::Ptr clone() const override
279  {
280  return std::make_shared<Scrollbar>(*this);
281  }
282 
283 
285  protected:
286 
287  enum class Part
288  {
289  Track,
290  Thumb,
291  ArrowUp,
292  ArrowDown
293  };
294 
295  // Keep track on which part of the scrollbar the mouse is standing
296  Part m_mouseHoverOverPart = Part::Thumb;
297 
298  // When the mouse went down, did it go down on top of the thumb? If so, where?
299  bool m_mouseDownOnThumb = false;
300  sf::Vector2f m_mouseDownOnThumbPos;
301 
302  unsigned int m_maximum = 10;
303  unsigned int m_value = 0;
304 
305  // Maximum should be above this value before the scrollbar is needed
306  unsigned int m_lowValue = 6;
307 
308  // Is the scrollbar draw vertically?
309  bool m_verticalScroll = true;
310 
311  // Does the image lie vertically?
312  bool m_verticalImage = true;
313 
314  // How far should the value change when pressing one of the arrows?
315  unsigned int m_scrollAmount = 1;
316 
317  // When no scrollbar is needed, should the scrollbar be drawn or stay hidden?
318  bool m_autoHide = true;
319 
320  // Did the mouse went down on one of the arrows?
321  bool m_mouseDownOnArrow = false;
322 
323  sf::FloatRect m_track;
324  sf::FloatRect m_thumb;
325  sf::FloatRect m_arrowUp;
326  sf::FloatRect m_arrowDown;
327 
328  Sprite m_spriteTrack;
329  Sprite m_spriteTrackHover;
330  Sprite m_spriteThumb;
331  Sprite m_spriteThumbHover;
332  Sprite m_spriteArrowUp;
333  Sprite m_spriteArrowUpHover;
334  Sprite m_spriteArrowDown;
335  Sprite m_spriteArrowDownHover;
336 
337  // Cached renderer properties
338  Color m_thumbColorCached;
339  Color m_thumbColorHoverCached;
340  Color m_trackColorCached;
341  Color m_trackColorHoverCached;
342  Color m_arrowColorCached;
343  Color m_arrowColorHoverCached;
344  Color m_arrowBackgroundColorCached;
345  Color m_arrowBackgroundColorHoverCached;
346 
348  };
349 
350 
355  class TGUI_API ScrollbarChildWidget : public Scrollbar
356  {
357  public:
358 
363  bool isMouseDown() const;
364 
365 
370  bool isMouseDownOnThumb() const;
371 
372 
381  bool isShown() const;
382 
383 
385  };
386 
388 }
389 
391 
392 #endif // TGUI_SCROLLBAR_HPP
Namespace that contains all TGUI functions and classes.
Definition: Animation.hpp:33
Definition: Sprite.hpp:40
Wrapper for colors.
Definition: Color.hpp:46
virtual Widget::Ptr clone() const override
Makes a copy of the widget if you don&#39;t know its exact type.
Definition: Scrollbar.hpp:278
Class to store the position or size of a widget.
Definition: Layout.hpp:263
The parent class for every widget.
Definition: Widget.hpp:72
std::shared_ptr< Scrollbar > Ptr
Shared widget pointer.
Definition: Scrollbar.hpp:52
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:355
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:76
ScrollbarRenderer * getRenderer() const
Returns the renderer, which gives access to functions that determine how the widget is displayed...
Definition: Scrollbar.hpp:88
Definition: ScrollbarRenderer.hpp:36
Scrollbar widget.
Definition: Scrollbar.hpp:48
std::shared_ptr< const Scrollbar > ConstPtr
Shared constant widget pointer.
Definition: Scrollbar.hpp:53