1//************************************ bs::framework - Copyright 2018 Marko Pintera **************************************//
2//*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********//
3#pragma once
4
5#include "BsPrerequisites.h"
6#include "GUI/BsGUIElementBase.h"
7#include "Math/BsVector2I.h"
8
9namespace bs
10{
11 /** @addtogroup Implementation
12 * @{
13 */
14
15 /**
16 * Base class for layout GUI element. Layout element positions and sizes any child elements according to element styles
17 * and layout options.
18 */
19 class BS_EXPORT GUILayout : public GUIElementBase
20 {
21 public:
22 GUILayout(const GUIDimensions& dimensions);
23 GUILayout();
24 virtual ~GUILayout();
25
26 /** Creates a new element and adds it to the layout after all existing elements. */
27 template<class Type, class... Args>
28 Type* addNewElement(Args &&...args)
29 {
30 Type* elem = Type::create(std::forward<Args>(args)...);
31 addElement(elem);
32 return elem;
33 }
34
35 /** Creates a new element and inserts it before the element at the specified index. */
36 template<class Type, class... Args>
37 Type* insertNewElement(UINT32 idx, Args &&...args)
38 {
39 Type* elem = Type::create(std::forward<Args>(args)...);
40 insertElement(idx, elem);
41 return elem;
42 }
43
44 /** Adds a new element to the layout after all existing elements. */
45 void addElement(GUIElementBase* element);
46
47 /** Removes the specified element from the layout. */
48 void removeElement(GUIElementBase* element);
49
50 /** Removes a child element at the specified index. */
51 void removeElementAt(UINT32 idx);
52
53 /** Inserts a GUI element before the element at the specified index. */
54 void insertElement(UINT32 idx, GUIElementBase* element);
55
56 /** Removes all child elements and destroys them. */
57 void clear();
58
59 /** Returns number of child elements in the layout. */
60 UINT32 getNumChildren() const { return (UINT32)mChildren.size(); }
61
62 /** Destroy the layout. Removes it from parent and widget, and deletes it. */
63 static void destroy(GUILayout* layout);
64
65 public: // ***** INTERNAL ******
66 /** @name Internal
67 * @{
68 */
69
70 /** @copydoc GUIElementBase::_getLayoutSizeRange */
71 LayoutSizeRange _getLayoutSizeRange() const override { return _getCachedSizeRange(); }
72
73 /** Returns a size range that was cached during the last GUIElementBase::_updateOptimalLayoutSizes call. */
74 LayoutSizeRange _getCachedSizeRange() const { return mSizeRange; }
75
76 /**
77 * Returns a size ranges for all children that was cached during the last GUIElementBase::_updateOptimalLayoutSizes
78 * call.
79 */
80 const Vector<LayoutSizeRange>& _getCachedChildSizeRanges() const { return mChildSizeRanges; }
81
82 /** @copydoc GUIElementBase::_getOptimalSize */
83 Vector2I _getOptimalSize() const override { return mSizeRange.optimal; }
84
85 /** @copydoc GUIElementBase::_getPadding */
86 const RectOffset& _getPadding() const override;
87
88 /** @copydoc GUIElementBase::_getType */
89 Type _getType() const override { return GUIElementBase::Type::Layout; }
90
91 /** @} */
92
93 protected:
94 Vector<LayoutSizeRange> mChildSizeRanges;
95 LayoutSizeRange mSizeRange;
96 };
97
98 /** @} */
99}