TGUI  0.10-beta
Container.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2022 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#include <list>
32
34
35namespace tgui
36{
46 class TGUI_API Container : public Widget
47 {
48 public:
49
50 typedef std::shared_ptr<Container> Ptr;
51 typedef std::shared_ptr<const Container> ConstPtr;
52
53
61 Container(const char* typeName, bool initRenderer);
62
63
67 Container(const Container& copy);
68
69
73 Container(Container&& copy) noexcept;
74
75
80
81
85 Container& operator= (const Container& right);
86
87
91 Container& operator= (Container&& right) noexcept;
92
93
98 void setSize(const Layout2d& size) override;
99 using Widget::setSize;
100
101
108 const std::vector<Widget::Ptr>& getWidgets() const
109 {
110 return m_widgets;
111 }
112
113
121 template<typename Function>
122 void sortWidgets(Function&& function)
123 {
124 std::sort(m_widgets.begin(), m_widgets.end(), std::forward<Function>(function));
125 }
126
127
137 virtual void add(const Widget::Ptr& widgetPtr, const String& widgetName = "");
138
139
153 Widget::Ptr get(const String& widgetName) const;
154
155
170 template <class T>
171 typename T::Ptr get(const String& widgetName) const
172 {
173 return std::dynamic_pointer_cast<T>(get(widgetName));
174 }
175
176
185 virtual bool remove(const Widget::Ptr& widget);
186
187
192 virtual void removeAllWidgets();
193
194
199 virtual Vector2f getInnerSize() const;
200
201
210 {
211 return Vector2f{0, 0};
212 }
213
214
223 void loadWidgetsFromFile(const String& filename, bool replaceExisting = true);
224
225
233 void saveWidgetsToFile(const String& filename);
234
235
242 void loadWidgetsFromStream(std::stringstream& stream, bool replaceExisting = true);
243
244
251 void loadWidgetsFromStream(std::stringstream&& stream, bool replaceExisting = true);
252
253
260 void saveWidgetsToStream(std::stringstream& stream, const String& rootDirectory = "") const;
261
262
273 void loadWidgetsFromNodeTree(const std::unique_ptr<DataIO::Node>& rootNode, bool replaceExisting = true);
274
275
287 std::unique_ptr<DataIO::Node> saveWidgetsToNodeTree(const String& rootDirectory = "") const;
288
289
297 void moveWidgetToFront(const Widget::Ptr& widget);
298
299
307 void moveWidgetToBack(const Widget::Ptr& widget);
308
309
320 std::size_t moveWidgetForward(const Widget::Ptr& widget);
321
322
333 std::size_t moveWidgetBackward(const Widget::Ptr& widget);
334
335
346 bool setWidgetIndex(const Widget::Ptr& widget, std::size_t index);
347
348
354 int getWidgetIndex(const Widget::Ptr& widget) const;
355
356
366
367
377
378
387
388
397 bool focusNextWidget(bool recursive = true);
398
399
408 bool focusPreviousWidget(bool recursive = true);
409
410
419 void setFocused(bool focused) override;
420
421
428
429
437
438
446
447
455
456
463
464
470 bool processTextEnteredEvent(char32_t key);
471
472
477 void childWidgetFocused(const Widget::Ptr& child);
478
479
483 void leftMousePressed(Vector2f pos) override;
484
488 void rightMousePressed(Vector2f pos) override;
489
493 void leftMouseReleased(Vector2f pos) override;
494
498 void rightMouseReleased(Vector2f pos) override;
499
503 void mouseMoved(Vector2f pos) override;
504
508 void keyPressed(const Event::KeyEvent& event) override;
509
513 void textEntered(char32_t key) override;
514
518 bool mouseWheelScrolled(float delta, Vector2f pos) override;
519
523 void mouseNoLongerOnWidget() override;
524
528 void leftMouseButtonNoLongerDown() override;
529
533 void rightMouseButtonNoLongerDown() override;
534
535
538 // Shows the tool tip when the widget is located below the mouse.
539 // Returns its tool tip or the tool tip from a child widget if the mouse is on top of the widget.
540 // A nullptr is returned when the mouse is not on top of the widget or when the tool tip is empty.
542 Widget::Ptr askToolTip(Vector2f mousePos) override;
543
544
547 // This function is called every frame with the time passed since the last frame.
549 bool updateTime(Duration elapsedTime) override;
550
551
556 void setParent(Container* parent) override;
557
558
563 void setParentGui(BackendGui* gui);
564
565
567 protected:
568
569
576 void rendererChanged(const String& property) override;
577
578
585 void draw(BackendRenderTarget& target, RenderStates states) const override;
586
587
591 std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
592
593
597 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
598
599
603 void updateTextSize() override;
604
605
607 // Checks above which widget the mouse is standing.
608 // If there is no widget below the mouse then this function will return a null pointer.
610 Widget::Ptr getWidgetBelowMouse(Vector2f mousePos) const;
611
612
614 // Checks which widget is below the mouse and updates the cached value.
615 // Note that a nullptr is stored and returned if the widget that is found by getWidgetBelowMouse is disabled.
617 Widget::Ptr updateWidgetBelowMouse(Vector2f mousePos);
618
619
621 // Find out what the index of the focused widget is. Returns 0 when no widget is focused and index+1 otherwise.
623 std::size_t getFocusedWidgetIndex() const;
624
625
627 // Try to focus the given child widget
629 bool tryFocusWidget(const tgui::Widget::Ptr &widget, bool reverseWidgetOrder, bool recursive);
630
631
633 // Transform the mouse position for the child widget based its origin, rotation and scaling.
635 Vector2f transformMousePos(const Widget::Ptr& widget, Vector2f mousePos) const;
636
637
639 // Finishes adding a widget to the container
641 void widgetAdded(Widget::Ptr widgetPtr);
642
643
645 // Turns texture and font filenames into paths relative to the form file
647 void injectFormFilePath(const std::unique_ptr<DataIO::Node>& node, const String& path, std::map<String, bool>& checkedFilenames) const;
648
649
651 // Mutual code in loadWidgetsFromFile and loadWidgetsFromStream
653 void loadWidgetsImpl(const std::unique_ptr<DataIO::Node>& rootNode, bool replaceExisting);
654
655
657 protected:
658
659 std::vector<Widget::Ptr> m_widgets;
660
661 Widget::Ptr m_widgetBelowMouse;
662 Widget::Ptr m_widgetWithLeftMouseDown;
663 Widget::Ptr m_widgetWithRightMouseDown;
664 Widget::Ptr m_focusedWidget;
665
666 Vector2f m_prevInnerSize;
667
668 // Does focusing the next widget always keep a widget from this container focused (e.g. in a ChildWindow)?
669 bool m_isolatedFocus = false;
670
671
672 friend class SubwidgetContainer; // Needs access to save and load functions
673
675 };
676
677
681 class TGUI_API RootContainer : public Container
682 {
683 public:
684
685 typedef std::shared_ptr<RootContainer> Ptr;
686 typedef std::shared_ptr<const RootContainer> ConstPtr;
687
688
696 RootContainer(const char* typeName = "RootContainer", bool initRenderer = true);
697
698
705 void setSize(const Layout2d& size) override;
706 using Widget::setSize;
707
708
715 void setFocused(bool focused) override;
716
717
726 bool isMouseOnWidget(Vector2f pos) const override;
727
728
735 void draw(BackendRenderTarget& target, RenderStates states) const override;
736
737
739 private:
740
742 // Returns a nullptr.
744 Widget::Ptr clone() const override
745 {
746 return nullptr;
747 }
748
750 };
751
752
754}
755
757
758#endif // TGUI_CONTAINER_HPP
Base class for the Gui.
Definition: BackendGui.hpp:45
Base class for render targets.
Definition: BackendRenderTarget.hpp:48
Container widget.
Definition: Container.hpp:47
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.
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.
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.
bool focusPreviousWidget(bool recursive=true)
Focuses the previous widget in this container.
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.
std::shared_ptr< Container > Ptr
Shared widget pointer.
Definition: Container.hpp:50
~Container()
Destructor.
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the child widgets to a render target.
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
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.
void loadWidgetsFromStream(std::stringstream &&stream, bool replaceExisting=true)
Loads the child widgets from a string stream.
bool processMouseWheelScrollEvent(float delta, Vector2f pos)
Inform the container about a mouse wheel scroll event.
virtual Vector2f getInnerSize() const
Returns the space available for widgets inside the container.
Widget::Ptr get(const String &widgetName) const
Returns a pointer to a widget that was added earlier.
bool focusNextWidget(bool recursive=true)
Focuses the next widget in this container.
const std::vector< Widget::Ptr > & getWidgets() const
Returns a list of all the widgets in this container.
Definition: Container.hpp:108
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.
bool setWidgetIndex(const Widget::Ptr &widget, std::size_t index)
Changes the index of a widget in this container.
void sortWidgets(Function &&function)
Sorts a list of all the widgets in this container.
Definition: Container.hpp:122
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.
T::Ptr get(const String &widgetName) const
Returns a pointer to a widget that was added earlier.
Definition: Container.hpp:171
std::size_t moveWidgetForward(const Widget::Ptr &widget)
Places a widget one step forward in the z-order.
virtual Vector2f getChildWidgetsOffset() const
Returns the distance between the position of the container and a widget that would be drawn inside th...
Definition: Container.hpp:209
int getWidgetIndex(const Widget::Ptr &widget) const
Returns the current index of a widget in this container.
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.
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.
std::shared_ptr< const Container > ConstPtr
Shared constant widget pointer.
Definition: Container.hpp:51
virtual Widget::Ptr getWidgetAtPosition(Vector2f pos) const
Returns the leaf child widget that is located at the given position.
Widget::Ptr getFocusedChild() const
Returns the child widget that is focused inside this container.
Widget::Ptr getFocusedLeaf() const
Returns the leaf child widget that is focused inside this container.
Wrapper for durations.
Definition: Duration.hpp:52
Class to store the position or size of a widget.
Definition: Layout.hpp:284
Definition: Container.hpp:682
std::shared_ptr< const RootContainer > ConstPtr
Shared constant widget pointer.
Definition: Container.hpp:686
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.
std::shared_ptr< RootContainer > Ptr
Shared widget pointer.
Definition: Container.hpp:685
void setSize(const Layout2d &size) override
Changes the size of the container.
Wrapper class to store strings.
Definition: String.hpp:79
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:70
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:73
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
KeyPressed event parameters.
Definition: Event.hpp:167
MouseButton
Mouse buttons.
Definition: Event.hpp:148
States used for drawing.
Definition: RenderStates.hpp:39