grid_container.cpp 10 KB

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