TGUI  0.8-dev
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 
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(Vector2f pos) const override;
214 
218  void leftMousePressed(Vector2f pos) override;
219 
223  void leftMouseReleased(Vector2f pos) override;
224 
228  void mouseMoved(Vector2f pos) override;
229 
233  void mouseWheelScrolled(float delta, Vector2f pos) override;
234 
238  void mouseNoLongerDown() override;
239 
240 
248  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
249 
250 
252  protected:
253 
263  Signal& getSignal(std::string signalName) override;
264 
265 
272  void rendererChanged(const std::string& property) override;
273 
274 
278  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
279 
280 
284  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
285 
286 
288  // Updates the position of the thumb based on the current value of the slider
290  void updateThumbPosition();
291 
292 
294  // Makes a copy of the widget
296  Widget::Ptr clone() const override
297  {
298  return std::make_shared<Scrollbar>(*this);
299  }
300 
301 
303  public:
304 
305  SignalUInt onValueChange = {"ValueChanged"};
306 
307 
309  protected:
310 
311  enum class Part
312  {
313  Track,
314  Thumb,
315  ArrowUp,
316  ArrowDown
317  };
318 
319  // Keep track on which part of the scrollbar the mouse is standing
320  Part m_mouseHoverOverPart = Part::Thumb;
321 
322  // When the mouse went down, did it go down on top of the thumb? If so, where?
323  bool m_mouseDownOnThumb = false;
324  Vector2f m_mouseDownOnThumbPos;
325 
326  unsigned int m_maximum = 10;
327  unsigned int m_value = 0;
328 
329  // Maximum should be above this value before the scrollbar is needed
330  unsigned int m_lowValue = 6;
331 
332  // Is the scrollbar draw vertically?
333  bool m_verticalScroll = true;
334 
335  // Does the image lie vertically?
336  bool m_verticalImage = true;
337 
338  // How far should the value change when pressing one of the arrows?
339  unsigned int m_scrollAmount = 1;
340 
341  // When no scrollbar is needed, should the scrollbar be drawn or stay hidden?
342  bool m_autoHide = true;
343 
344  // Did the mouse went down on one of the arrows?
345  bool m_mouseDownOnArrow = false;
346 
347  FloatRect m_track;
348  FloatRect m_thumb;
349  FloatRect m_arrowUp;
350  FloatRect m_arrowDown;
351 
352  Sprite m_spriteTrack;
353  Sprite m_spriteTrackHover;
354  Sprite m_spriteThumb;
355  Sprite m_spriteThumbHover;
356  Sprite m_spriteArrowUp;
357  Sprite m_spriteArrowUpHover;
358  Sprite m_spriteArrowDown;
359  Sprite m_spriteArrowDownHover;
360 
361  // Cached renderer properties
362  Color m_thumbColorCached;
363  Color m_thumbColorHoverCached;
364  Color m_trackColorCached;
365  Color m_trackColorHoverCached;
366  Color m_arrowColorCached;
367  Color m_arrowColorHoverCached;
368  Color m_arrowBackgroundColorCached;
369  Color m_arrowBackgroundColorHoverCached;
370 
372  };
373 
374 
379  class TGUI_API ScrollbarChildWidget : public Scrollbar
380  {
381  public:
382 
387  bool isMouseDown() const;
388 
389 
394  bool isMouseDownOnThumb() const;
395 
396 
405  bool isShown() const;
406 
407 
409  };
410 
412 }
413 
415 
416 #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:379
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:296
Scrollbar widget.
Definition: Scrollbar.hpp:40
std::shared_ptr< const Scrollbar > ConstPtr
Shared constant widget pointer.
Definition: Scrollbar.hpp:45