14 Component* Parent::getHitComponent(int32_t x, int32_t y)
16 if (!isInteractable()) {
return nullptr; }
18 for (
auto child = m_Children.rbegin(); child != m_Children.rend(); ++child)
20 if (!(*child)->isInteractable())
25 Component* hitted = (*child)->getHitComponent(x - getLayoutX(), y - getLayoutY());
27 if (hitted !=
nullptr)
36 void Parent::render(
const Sml::Rectangle<int32_t>& targetRegion)
43 renderShadow(targetRegion);
45 Sml::Rectangle<int32_t> translatedTargetRegion = getLayoutBounds();
46 translatedTargetRegion.pos += targetRegion.pos;
48 if (m_Snapshot !=
nullptr)
50 Sml::Rectangle<int32_t> originBounds = getOriginBounds();
51 renderTexture(*m_Snapshot, &translatedTargetRegion, &originBounds);
54 for (Component* child : getChildren())
56 if (child->isVisible())
58 child->render(translatedTargetRegion);
68 for (Component* child : m_Children)
73 m_NeedLayoutPass =
false;
76 void Parent::prerender()
78 for (Component* child : m_Children)
85 Sml::Renderer::getInstance().pushSetTarget(m_Snapshot);
87 Sml::Renderer::getInstance().setColor(Sml::COLOR_TRANSPARENT);
88 Sml::Renderer::getInstance().clear();
91 Sml::Renderer::getInstance().popTarget();
96 bool Parent::containsComponent(Component* component)
100 if (component ==
this)
105 for (
auto child : m_Children)
107 if (child->containsComponent(component))
116 void Parent::prerenderSelf(){}
118 void Parent::layoutChildren()
120 for (Component* component : m_Children)
122 component->setLayoutWidth(component->computePrefWidth());
123 component->setLayoutHeight(component->computePrefHeight());
127 bool Parent::isResizable()
const {
return true; }
133 for (
auto existingChild : m_Children)
135 if (existingChild == child)
141 m_Children.push_back(child);
142 child->setParent(
this);
143 child->setScene(getScene());
149 assert(child->getParent() ==
this);
151 m_Children.remove(child);
152 child->setParent(
nullptr);
160 std::list<Component*>& Parent::getChildren() {
return m_Children; }
161 const std::list<Component*>& Parent::getChildrenReadonly()
const {
return m_Children; }
163 Sml::Texture* Parent::getSnapshot() {
return m_Snapshot; }
165 bool Parent::needLayoutPass()
const {
return m_NeedLayoutPass; }
166 void Parent::requestLayoutPass() { m_NeedLayoutPass =
true; }
168 const Background* Parent::getBackground()
const {
return m_Background; }
169 void Parent::setBackground(
const Background* background) { m_Background = background; }
171 const Border* Parent::getBorder()
const {
return m_Border; }
172 void Parent::setBorder(
const Border* border) { m_Border = border; }
174 Insets Parent::getInsets()
const
176 return (m_Border ==
nullptr ? Insets::EMPTY : m_Border->getThickness()) + getPadding();
179 Sml::Rectangle<int32_t> Parent::getContentArea()
const
181 Insets insets = getInsets();
182 Sml::Rectangle<int32_t> contentArea = getOriginBounds();
183 contentArea.pos += {insets.left, insets.top};
184 contentArea.width -= insets.left + insets.right;
185 contentArea.height -= insets.top + insets.bottom;
190 Insets Parent::getPadding()
const {
return m_Padding; }
191 void Parent::setPadding(
const Insets& padding) { m_Padding = padding; }
193 void Parent::setPrefWidth (int32_t width) { m_PrefWidth = width; }
194 void Parent::setPrefHeight(int32_t height) { m_PrefHeight = height; }
196 void Parent::setMinWidth (int32_t width) { m_MinWidth = width; }
197 void Parent::setMinHeight(int32_t height) { m_MinHeight = height; }
199 void Parent::setMaxWidth (int32_t width) { m_MaxWidth = width; }
200 void Parent::setMaxHeight(int32_t height) { m_MaxHeight = height; }
202 int32_t Parent::getPrefWidth()
const {
return m_PrefWidth; }
203 int32_t Parent::getPrefHeight()
const {
return m_PrefHeight; }
205 int32_t Parent::getMinWidth()
const {
return m_MinWidth; }
206 int32_t Parent::getMinHeight()
const {
return m_MinHeight; }
208 int32_t Parent::getMaxWidth()
const {
return m_MaxWidth; }
209 int32_t Parent::getMaxHeight()
const {
return m_MaxHeight; }
211 #define CHECK_ENABLED_SIZE(size) if (size != USE_COMPUTED_SIZE) { return size; }
213 int32_t Parent::computePrefWidth(int32_t height)
const
215 CHECK_ENABLED_SIZE(m_PrefWidth);
216 return computeCustomPrefWidth(height);
219 int32_t Parent::computePrefHeight(int32_t width)
const
221 CHECK_ENABLED_SIZE(m_PrefHeight);
222 return computeCustomPrefHeight(width);
225 int32_t Parent::computeMinWidth(int32_t height)
const
227 CHECK_ENABLED_SIZE(m_MinWidth);
228 return computeCustomMinWidth(height);
231 int32_t Parent::computeMinHeight(int32_t width)
const
233 CHECK_ENABLED_SIZE(m_MinHeight);
234 return computeCustomMinHeight(width);
237 int32_t Parent::computeMaxWidth(int32_t height)
const
239 CHECK_ENABLED_SIZE(m_MaxWidth);
240 return computeCustomMaxWidth(height);
243 int32_t Parent::computeMaxHeight(int32_t width)
const
245 CHECK_ENABLED_SIZE(m_MaxHeight);
246 return computeCustomMaxHeight(width);
249 int32_t Parent::computeCustomPrefWidth(int32_t height)
const
251 COMPONENT_COMPUTE_DIMENSION(Pref, Width, height, X, m_Children, m_PrefWidth,
252 getInsets().left + getInsets().right);
254 int32_t Parent::computeCustomPrefHeight(int32_t width)
const
256 COMPONENT_COMPUTE_DIMENSION(Pref, Height, width, Y, m_Children, m_PrefHeight,
257 getInsets().top + getInsets().bottom);
260 int32_t Parent::computeCustomMinWidth(int32_t height)
const {
return computePrefWidth(height); }
261 int32_t Parent::computeCustomMinHeight(int32_t width)
const {
return computePrefHeight(width); }
263 int32_t Parent::computeCustomMaxWidth(int32_t height)
const {
return UNLIMITED_SIZE; }
264 int32_t Parent::computeCustomMaxHeight(int32_t width)
const {
return UNLIMITED_SIZE; }
266 void Parent::updateSnapshotSize()
268 int32_t width = m_LayoutBounds.width;
269 int32_t height = m_LayoutBounds.height;
271 if (width <= 0 || height <= 0)
276 if (m_Snapshot ==
nullptr)
278 m_Snapshot =
new Sml::Texture(width, height);
280 else if (
static_cast<int32_t
>(m_Snapshot->getWidth()) < width ||
281 static_cast<int32_t
>(m_Snapshot->getHeight()) < height)
284 m_Snapshot =
new Sml::Texture(width, height);
288 void Parent::setSceneInSceneTree(Scene* scene)
290 Component::setSceneInSceneTree(scene);
292 for (Component* child : m_Children)
294 child->setScene(scene);
void addChild(Component *child)
Convenience method for adding a child to Parent.
void removeChild(Component *child)
Convenience method for removing a child to Parent.