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 {
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 
109  void setMaximum(unsigned int maximum);
110 
111 
120  unsigned int getMaximum() const;
121 
122 
131  void setValue(unsigned int value);
132 
133 
142  unsigned int getValue() const;
143 
144 
156  void setLowValue(unsigned int lowValue);
157 
158 
165  unsigned int getLowValue() const;
166 
167 
174  void setScrollAmount(unsigned int scrollAmount);
175 
176 
183  unsigned int getScrollAmount() const;
184 
185 
194  void setAutoHide(bool autoHide);
195 
196 
204  bool getAutoHide() const;
205 
206 
213  bool mouseOnWidget(sf::Vector2f pos) const override;
214 
218  void leftMousePressed(sf::Vector2f pos) override;
219 
223  void leftMouseReleased(sf::Vector2f pos) override;
224 
228  void mouseMoved(sf::Vector2f pos) override;
229 
233  void mouseWheelScrolled(float delta, sf::Vector2f pos) override;
234 
238  void widgetFocused() override;
239 
243  void mouseNoLongerDown() override;
244 
245 
253  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
254 
255 
257  protected:
258 
268  Signal& getSignal(std::string signalName) override;
269 
270 
277  void rendererChanged(const std::string& property) override;
278 
279 
281  // Updates the position of the thumb based on the current value of the slider
283  void updateThumbPosition();
284 
285 
287  // Makes a copy of the widget
289  Widget::Ptr clone() const override
290  {
291  return std::make_shared<Scrollbar>(*this);
292  }
293 
294 
296  public:
297 
298  SignalUInt onValueChange = {"ValueChanged"};
299 
300 
302  protected:
303 
304  enum class Part
305  {
306  Track,
307  Thumb,
308  ArrowUp,
309  ArrowDown
310  };
311 
312  // Keep track on which part of the scrollbar the mouse is standing
313  Part m_mouseHoverOverPart = Part::Thumb;
314 
315  // When the mouse went down, did it go down on top of the thumb? If so, where?
316  bool m_mouseDownOnThumb = false;
317  sf::Vector2f m_mouseDownOnThumbPos;
318 
319  unsigned int m_maximum = 10;
320  unsigned int m_value = 0;
321 
322  // Maximum should be above this value before the scrollbar is needed
323  unsigned int m_lowValue = 6;
324 
325  // Is the scrollbar draw vertically?
326  bool m_verticalScroll = true;
327 
328  // Does the image lie vertically?
329  bool m_verticalImage = true;
330 
331  // How far should the value change when pressing one of the arrows?
332  unsigned int m_scrollAmount = 1;
333 
334  // When no scrollbar is needed, should the scrollbar be drawn or stay hidden?
335  bool m_autoHide = true;
336 
337  // Did the mouse went down on one of the arrows?
338  bool m_mouseDownOnArrow = false;
339 
340  sf::FloatRect m_track;
341  sf::FloatRect m_thumb;
342  sf::FloatRect m_arrowUp;
343  sf::FloatRect m_arrowDown;
344 
345  Sprite m_spriteTrack;
346  Sprite m_spriteTrackHover;
347  Sprite m_spriteThumb;
348  Sprite m_spriteThumbHover;
349  Sprite m_spriteArrowUp;
350  Sprite m_spriteArrowUpHover;
351  Sprite m_spriteArrowDown;
352  Sprite m_spriteArrowDownHover;
353 
354  // Cached renderer properties
355  Color m_thumbColorCached;
356  Color m_thumbColorHoverCached;
357  Color m_trackColorCached;
358  Color m_trackColorHoverCached;
359  Color m_arrowColorCached;
360  Color m_arrowColorHoverCached;
361  Color m_arrowBackgroundColorCached;
362  Color m_arrowBackgroundColorHoverCached;
363 
365  };
366 
367 
372  class TGUI_API ScrollbarChildWidget : public Scrollbar
373  {
374  public:
375 
380  bool isMouseDown() const;
381 
382 
387  bool isMouseDownOnThumb() const;
388 
389 
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
Definition: Sprite.hpp:40
Wrapper for colors.
Definition: Color.hpp:46
Class to store the position or size of a widget.
Definition: Layout.hpp:243
The parent class for every widget.
Definition: Widget.hpp:62
std::shared_ptr< Scrollbar > Ptr
Shared widget pointer.
Definition: Scrollbar.hpp:44
Wrapper around scrollbar to be used inside widgets that need a scrollbar.
Definition: Scrollbar.hpp:372
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:66
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&#39;t know its exact type.
Definition: Scrollbar.hpp:289
Scrollbar widget.
Definition: Scrollbar.hpp:40
std::shared_ptr< const Scrollbar > ConstPtr
Shared constant widget pointer.
Definition: Scrollbar.hpp:45