TGUI  1.3-dev
Loading...
Searching...
No Matches
Container.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_CONTAINER_HPP
27#define TGUI_CONTAINER_HPP
28
29#include <TGUI/Widget.hpp>
30
31#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
32 #include <list>
33#endif
34
36
37TGUI_MODULE_EXPORT namespace tgui
38{
48 class TGUI_API Container : public Widget
49 {
50 public:
51
52 using Ptr = std::shared_ptr<Container>;
53 using ConstPtr = std::shared_ptr<const Container>;
54
62 Container(const char* typeName, bool initRenderer);
63
67 Container(const Container& copy);
68
72 Container(Container&& copy) noexcept;
73
77 ~Container() override;
78
82 Container& operator= (const Container& right);
83
87 Container& operator= (Container&& right) noexcept;
88
93 void setSize(const Layout2d& size) override;
94 using Widget::setSize;
95
102 TGUI_NODISCARD const std::vector<Widget::Ptr>& getWidgets() const
103 {
104 return m_widgets;
105 }
106
114 template<typename Function>
115 void sortWidgets(Function&& function)
116 {
117 std::sort(m_widgets.begin(), m_widgets.end(), std::forward<Function>(function));
118 }
119
132 virtual void add(const Widget::Ptr& widgetPtr, const String& widgetName = "");
133
147 TGUI_NODISCARD Widget::Ptr get(const String& widgetName) const;
148
163 template <class WidgetType>
164 TGUI_NODISCARD typename WidgetType::Ptr get(const String& widgetName) const
165 {
166 return std::dynamic_pointer_cast<WidgetType>(get(widgetName));
167 }
168
177 virtual bool remove(const Widget::Ptr& widget);
178
183 virtual void removeAllWidgets();
184
189 TGUI_NODISCARD virtual Vector2f getInnerSize() const;
190
198 TGUI_NODISCARD virtual Vector2f getChildWidgetsOffset() const
199 {
200 return Vector2f{0, 0};
201 }
202
211 void loadWidgetsFromFile(const String& filename, bool replaceExisting = true);
212
220 void saveWidgetsToFile(const String& filename);
221
228 void loadWidgetsFromStream(std::stringstream& stream, bool replaceExisting = true);
229
236 void loadWidgetsFromStream(std::stringstream&& stream, bool replaceExisting = true);
237
244 void saveWidgetsToStream(std::stringstream& stream, const String& rootDirectory = "") const;
245
256 void loadWidgetsFromNodeTree(const std::unique_ptr<DataIO::Node>& rootNode, bool replaceExisting = true);
257
269 TGUI_NODISCARD std::unique_ptr<DataIO::Node> saveWidgetsToNodeTree(const String& rootDirectory = "") const;
270
278 void moveWidgetToFront(const Widget::Ptr& widget);
279
287 void moveWidgetToBack(const Widget::Ptr& widget);
288
299 std::size_t moveWidgetForward(const Widget::Ptr& widget);
300
311 std::size_t moveWidgetBackward(const Widget::Ptr& widget);
312
323 virtual bool setWidgetIndex(const Widget::Ptr& widget, std::size_t index);
324
330 TGUI_NODISCARD int getWidgetIndex(const Widget::Ptr& widget) const;
331
340 TGUI_NODISCARD Widget::Ptr getFocusedChild() const;
341
350 TGUI_NODISCARD Widget::Ptr getFocusedLeaf() const;
351
352#ifndef TGUI_REMOVE_DEPRECATED_CODE
362 TGUI_DEPRECATED("Use getWidgetAtPos instead") TGUI_NODISCARD virtual Widget::Ptr getWidgetAtPosition(Vector2f pos) const;
363#endif
376 TGUI_NODISCARD virtual Widget::Ptr getWidgetAtPos(Vector2f pos, bool recursive) const;
377
386 bool focusNextWidget(bool recursive = true);
387
396 bool focusPreviousWidget(bool recursive = true);
397
406 void setFocused(bool focused) override;
407
414
422
430
438 bool processScrollEvent(float delta, Vector2f pos, bool touch);
439
446
452 bool processTextEnteredEvent(char32_t key);
453
458 void childWidgetFocused(const Widget::Ptr& child);
459
463 bool leftMousePressed(Vector2f pos) override;
464
468 void rightMousePressed(Vector2f pos) override;
469
473 void leftMouseReleased(Vector2f pos) override;
474
478 void rightMouseReleased(Vector2f pos) override;
479
483 void mouseMoved(Vector2f pos) override;
484
488 void keyPressed(const Event::KeyEvent& event) override;
489
499 bool canHandleKeyPress(const Event::KeyEvent& event) override;
500
504 void textEntered(char32_t key) override;
505
509 bool scrolled(float delta, Vector2f pos, bool touch) override;
510
514 void mouseNoLongerOnWidget() override;
515
519 void leftMouseButtonNoLongerDown() override;
520
524 void rightMouseButtonNoLongerDown() override;
525
528 // Shows the tool tip when the widget is located below the mouse.
529 // Returns its tool tip or the tool tip from a child widget if the mouse is on top of the widget.
530 // A nullptr is returned when the mouse is not on top of the widget or when the tool tip is empty.
532 TGUI_NODISCARD Widget::Ptr askToolTip(Vector2f mousePos) override;
533
536 // This function is called every frame with the time passed since the last frame.
538 bool updateTime(Duration elapsedTime) override;
539
544 void setParent(Container* parent) override;
545
550 void setParentGui(BackendGui* gui);
551
557 void updateChildrenWithAutoLayout();
558
560 protected:
561
568 void rendererChanged(const String& property) override;
569
576 void draw(BackendRenderTarget& target, RenderStates states) const override;
577
581 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
582
586 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
587
591 void updateTextSize() override;
592
594 // Checks above which widget the mouse is standing.
595 // If there is no widget below the mouse then this function will return a null pointer.
597 TGUI_NODISCARD Widget::Ptr getWidgetBelowMouse(Vector2f mousePos) const;
598
600 // Checks which widget is below the mouse and updates the cached value.
601 // Note that a nullptr is stored and returned if the widget that is found by getWidgetBelowMouse is disabled.
603 Widget::Ptr updateWidgetBelowMouse(Vector2f mousePos);
604
606 // Find out what the index of the focused widget is. Returns 0 when no widget is focused and index+1 otherwise.
608 TGUI_NODISCARD std::size_t getFocusedWidgetIndex() const;
609
611 // Try to focus the given child widget
613 bool tryFocusWidget(const Widget::Ptr &widget, bool reverseWidgetOrder, bool recursive);
614
616 // Transform the mouse position for the child widget based its origin, rotation and scaling.
618 TGUI_NODISCARD Vector2f transformMousePos(const Widget::Ptr& widget, Vector2f mousePos) const;
619
621 // Finishes adding a widget to the container
623 void widgetAdded(const Widget::Ptr& widgetPtr);
624
626 // Turns texture and font filenames into paths relative to the form file
628 void injectFormFilePath(const std::unique_ptr<DataIO::Node>& node, const String& path, std::map<String, bool>& checkedFilenames) const;
629
631 // Mutual code in loadWidgetsFromFile and loadWidgetsFromStream
633 void loadWidgetsImpl(const std::unique_ptr<DataIO::Node>& rootNode, bool replaceExisting);
634
636 protected:
637
638 std::vector<Widget::Ptr> m_widgets;
639
640 Widget::Ptr m_widgetBelowMouse;
641 Widget::Ptr m_widgetWithLeftMouseDown;
642 Widget::Ptr m_widgetWithRightMouseDown;
643 Widget::Ptr m_focusedWidget;
644 bool m_draggingWidget = false;
645
646 Vector2f m_prevInnerSize;
647
648 // Does focusing the next widget always keep a widget from this container focused (e.g. in a ChildWindow)?
649 bool m_isolatedFocus = false;
650
651
652 friend class SubwidgetContainer; // Needs access to save and load functions
653
655 };
656
657
661 class TGUI_API RootContainer : public Container
662 {
663 public:
664
665 using Ptr = std::shared_ptr<RootContainer>;
666 using ConstPtr = std::shared_ptr<const RootContainer>;
667
668 static constexpr const char StaticWidgetType[] = "RootContainer";
669
677 RootContainer(const char* typeName = StaticWidgetType, bool initRenderer = true);
678
685 void setFocused(bool focused) override;
686
695 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
696
700 void mouseNoLongerOnWidget() override;
701
708 void draw(BackendRenderTarget& target, RenderStates states) const override;
709
711 private:
712
714 // Returns a nullptr.
716 Widget::Ptr clone() const override
717 {
718 return nullptr;
719 }
720
722 };
723
725}
726
728
729#endif // TGUI_CONTAINER_HPP
Base class for the Gui.
Definition BackendGui.hpp:48
Base class for render targets.
Definition BackendRenderTarget.hpp:48
Container widget.
Definition Container.hpp:49
virtual bool remove(const Widget::Ptr &widget)
Removes a single widget that was added to the container.
bool processMousePressEvent(Event::MouseButton button, Vector2f pos)
Inform the container about a mouse press event.
Container(const Container &copy)
Copy constructor.
TGUI_NODISCARD WidgetType::Ptr get(const String &widgetName) const
Returns a pointer to a widget that was added earlier.
Definition Container.hpp:164
bool processMouseReleaseEvent(Event::MouseButton button, Vector2f pos)
Inform the container about a mouse release event.
Container(Container &&copy) noexcept
Move constructor.
std::size_t moveWidgetBackward(const Widget::Ptr &widget)
Places a widget one step backward in the z-order.
virtual TGUI_NODISCARD Widget::Ptr getWidgetAtPos(Vector2f pos, bool recursive) const
Returns the widget that is located at the given position.
bool focusPreviousWidget(bool recursive=true)
Focuses the previous widget in this container.
~Container() override
Destructor.
TGUI_NODISCARD Widget::Ptr getFocusedLeaf() const
Returns the leaf child widget that is focused inside this container.
bool canHandleKeyPress(const Event::KeyEvent &event) override
Called by the parent of the widget to check if keyPressed would process the event.
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the child widgets to a render target.
std::shared_ptr< Container > Ptr
Shared widget pointer.
Definition Container.hpp:52
bool processScrollEvent(float delta, Vector2f pos, bool touch)
Inform the container about a scroll event (either mouse wheel or two finger scrolling on touchscreen)
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
TGUI_NODISCARD Widget::Ptr getFocusedChild() const
Returns the child widget that is focused inside this container.
virtual void add(const Widget::Ptr &widgetPtr, const String &widgetName="")
Adds a widget to the container.
virtual void removeAllWidgets()
Removes all widgets that were added to the container.
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.
void loadWidgetsFromStream(std::stringstream &&stream, bool replaceExisting=true)
Loads the child widgets from a string stream.
TGUI_NODISCARD const std::vector< Widget::Ptr > & getWidgets() const
Returns a list of all the widgets in this container.
Definition Container.hpp:102
bool focusNextWidget(bool recursive=true)
Focuses the next widget in this container.
void loadWidgetsFromNodeTree(const std::unique_ptr< DataIO::Node > &rootNode, bool replaceExisting=true)
Loads the child widgets from a tree of nodes that contain all information about the widgets.
void sortWidgets(Function &&function)
Sorts a list of all the widgets in this container.
Definition Container.hpp:115
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
void moveWidgetToFront(const Widget::Ptr &widget)
Places a widget before all other widgets, to the front of the z-order.
void loadWidgetsFromFile(const String &filename, bool replaceExisting=true)
Loads the child widgets from a text file.
void moveWidgetToBack(const Widget::Ptr &widget)
Places a widget behind all other widgets, to the back of the z-order.
virtual bool setWidgetIndex(const Widget::Ptr &widget, std::size_t index)
Changes the index of a widget in this container.
TGUI_NODISCARD Widget::Ptr get(const String &widgetName) const
Returns a pointer to a widget that was added earlier.
virtual TGUI_NODISCARD Vector2f getInnerSize() const
Returns the space available for widgets inside the container.
std::size_t moveWidgetForward(const Widget::Ptr &widget)
Places a widget one step forward in the z-order.
TGUI_NODISCARD int getWidgetIndex(const Widget::Ptr &widget) const
Returns the current index of a widget in this container.
bool leftMousePressed(Vector2f pos) override
Called by the parent when the left mouse button goes down on top of the widget.
void updateTextSize() override
Called when the text size is changed (either by setTextSize or via the renderer)
bool processTextEnteredEvent(char32_t key)
Inform the container about a key press event.
void setFocused(bool focused) override
Focus or unfocus the widget.
bool processKeyPressEvent(Event::KeyEvent event)
Inform the container about a key press event.
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 saveWidgetsToFile(const String &filename)
Saves the child widgets to a text file.
void saveWidgetsToStream(std::stringstream &stream, const String &rootDirectory="") const
Saves the child widgets to a text file.
void setSize(const Layout2d &size) override
Changes the size of the container.
void loadWidgetsFromStream(std::stringstream &stream, bool replaceExisting=true)
Loads the child widgets from a string stream.
bool processMouseMoveEvent(Vector2f pos)
Inform the container about a mouse move event.
virtual TGUI_NODISCARD Vector2f getChildWidgetsOffset() const
Returns the distance between the position of the container and a widget that would be drawn inside th...
Definition Container.hpp:198
TGUI_NODISCARD std::unique_ptr< DataIO::Node > saveWidgetsToNodeTree(const String &rootDirectory="") const
Saves the child widgets to a tree of nodes that contain all information about the widgets.
std::shared_ptr< const Container > ConstPtr
Shared constant widget pointer.
Definition Container.hpp:53
Wrapper for durations.
Definition Duration.hpp:56
Class to store the position or size of a widget.
Definition Layout.hpp:305
Definition Container.hpp:662
std::shared_ptr< RootContainer > Ptr
Shared widget pointer.
Definition Container.hpp:665
std::shared_ptr< const RootContainer > ConstPtr
Shared constant widget pointer.
Definition Container.hpp:666
TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
void draw(BackendRenderTarget &target, RenderStates states) const override
Draws all widgets to a render target.
void setFocused(bool focused) override
Focus or unfocus the widget.
Wrapper class to store strings.
Definition String.hpp:101
Base class for widgets that consist of subwidgets that act together as if they are a single widget.
Definition SubwidgetContainer.hpp:43
The parent class for every widget.
Definition Widget.hpp:84
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:87
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:39
KeyPressed event parameters.
Definition Event.hpp:169
MouseButton
Mouse buttons.
Definition Event.hpp:150
States used for drawing.
Definition RenderStates.hpp:39