TGUI  0.8.1
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  float getDefaultWidth();
209 
210 
217  bool mouseOnWidget(Vector2f pos) const override;
218 
222  void leftMousePressed(Vector2f pos) override;
223 
227  void leftMouseReleased(Vector2f pos) override;
228 
232  void mouseMoved(Vector2f pos) override;
233 
237  bool mouseWheelScrolled(float delta, Vector2f pos) override;
238 
242  void mouseNoLongerDown() override;
243 
244 
252  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
253 
254 
256  protected:
257 
258 
260  // Updates the scrollbar after a size change
262  void updateSize();
263 
264 
274  Signal& getSignal(std::string signalName) override;
275 
276 
283  void rendererChanged(const std::string& property) override;
284 
285 
289  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
290 
291 
295  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
296 
297 
299  // Updates the position of the thumb based on the current value of the slider
301  void updateThumbPosition();
302 
303 
305  // Makes a copy of the widget
307  Widget::Ptr clone() const override
308  {
309  return std::make_shared<Scrollbar>(*this);
310  }
311 
312 
314  public:
315 
316  SignalUInt onValueChange = {"ValueChanged"};
317 
318 
320  protected:
321 
322  enum class Part
323  {
324  Track,
325  Thumb,
326  ArrowUp,
327  ArrowDown
328  };
329 
330  // Keep track on which part of the scrollbar the mouse is standing
331  Part m_mouseHoverOverPart = Part::Thumb;
332 
333  // When the mouse went down, did it go down on top of the thumb? If so, where?
334  bool m_mouseDownOnThumb = false;
335  Vector2f m_mouseDownOnThumbPos;
336 
337  unsigned int m_maximum = 10;
338  unsigned int m_value = 0;
339 
340  // Maximum should be above this value before the scrollbar is needed
341  unsigned int m_viewportSize = 1;
342 
343  // Is the scrollbar draw vertically?
344  bool m_verticalScroll = true;
345 
346  // Does the image lie vertically?
347  bool m_verticalImage = true;
348 
349  // How far should the value change when pressing one of the arrows?
350  unsigned int m_scrollAmount = 1;
351 
352  // When no scrollbar is needed, should the scrollbar be drawn or stay hidden?
353  bool m_autoHide = true;
354 
355  // Did the mouse went down on one of the arrows?
356  bool m_mouseDownOnArrow = false;
357 
358  bool m_sizeSet = false; // Has setSize been called?
359 
360  FloatRect m_track;
361  FloatRect m_thumb;
362  FloatRect m_arrowUp;
363  FloatRect m_arrowDown;
364 
365  Sprite m_spriteTrack;
366  Sprite m_spriteTrackHover;
367  Sprite m_spriteThumb;
368  Sprite m_spriteThumbHover;
369  Sprite m_spriteArrowUp;
370  Sprite m_spriteArrowUpHover;
371  Sprite m_spriteArrowDown;
372  Sprite m_spriteArrowDownHover;
373 
374  // Cached renderer properties
375  Color m_thumbColorCached;
376  Color m_thumbColorHoverCached;
377  Color m_trackColorCached;
378  Color m_trackColorHoverCached;
379  Color m_arrowColorCached;
380  Color m_arrowColorHoverCached;
381  Color m_arrowBackgroundColorCached;
382  Color m_arrowBackgroundColorHoverCached;
383 
385  };
386 
387 
392  class TGUI_API ScrollbarChildWidget : public Scrollbar
393  {
394  public:
395 
400  bool isMouseDown() const;
401 
402 
407  bool isMouseDownOnThumb() const;
408 
409 
416  bool isShown() const;
417 
418 
420  };
421 
423 }
424 
426 
427 #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:245
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:392
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: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:307
Scrollbar widget.
Definition: Scrollbar.hpp:40
std::shared_ptr< const Scrollbar > ConstPtr
Shared constant widget pointer.
Definition: Scrollbar.hpp:45