1 | //************************************ bs::framework - Copyright 2018 Marko Pintera **************************************// |
2 | //*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********// |
3 | #include "GUI/BsGUIDimensions.h" |
4 | #include "GUI/BsGUIElementStyle.h" |
5 | #include "GUI/BsGUIOptions.h" |
6 | |
7 | namespace bs |
8 | { |
9 | GUIDimensions GUIDimensions::create() |
10 | { |
11 | return GUIDimensions(); |
12 | } |
13 | |
14 | GUIDimensions GUIDimensions::create(const GUIOptions& options) |
15 | { |
16 | GUIDimensions dimensions; |
17 | |
18 | for(auto& option : options.mOptions) |
19 | { |
20 | switch(option.type) |
21 | { |
22 | case GUIOption::Type::Position: |
23 | dimensions.x = (INT32)option.min; |
24 | dimensions.y = (INT32)option.max; |
25 | break; |
26 | case GUIOption::Type::FixedWidth: |
27 | dimensions.flags |= GUIDF_FixedWidth | GUIDF_OverWidth; |
28 | dimensions.minWidth = dimensions.maxWidth = option.min; |
29 | break; |
30 | case GUIOption::Type::FixedHeight: |
31 | dimensions.flags |= GUIDF_FixedHeight | GUIDF_OverHeight; |
32 | dimensions.minHeight = dimensions.maxHeight = option.min; |
33 | break; |
34 | case GUIOption::Type::FlexibleWidth: |
35 | dimensions.flags |= GUIDF_OverWidth; |
36 | dimensions.flags &= ~GUIDF_FixedWidth; |
37 | dimensions.minWidth = option.min; |
38 | dimensions.maxWidth = option.max; |
39 | break; |
40 | case GUIOption::Type::FlexibleHeight: |
41 | dimensions.flags |= GUIDF_OverHeight; |
42 | dimensions.flags &= ~GUIDF_FixedHeight; |
43 | dimensions.minHeight = option.min; |
44 | dimensions.maxHeight = option.max; |
45 | break; |
46 | } |
47 | } |
48 | |
49 | return dimensions; |
50 | } |
51 | |
52 | void GUIDimensions::updateWithStyle(const GUIElementStyle* style) |
53 | { |
54 | if(!overridenWidth()) |
55 | { |
56 | if (style->fixedWidth) |
57 | { |
58 | flags |= GUIDF_FixedWidth; |
59 | minWidth = maxWidth = style->width; |
60 | } |
61 | else |
62 | { |
63 | flags &= ~GUIDF_FixedWidth; |
64 | minWidth = style->minWidth; |
65 | maxWidth = style->maxWidth; |
66 | } |
67 | } |
68 | |
69 | if(!overridenHeight()) |
70 | { |
71 | if (style->fixedHeight) |
72 | { |
73 | flags |= GUIDF_FixedHeight; |
74 | minHeight = maxHeight = style->height; |
75 | } |
76 | else |
77 | { |
78 | flags &= ~GUIDF_FixedHeight; |
79 | minHeight = style->minHeight; |
80 | maxHeight = style->maxHeight; |
81 | } |
82 | } |
83 | } |
84 | |
85 | LayoutSizeRange GUIDimensions::calculateSizeRange(const Vector2I& optimal) const |
86 | { |
87 | LayoutSizeRange sizeRange; |
88 | |
89 | if (fixedHeight()) |
90 | { |
91 | sizeRange.optimal.y = std::max(0, (INT32)minHeight); |
92 | sizeRange.min.y = sizeRange.optimal.y; |
93 | sizeRange.max.y = sizeRange.optimal.y; |
94 | } |
95 | else |
96 | { |
97 | sizeRange.optimal.y = optimal.y; |
98 | |
99 | if (minHeight > 0) |
100 | { |
101 | sizeRange.optimal.y = std::max(std::max(0, (INT32)minHeight), sizeRange.optimal.y); |
102 | sizeRange.min.y = std::max(0, (INT32)minHeight); |
103 | } |
104 | |
105 | if (maxHeight > 0) |
106 | { |
107 | sizeRange.optimal.y = std::min(std::max(0, (INT32)maxHeight), sizeRange.optimal.y); |
108 | sizeRange.max.y = std::max(0, (INT32)maxHeight); |
109 | } |
110 | } |
111 | |
112 | if (fixedWidth()) |
113 | { |
114 | sizeRange.optimal.x = std::max(0, (INT32)minWidth); |
115 | sizeRange.min.x = sizeRange.optimal.x; |
116 | sizeRange.max.x = sizeRange.optimal.x; |
117 | } |
118 | else |
119 | { |
120 | sizeRange.optimal.x = optimal.x; |
121 | |
122 | if (minWidth > 0) |
123 | { |
124 | sizeRange.optimal.x = std::max(std::max(0, (INT32)minWidth), sizeRange.optimal.x); |
125 | sizeRange.min.x = std::max(0, (INT32)minWidth); |
126 | } |
127 | |
128 | if (maxWidth > 0) |
129 | { |
130 | sizeRange.optimal.x = std::min(std::max(0, (INT32)maxWidth), sizeRange.optimal.x); |
131 | sizeRange.max.x = std::max(0, (INT32)maxWidth); |
132 | } |
133 | } |
134 | |
135 | return sizeRange; |
136 | } |
137 | } |