1 /*************************************************************************/
2 /*  grid_container.cpp                                                   */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md)    */
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 #include "grid_container.h"
31 
_notification(int p_what)32 void GridContainer::_notification(int p_what) {
33 
34 	switch (p_what) {
35 
36 		case NOTIFICATION_SORT_CHILDREN: {
37 
38 			Map<int, int> col_minw;
39 			Map<int, int> row_minh;
40 			Set<int> col_expanded;
41 			Set<int> row_expanded;
42 
43 			int hsep = get_constant("hseparation");
44 			int vsep = get_constant("vseparation");
45 
46 			int idx = 0;
47 			int max_row = 0;
48 			int max_col = 0;
49 
50 			Size2 size = get_size();
51 
52 			for (int i = 0; i < get_child_count(); i++) {
53 
54 				Control *c = get_child(i)->cast_to<Control>();
55 				if (!c || !c->is_visible())
56 					continue;
57 
58 				int row = idx / columns;
59 				int col = idx % columns;
60 
61 				Size2i ms = c->get_combined_minimum_size();
62 				if (col_minw.has(col))
63 					col_minw[col] = MAX(col_minw[col], ms.width);
64 				else
65 					col_minw[col] = ms.width;
66 
67 				if (row_minh.has(row))
68 					row_minh[row] = MAX(row_minh[row], ms.height);
69 				else
70 					row_minh[row] = ms.height;
71 
72 				//	print_line("store row "+itos(row)+" mw "+itos(ms.height));
73 
74 				if (c->get_h_size_flags() & SIZE_EXPAND)
75 					col_expanded.insert(col);
76 				if (c->get_v_size_flags() & SIZE_EXPAND)
77 					row_expanded.insert(row);
78 
79 				max_col = MAX(col, max_col);
80 				max_row = MAX(row, max_row);
81 				idx++;
82 			}
83 
84 			Size2 ms;
85 			int expand_rows = 0;
86 			int expand_cols = 0;
87 
88 			for (Map<int, int>::Element *E = col_minw.front(); E; E = E->next()) {
89 				ms.width += E->get();
90 				if (col_expanded.has(E->key()))
91 					expand_cols++;
92 			}
93 
94 			for (Map<int, int>::Element *E = row_minh.front(); E; E = E->next()) {
95 				ms.height += E->get();
96 				if (row_expanded.has(E->key()))
97 					expand_rows++;
98 			}
99 
100 			ms.height += vsep * max_row;
101 			ms.width += hsep * max_col;
102 
103 			int row_expand = expand_rows ? (size.y - ms.y) / expand_rows : 0;
104 			int col_expand = expand_cols ? (size.x - ms.x) / expand_cols : 0;
105 
106 			int col_ofs = 0;
107 			int row_ofs = 0;
108 			idx = 0;
109 
110 			for (int i = 0; i < get_child_count(); i++) {
111 
112 				Control *c = get_child(i)->cast_to<Control>();
113 				if (!c || !c->is_visible())
114 					continue;
115 				int row = idx / columns;
116 				int col = idx % columns;
117 
118 				if (col == 0) {
119 					col_ofs = 0;
120 					if (row > 0 && row_minh.has(row - 1))
121 						row_ofs += row_minh[row - 1] + vsep + (row_expanded.has(row - 1) ? row_expand : 0);
122 				}
123 
124 				Size2 s;
125 				if (col_minw.has(col))
126 					s.width = col_minw[col];
127 				if (row_minh.has(row))
128 					s.height = row_minh[row];
129 
130 				if (row_expanded.has(row))
131 					s.height += row_expand;
132 				if (col_expanded.has(col))
133 					s.width += col_expand;
134 
135 				Point2 p(col_ofs, row_ofs);
136 
137 				//				print_line("col: "+itos(col)+" row: "+itos(row)+" col_ofs: "+itos(col_ofs)+" row_ofs: "+itos(row_ofs));
138 				fit_child_in_rect(c, Rect2(p, s));
139 				//print_line("col: "+itos(col)+" row: "+itos(row)+" rect: "+Rect2(p,s));
140 
141 				if (col_minw.has(col)) {
142 					col_ofs += col_minw[col] + hsep + (col_expanded.has(col) ? col_expand : 0);
143 				}
144 
145 				idx++;
146 			}
147 
148 		} break;
149 	}
150 }
151 
set_columns(int p_columns)152 void GridContainer::set_columns(int p_columns) {
153 
154 	ERR_FAIL_COND(p_columns < 1);
155 	columns = p_columns;
156 	queue_sort();
157 	minimum_size_changed();
158 }
159 
get_columns() const160 int GridContainer::get_columns() const {
161 
162 	return columns;
163 }
164 
_bind_methods()165 void GridContainer::_bind_methods() {
166 
167 	ObjectTypeDB::bind_method(_MD("set_columns", "columns"), &GridContainer::set_columns);
168 	ObjectTypeDB::bind_method(_MD("get_columns"), &GridContainer::get_columns);
169 
170 	ADD_PROPERTY(PropertyInfo(Variant::INT, "columns", PROPERTY_HINT_RANGE, "1,1024,1"), _SCS("set_columns"), _SCS("get_columns"));
171 }
172 
get_minimum_size() const173 Size2 GridContainer::get_minimum_size() const {
174 
175 	Map<int, int> col_minw;
176 	Map<int, int> row_minh;
177 
178 	int hsep = get_constant("hseparation");
179 	int vsep = get_constant("vseparation");
180 
181 	int idx = 0;
182 	int max_row = 0;
183 	int max_col = 0;
184 
185 	for (int i = 0; i < get_child_count(); i++) {
186 
187 		Control *c = get_child(i)->cast_to<Control>();
188 		if (!c || !c->is_visible())
189 			continue;
190 		int row = idx / columns;
191 		int col = idx % columns;
192 		Size2i ms = c->get_combined_minimum_size();
193 		if (col_minw.has(col))
194 			col_minw[col] = MAX(col_minw[col], ms.width);
195 		else
196 			col_minw[col] = ms.width;
197 
198 		if (row_minh.has(row))
199 			row_minh[row] = MAX(row_minh[row], ms.height);
200 		else
201 			row_minh[row] = ms.height;
202 		max_col = MAX(col, max_col);
203 		max_row = MAX(row, max_row);
204 		idx++;
205 	}
206 
207 	Size2 ms;
208 
209 	for (Map<int, int>::Element *E = col_minw.front(); E; E = E->next()) {
210 		ms.width += E->get();
211 	}
212 
213 	for (Map<int, int>::Element *E = row_minh.front(); E; E = E->next()) {
214 		ms.height += E->get();
215 	}
216 
217 	ms.height += vsep * max_row;
218 	ms.width += hsep * max_col;
219 
220 	return ms;
221 }
222 
GridContainer()223 GridContainer::GridContainer() {
224 
225 	set_stop_mouse(false);
226 	columns = 1;
227 }
228