TGUI  0.9.1
Scrollbar.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2021 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  enum class Policy
50  {
51  Automatic,
52  Always,
53  Never
54  };
55 
56 
64  Scrollbar(const char* typeName = "Scrollbar", bool initRenderer = true);
65 
66 
74 
75 
85 
86 
92  const ScrollbarRenderer* getSharedRenderer() const;
93 
100  const ScrollbarRenderer* getRenderer() const;
101 
102 
110  void setSize(const Layout2d& size) override;
111  using Widget::setSize;
112 
113 
122  void setMaximum(unsigned int maximum);
123 
124 
133  unsigned int getMaximum() const;
134 
135 
143  void setValue(unsigned int value);
144 
145 
154  unsigned int getValue() const;
155 
156 
169  void setViewportSize(unsigned int viewport);
170 
171 
176  unsigned int getViewportSize() const;
177 
178 
185  void setScrollAmount(unsigned int scrollAmount);
186 
187 
194  unsigned int getScrollAmount() const;
195 
196 
204  void setAutoHide(bool autoHide);
205 
206 
213  bool getAutoHide() const;
214 
215 
222  void setVerticalScroll(bool vertical);
223 
224 
229  bool getVerticalScroll() const;
230 
231 
239 
240 
247  bool isMouseOnWidget(Vector2f pos) const override;
248 
252  void leftMousePressed(Vector2f pos) override;
253 
257  void leftMouseReleased(Vector2f pos) override;
258 
262  void mouseMoved(Vector2f pos) override;
263 
267  bool mouseWheelScrolled(float delta, Vector2f pos) override;
268 
272  void leftMouseButtonNoLongerDown() override;
273 
274 
282  void draw(BackendRenderTargetBase& target, RenderStates states) const override;
283 
284 
286  protected:
287 
288 
290  // Updates the scrollbar after a size change
292  void updateSize();
293 
294 
304  Signal& getSignal(String signalName) override;
305 
306 
313  void rendererChanged(const String& property) override;
314 
315 
319  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
320 
321 
325  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
326 
327 
329  // Updates the position of the thumb based on the current value of the slider
331  void updateThumbPosition();
332 
333 
335  // Makes a copy of the widget
337  Widget::Ptr clone() const override
338  {
339  return std::make_shared<Scrollbar>(*this);
340  }
341 
342 
344  public:
345 
346  SignalUInt onValueChange = {"ValueChanged"};
347 
348 
350  protected:
351 
352  enum class Part
353  {
354  Track,
355  Thumb,
356  ArrowUp,
357  ArrowDown
358  };
359 
360  // Keep track on which part of the scrollbar the mouse is standing
361  Part m_mouseHoverOverPart = Part::Thumb;
362 
363  // When the mouse went down, did it go down on top of the thumb? If so, where?
364  bool m_mouseDownOnThumb = false;
365  Vector2f m_mouseDownOnThumbPos;
366 
367  unsigned int m_maximum = 10;
368  unsigned int m_value = 0;
369 
370  // Maximum should be above this value before the scrollbar is needed
371  unsigned int m_viewportSize = 1;
372 
373  // Is the scrollbar draw vertically?
374  bool m_verticalScroll = true;
375 
376  // Does the image lie vertically?
377  bool m_verticalImage = true;
378 
379  // How far should the value change when pressing one of the arrows?
380  unsigned int m_scrollAmount = 1;
381 
382  // When no scrollbar is needed, should the scrollbar be drawn or stay hidden?
383  bool m_autoHide = true;
384 
385  // Did the mouse went down on one of the arrows?
386  bool m_mouseDownOnArrow = false;
387 
388  bool m_sizeSet = false; // Has setSize been called?
389 
390  FloatRect m_track;
391  FloatRect m_thumb;
392  FloatRect m_arrowUp;
393  FloatRect m_arrowDown;
394 
395  Sprite m_spriteTrack;
396  Sprite m_spriteTrackHover;
397  Sprite m_spriteThumb;
398  Sprite m_spriteThumbHover;
399  Sprite m_spriteArrowUp;
400  Sprite m_spriteArrowUpHover;
401  Sprite m_spriteArrowDown;
402  Sprite m_spriteArrowDownHover;
403 
404  // Cached renderer properties
405  Color m_thumbColorCached;
406  Color m_thumbColorHoverCached;
407  Color m_trackColorCached;
408  Color m_trackColorHoverCached;
409  Color m_arrowColorCached;
410  Color m_arrowColorHoverCached;
411  Color m_arrowBackgroundColorCached;
412  Color m_arrowBackgroundColorHoverCached;
413 
415  };
416 
417 
422  class TGUI_API ScrollbarChildWidget : public Scrollbar
423  {
424  public:
425 
429  bool isMouseDownOnThumb() const;
430 
431 
438  bool isShown() const;
439 
440 
447  void draw(BackendRenderTargetBase& target, RenderStates states) const override;
448 
449 
451  };
452 
454 }
455 
457 
458 #endif // TGUI_SCROLLBAR_HPP
Base class for render targets.
Definition: BackendRenderTarget.hpp:48
Class to store the position or size of a widget.
Definition: Layout.hpp:262
Wrapper around scrollbar to be used inside widgets that need a scrollbar.
Definition: Scrollbar.hpp:423
bool isMouseDownOnThumb() const
Returns whether the left mouse button has been pressed on top of the thumb of the scrollbar.
void draw(BackendRenderTargetBase &target, RenderStates states) const override
Draw the widget to a render target.
bool isShown() const
Returns whether the scrollbar is currently visible.
Definition: ScrollbarRenderer.hpp:37
Scrollbar widget.
Definition: Scrollbar.hpp:41
unsigned int getScrollAmount() const
Returns how much the value changes when scrolling or pressing one of the arrows of the scrollbar.
std::unique_ptr< DataIO::Node > save(SavingRenderersMap &renderers) const override
Saves the widget as a tree node in order to save it to a file.
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
Policy
Defines when the scrollbar shows up.
Definition: Scrollbar.hpp:50
ScrollbarRenderer * getSharedRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
unsigned int getMaximum() const
Returns the maximum value.
void setValue(unsigned int value)
Changes the current value.
bool getVerticalScroll() const
Returns whether the scrollbar lies horizontally or vertically.
static Scrollbar::Ptr create()
Creates a new scrollbar widget.
void setMaximum(unsigned int maximum)
Sets a maximum value.
void setSize(const Layout2d &size) override
Changes the size of the scrollbar.
void setViewportSize(unsigned int viewport)
Changes the viewport size.
float getDefaultWidth()
Returns the default width of the scrollbar.
void draw(BackendRenderTargetBase &target, RenderStates states) const override
Draw the widget to a render target.
ScrollbarRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
unsigned int getViewportSize() const
Returns the viewport size.
bool isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
unsigned int getValue() const
Returns the current value.
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: Scrollbar.hpp:337
void setVerticalScroll(bool vertical)
Changes whether the scrollbar lies horizontally or vertically.
std::shared_ptr< Scrollbar > Ptr
Shared widget pointer.
Definition: Scrollbar.hpp:44
void setScrollAmount(unsigned int scrollAmount)
Changes how much the value changes when scrolling or pressing one of the arrows of the scrollbar.
std::shared_ptr< const Scrollbar > ConstPtr
Shared constant widget pointer.
Definition: Scrollbar.hpp:45
void setAutoHide(bool autoHide)
Changes whether the scrollbar should hide automatically or not.
static Scrollbar::Ptr copy(Scrollbar::ConstPtr scrollbar)
Makes a copy of another scrollbar.
bool getAutoHide() const
Returns whether the scrollbar is hiding automatically or not.
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:58
Wrapper class to store strings.
Definition: String.hpp:74
The parent class for every widget.
Definition: Widget.hpp:70
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:73
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
States used for drawing.
Definition: RenderStates.hpp:39