Debian Codegraph @

scene/gui/aspect_ratio_container.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* aspect_ratio_container.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "aspect_ratio_container.h"
32
33 #include "scene/gui/texture_rect.h"
34
35 Size2 AspectRatioContainer::get_minimum_size() const {
36 Size2 ms;
37 for (int i = 0; i < get_child_count(); i++) {
38 Control *c = as_sortable_control(get_child(i), SortableVisibilityMode::VISIBLE);
39 if (!c) {
40 continue;
41 }
42 Size2 minsize = c->get_combined_minimum_size();
43 ms = ms.max(minsize);
44 }
45 return ms;
46 }
47
48 void AspectRatioContainer::set_ratio(float p_ratio) {
49 if (ratio == p_ratio) {
50 return;
51 }
52 ratio = p_ratio;
53 queue_sort();
54 }
55
56 void AspectRatioContainer::set_stretch_mode(StretchMode p_mode) {
57 if (stretch_mode == p_mode) {
58 return;
59 }
60 stretch_mode = p_mode;
61 queue_sort();
62 }
63
64 void AspectRatioContainer::set_alignment_horizontal(AlignmentMode p_alignment_horizontal) {
65 if (alignment_horizontal == p_alignment_horizontal) {
66 return;
67 }
68 alignment_horizontal = p_alignment_horizontal;
69 queue_sort();
70 }
71
72 void AspectRatioContainer::set_alignment_vertical(AlignmentMode p_alignment_vertical) {
73 if (alignment_vertical == p_alignment_vertical) {
74 return;
75 }
76 alignment_vertical = p_alignment_vertical;
77 queue_sort();
78 }
79
80 Vector<int> AspectRatioContainer::get_allowed_size_flags_horizontal() const {
81 Vector<int> flags;
82 flags.append(SIZE_FILL);
83 flags.append(SIZE_SHRINK_BEGIN);
84 flags.append(SIZE_SHRINK_CENTER);
85 flags.append(SIZE_SHRINK_END);
86 return flags;
87 }
88
89 Vector<int> AspectRatioContainer::get_allowed_size_flags_vertical() const {
90 Vector<int> flags;
91 flags.append(SIZE_FILL);
92 flags.append(SIZE_SHRINK_BEGIN);
93 flags.append(SIZE_SHRINK_CENTER);
94 flags.append(SIZE_SHRINK_END);
95 return flags;
96 }
97
98 void AspectRatioContainer::_notification(int p_what) {
99 switch (p_what) {
100 case NOTIFICATION_SORT_CHILDREN: {
101 bool rtl = is_layout_rtl();
102 Size2 size = get_size();
103 for (int i = 0; i < get_child_count(); i++) {
104 Control *c = as_sortable_control(get_child(i));
105 if (!c) {
106 continue;
107 }
108
109 // Temporary fix for editor crash.
110 TextureRect *trect = Object::cast_to<TextureRect>(c);
111 if (trect) {
112 if (trect->get_expand_mode() == TextureRect::EXPAND_FIT_WIDTH_PROPORTIONAL || trect->get_expand_mode() == TextureRect::EXPAND_FIT_HEIGHT_PROPORTIONAL) {
113 WARN_PRINT_ONCE("Proportional TextureRect is currently not supported inside AspectRatioContainer");
114 continue;
115 }
116 }
117
118 Size2 child_minsize = c->get_combined_minimum_size();
119 Size2 child_size = Size2(ratio, 1.0);
120 float scale_factor = 1.0;
121
122 switch (stretch_mode) {
123 case STRETCH_WIDTH_CONTROLS_HEIGHT: {
124 scale_factor = size.x / child_size.x;
125 } break;
126 case STRETCH_HEIGHT_CONTROLS_WIDTH: {
127 scale_factor = size.y / child_size.y;
128 } break;
129 case STRETCH_FIT: {
130 scale_factor = MIN(size.x / child_size.x, size.y / child_size.y);
131 } break;
132 case STRETCH_COVER: {
133 scale_factor = MAX(size.x / child_size.x, size.y / child_size.y);
134 } break;
135 }
136 child_size *= scale_factor;
137 child_size = child_size.max(child_minsize);
138
139 float align_x = 0.5;
140 switch (alignment_horizontal) {
141 case ALIGNMENT_BEGIN: {
142 align_x = 0.0;
143 } break;
144 case ALIGNMENT_CENTER: {
145 align_x = 0.5;
146 } break;
147 case ALIGNMENT_END: {
148 align_x = 1.0;
149 } break;
150 }
151 float align_y = 0.5;
152 switch (alignment_vertical) {
153 case ALIGNMENT_BEGIN: {
154 align_y = 0.0;
155 } break;
156 case ALIGNMENT_CENTER: {
157 align_y = 0.5;
158 } break;
159 case ALIGNMENT_END: {
160 align_y = 1.0;
161 } break;
162 }
163 Vector2 offset = (size - child_size) * Vector2(align_x, align_y);
164
165 if (rtl) {
166 fit_child_in_rect(c, Rect2(Vector2(size.x - offset.x - child_size.x, offset.y), child_size));
167 } else {
168 fit_child_in_rect(c, Rect2(offset, child_size));
169 }
170 }
171 } break;
172 }
173 }
174
175 void AspectRatioContainer::_bind_methods() {
176 ClassDB::bind_method(D_METHOD("set_ratio", "ratio"), &AspectRatioContainer::set_ratio);
177 ClassDB::bind_method(D_METHOD("get_ratio"), &AspectRatioContainer::get_ratio);
178
179 ClassDB::bind_method(D_METHOD("set_stretch_mode", "stretch_mode"), &AspectRatioContainer::set_stretch_mode);
180 ClassDB::bind_method(D_METHOD("get_stretch_mode"), &AspectRatioContainer::get_stretch_mode);
181
182 ClassDB::bind_method(D_METHOD("set_alignment_horizontal", "alignment_horizontal"), &AspectRatioContainer::set_alignment_horizontal);
183 ClassDB::bind_method(D_METHOD("get_alignment_horizontal"), &AspectRatioContainer::get_alignment_horizontal);
184
185 ClassDB::bind_method(D_METHOD("set_alignment_vertical", "alignment_vertical"), &AspectRatioContainer::set_alignment_vertical);
186 ClassDB::bind_method(D_METHOD("get_alignment_vertical"), &AspectRatioContainer::get_alignment_vertical);
187
188 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "ratio", PROPERTY_HINT_RANGE, "0.001,10.0,0.0001,or_greater"), "set_ratio", "get_ratio");
189 ADD_PROPERTY(PropertyInfo(Variant::INT, "stretch_mode", PROPERTY_HINT_ENUM, "Width Controls Height,Height Controls Width,Fit,Cover"), "set_stretch_mode", "get_stretch_mode");
190
191 ADD_GROUP("Alignment", "alignment_");
192 ADD_PROPERTY(PropertyInfo(Variant::INT, "alignment_horizontal", PROPERTY_HINT_ENUM, "Begin,Center,End"), "set_alignment_horizontal", "get_alignment_horizontal");
193 ADD_PROPERTY(PropertyInfo(Variant::INT, "alignment_vertical", PROPERTY_HINT_ENUM, "Begin,Center,End"), "set_alignment_vertical", "get_alignment_vertical");
194
195 BIND_ENUM_CONSTANT(STRETCH_WIDTH_CONTROLS_HEIGHT);
196 BIND_ENUM_CONSTANT(STRETCH_HEIGHT_CONTROLS_WIDTH);
197 BIND_ENUM_CONSTANT(STRETCH_FIT);
198 BIND_ENUM_CONSTANT(STRETCH_COVER);
199
200 BIND_ENUM_CONSTANT(ALIGNMENT_BEGIN);
201 BIND_ENUM_CONSTANT(ALIGNMENT_CENTER);
202 BIND_ENUM_CONSTANT(ALIGNMENT_END);
203 }