Simple GUI Library
parent.h
Go to the documentation of this file.
1/**
2 * @author Nikita Mochalov (github.com/tralf-strues)
3 * @file parent.h
4 * @date 2021-11-06
5 *
6 * @copyright Copyright (c) 2021
7 */
8
9#pragma once
10
11#include "component.h"
12#include "../paint/insets.h"
13#include "../paint/border.h"
14#include "../paint/background.h"
15
16namespace Sgl
17{
18 class Parent : public Component
19 {
20 public:
21 Parent() = default;
22 virtual ~Parent() override = default;
23
24 virtual Component* getHitComponent(int32_t x, int32_t y) override;
25 virtual void render(const Sml::Rectangle<int32_t>& targetRegion) override;
26 virtual void layout() override final;
27 virtual void prerender() override final;
28 virtual bool containsComponent(Component* component) override final;
29
30 virtual bool isResizable() const override;
31
32 /**
33 * @brief Convenience method for adding a child to @ref Parent.
34 *
35 * @attention For @ref Control this should only be used by custom skins! It's undefined behavior
36 * if you simply add children to a @ref Control. For @ref Container this method can
37 * easily be used for adding any number of custom components.
38 *
39 * @param child
40 */
41 void addChild(Component* child);
42
43 void addChildren() {}
44
45 /**
46 * @brief Convenience method for adding multiple children to @ref Parent.
47 *
48 * @param children
49 *
50 * @see addChild() for more information on the restrictions.
51 */
52 template <typename Head, typename... Tail>
53 void addChildren(Head head, Tail... tail)
54 {
55 addChild(head);
56 addChildren(tail...);
57 }
58
59 /**
60 * @brief Convenience method for removing a child to @ref Parent.
61 *
62 * @attention For @ref Control this should only be used by custom skins! It's undefined behavior
63 * if you simply remove children from a @ref Control. For @ref Container this method
64 * can easily be used for removing children from it.
65 *
66 * @param child
67 */
68 void removeChild(Component* child);
69
70 /**
71 * @warning Doesn't delete the children!
72 */
73 void removeChildren();
74
75 std::list<Component*>& getChildren();
76 const std::list<Component*>& getChildrenReadonly() const;
77
78 Sml::Texture* getSnapshot();
79
80 bool needLayoutPass() const;
81 void requestLayoutPass();
82
83 const Background* getBackground() const;
84 void setBackground(const Background* background);
85
86 const Border* getBorder() const;
87 void setBorder(const Border* border);
88
89 Insets getInsets() const;
90 Sml::Rectangle<int32_t> getContentArea() const;
91
92 Insets getPadding() const;
93 void setPadding(const Insets& padding);
94
95 void setPrefWidth(int32_t width);
96 void setPrefHeight(int32_t height);
97
98 void setMinWidth(int32_t width);
99 void setMinHeight(int32_t height);
100
101 void setMaxWidth(int32_t width);
102 void setMaxHeight(int32_t height);
103
104 int32_t getPrefWidth() const;
105 int32_t getPrefHeight() const;
106
107 int32_t getMinWidth() const;
108 int32_t getMinHeight() const;
109
110 int32_t getMaxWidth() const;
111 int32_t getMaxHeight() const;
112
113 virtual int32_t computePrefWidth(int32_t height = -1) const override final;
114 virtual int32_t computePrefHeight(int32_t width = -1) const override final;
115
116 virtual int32_t computeMinWidth(int32_t height = -1) const override final;
117 virtual int32_t computeMinHeight(int32_t width = -1) const override final;
118
119 virtual int32_t computeMaxWidth(int32_t height = -1) const override final;
120 virtual int32_t computeMaxHeight(int32_t width = -1) const override final;
121
122 protected:
123 std::list<Component*> m_Children;
124 Sml::Texture* m_Snapshot = nullptr;
125
126 bool m_NeedLayoutPass = false;
127 const Background* m_Background = nullptr;
128 const Border* m_Border = nullptr;
129 Insets m_Padding = Insets::EMPTY;
130
131 int32_t m_PrefWidth = USE_COMPUTED_SIZE;
132 int32_t m_PrefHeight = USE_COMPUTED_SIZE;
133 int32_t m_MinWidth = USE_COMPUTED_SIZE;
134 int32_t m_MinHeight = USE_COMPUTED_SIZE;
135 int32_t m_MaxWidth = USE_COMPUTED_SIZE;
136 int32_t m_MaxHeight = USE_COMPUTED_SIZE;
137
138 virtual void setSceneInSceneTree(Scene* scene) override final;
139
140 virtual void layoutChildren();
141 virtual void prerenderSelf();
142 void updateSnapshotSize();
143
144 virtual int32_t computeCustomPrefWidth(int32_t height = -1) const;
145 virtual int32_t computeCustomPrefHeight(int32_t width = -1) const;
146
147 virtual int32_t computeCustomMinWidth(int32_t height = -1) const;
148 virtual int32_t computeCustomMinHeight(int32_t width = -1) const;
149
150 virtual int32_t computeCustomMaxWidth(int32_t height = -1) const;
151 virtual int32_t computeCustomMaxHeight(int32_t width = -1) const;
152 };
153}
void addChild(Component *child)
Convenience method for adding a child to Parent.
Definition: parent.cpp:129
void removeChildren()
Definition: parent.cpp:155
void removeChild(Component *child)
Convenience method for removing a child to Parent.
Definition: parent.cpp:146
void addChildren(Head head, Tail... tail)
Convenience method for adding multiple children to Parent.
Definition: parent.h:53