TGUI  1.0-rc2
Loading...
Searching...
No Matches
ScrollablePanel.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2023 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
332 TGUI_NODISCARD Widget::Ptr getWidgetAtPosition(Vector2f pos) const override;
333
334
338 bool leftMousePressed(Vector2f pos) override;
339
343 void leftMouseReleased(Vector2f pos) override;
344
348 void mouseMoved(Vector2f pos) override;
349
353 bool scrolled(float delta, Vector2f pos, bool touch) override;
354
358 void mouseNoLongerOnWidget() override;
359
363 void leftMouseButtonNoLongerDown() override;
364
365
368 // Shows the tool tip when the widget is located below the mouse.
369 // Returns its tool tip or the tool tip from a child widget if the mouse is on top of the widget.
370 // A nullptr is returned when the mouse is not on top of the widget or when the tool tip is empty.
372 TGUI_NODISCARD Widget::Ptr askToolTip(Vector2f mousePos) override;
373
374
381 void draw(BackendRenderTarget& target, RenderStates states) const override;
382
383
385 protected:
386
393 void rendererChanged(const String& property) override;
394
395
399 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
400
401
405 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
406
407
409 // Makes a copy of the widget
411 TGUI_NODISCARD Widget::Ptr clone() const override;
412
413
415 private:
416
418 // Update the position, size and value of the scrollbars
420 void updateScrollbars();
421
422
424 // Find out what the most right and bottom positions are that are in use by the child widgets
426 void recalculateMostBottomRightPosition();
427
428
430 // Connect the position and size events to recalculate the bottom right position when a widget is updated
432 void connectPositionAndSize(const Widget::Ptr& widget);
433
434
436 // Disconnect the position and size events that were connected to keep track of widget changes
438 void disconnectAllChildWidgets();
439
440
442 protected:
443
444 Vector2f m_contentSize;
445 Vector2f m_mostBottomRightPosition;
446 CopiedSharedPtr<ScrollbarChildWidget> m_verticalScrollbar;
447 CopiedSharedPtr<ScrollbarChildWidget> m_horizontalScrollbar;
448
449 Scrollbar::Policy m_verticalScrollbarPolicy = Scrollbar::Policy::Automatic;
450 Scrollbar::Policy m_horizontalScrollbarPolicy = Scrollbar::Policy::Automatic;
451
452 std::chrono::steady_clock::time_point m_lastSuccessfulScrollTime; // Timestamp of the last mouse wheel scroll event
453 Vector2f m_lastSuccessfulScrollPos; // Mouse position at the last mouse wheel scroll event
454
455 unsigned int m_verticalScrollAmount = 0;
456 unsigned int m_horizontalScrollAmount = 0;
457
458 std::unordered_map<Widget::Ptr, unsigned int> m_connectedPositionCallbacks;
459 std::unordered_map<Widget::Ptr, unsigned int> m_connectedSizeCallbacks;
460
462 };
463
465}
466
468
469#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:289
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.
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 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.
TGUI_NODISCARD Widget::Ptr getWidgetAtPosition(Vector2f pos) const override
Returns the leaf child widget that is located at the given position.
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