TGUI  1.6.1
Loading...
Searching...
No Matches
Scrollbar.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2024 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#ifndef TGUI_SCROLLBAR_HPP
26#define TGUI_SCROLLBAR_HPP
27
28#include <TGUI/Widget.hpp>
29#include <TGUI/Renderers/ScrollbarRenderer.hpp>
30#include <TGUI/CopiedSharedPtr.hpp>
31
32#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
33 #include <chrono>
34#endif
35
37
38TGUI_MODULE_EXPORT namespace tgui
39{
43 class TGUI_API Scrollbar : public Widget
44 {
45 public:
46
47 using Ptr = std::shared_ptr<Scrollbar>;
48 using ConstPtr = std::shared_ptr<const Scrollbar>;
49
50 static constexpr const char StaticWidgetType[] = "Scrollbar";
51
53 enum class Policy
54 {
55 Automatic,
56 Always,
57 Never
58 };
59
67 Scrollbar(const char* typeName = StaticWidgetType, bool initRenderer = true);
68
74 TGUI_NODISCARD static Scrollbar::Ptr create(Orientation orientation = Orientation::Vertical);
75
83 TGUI_NODISCARD static Scrollbar::Ptr copy(const Scrollbar::ConstPtr& scrollbar);
84
89 TGUI_NODISCARD ScrollbarRenderer* getSharedRenderer() override;
90 TGUI_NODISCARD const ScrollbarRenderer* getSharedRenderer() const override;
91
97 TGUI_NODISCARD ScrollbarRenderer* getRenderer() override;
98
107 void setSize(const Layout2d& size) override;
108 using Widget::setSize;
109
118 void setMaximum(unsigned int maximum);
119
127 TGUI_NODISCARD unsigned int getMaximum() const;
128
136 void setValue(unsigned int value);
137
145 TGUI_NODISCARD unsigned int getValue() const;
146
159 void setViewportSize(unsigned int viewport);
160
165 TGUI_NODISCARD unsigned int getViewportSize() const;
166
174 TGUI_NODISCARD unsigned int getMaxValue() const;
175
181 void setScrollAmount(unsigned int scrollAmount);
182
188 TGUI_NODISCARD unsigned int getScrollAmount() const;
189
190#ifndef TGUI_REMOVE_DEPRECATED_CODE
198 TGUI_DEPRECATED("Use setPolicy instead") void setAutoHide(bool autoHide);
199
206 TGUI_DEPRECATED("Use getPolicy instead") TGUI_NODISCARD bool getAutoHide() const;
207#endif
208
214 void setPolicy(Policy policy);
215
221 TGUI_NODISCARD Scrollbar::Policy getPolicy() const;
222
237 TGUI_NODISCARD bool isShown() const;
238
245 TGUI_DEPRECATED("Use setOrientation instead") void setVerticalScroll(bool vertical);
246
251 TGUI_DEPRECATED("Use getOrientation instead") TGUI_NODISCARD bool getVerticalScroll() const;
252
261 void setOrientation(Orientation orientation);
262
268 TGUI_NODISCARD Orientation getOrientation() const;
269
276 TGUI_NODISCARD float getDefaultWidth() const;
277
284 TGUI_NODISCARD bool canGainFocus() const override;
285
291 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
292
296 bool leftMousePressed(Vector2f pos) override;
297
301 void leftMouseReleased(Vector2f pos) override;
302
306 void mouseMoved(Vector2f pos) override;
307
311 bool scrolled(float delta, Vector2f pos, bool touch) override;
312
316 void leftMouseButtonNoLongerDown() override;
317
324 void draw(BackendRenderTarget& target, RenderStates states) const override;
325
327 protected:
328
330 // Updates the scrollbar after a size change
332 void updateSize();
333
343 TGUI_NODISCARD Signal& getSignal(String signalName) override;
344
350 void rendererChanged(const String& property) override;
351
355 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
356
360 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
361
363 // Updates the position of the thumb based on the current value of the slider
365 void updateThumbPosition();
366
368 // Makes a copy of the widget
370 TGUI_NODISCARD Widget::Ptr clone() const override;
371
373 public:
374
375 SignalUInt onValueChange = {"ValueChanged"};
376
378 protected:
379
380 enum class Part
381 {
382 Track,
383 Thumb,
384 ArrowUp,
385 ArrowDown
386 };
387
388 // Keep track on which part of the scrollbar the mouse is standing
389 Part m_mouseHoverOverPart = Part::Thumb;
390
391 // When the mouse went down, did it go down on top of the thumb? If so, where?
392 bool m_mouseDownOnThumb = false;
393 Vector2f m_mouseDownOnThumbPos;
394
395 unsigned int m_maximum = 10;
396 unsigned int m_value = 0;
397
398 // Maximum should be above this value before the scrollbar is needed
399 unsigned int m_viewportSize = 1;
400
401 Orientation m_orientation = Orientation::Vertical; // Is the scrollbar drawn horizontally or vertically?
402 Orientation m_imageOrientation = Orientation::Vertical; // Does the loaded image lie horizontally or vertically?
403 bool m_orientationLocked = false; // TGUI_NEXT: Remove property and make locked the default
404
405 // How far should the value change when pressing one of the arrows?
406 unsigned int m_scrollAmount = 1;
407
408 // Defines when the scrollbar should be visible or hidden
409 Scrollbar::Policy m_policy = Scrollbar::Policy::Automatic;
410
411 // Did the mouse went down on one of the arrows?
412 bool m_mouseDownOnArrow = false;
413
414 bool m_sizeSet = false; // Has setSize been called?
415
416 std::chrono::steady_clock::time_point m_lastSuccessfulScrollTime; // Timestamp of the last mouse wheel scroll event
417 Vector2f m_lastSuccessfulScrollPos; // Mouse position at the last mouse wheel scroll event
418
419 FloatRect m_track;
420 FloatRect m_thumb;
421 FloatRect m_arrowUp;
422 FloatRect m_arrowDown;
423
424 Sprite m_spriteTrack;
425 Sprite m_spriteTrackHover;
426 Sprite m_spriteThumb;
427 Sprite m_spriteThumbHover;
428 Sprite m_spriteArrowUp;
429 Sprite m_spriteArrowUpHover;
430 Sprite m_spriteArrowDown;
431 Sprite m_spriteArrowDownHover;
432
433 // Cached renderer properties
434 Color m_thumbColorCached;
435 Color m_thumbColorHoverCached;
436 Color m_trackColorCached;
437 Color m_trackColorHoverCached;
438 Color m_arrowColorCached;
439 Color m_arrowColorHoverCached;
440 Color m_arrowBackgroundColorCached;
441 Color m_arrowBackgroundColorHoverCached;
442
444 };
445
450 class TGUI_API ScrollbarChildWidget : public Scrollbar
451 {
452 public:
453
458 ScrollbarChildWidget(Orientation orientation = Orientation::Vertical); // TGUI_NEXT: No more default option
459
463 TGUI_NODISCARD bool isMouseDownOnThumb() const;
464
471 void draw(BackendRenderTarget& target, RenderStates states) const override;
472 };
473
478 class TGUI_API ScrollbarAccessor
479 {
480 public:
481
490 std::function<void()> valueChangedCallback,
491 std::function<void()> policyChangedCallback,
492 std::function<void()> scrollAmountChangedCallback);
493
499 void setValue(unsigned int value);
500
506 TGUI_NODISCARD unsigned int getValue() const;
507
513 void setScrollAmount(unsigned int scrollAmount);
514
519 TGUI_NODISCARD unsigned int getScrollAmount() const;
520
526
531 TGUI_NODISCARD Scrollbar::Policy getPolicy() const;
532
538 TGUI_NODISCARD unsigned int getMaximum() const;
539
545 TGUI_NODISCARD unsigned int getViewportSize() const;
546
552 TGUI_NODISCARD unsigned int getMaxValue() const;
553
559 TGUI_NODISCARD bool isShown() const;
560
570 TGUI_NODISCARD float getWidth() const;
571
572 private:
573 ScrollbarChildWidget* m_scrollbar;
574 std::function<void()> m_valueChangedCallback;
575 std::function<void()> m_policyChangedCallback;
576 std::function<void()> m_scrollAmountChangedCallback;
577 };
578
586 {
587 public:
588
593
598
603
607 virtual ~ScrollbarChildInterface() = default;
608
613
618
623 TGUI_NODISCARD ScrollbarAccessor* getScrollbar();
624
629 TGUI_NODISCARD const ScrollbarAccessor* getScrollbar() const;
630
632 protected:
633
637 virtual void scrollbarValueChanged();
638
642 virtual void scrollbarPolicyChanged();
643
647 virtual void scrollbarScrollAmountChanged();
648
652 void saveScrollbarPolicy(std::unique_ptr<DataIO::Node>& node) const;
653
657 void loadScrollbarPolicy(const std::unique_ptr<DataIO::Node>& node);
658
660 protected:
661
663 ScrollbarAccessor m_scrollbarAccessor;
664 };
665
673 {
674 public:
675
680
685
690
694 virtual ~DualScrollbarChildInterface() = default;
695
700
705
710 TGUI_NODISCARD ScrollbarAccessor* getVerticalScrollbar();
711
716 TGUI_NODISCARD const ScrollbarAccessor* getVerticalScrollbar() const;
717
722 TGUI_NODISCARD ScrollbarAccessor* getHorizontalScrollbar();
723
728 TGUI_NODISCARD const ScrollbarAccessor* getHorizontalScrollbar() const;
729
731 protected:
732
739 virtual void scrollbarValueChanged(Orientation orientation);
740
747 virtual void scrollbarPolicyChanged(Orientation orientation);
748
755 virtual void scrollbarScrollAmountChanged(Orientation orientation);
756
760 void saveScrollbarPolicies(std::unique_ptr<DataIO::Node>& node) const;
761
765 void loadScrollbarPolicies(const std::unique_ptr<DataIO::Node>& node);
766
768 protected:
769
770 CopiedSharedPtr<ScrollbarChildWidget> m_verticalScrollbar;
771 CopiedSharedPtr<ScrollbarChildWidget> m_horizontalScrollbar;
772 ScrollbarAccessor m_verticalScrollbarAccessor;
773 ScrollbarAccessor m_horizontalScrollbarAccessor;
774 };
775
777
778}
779
781
782#endif // TGUI_SCROLLBAR_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:46
Definition CopiedSharedPtr.hpp:42
Base class for widgets with both a vertical and horizontal scrollbar.
Definition Scrollbar.hpp:673
DualScrollbarChildInterface(const DualScrollbarChildInterface &)
Copy constructor.
DualScrollbarChildInterface()
Default constructor.
DualScrollbarChildInterface(DualScrollbarChildInterface &&) noexcept
Move constructor.
Class to store the position or size of a widget.
Definition Layout.hpp:323
Class returned by widgets that have a scrollbar to let the user access scrollbar properties.
Definition Scrollbar.hpp:479
void setScrollAmount(unsigned int scrollAmount)
Changes how much the value changes when scrolling or pressing one of the arrows of the scrollbar.
void setPolicy(Scrollbar::Policy policy)
Changes when the scrollbar should be displayed.
unsigned int getMaxValue() const
Returns the current maximum value of the scrollbar.
float getWidth() const
Returns the width of the scrollbar.
unsigned int getMaximum() const
Returns the current maximum of the scrollbar.
unsigned int getViewportSize() const
Returns the current viewport size of the scrollbar.
void setValue(unsigned int value)
Changes the current value of the scrollbar.
Scrollbar::Policy getPolicy() const
Returns when the scrollbar should be displayed.
bool isShown() const
Returns whether the scrollbar is currently visible.
unsigned int getScrollAmount() const
Returns how much the value changes when scrolling or pressing one of the arrows of the scrollbar.
ScrollbarAccessor(ScrollbarChildWidget &scrollbar, std::function< void()> valueChangedCallback, std::function< void()> policyChangedCallback, std::function< void()> scrollAmountChangedCallback)
Constructor.
unsigned int getValue() const
Returns the current value of the scrollbar.
Base class for widgets with a single scrollbar.
Definition Scrollbar.hpp:586
ScrollbarChildInterface(ScrollbarChildInterface &&) noexcept
Move constructor.
ScrollbarChildInterface()
Default constructor.
ScrollbarChildInterface(const ScrollbarChildInterface &)
Copy constructor.
Wrapper around scrollbar to be used inside widgets that need a scrollbar.
Definition Scrollbar.hpp:451
ScrollbarChildWidget(Orientation orientation=Orientation::Vertical)
Default constructor.
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
bool isMouseDownOnThumb() const
Returns whether the left mouse button has been pressed on top of the thumb of the scrollbar.
Definition ScrollbarRenderer.hpp:35
Scrollbar widget.
Definition Scrollbar.hpp:44
void setPolicy(Policy policy)
Changes when the scrollbar should be displayed.
std::shared_ptr< Scrollbar > Ptr
Shared widget pointer.
Definition Scrollbar.hpp:47
unsigned int getScrollAmount() const
Returns how much the value changes when scrolling or pressing one of the arrows of the scrollbar.
Policy
Defines when the scrollbar shows up.
Definition Scrollbar.hpp:54
static Scrollbar::Ptr create(Orientation orientation=Orientation::Vertical)
Creates a new scrollbar widget.
Scrollbar::Policy getPolicy() const
Returns when the scrollbar should be displayed.
unsigned int getMaximum() const
Returns the maximum value.
void setValue(unsigned int value)
Changes the current value.
unsigned int getMaxValue() const
Returns the maximum value that can be set with the setValue function.
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.
static Scrollbar::Ptr copy(const Scrollbar::ConstPtr &scrollbar)
Makes a copy of another scrollbar.
unsigned int getViewportSize() const
Returns the viewport size.
unsigned int getValue() const
Returns the current value.
ScrollbarRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
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:48
bool isShown() const
Returns whether the scrollbar is currently visible.
ScrollbarRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:61
Wrapper class to store strings.
Definition String.hpp:96
The parent class for every widget.
Definition Widget.hpp:83
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38
Orientation
Orientation of the object.
Definition Layout.hpp:52
States used for drawing.
Definition RenderStates.hpp:38