--- --- TGUI: include/TGUI/Widgets/Scrollbar.hpp Source File
TGUI  1.x-dev
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
106 void setSize(const Layout2d& size) override;
107 using Widget::setSize;
108
117 void setMaximum(unsigned int maximum);
118
126 TGUI_NODISCARD unsigned int getMaximum() const;
127
135 void setValue(unsigned int value);
136
144 TGUI_NODISCARD unsigned int getValue() const;
145
158 void setViewportSize(unsigned int viewport);
159
164 TGUI_NODISCARD unsigned int getViewportSize() const;
165
173 TGUI_NODISCARD unsigned int getMaxValue() const;
174
180 void setScrollAmount(unsigned int scrollAmount);
181
187 TGUI_NODISCARD unsigned int getScrollAmount() const;
188
189#ifndef TGUI_REMOVE_DEPRECATED_CODE
197 TGUI_DEPRECATED("Use setPolicy instead") void setAutoHide(bool autoHide);
198
205 TGUI_DEPRECATED("Use getPolicy instead") TGUI_NODISCARD bool getAutoHide() const;
206#endif
207
213 void setPolicy(Policy policy);
214
220 TGUI_NODISCARD Scrollbar::Policy getPolicy() const;
221
236 TGUI_NODISCARD bool isShown() const;
237
244 TGUI_DEPRECATED("Use setOrientation instead") void setVerticalScroll(bool vertical);
245
250 TGUI_DEPRECATED("Use getOrientation instead") TGUI_NODISCARD bool getVerticalScroll() const;
251
260 void setOrientation(Orientation orientation);
261
267 TGUI_NODISCARD Orientation getOrientation() const;
268
275 TGUI_NODISCARD float getDefaultWidth() const;
276
283 TGUI_NODISCARD bool canGainFocus() const override;
284
290 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
291
295 bool leftMousePressed(Vector2f pos) override;
296
300 void leftMouseReleased(Vector2f pos) override;
301
305 void mouseMoved(Vector2f pos) override;
306
310 bool scrolled(float delta, Vector2f pos, bool touch) override;
311
315 void leftMouseButtonNoLongerDown() override;
316
323 void draw(BackendRenderTarget& target, RenderStates states) const override;
324
326 protected:
327
329 // Updates the scrollbar after a size change
331 void updateSize();
332
342 TGUI_NODISCARD Signal& getSignal(String signalName) override;
343
349 void rendererChanged(const String& property) override;
350
354 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
355
359 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
360
362 // Updates the position of the thumb based on the current value of the slider
364 void updateThumbPosition();
365
367 // Makes a copy of the widget
369 TGUI_NODISCARD Widget::Ptr clone() const override;
370
372 public:
373
374 SignalUInt onValueChange = {"ValueChanged"};
375
377 protected:
378
379 enum class Part
380 {
381 Track,
382 Thumb,
383 ArrowUp,
384 ArrowDown
385 };
386
387 // Keep track on which part of the scrollbar the mouse is standing
388 Part m_mouseHoverOverPart = Part::Thumb;
389
390 // When the mouse went down, did it go down on top of the thumb? If so, where?
391 bool m_mouseDownOnThumb = false;
392 Vector2f m_mouseDownOnThumbPos;
393
394 unsigned int m_maximum = 10;
395 unsigned int m_value = 0;
396
397 // Maximum should be above this value before the scrollbar is needed
398 unsigned int m_viewportSize = 1;
399
400 Orientation m_orientation = Orientation::Vertical; // Is the scrollbar drawn horizontally or vertically?
401 Orientation m_imageOrientation = Orientation::Vertical; // Does the loaded image lie horizontally or vertically?
402 bool m_orientationLocked = false; // TGUI_NEXT: Remove property and make locked the default
403
404 // How far should the value change when pressing one of the arrows?
405 unsigned int m_scrollAmount = 1;
406
407 // Defines when the scrollbar should be visible or hidden
408 Scrollbar::Policy m_policy = Scrollbar::Policy::Automatic;
409
410 // Did the mouse went down on one of the arrows?
411 bool m_mouseDownOnArrow = false;
412
413 bool m_sizeSet = false; // Has setSize been called?
414
415 std::chrono::steady_clock::time_point m_lastSuccessfulScrollTime; // Timestamp of the last mouse wheel scroll event
416 Vector2f m_lastSuccessfulScrollPos; // Mouse position at the last mouse wheel scroll event
417
418 FloatRect m_track;
419 FloatRect m_thumb;
420 FloatRect m_arrowUp;
421 FloatRect m_arrowDown;
422
423 Sprite m_spriteTrack;
424 Sprite m_spriteTrackHover;
425 Sprite m_spriteThumb;
426 Sprite m_spriteThumbHover;
427 Sprite m_spriteArrowUp;
428 Sprite m_spriteArrowUpHover;
429 Sprite m_spriteArrowDown;
430 Sprite m_spriteArrowDownHover;
431
432 // Cached renderer properties
433 Color m_thumbColorCached;
434 Color m_thumbColorHoverCached;
435 Color m_trackColorCached;
436 Color m_trackColorHoverCached;
437 Color m_arrowColorCached;
438 Color m_arrowColorHoverCached;
439 Color m_arrowBackgroundColorCached;
440 Color m_arrowBackgroundColorHoverCached;
441
443 };
444
449 class TGUI_API ScrollbarChildWidget : public Scrollbar
450 {
451 public:
452
457 ScrollbarChildWidget(Orientation orientation = Orientation::Vertical); // TGUI_NEXT: No more default option
458
462 TGUI_NODISCARD bool isMouseDownOnThumb() const;
463
470 void draw(BackendRenderTarget& target, RenderStates states) const override;
471 };
472
477 class TGUI_API ScrollbarAccessor
478 {
479 public:
480
489 std::function<void()> valueChangedCallback,
490 std::function<void()> policyChangedCallback,
491 std::function<void()> scrollAmountChangedCallback);
492
498 void setValue(unsigned int value);
499
505 TGUI_NODISCARD unsigned int getValue() const;
506
512 void setScrollAmount(unsigned int scrollAmount);
513
518 TGUI_NODISCARD unsigned int getScrollAmount() const;
519
525
530 TGUI_NODISCARD Scrollbar::Policy getPolicy() const;
531
537 TGUI_NODISCARD unsigned int getMaximum() const;
538
544 TGUI_NODISCARD unsigned int getViewportSize() const;
545
551 TGUI_NODISCARD unsigned int getMaxValue() const;
552
558 TGUI_NODISCARD bool isShown() const;
559
569 TGUI_NODISCARD float getWidth() const;
570
571 private:
572 ScrollbarChildWidget* m_scrollbar;
573 std::function<void()> m_valueChangedCallback;
574 std::function<void()> m_policyChangedCallback;
575 std::function<void()> m_scrollAmountChangedCallback;
576 };
577
585 {
586 public:
587
592
597
602
606 virtual ~ScrollbarChildInterface() = default;
607
612
617
622 ScrollbarAccessor* getScrollbar();
623
628 const ScrollbarAccessor* getScrollbar() const;
629
631 protected:
632
636 virtual void scrollbarValueChanged();
637
641 virtual void scrollbarPolicyChanged();
642
646 virtual void scrollbarScrollAmountChanged();
647
651 void saveScrollbarPolicy(std::unique_ptr<DataIO::Node>& node) const;
652
656 void loadScrollbarPolicy(const std::unique_ptr<DataIO::Node>& node);
657
659 protected:
660
662 ScrollbarAccessor m_scrollbarAccessor;
663 };
664
672 {
673 public:
674
679
684
689
693 virtual ~DualScrollbarChildInterface() = default;
694
699
704
709 ScrollbarAccessor* getVerticalScrollbar();
710
715 const ScrollbarAccessor* getVerticalScrollbar() const;
716
721 ScrollbarAccessor* getHorizontalScrollbar();
722
727 const ScrollbarAccessor* getHorizontalScrollbar() const;
728
730 protected:
731
738 virtual void scrollbarValueChanged(Orientation orientation);
739
746 virtual void scrollbarPolicyChanged(Orientation orientation);
747
754 virtual void scrollbarScrollAmountChanged(Orientation orientation);
755
759 void saveScrollbarPolicies(std::unique_ptr<DataIO::Node>& node) const;
760
764 void loadScrollbarPolicies(const std::unique_ptr<DataIO::Node>& node);
765
767 protected:
768
769 CopiedSharedPtr<ScrollbarChildWidget> m_verticalScrollbar;
770 CopiedSharedPtr<ScrollbarChildWidget> m_horizontalScrollbar;
771 ScrollbarAccessor m_verticalScrollbarAccessor;
772 ScrollbarAccessor m_horizontalScrollbarAccessor;
773 };
774
776
777}
778
780
781#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:672
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:313
Class returned by widgets that have a scrollbar to let the user access scrollbar properties.
Definition Scrollbar.hpp:478
void setScrollAmount(unsigned int scrollAmount)
Changes how much the value changes when scrolling or pressing one of the arrows of the scrollbar.
TGUI_NODISCARD unsigned int getMaxValue() const
Returns the current maximum value of the scrollbar.
void setPolicy(Scrollbar::Policy policy)
Changes when the scrollbar should be displayed.
TGUI_NODISCARD unsigned int getViewportSize() const
Returns the current viewport size of the scrollbar.
TGUI_NODISCARD unsigned int getMaximum() const
Returns the current maximum of the scrollbar.
void setValue(unsigned int value)
Changes the current value of the scrollbar.
TGUI_NODISCARD bool isShown() const
Returns whether the scrollbar is currently visible.
TGUI_NODISCARD unsigned int getScrollAmount() const
Returns how much the value changes when scrolling or pressing one of the arrows of the scrollbar.
TGUI_NODISCARD unsigned int getValue() const
Returns the current value of the scrollbar.
ScrollbarAccessor(ScrollbarChildWidget &scrollbar, std::function< void()> valueChangedCallback, std::function< void()> policyChangedCallback, std::function< void()> scrollAmountChangedCallback)
Constructor.
TGUI_NODISCARD Scrollbar::Policy getPolicy() const
Returns when the scrollbar should be displayed.
TGUI_NODISCARD float getWidth() const
Returns the width of the scrollbar.
Base class for widgets with a single scrollbar.
Definition Scrollbar.hpp:585
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:450
ScrollbarChildWidget(Orientation orientation=Orientation::Vertical)
Default constructor.
TGUI_NODISCARD bool isMouseDownOnThumb() const
Returns whether the left mouse button has been pressed on top of the thumb of the scrollbar.
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
Scrollbar widget.
Definition Scrollbar.hpp:44
void setPolicy(Policy policy)
Changes when the scrollbar should be displayed.
TGUI_NODISCARD unsigned int getMaximum() const
Returns the maximum value.
std::shared_ptr< Scrollbar > Ptr
Shared widget pointer.
Definition Scrollbar.hpp:47
Policy
Defines when the scrollbar shows up.
Definition Scrollbar.hpp:54
TGUI_NODISCARD bool isShown() const
Returns whether the scrollbar is currently visible.
TGUI_NODISCARD unsigned int getViewportSize() const
Returns the viewport size.
void setValue(unsigned int value)
Changes the current value.
TGUI_NODISCARD ScrollbarRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
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 TGUI_NODISCARD Scrollbar::Ptr copy(const Scrollbar::ConstPtr &scrollbar)
Makes a copy of another scrollbar.
TGUI_NODISCARD unsigned int getMaxValue() const
Returns the maximum value that can be set with the setValue function.
TGUI_NODISCARD Scrollbar::Policy getPolicy() const
Returns when the scrollbar should be displayed.
TGUI_NODISCARD ScrollbarRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
TGUI_NODISCARD unsigned int getValue() const
Returns the current value.
TGUI_NODISCARD unsigned int getScrollAmount() const
Returns how much the value changes when scrolling or pressing one of the arrows of the scrollbar.
void setScrollAmount(unsigned int scrollAmount)
Changes how much the value changes when scrolling or pressing one of the arrows of the scrollbar.
static TGUI_NODISCARD Scrollbar::Ptr create(Orientation orientation=Orientation::Vertical)
Creates a new scrollbar widget.
std::shared_ptr< const Scrollbar > ConstPtr
Shared constant widget pointer.
Definition Scrollbar.hpp:48
Definition ScrollbarRenderer.hpp:35
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:51
States used for drawing.
Definition RenderStates.hpp:38