TGUI  1.3-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
26#ifndef TGUI_SCROLLABLE_PANEL_HPP
27#define TGUI_SCROLLABLE_PANEL_HPP
28
29#include <TGUI/CopiedSharedPtr.hpp>
30#include <TGUI/Widgets/Panel.hpp>
31#include <TGUI/Widgets/Scrollbar.hpp>
32#include <TGUI/Renderers/ScrollablePanelRenderer.hpp>
33
34#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
35 #include <chrono>
36#endif
37
39
40TGUI_MODULE_EXPORT namespace tgui
41{
45 class TGUI_API ScrollablePanel : public Panel
46 {
47 public:
48
49 using Ptr = std::shared_ptr<ScrollablePanel>;
50 using ConstPtr = std::shared_ptr<const ScrollablePanel>;
51
52 static constexpr const char StaticWidgetType[] = "ScrollablePanel";
53
54
62 ScrollablePanel(const char* typeName = StaticWidgetType, bool initRenderer = true);
63
64
69
70
75
76
80 ScrollablePanel& operator= (const ScrollablePanel& other);
81
82
86 ScrollablePanel& operator= (ScrollablePanel&& other) noexcept;
87
88
99 TGUI_NODISCARD static ScrollablePanel::Ptr create(const Layout2d& size = {"100%", "100%"}, Vector2f contentSize = {0, 0});
100
101
109 TGUI_NODISCARD static ScrollablePanel::Ptr copy(const ScrollablePanel::ConstPtr& panel);
110
111
116 TGUI_NODISCARD ScrollablePanelRenderer* getSharedRenderer() override;
117 TGUI_NODISCARD const ScrollablePanelRenderer* getSharedRenderer() const override;
118
124 TGUI_NODISCARD ScrollablePanelRenderer* getRenderer() override;
125
126
132 void setSize(const Layout2d& size) override;
133 using Widget::setSize;
134
135
140 TGUI_NODISCARD Vector2f getInnerSize() const override;
141
142
147 TGUI_NODISCARD Vector2f getAbsolutePosition(Vector2f offset) const override;
148
149
161 void add(const Widget::Ptr& widget, const String& widgetName = "") override;
162
163
171 bool remove(const Widget::Ptr& widget) override;
172
173
177 void removeAllWidgets() override;
178
179
190
191
199 TGUI_NODISCARD Vector2f getContentSize() const;
200
201
207 TGUI_NODISCARD Vector2f getContentOffset() const;
208
209
214 TGUI_NODISCARD float getScrollbarWidth() const;
215
216
222
223
229
230
236
237
243
244
250 void setVerticalScrollAmount(unsigned int scrollAmount);
251
252
258 TGUI_NODISCARD unsigned int getVerticalScrollAmount() const;
259
260
266 void setHorizontalScrollAmount(unsigned int scrollAmount);
267
268
274 TGUI_NODISCARD unsigned int getHorizontalScrollAmount() const;
275
276
282 void setVerticalScrollbarValue(unsigned int value);
283
284
290 TGUI_NODISCARD unsigned int getVerticalScrollbarValue() const;
291
292
298 void setHorizontalScrollbarValue(unsigned int value);
299
300
306 TGUI_NODISCARD unsigned int getHorizontalScrollbarValue() const;
307
308
314 TGUI_NODISCARD bool isVerticalScrollbarShown() const;
315
316
322 TGUI_NODISCARD bool isHorizontalScrollbarShown() const;
323
324
337 TGUI_NODISCARD Widget::Ptr getWidgetAtPos(Vector2f pos, bool recursive) const override;
338
339
343 bool leftMousePressed(Vector2f pos) override;
344
348 void leftMouseReleased(Vector2f pos) override;
349
353 void mouseMoved(Vector2f pos) override;
354
358 bool scrolled(float delta, Vector2f pos, bool touch) override;
359
363 void mouseNoLongerOnWidget() override;
364
368 void leftMouseButtonNoLongerDown() override;
369
370
373 // Shows the tool tip when the widget is located below the mouse.
374 // Returns its tool tip or the tool tip from a child widget if the mouse is on top of the widget.
375 // A nullptr is returned when the mouse is not on top of the widget or when the tool tip is empty.
377 TGUI_NODISCARD Widget::Ptr askToolTip(Vector2f mousePos) override;
378
379
386 void draw(BackendRenderTarget& target, RenderStates states) const override;
387
388
390 protected:
391
398 void rendererChanged(const String& property) override;
399
400
404 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
405
406
410 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
411
412
414 // Makes a copy of the widget
416 TGUI_NODISCARD Widget::Ptr clone() const override;
417
418
420 private:
421
423 // Update the position, size and value of the scrollbars
425 void updateScrollbars();
426
427
429 // Find out what the most right and bottom positions are that are in use by the child widgets
431 void recalculateMostBottomRightPosition();
432
433
435 // Connect the position and size events to recalculate the bottom right position when a widget is updated
437 void connectPositionAndSize(const Widget::Ptr& widget);
438
439
441 // Disconnect the position and size events that were connected to keep track of widget changes
443 void disconnectAllChildWidgets();
444
445
447 protected:
448
449 Vector2f m_contentSize;
450 Vector2f m_mostBottomRightPosition;
451 CopiedSharedPtr<ScrollbarChildWidget> m_verticalScrollbar;
452 CopiedSharedPtr<ScrollbarChildWidget> m_horizontalScrollbar;
453
454 Scrollbar::Policy m_verticalScrollbarPolicy = Scrollbar::Policy::Automatic;
455 Scrollbar::Policy m_horizontalScrollbarPolicy = Scrollbar::Policy::Automatic;
456
457 std::chrono::steady_clock::time_point m_lastSuccessfulScrollTime; // Timestamp of the last mouse wheel scroll event
458 Vector2f m_lastSuccessfulScrollPos; // Mouse position at the last mouse wheel scroll event
459
460 unsigned int m_verticalScrollAmount = 0;
461 unsigned int m_horizontalScrollAmount = 0;
462
463 std::unordered_map<Widget::Ptr, unsigned int> m_connectedPositionCallbacks;
464 std::unordered_map<Widget::Ptr, unsigned int> m_connectedSizeCallbacks;
465
467 };
468
470}
471
473
474#endif // TGUI_SCROLLABLE_PANEL_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:48
Definition CopiedSharedPtr.hpp:45
Class to store the position or size of a widget.
Definition Layout.hpp:305
Group of widgets that has a background color and optional borders.
Definition Panel.hpp:41
Definition ScrollablePanelRenderer.hpp:37
Group of widgets that has a background color and optional borders.
Definition ScrollablePanel.hpp:46
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.
std::shared_ptr< const ScrollablePanel > ConstPtr
Shared constant widget pointer.
Definition ScrollablePanel.hpp:50
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 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:49
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.
Policy
Defines when the scrollbar shows up.
Definition Scrollbar.hpp:56
Wrapper class to store strings.
Definition String.hpp:101
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:87
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:39
States used for drawing.
Definition RenderStates.hpp:39