14 Component* Component::getHitComponent(int32_t x, int32_t y)
16 if (Sml::isPointInsideRectangle({x, y}, getLayoutBounds()))
24 void Component::layout() {}
25 void Component::prerender() {}
27 bool Component::containsComponent(Component* component)
30 return this == component;
33 Sml::EventDispatchChain* Component::buildEventDispatchChain(Sml::EventDispatchChain* chain)
37 chain->prependDispatcher(&m_Dispatcher);
39 if (m_Parent ==
nullptr)
44 return m_Parent->buildEventDispatchChain(chain);
47 bool Component::isVisible()
const {
return GET_BIT(
static_cast<uint32_t
>(m_Visibility), 1) == 1; }
49 void Component::setVisible(
bool visible)
51 uint32_t mask =
static_cast<uint32_t
>(m_Visibility);
65 bool Component::isInteractable()
const {
return GET_BIT(
static_cast<uint32_t
>(m_Visibility), 0) == 1; }
67 void Component::setInteractable(
bool interactable)
69 uint32_t mask =
static_cast<uint32_t
>(m_Visibility);
84 void Component::setVisibility(Visibility visibility) { m_Visibility = visibility; }
86 bool Component::isFocused()
const {
return m_Focused; }
87 bool Component::isHovered()
const {
return m_Hovered; }
89 void Component::requestFocus()
93 getScene()->requestFocus(
this);
96 void Component::requestDrag()
100 getScene()->requestDrag(
this);
103 const Shadow& Component::getShadow()
const
108 void Component::setShadow(
const ShadowSpecification* specification)
110 m_Shadow.setSpecification(specification);
115 return &m_Dispatcher;
118 Scene* Component::getScene()
123 void Component::setScene(Scene* scene)
125 setSceneInSceneTree(scene);
128 const Parent* Component::getParent()
const
133 Parent* Component::getModifiableParent()
138 void Component::setParent(Parent* parent)
142 if (m_Parent !=
nullptr)
144 m_Scene = m_Parent->getScene();
148 Sml::Vec2i Component::computeLocalToScenePos(
const Sml::Vec2i& localPos)
const
150 if (getParent() ==
nullptr) {
return localPos; }
151 return getParent()->computeLocalToScenePos(localPos + getLayoutPos());
154 Sml::Vec2i Component::computeSceneToLocalPos(
const Sml::Vec2i& scenePos)
const
156 if (getParent() ==
nullptr) {
return scenePos - getLayoutPos(); }
157 return getParent()->computeSceneToLocalPos(scenePos - getLayoutPos());
160 Sml::Rectangle<int32_t> Component::getOriginBounds()
const
162 return Sml::Rectangle<int32_t>{{0, 0}, getLayoutWidth(), getLayoutHeight()};
165 const Sml::Rectangle<int32_t>& Component::getLayoutBounds()
const
167 return m_LayoutBounds;
170 const Sml::Vec2i& Component::getLayoutPos()
const
172 return m_LayoutBounds.pos;
175 int32_t Component::getLayoutX()
const
177 return m_LayoutBounds.pos.x;
180 int32_t Component::getLayoutY()
const
182 return m_LayoutBounds.pos.y;
185 int32_t Component::getLayoutWidth()
const
187 return m_LayoutBounds.width;
190 int32_t Component::getLayoutHeight()
const
192 return m_LayoutBounds.height;
195 void Component::setLayoutX(int32_t x)
197 m_LayoutBounds.pos.x = x;
200 void Component::setLayoutY(int32_t y)
202 m_LayoutBounds.pos.y = y;
205 void Component::setLayoutWidth(int32_t width)
207 m_LayoutBounds.width = width;
210 void Component::setLayoutHeight(int32_t height)
212 m_LayoutBounds.height = height;
215 Sml::Vec2i Component::computeScenePos()
217 Sml::Vec2i scenePos = getLayoutBounds().pos;
219 const Component* parent = getParent();
220 while (parent !=
nullptr)
222 scenePos += parent->getLayoutBounds().pos;
223 parent = parent->getParent();
229 void Component::updateShadow()
231 if (getLayoutWidth() == 0 || getLayoutHeight() == 0 || m_Shadow.getSpecification() ==
nullptr || !isVisible())
236 Sml::Renderer& renderer = Sml::Renderer::getInstance();
238 Sml::Texture* renderedComponent =
new Sml::Texture(m_LayoutBounds.width, m_LayoutBounds.height);
240 renderer.pushSetTarget(renderedComponent);
242 Sml::Vec2i savedLayoutPos = m_LayoutBounds.pos;
243 m_LayoutBounds.pos = {0, 0};
244 render(getOriginBounds());
245 m_LayoutBounds.pos = savedLayoutPos;
247 renderer.popTarget();
249 m_Shadow.update(renderedComponent, getLayoutBounds());
251 delete renderedComponent;
254 void Component::renderShadow(
const Sml::Rectangle<int32_t>& targetRegion)
258 m_Shadow.render(getLayoutBounds(), targetRegion);
262 void Component::setSceneInSceneTree(Scene* scene)
Visibility
Used to determine if Component should be rendered and targeted by events.