Debian Codegraph @

scene/gui/grid_container.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* grid_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 "grid_container.h"
32
33 #include "core/templates/rb_set.h"
34 #include "scene/theme/theme_db.h"
35
36 void GridContainer::_notification(int p_what) {
37 switch (p_what) {
38 case NOTIFICATION_SORT_CHILDREN: {
39 RBMap<int, int> col_minw; // Max of min_width of all controls in each col (indexed by col).
40 RBMap<int, int> row_minh; // Max of min_height of all controls in each row (indexed by row).
41 RBSet<int> col_expanded; // Columns which have the SIZE_EXPAND flag set.
42 RBSet<int> row_expanded; // Rows which have the SIZE_EXPAND flag set.
43
44 // Compute the per-column/per-row data.
45 int valid_controls_index = 0;
46 for (int i = 0; i < get_child_count(); i++) {
47 Control *c = as_sortable_control(get_child(i));
48 if (!c) {
49 continue;
50 }
51
52 int row = valid_controls_index / columns;
53 int col = valid_controls_index % columns;
54 valid_controls_index++;
55
56 Size2i ms = c->get_combined_minimum_size();
57 if (col_minw.has(col)) {
58 col_minw[col] = MAX(col_minw[col], ms.width);
59 } else {
60 col_minw[col] = ms.width;
61 }
62 if (row_minh.has(row)) {
63 row_minh[row] = MAX(row_minh[row], ms.height);
64 } else {
65 row_minh[row] = ms.height;
66 }
67
68 if (c->get_h_size_flags().has_flag(SIZE_EXPAND)) {
69 col_expanded.insert(col);
70 }
71 if (c->get_v_size_flags().has_flag(SIZE_EXPAND)) {
72 row_expanded.insert(row);
73 }
74 }
75
76 int max_col = MIN(valid_controls_index, columns);
77 int max_row = std::ceil((float)valid_controls_index / (float)columns);
78
79 // Consider all empty columns expanded.
80 for (int i = valid_controls_index; i < columns; i++) {
81 col_expanded.insert(i);
82 }
83
84 // Evaluate the remaining space for expanded columns/rows.
85 Size2 remaining_space = get_size();
86 for (const KeyValue<int, int> &E : col_minw) {
87 if (!col_expanded.has(E.key)) {
88 remaining_space.width -= E.value;
89 }
90 }
91
92 for (const KeyValue<int, int> &E : row_minh) {
93 if (!row_expanded.has(E.key)) {
94 remaining_space.height -= E.value;
95 }
96 }
97 remaining_space.height -= theme_cache.v_separation * MAX(max_row - 1, 0);
98 remaining_space.width -= theme_cache.h_separation * MAX(max_col - 1, 0);
99
100 bool can_fit = false;
101 while (!can_fit && col_expanded.size() > 0) {
102 // Check if all minwidth constraints are OK if we use the remaining space.
103 can_fit = true;
104 int max_index = col_expanded.front()->get();
105 for (const int &E : col_expanded) {
106 if (col_minw[E] > col_minw[max_index]) {
107 max_index = E;
108 }
109 if (can_fit && (remaining_space.width / col_expanded.size()) < col_minw[E]) {
110 can_fit = false;
111 }
112 }
113
114 // If not, the column with maximum minwidth is not expanded.
115 if (!can_fit) {
116 col_expanded.erase(max_index);
117 remaining_space.width -= col_minw[max_index];
118 }
119 }
120
121 can_fit = false;
122 while (!can_fit && row_expanded.size() > 0) {
123 // Check if all minheight constraints are OK if we use the remaining space.
124 can_fit = true;
125 int max_index = row_expanded.front()->get();
126 for (const int &E : row_expanded) {
127 if (row_minh[E] > row_minh[max_index]) {
128 max_index = E;
129 }
130 if (can_fit && (remaining_space.height / row_expanded.size()) < row_minh[E]) {
131 can_fit = false;
132 }
133 }
134
135 // If not, the row with maximum minheight is not expanded.
136 if (!can_fit) {
137 row_expanded.erase(max_index);
138 remaining_space.height -= row_minh[max_index];
139 }
140 }
141
142 // Finally, fit the nodes.
143 int col_remaining_pixel = 0;
144 int col_expand = 0;
145 if (col_expanded.size() > 0) {
146 col_expand = remaining_space.width / col_expanded.size();
147 col_remaining_pixel = remaining_space.width - col_expanded.size() * col_expand;
148 }
149
150 int row_remaining_pixel = 0;
151 int row_expand = 0;
152 if (row_expanded.size() > 0) {
153 row_expand = remaining_space.height / row_expanded.size();
154 row_remaining_pixel = remaining_space.height - row_expanded.size() * row_expand;
155 }
156
157 bool rtl = is_layout_rtl();
158
159 int col_ofs = 0;
160 int row_ofs = 0;
161
162 // Calculate the index of rows and columns that receive the remaining pixel.
163 int col_remaining_pixel_index = 0;
164 for (int i = 0; i < max_col; i++) {
165 if (col_remaining_pixel == 0) {
166 break;
167 }
168 if (col_expanded.has(i)) {
169 col_remaining_pixel_index = i + 1;
170 col_remaining_pixel--;
171 }
172 }
173 int row_remaining_pixel_index = 0;
174 for (int i = 0; i < max_row; i++) {
175 if (row_remaining_pixel == 0) {
176 break;
177 }
178 if (row_expanded.has(i)) {
179 row_remaining_pixel_index = i + 1;
180 row_remaining_pixel--;
181 }
182 }
183
184 valid_controls_index = 0;
185 for (int i = 0; i < get_child_count(); i++) {
186 Control *c = as_sortable_control(get_child(i));
187 if (!c) {
188 continue;
189 }
190 int row = valid_controls_index / columns;
191 int col = valid_controls_index % columns;
192 valid_controls_index++;
193
194 if (col == 0) {
195 if (rtl) {
196 col_ofs = get_size().width;
197 } else {
198 col_ofs = 0;
199 }
200 if (row > 0) {
201 row_ofs += (row_expanded.has(row - 1) ? row_expand : row_minh[row - 1]) + theme_cache.v_separation;
202
203 if (row_expanded.has(row - 1) && row - 1 < row_remaining_pixel_index) {
204 // Apply the remaining pixel of the previous row.
205 row_ofs++;
206 }
207 }
208 }
209
210 Size2 s(col_expanded.has(col) ? col_expand : col_minw[col], row_expanded.has(row) ? row_expand : row_minh[row]);
211
212 // Add the remaining pixel to the expanding columns and rows, starting from left and top.
213 if (col_expanded.has(col) && col < col_remaining_pixel_index) {
214 s.x++;
215 }
216 if (row_expanded.has(row) && row < row_remaining_pixel_index) {
217 s.y++;
218 }
219
220 if (rtl) {
221 Point2 p(col_ofs - s.width, row_ofs);
222 fit_child_in_rect(c, Rect2(p, s));
223 col_ofs -= s.width + theme_cache.h_separation;
224 } else {
225 Point2 p(col_ofs, row_ofs);
226 fit_child_in_rect(c, Rect2(p, s));
227 col_ofs += s.width + theme_cache.h_separation;
228 }
229 }
230 } break;
231
232 case NOTIFICATION_THEME_CHANGED: {
233 update_minimum_size();
234 } break;
235
236 case NOTIFICATION_TRANSLATION_CHANGED:
237 case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
238 queue_sort();
239 } break;
240 }
241 }
242
243 void GridContainer::set_columns(int p_columns) {
244 ERR_FAIL_COND(p_columns < 1);
245
246 if (columns == p_columns) {
247 return;
248 }
249
250 columns = p_columns;
251 queue_sort();
252 update_minimum_size();
253 }
254
255 int GridContainer::get_columns() const {
256 return columns;
257 }
258
259 int GridContainer::get_h_separation() const {
260 return theme_cache.h_separation;
261 }
262
263 void GridContainer::_bind_methods() {
264 ClassDB::bind_method(D_METHOD("set_columns", "columns"), &GridContainer::set_columns);
265 ClassDB::bind_method(D_METHOD("get_columns"), &GridContainer::get_columns);
266
267 ADD_PROPERTY(PropertyInfo(Variant::INT, "columns", PROPERTY_HINT_RANGE, "1,1024,1"), "set_columns", "get_columns");
268
269 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, GridContainer, h_separation);
270 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, GridContainer, v_separation);
271 }
272
273 Size2 GridContainer::get_minimum_size() const {
274 RBMap<int, int> col_minw;
275 RBMap<int, int> row_minh;
276
277 int max_row = 0;
278 int max_col = 0;
279
280 int valid_controls_index = 0;
281 for (int i = 0; i < get_child_count(); i++) {
282 Control *c = as_sortable_control(get_child(i), SortableVisibilityMode::VISIBLE);
283 if (!c) {
284 continue;
285 }
286 int row = valid_controls_index / columns;
287 int col = valid_controls_index % columns;
288 valid_controls_index++;
289
290 Size2i ms = c->get_combined_minimum_size();
291 if (col_minw.has(col)) {
292 col_minw[col] = MAX(col_minw[col], ms.width);
293 } else {
294 col_minw[col] = ms.width;
295 }
296
297 if (row_minh.has(row)) {
298 row_minh[row] = MAX(row_minh[row], ms.height);
299 } else {
300 row_minh[row] = ms.height;
301 }
302 max_col = MAX(col, max_col);
303 max_row = MAX(row, max_row);
304 }
305
306 Size2 ms;
307
308 for (const KeyValue<int, int> &E : col_minw) {
309 ms.width += E.value;
310 }
311
312 for (const KeyValue<int, int> &E : row_minh) {
313 ms.height += E.value;
314 }
315
316 ms.height += theme_cache.v_separation * max_row;
317 ms.width += theme_cache.h_separation * max_col;
318
319 return ms;
320 }