Simple GUI Library
container.cpp
Go to the documentation of this file.
1/**
2 * @author Nikita Mochalov (github.com/tralf-strues)
3 * @file container.cpp
4 * @date 2021-11-07
5 *
6 * @copyright Copyright (c) 2021
7 */
8
10
11namespace Sgl
12{
13 const ColorFill Container::DEFAULT_FILL = ColorFill{Sml::COLOR_WHITE};
14 const Background Container::DEFAULT_BACKGROUND = Background{&Container::DEFAULT_FILL};
15
16 Container::Container() : Parent()
17 {
18 m_Background = &DEFAULT_BACKGROUND;
19 }
20
21 Component* Container::getHitComponent(int32_t x, int32_t y)
22 {
23 if (!isInteractable()) { return nullptr; }
24
25 Component* hitted = Parent::getHitComponent(x, y);
26
27 if (hitted != nullptr)
28 {
29 return hitted;
30 }
31
32 if (Sml::isPointInsideRectangle({x, y}, getLayoutBounds()))
33 {
34 return this;
35 }
36
37 return nullptr;
38 }
39
40 void Container::render(const Sml::Rectangle<int32_t>& targetRegion)
41 {
42 if (!isVisible())
43 {
44 return;
45 }
46
47 renderShadow(targetRegion);
48
49 Sml::Rectangle<int32_t> translatedTargetRegion = getLayoutBounds();
50 translatedTargetRegion.pos += targetRegion.pos;
51
52 if (m_Snapshot != nullptr)
53 {
54 Sml::Rectangle<int32_t> originBounds = getOriginBounds();
55 renderTexture(*m_Snapshot, &translatedTargetRegion, &originBounds);
56 }
57
58 for (Component* child : getChildren())
59 {
60 child->render(translatedTargetRegion);
61 }
62 }
63
64 void Container::prerenderSelf()
65 {
66 if (m_Background != nullptr)
67 {
68 Background::fillArea(m_Background, getOriginBounds());
69 }
70
71 if (m_Border != nullptr)
72 {
73 Border::encloseArea(m_Border, getOriginBounds());
74 }
75 }
76
77 int32_t BlankContainer::computeCustomPrefWidth(int32_t height) const { return 0; }
78 int32_t BlankContainer::computeCustomPrefHeight(int32_t width) const { return 0; }
79}