Simple GUI Library
skin.cpp
Go to the documentation of this file.
1/**
2 * @author Nikita Mochalov (github.com/tralf-strues)
3 * @file skin.cpp
4 * @date 2021-10-29
5 *
6 * @copyright Copyright (c) 2021
7 */
8
9#include "sml/graphics_wrapper/primitives.h"
10#include "scene/style/skin.h"
12#include "scene/utils.h"
13
14namespace Sgl
15{
16 void Skin::prerenderControl() {}
17
18 Component* Skin::getHitComponent(int32_t x, int32_t y)
19 {
20 if (Sml::isPointInsideRectangle({x, y}, getControl()->getLayoutBounds()))
21 {
22 return getModifiableControl();
23 }
24
25 return nullptr;
26 }
27
28 #define CHECK_ENABLED_SIZE(size) if (size != USE_COMPUTED_SIZE) { return size; }
29
30 int32_t Skin::computePrefHeight(int32_t width) const
31 {
32 const Control* control = getControl();
33 assert(control);
34
35 COMPONENT_COMPUTE_DIMENSION(Pref, Height, width, Y, control->getChildrenReadonly(),
36 control->getPrefHeight(),
37 control->getInsets().top + control->getInsets().bottom);
38 }
39
40 int32_t Skin::computePrefWidth(int32_t height) const
41 {
42 const Control* control = getControl();
43 assert(control);
44
45 COMPONENT_COMPUTE_DIMENSION(Pref, Width, height, X, control->getChildrenReadonly(),
46 control->getPrefWidth(),
47 control->getInsets().left + control->getInsets().right);
48 }
49
50 int32_t Skin::computeMinHeight(int32_t width) const
51 {
52 const Control* control = getControl();
53 assert(control);
54
55 COMPONENT_COMPUTE_DIMENSION(Min, Height, width, Y, control->getChildrenReadonly(),
56 control->getMinHeight(),
57 control->getInsets().top + control->getInsets().bottom);
58 }
59
60 int32_t Skin::computeMinWidth(int32_t height) const
61 {
62 const Control* control = getControl();
63 assert(control);
64
65 COMPONENT_COMPUTE_DIMENSION(Min, Width, height, X, control->getChildrenReadonly(),
66 control->getMinWidth(),
67 control->getInsets().left + control->getInsets().right);
68 }
69
70 int32_t Skin::computeMaxHeight(int32_t width) const { return Component::UNLIMITED_SIZE; }
71 int32_t Skin::computeMaxWidth(int32_t height) const { return Component::UNLIMITED_SIZE; }
72
73 void Skin::layoutChildren()
74 {
75 Control* control = getModifiableControl();
76 assert(control);
77
78 for (Component* child : control->getChildren())
79 {
80 child->setLayoutWidth(child->computePrefWidth());
81 child->setLayoutHeight(child->computePrefHeight());
82 }
83 }
84
85 Skin* Skinnable::getSkin()
86 {
87 return m_Skin;
88 }
89
90 void Skinnable::setSkin(Skin* skin)
91 {
92 if (m_Skin != nullptr)
93 {
94 m_Skin->dispose();
95 }
96
97 m_Skin = skin;
98 }
99}
virtual void dispose()=0
Should be called by Control when this skin is replaced.