grid_container.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. Map<int, int> col_minw; // Max of min_width of all controls in each col (indexed by col).
  35. Map<int, int> row_minh; // Max of min_height of all controls in each row (indexed by row).
  36. Set<int> col_expanded; // Columns which have the SIZE_EXPAND flag set.
  37. Set<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 (Set<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 (Set<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_expand = col_expanded.size() > 0 ? remaining_space.width / col_expanded.size() : 0;
  131. int row_expand = row_expanded.size() > 0 ? remaining_space.height / row_expanded.size() : 0;
  132. bool rtl = is_layout_rtl();
  133. int col_ofs = 0;
  134. int row_ofs = 0;
  135. valid_controls_index = 0;
  136. for (int i = 0; i < get_child_count(); i++) {
  137. Control *c = Object::cast_to<Control>(get_child(i));
  138. if (!c || !c->is_visible_in_tree()) {
  139. continue;
  140. }
  141. int row = valid_controls_index / columns;
  142. int col = valid_controls_index % columns;
  143. valid_controls_index++;
  144. if (col == 0) {
  145. if (rtl) {
  146. col_ofs = get_size().width;
  147. } else {
  148. col_ofs = 0;
  149. }
  150. if (row > 0) {
  151. row_ofs += (row_expanded.has(row - 1) ? row_expand : row_minh[row - 1]) + vsep;
  152. }
  153. }
  154. if (rtl) {
  155. Size2 s(col_expanded.has(col) ? col_expand : col_minw[col], row_expanded.has(row) ? row_expand : row_minh[row]);
  156. Point2 p(col_ofs - s.width, row_ofs);
  157. fit_child_in_rect(c, Rect2(p, s));
  158. col_ofs -= s.width + hsep;
  159. } else {
  160. Point2 p(col_ofs, row_ofs);
  161. Size2 s(col_expanded.has(col) ? col_expand : col_minw[col], row_expanded.has(row) ? row_expand : row_minh[row]);
  162. fit_child_in_rect(c, Rect2(p, s));
  163. col_ofs += s.width + hsep;
  164. }
  165. }
  166. } break;
  167. case NOTIFICATION_THEME_CHANGED: {
  168. update_minimum_size();
  169. } break;
  170. case NOTIFICATION_TRANSLATION_CHANGED:
  171. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  172. queue_sort();
  173. } break;
  174. }
  175. }
  176. void GridContainer::set_columns(int p_columns) {
  177. ERR_FAIL_COND(p_columns < 1);
  178. columns = p_columns;
  179. queue_sort();
  180. update_minimum_size();
  181. }
  182. int GridContainer::get_columns() const {
  183. return columns;
  184. }
  185. void GridContainer::_bind_methods() {
  186. ClassDB::bind_method(D_METHOD("set_columns", "columns"), &GridContainer::set_columns);
  187. ClassDB::bind_method(D_METHOD("get_columns"), &GridContainer::get_columns);
  188. ADD_PROPERTY(PropertyInfo(Variant::INT, "columns", PROPERTY_HINT_RANGE, "1,1024,1"), "set_columns", "get_columns");
  189. }
  190. Size2 GridContainer::get_minimum_size() const {
  191. Map<int, int> col_minw;
  192. Map<int, int> row_minh;
  193. int hsep = get_theme_constant(SNAME("h_separation"));
  194. int vsep = get_theme_constant(SNAME("v_separation"));
  195. int max_row = 0;
  196. int max_col = 0;
  197. int valid_controls_index = 0;
  198. for (int i = 0; i < get_child_count(); i++) {
  199. Control *c = Object::cast_to<Control>(get_child(i));
  200. if (!c || !c->is_visible()) {
  201. continue;
  202. }
  203. int row = valid_controls_index / columns;
  204. int col = valid_controls_index % columns;
  205. valid_controls_index++;
  206. Size2i ms = c->get_combined_minimum_size();
  207. if (col_minw.has(col)) {
  208. col_minw[col] = MAX(col_minw[col], ms.width);
  209. } else {
  210. col_minw[col] = ms.width;
  211. }
  212. if (row_minh.has(row)) {
  213. row_minh[row] = MAX(row_minh[row], ms.height);
  214. } else {
  215. row_minh[row] = ms.height;
  216. }
  217. max_col = MAX(col, max_col);
  218. max_row = MAX(row, max_row);
  219. }
  220. Size2 ms;
  221. for (const KeyValue<int, int> &E : col_minw) {
  222. ms.width += E.value;
  223. }
  224. for (const KeyValue<int, int> &E : row_minh) {
  225. ms.height += E.value;
  226. }
  227. ms.height += vsep * max_row;
  228. ms.width += hsep * max_col;
  229. return ms;
  230. }
  231. GridContainer::GridContainer() {}