grid_container.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*************************************************************************/
  2. /* grid_container.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_constant("hseparation");
  39. int vsep = get_constant("vseparation");
  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. int row = valid_controls_index / columns;
  49. int col = valid_controls_index % columns;
  50. valid_controls_index++;
  51. Size2i ms = c->get_combined_minimum_size();
  52. if (col_minw.has(col))
  53. col_minw[col] = MAX(col_minw[col], ms.width);
  54. else
  55. col_minw[col] = ms.width;
  56. if (row_minh.has(row))
  57. row_minh[row] = MAX(row_minh[row], ms.height);
  58. else
  59. row_minh[row] = ms.height;
  60. if (c->get_h_size_flags() & SIZE_EXPAND) {
  61. col_expanded.insert(col);
  62. }
  63. if (c->get_v_size_flags() & SIZE_EXPAND) {
  64. row_expanded.insert(row);
  65. }
  66. }
  67. // Consider all empty columns expanded.
  68. for (int i = valid_controls_index; i < columns; i++) {
  69. col_expanded.insert(i);
  70. }
  71. // Evaluate the remaining space for expanded columns/rows.
  72. Size2 remaining_space = get_size();
  73. for (Map<int, int>::Element *E = col_minw.front(); E; E = E->next()) {
  74. if (!col_expanded.has(E->key()))
  75. remaining_space.width -= E->get();
  76. }
  77. for (Map<int, int>::Element *E = row_minh.front(); E; E = E->next()) {
  78. if (!row_expanded.has(E->key()))
  79. remaining_space.height -= E->get();
  80. }
  81. remaining_space.height -= vsep * MAX(max_row - 1, 0);
  82. remaining_space.width -= hsep * MAX(max_col - 1, 0);
  83. bool can_fit = false;
  84. while (!can_fit && col_expanded.size() > 0) {
  85. // Check if all minwidth constraints are OK if we use the remaining space.
  86. can_fit = true;
  87. int max_index = col_expanded.front()->get();
  88. for (Set<int>::Element *E = col_expanded.front(); E; E = E->next()) {
  89. if (col_minw[E->get()] > col_minw[max_index]) {
  90. max_index = E->get();
  91. }
  92. if (can_fit && (remaining_space.width / col_expanded.size()) < col_minw[E->get()]) {
  93. can_fit = false;
  94. }
  95. }
  96. // If not, the column with maximum minwidth is not expanded.
  97. if (!can_fit) {
  98. col_expanded.erase(max_index);
  99. remaining_space.width -= col_minw[max_index];
  100. }
  101. }
  102. can_fit = false;
  103. while (!can_fit && row_expanded.size() > 0) {
  104. // Check if all minheight constraints are OK if we use the remaining space.
  105. can_fit = true;
  106. int max_index = row_expanded.front()->get();
  107. for (Set<int>::Element *E = row_expanded.front(); E; E = E->next()) {
  108. if (row_minh[E->get()] > row_minh[max_index]) {
  109. max_index = E->get();
  110. }
  111. if (can_fit && (remaining_space.height / row_expanded.size()) < row_minh[E->get()]) {
  112. can_fit = false;
  113. }
  114. }
  115. // If not, the row with maximum minheight is not expanded.
  116. if (!can_fit) {
  117. row_expanded.erase(max_index);
  118. remaining_space.height -= row_minh[max_index];
  119. }
  120. }
  121. // Finally, fit the nodes.
  122. int col_expand = col_expanded.size() > 0 ? remaining_space.width / col_expanded.size() : 0;
  123. int row_expand = row_expanded.size() > 0 ? remaining_space.height / row_expanded.size() : 0;
  124. int col_ofs = 0;
  125. int row_ofs = 0;
  126. valid_controls_index = 0;
  127. for (int i = 0; i < get_child_count(); i++) {
  128. Control *c = Object::cast_to<Control>(get_child(i));
  129. if (!c || !c->is_visible_in_tree())
  130. continue;
  131. int row = valid_controls_index / columns;
  132. int col = valid_controls_index % columns;
  133. valid_controls_index++;
  134. if (col == 0) {
  135. col_ofs = 0;
  136. if (row > 0)
  137. row_ofs += (row_expanded.has(row - 1) ? row_expand : row_minh[row - 1]) + vsep;
  138. }
  139. Point2 p(col_ofs, row_ofs);
  140. Size2 s(col_expanded.has(col) ? col_expand : col_minw[col], row_expanded.has(row) ? row_expand : row_minh[row]);
  141. fit_child_in_rect(c, Rect2(p, s));
  142. col_ofs += s.width + hsep;
  143. }
  144. } break;
  145. case NOTIFICATION_THEME_CHANGED: {
  146. minimum_size_changed();
  147. } break;
  148. }
  149. }
  150. void GridContainer::set_columns(int p_columns) {
  151. ERR_FAIL_COND(p_columns < 1);
  152. columns = p_columns;
  153. queue_sort();
  154. minimum_size_changed();
  155. }
  156. int GridContainer::get_columns() const {
  157. return columns;
  158. }
  159. void GridContainer::_bind_methods() {
  160. ClassDB::bind_method(D_METHOD("set_columns", "columns"), &GridContainer::set_columns);
  161. ClassDB::bind_method(D_METHOD("get_columns"), &GridContainer::get_columns);
  162. ADD_PROPERTY(PropertyInfo(Variant::INT, "columns", PROPERTY_HINT_RANGE, "1,1024,1"), "set_columns", "get_columns");
  163. }
  164. Size2 GridContainer::get_minimum_size() const {
  165. Map<int, int> col_minw;
  166. Map<int, int> row_minh;
  167. int hsep = get_constant("hseparation");
  168. int vsep = get_constant("vseparation");
  169. int max_row = 0;
  170. int max_col = 0;
  171. int valid_controls_index = 0;
  172. for (int i = 0; i < get_child_count(); i++) {
  173. Control *c = Object::cast_to<Control>(get_child(i));
  174. if (!c || !c->is_visible())
  175. continue;
  176. int row = valid_controls_index / columns;
  177. int col = valid_controls_index % columns;
  178. valid_controls_index++;
  179. Size2i ms = c->get_combined_minimum_size();
  180. if (col_minw.has(col))
  181. col_minw[col] = MAX(col_minw[col], ms.width);
  182. else
  183. col_minw[col] = ms.width;
  184. if (row_minh.has(row))
  185. row_minh[row] = MAX(row_minh[row], ms.height);
  186. else
  187. row_minh[row] = ms.height;
  188. max_col = MAX(col, max_col);
  189. max_row = MAX(row, max_row);
  190. }
  191. Size2 ms;
  192. for (Map<int, int>::Element *E = col_minw.front(); E; E = E->next()) {
  193. ms.width += E->get();
  194. }
  195. for (Map<int, int>::Element *E = row_minh.front(); E; E = E->next()) {
  196. ms.height += E->get();
  197. }
  198. ms.height += vsep * max_row;
  199. ms.width += hsep * max_col;
  200. return ms;
  201. }
  202. GridContainer::GridContainer() {
  203. set_mouse_filter(MOUSE_FILTER_PASS);
  204. columns = 1;
  205. }