--- --- TGUI: include/TGUI/Widgets/ScrollablePanel.hpp Source File
TGUI  1.x-dev
Loading...
Searching...
No Matches
ScrollablePanel.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_SCROLLABLE_PANEL_HPP
26#define TGUI_SCROLLABLE_PANEL_HPP
27
28#include <TGUI/CopiedSharedPtr.hpp>
29#include <TGUI/Widgets/Panel.hpp>
30#include <TGUI/Widgets/Scrollbar.hpp>
31#include <TGUI/Renderers/ScrollablePanelRenderer.hpp>
32
33#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
34 #include <chrono>
35#endif
36
38
39TGUI_MODULE_EXPORT namespace tgui
40{
44 class TGUI_API ScrollablePanel : public Panel
45 {
46 public:
47
48 using Ptr = std::shared_ptr<ScrollablePanel>;
49 using ConstPtr = std::shared_ptr<const ScrollablePanel>;
50
51 static constexpr const char StaticWidgetType[] = "ScrollablePanel";
52
60 ScrollablePanel(const char* typeName = StaticWidgetType, bool initRenderer = true);
61
66
71
75 ScrollablePanel& operator= (const ScrollablePanel& other);
76
80 ScrollablePanel& operator= (ScrollablePanel&& other) noexcept;
81
92 TGUI_NODISCARD static ScrollablePanel::Ptr create(const Layout2d& size = {"100%", "100%"}, Vector2f contentSize = {0, 0});
93
101 TGUI_NODISCARD static ScrollablePanel::Ptr copy(const ScrollablePanel::ConstPtr& panel);
102
107 TGUI_NODISCARD ScrollablePanelRenderer* getSharedRenderer() override;
108 TGUI_NODISCARD const ScrollablePanelRenderer* getSharedRenderer() const override;
109
115 TGUI_NODISCARD ScrollablePanelRenderer* getRenderer() override;
116
122 void setSize(const Layout2d& size) override;
123 using Widget::setSize;
124
129 TGUI_NODISCARD Vector2f getInnerSize() const override;
130
135 TGUI_NODISCARD Vector2f getAbsolutePosition(Vector2f offset) const override;
136
148 void add(const Widget::Ptr& widget, const String& widgetName = "") override;
149
157 bool remove(const Widget::Ptr& widget) override;
158
162 void removeAllWidgets() override;
163
174
182 TGUI_NODISCARD Vector2f getContentSize() const;
183
189 TGUI_NODISCARD Vector2f getContentOffset() const;
190
195 TGUI_NODISCARD float getScrollbarWidth() const;
196
202
208
214
220
226 void setVerticalScrollAmount(unsigned int scrollAmount);
227
233 TGUI_NODISCARD unsigned int getVerticalScrollAmount() const;
234
240 void setHorizontalScrollAmount(unsigned int scrollAmount);
241
247 TGUI_NODISCARD unsigned int getHorizontalScrollAmount() const;
248
254 void setVerticalScrollbarValue(unsigned int value);
255
261 TGUI_NODISCARD unsigned int getVerticalScrollbarValue() const;
262
270 TGUI_NODISCARD unsigned int getVerticalScrollbarMaxValue() const;
271
277 void setHorizontalScrollbarValue(unsigned int value);
278
284 TGUI_NODISCARD unsigned int getHorizontalScrollbarValue() const;
285
293 TGUI_NODISCARD unsigned int getHorizontalScrollbarMaxValue() const;
294
300 TGUI_NODISCARD bool isVerticalScrollbarShown() const;
301
307 TGUI_NODISCARD bool isHorizontalScrollbarShown() const;
308
321 TGUI_NODISCARD Widget::Ptr getWidgetAtPos(Vector2f pos, bool recursive) const override;
322
326 bool leftMousePressed(Vector2f pos) override;
327
331 void leftMouseReleased(Vector2f pos) override;
332
336 void mouseMoved(Vector2f pos) override;
337
341 bool scrolled(float delta, Vector2f pos, bool touch) override;
342
346 void mouseNoLongerOnWidget() override;
347
351 void leftMouseButtonNoLongerDown() override;
352
355 // Shows the tool tip when the widget is located below the mouse.
356 // Returns its tool tip or the tool tip from a child widget if the mouse is on top of the widget.
357 // A nullptr is returned when the mouse is not on top of the widget or when the tool tip is empty.
359 TGUI_NODISCARD Widget::Ptr askToolTip(Vector2f mousePos) override;
360
367 void draw(BackendRenderTarget& target, RenderStates states) const override;
368
370 protected:
371
377 void rendererChanged(const String& property) override;
378
382 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
383
387 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
388
390 // Makes a copy of the widget
392 TGUI_NODISCARD Widget::Ptr clone() const override;
393
395 private:
396
398 // Update the position, size and value of the scrollbars
400 void updateScrollbars();
401
403 // Find out what the most right and bottom positions are that are in use by the child widgets
405 void recalculateMostBottomRightPosition();
406
408 // Connect the position and size events to recalculate the bottom right position when a widget is updated
410 void connectPositionAndSize(const Widget::Ptr& widget);
411
413 // Disconnect the position and size events that were connected to keep track of widget changes
415 void disconnectAllChildWidgets();
416
418 protected:
419
420 Vector2f m_contentSize;
421 Vector2f m_mostBottomRightPosition;
422 CopiedSharedPtr<ScrollbarChildWidget> m_verticalScrollbar;
423 CopiedSharedPtr<ScrollbarChildWidget> m_horizontalScrollbar;
424
425 Scrollbar::Policy m_verticalScrollbarPolicy = Scrollbar::Policy::Automatic;
426 Scrollbar::Policy m_horizontalScrollbarPolicy = Scrollbar::Policy::Automatic;
427
428 std::chrono::steady_clock::time_point m_lastSuccessfulScrollTime; // Timestamp of the last mouse wheel scroll event
429 Vector2f m_lastSuccessfulScrollPos; // Mouse position at the last mouse wheel scroll event
430
431 unsigned int m_verticalScrollAmount = 0;
432 unsigned int m_horizontalScrollAmount = 0;
433
434 bool m_verticalScrollbarWasVisibleOnSizeUpdate = false;
435 bool m_horizontalScrollbarWasVisibleOnSizeUpdate = false;
436
437 std::unordered_map<Widget::Ptr, unsigned int> m_connectedPositionCallbacks;
438 std::unordered_map<Widget::Ptr, unsigned int> m_connectedSizeCallbacks;
439
441 };
442
444}
445
447
448#endif // TGUI_SCROLLABLE_PANEL_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:46
Definition CopiedSharedPtr.hpp:42
Class to store the position or size of a widget.
Definition Layout.hpp:313
Group of widgets that has a background color and optional borders.
Definition Panel.hpp:39
Group of widgets that has a background color and optional borders.
Definition ScrollablePanel.hpp:45
TGUI_NODISCARD unsigned int getVerticalScrollbarValue() const
Returns the thumb position of the vertical scrollbar.
TGUI_NODISCARD Widget::Ptr getWidgetAtPos(Vector2f pos, bool recursive) const override
Returns the widget that is located at the given position.
bool scrolled(float delta, Vector2f pos, bool touch) override
Called by the parent on scroll event (either from mouse wheel of from two finger scrolling on a touch...
void setContentSize(Vector2f size)
Channges the size available for child widgets.
TGUI_NODISCARD unsigned int getVerticalScrollAmount() const
Returns how much the value changes when scrolling or pressing one of the arrows of the vertical scrol...
TGUI_NODISCARD Vector2f getContentSize() const
Returns the size available for child widgets.
ScrollablePanel(const ScrollablePanel &other)
Copy constructor.
void setVerticalScrollAmount(unsigned int scrollAmount)
Changes how much the value changes when scrolling or pressing one of the arrows of the vertical scrol...
static TGUI_NODISCARD ScrollablePanel::Ptr copy(const ScrollablePanel::ConstPtr &panel)
Makes a copy of another scrollbable panel.
TGUI_NODISCARD Vector2f getAbsolutePosition(Vector2f offset) const override
TGUI_NODISCARD Vector2f getInnerSize() const override
Returns the space available for widgets inside the container.
void removeAllWidgets() override
Removes all widgets that were added to the container.
TGUI_NODISCARD Scrollbar::Policy getHorizontalScrollbarPolicy() const
Returns when the horizontal scrollbar should be displayed.
TGUI_NODISCARD ScrollablePanelRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void setHorizontalScrollAmount(unsigned int scrollAmount)
Changes how much the value changes when scrolling or pressing one of the arrows of the horizontal scr...
TGUI_NODISCARD ScrollablePanelRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
TGUI_NODISCARD unsigned int getHorizontalScrollbarMaxValue() const
Returns the maximum thumb position of the horizontal scrollbar.
std::shared_ptr< const ScrollablePanel > ConstPtr
Shared constant widget pointer.
Definition ScrollablePanel.hpp:49
TGUI_NODISCARD Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
bool leftMousePressed(Vector2f pos) override
Called by the parent when the left mouse button goes down on top of the widget.
bool remove(const Widget::Ptr &widget) override
Removes a single widget that was added to the container.
void add(const Widget::Ptr &widget, const String &widgetName="") override
Adds a widget at the end of the layout.
TGUI_NODISCARD 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.
TGUI_NODISCARD unsigned int getVerticalScrollbarMaxValue() const
Returns the maximum thumb position of the vertical scrollbar.
TGUI_NODISCARD Vector2f getContentOffset() const
Returns the amount of pixels the child widgets have been shifted to be displayed by the scrollable pa...
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
void setVerticalScrollbarValue(unsigned int value)
Changes the thumb position of the vertical scrollbar.
void setHorizontalScrollbarPolicy(Scrollbar::Policy policy)
Changes when the horizontal scrollbar should be displayed.
ScrollablePanel(ScrollablePanel &&other) noexcept
Move constructor.
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
TGUI_NODISCARD Scrollbar::Policy getVerticalScrollbarPolicy() const
Returns when the vertical scrollbar should be displayed.
TGUI_NODISCARD unsigned int getHorizontalScrollAmount() const
Returns how much the value changes when scrolling or pressing one of the arrows of the horizontal scr...
void setSize(const Layout2d &size) override
Changes the size of the panel.
void setHorizontalScrollbarValue(unsigned int value)
Changes the thumb position of the horizontal scrollbar.
static TGUI_NODISCARD ScrollablePanel::Ptr create(const Layout2d &size={"100%", "100%"}, Vector2f contentSize={0, 0})
Creates a new scrollable panel widget.
std::shared_ptr< ScrollablePanel > Ptr
Shared widget pointer.
Definition ScrollablePanel.hpp:48
TGUI_NODISCARD float getScrollbarWidth() const
Returns the width of the scrollbars.
TGUI_NODISCARD bool isHorizontalScrollbarShown() const
Returns whether the horizontal scrollbar is currently visible.
TGUI_NODISCARD bool isVerticalScrollbarShown() const
Returns whether the vertical scrollbar is currently visible.
TGUI_NODISCARD unsigned int getHorizontalScrollbarValue() const
Returns the thumb position of the horizontal scrollbar.
void setVerticalScrollbarPolicy(Scrollbar::Policy policy)
Changes when the vertical scrollbar should be displayed.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
Definition ScrollablePanelRenderer.hpp:35
Policy
Defines when the scrollbar shows up.
Definition Scrollbar.hpp:53
Wrapper class to store strings.
Definition String.hpp:96
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:86
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38
States used for drawing.
Definition RenderStates.hpp:38