grid_container.cpp 7.9 KB

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