grid_container.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. void GridContainer::_notification(int p_what) {
  32. switch (p_what) {
  33. case NOTIFICATION_SORT_CHILDREN: {
  34. int valid_controls_index;
  35. Map<int, int> col_minw; // max of min_width of all controls in each col (indexed by col)
  36. Map<int, int> row_minh; // max of min_height of all controls in each row (indexed by row)
  37. Set<int> col_expanded; // columns which have the SIZE_EXPAND flag set
  38. Set<int> row_expanded; // rows which have the SIZE_EXPAND flag set
  39. int hsep = get_constant("hseparation");
  40. int vsep = get_constant("vseparation");
  41. int max_col = MIN(get_child_count(), columns);
  42. int max_row = get_child_count() / columns;
  43. // Compute the per-column/per-row data
  44. valid_controls_index = 0;
  45. for (int i = 0; i < get_child_count(); i++) {
  46. Control *c = Object::cast_to<Control>(get_child(i));
  47. if (!c || !c->is_visible_in_tree())
  48. continue;
  49. int row = valid_controls_index / columns;
  50. int col = valid_controls_index % columns;
  51. valid_controls_index++;
  52. Size2i ms = c->get_combined_minimum_size();
  53. if (col_minw.has(col))
  54. col_minw[col] = MAX(col_minw[col], ms.width);
  55. else
  56. col_minw[col] = ms.width;
  57. if (row_minh.has(row))
  58. row_minh[row] = MAX(row_minh[row], ms.height);
  59. else
  60. row_minh[row] = ms.height;
  61. if (c->get_h_size_flags() & SIZE_EXPAND) {
  62. col_expanded.insert(col);
  63. }
  64. if (c->get_v_size_flags() & SIZE_EXPAND) {
  65. row_expanded.insert(row);
  66. }
  67. }
  68. // Evaluate the remaining space for expanded columns/rows
  69. Size2 remaining_space = get_size();
  70. for (Map<int, int>::Element *E = col_minw.front(); E; E = E->next()) {
  71. if (!col_expanded.has(E->key()))
  72. remaining_space.width -= E->get();
  73. }
  74. for (Map<int, int>::Element *E = row_minh.front(); E; E = E->next()) {
  75. if (!row_expanded.has(E->key()))
  76. remaining_space.height -= E->get();
  77. }
  78. remaining_space.height -= vsep * MAX(max_row - 1, 0);
  79. remaining_space.width -= hsep * MAX(max_col - 1, 0);
  80. bool can_fit = false;
  81. while (!can_fit && col_expanded.size() > 0) {
  82. // Check if all minwidth constraints are ok if we use the remaining space
  83. can_fit = true;
  84. int max_index = col_expanded.front()->get();
  85. for (Set<int>::Element *E = col_expanded.front(); E; E = E->next()) {
  86. if (col_minw[E->get()] > col_minw[max_index]) {
  87. max_index = E->get();
  88. }
  89. if (can_fit && (remaining_space.width / col_expanded.size()) < col_minw[E->get()]) {
  90. can_fit = false;
  91. }
  92. }
  93. // If not, the column with maximum minwidth is not expanded
  94. if (!can_fit) {
  95. col_expanded.erase(max_index);
  96. remaining_space.width -= col_minw[max_index];
  97. }
  98. }
  99. can_fit = false;
  100. while (!can_fit && row_expanded.size() > 0) {
  101. // Check if all minwidth constraints are ok if we use the remaining space
  102. can_fit = true;
  103. int max_index = row_expanded.front()->get();
  104. for (Set<int>::Element *E = row_expanded.front(); E; E = E->next()) {
  105. if (row_minh[E->get()] > row_minh[max_index]) {
  106. max_index = E->get();
  107. }
  108. if (can_fit && (remaining_space.height / row_expanded.size()) < row_minh[E->get()]) {
  109. can_fit = false;
  110. }
  111. }
  112. // If not, the row with maximum minwidth is not expanded
  113. if (!can_fit) {
  114. row_expanded.erase(max_index);
  115. remaining_space.height -= row_minh[max_index];
  116. }
  117. }
  118. // Finally, fit the nodes
  119. int col_expand = col_expanded.size() > 0 ? remaining_space.width / col_expanded.size() : 0;
  120. int row_expand = row_expanded.size() > 0 ? remaining_space.height / row_expanded.size() : 0;
  121. int col_ofs = 0;
  122. int row_ofs = 0;
  123. valid_controls_index = 0;
  124. for (int i = 0; i < get_child_count(); i++) {
  125. Control *c = Object::cast_to<Control>(get_child(i));
  126. if (!c || !c->is_visible_in_tree())
  127. continue;
  128. int row = valid_controls_index / columns;
  129. int col = valid_controls_index % columns;
  130. valid_controls_index++;
  131. if (col == 0) {
  132. col_ofs = 0;
  133. if (row > 0)
  134. row_ofs += ((row_expanded.has(row - 1)) ? row_expand : row_minh[row - 1]) + vsep;
  135. }
  136. Point2 p(col_ofs, row_ofs);
  137. Size2 s((col_expanded.has(col)) ? col_expand : col_minw[col], (row_expanded.has(row)) ? row_expand : row_minh[row]);
  138. fit_child_in_rect(c, Rect2(p, s));
  139. col_ofs += s.width + hsep;
  140. }
  141. } break;
  142. }
  143. }
  144. void GridContainer::set_columns(int p_columns) {
  145. ERR_FAIL_COND(p_columns < 1);
  146. columns = p_columns;
  147. queue_sort();
  148. minimum_size_changed();
  149. }
  150. int GridContainer::get_columns() const {
  151. return columns;
  152. }
  153. void GridContainer::_bind_methods() {
  154. ClassDB::bind_method(D_METHOD("set_columns", "columns"), &GridContainer::set_columns);
  155. ClassDB::bind_method(D_METHOD("get_columns"), &GridContainer::get_columns);
  156. ADD_PROPERTY(PropertyInfo(Variant::INT, "columns", PROPERTY_HINT_RANGE, "1,1024,1"), "set_columns", "get_columns");
  157. }
  158. Size2 GridContainer::get_minimum_size() const {
  159. Map<int, int> col_minw;
  160. Map<int, int> row_minh;
  161. int hsep = get_constant("hseparation");
  162. int vsep = get_constant("vseparation");
  163. int max_row = 0;
  164. int max_col = 0;
  165. int valid_controls_index = 0;
  166. for (int i = 0; i < get_child_count(); i++) {
  167. Control *c = Object::cast_to<Control>(get_child(i));
  168. if (!c || !c->is_visible_in_tree())
  169. continue;
  170. int row = valid_controls_index / columns;
  171. int col = valid_controls_index % columns;
  172. valid_controls_index++;
  173. Size2i ms = c->get_combined_minimum_size();
  174. if (col_minw.has(col))
  175. col_minw[col] = MAX(col_minw[col], ms.width);
  176. else
  177. col_minw[col] = ms.width;
  178. if (row_minh.has(row))
  179. row_minh[row] = MAX(row_minh[row], ms.height);
  180. else
  181. row_minh[row] = ms.height;
  182. max_col = MAX(col, max_col);
  183. max_row = MAX(row, max_row);
  184. }
  185. Size2 ms;
  186. for (Map<int, int>::Element *E = col_minw.front(); E; E = E->next()) {
  187. ms.width += E->get();
  188. }
  189. for (Map<int, int>::Element *E = row_minh.front(); E; E = E->next()) {
  190. ms.height += E->get();
  191. }
  192. ms.height += vsep * max_row;
  193. ms.width += hsep * max_col;
  194. return ms;
  195. }
  196. GridContainer::GridContainer() {
  197. set_mouse_filter(MOUSE_FILTER_PASS);
  198. columns = 1;
  199. }