grid_container.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*************************************************************************/
  2. /* grid_container.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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;
  35. Map<int, int> row_minh;
  36. Set<int> col_expanded;
  37. Set<int> row_expanded;
  38. int hsep = get_constant("hseparation");
  39. int vsep = get_constant("vseparation");
  40. int idx = 0;
  41. int max_row = 0;
  42. int max_col = 0;
  43. Size2 size = get_size();
  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 = idx / columns;
  49. int col = idx % columns;
  50. Size2i ms = c->get_combined_minimum_size();
  51. if (col_minw.has(col))
  52. col_minw[col] = MAX(col_minw[col], ms.width);
  53. else
  54. col_minw[col] = ms.width;
  55. if (row_minh.has(row))
  56. row_minh[row] = MAX(row_minh[row], ms.height);
  57. else
  58. row_minh[row] = ms.height;
  59. //print_line("store row "+itos(row)+" mw "+itos(ms.height));
  60. if (c->get_h_size_flags() & SIZE_EXPAND)
  61. col_expanded.insert(col);
  62. if (c->get_v_size_flags() & SIZE_EXPAND)
  63. row_expanded.insert(row);
  64. max_col = MAX(col, max_col);
  65. max_row = MAX(row, max_row);
  66. idx++;
  67. }
  68. Size2 ms;
  69. int expand_rows = 0;
  70. int expand_cols = 0;
  71. for (Map<int, int>::Element *E = col_minw.front(); E; E = E->next()) {
  72. ms.width += E->get();
  73. if (col_expanded.has(E->key()))
  74. expand_cols++;
  75. }
  76. for (Map<int, int>::Element *E = row_minh.front(); E; E = E->next()) {
  77. ms.height += E->get();
  78. if (row_expanded.has(E->key()))
  79. expand_rows++;
  80. }
  81. ms.height += vsep * max_row;
  82. ms.width += hsep * max_col;
  83. int row_expand = expand_rows ? (size.y - ms.y) / expand_rows : 0;
  84. int col_expand = expand_cols ? (size.x - ms.x) / expand_cols : 0;
  85. int col_ofs = 0;
  86. int row_ofs = 0;
  87. idx = 0;
  88. for (int i = 0; i < get_child_count(); i++) {
  89. Control *c = Object::cast_to<Control>(get_child(i));
  90. if (!c || !c->is_visible_in_tree())
  91. continue;
  92. int row = idx / columns;
  93. int col = idx % columns;
  94. if (col == 0) {
  95. col_ofs = 0;
  96. if (row > 0 && row_minh.has(row - 1))
  97. row_ofs += row_minh[row - 1] + vsep + (row_expanded.has(row - 1) ? row_expand : 0);
  98. }
  99. Size2 s;
  100. if (col_minw.has(col))
  101. s.width = col_minw[col];
  102. if (row_minh.has(row))
  103. s.height = row_minh[row];
  104. if (row_expanded.has(row))
  105. s.height += row_expand;
  106. if (col_expanded.has(col))
  107. s.width += col_expand;
  108. Point2 p(col_ofs, row_ofs);
  109. //print_line("col: "+itos(col)+" row: "+itos(row)+" col_ofs: "+itos(col_ofs)+" row_ofs: "+itos(row_ofs));
  110. fit_child_in_rect(c, Rect2(p, s));
  111. //print_line("col: "+itos(col)+" row: "+itos(row)+" rect: "+Rect2(p,s));
  112. if (col_minw.has(col)) {
  113. col_ofs += col_minw[col] + hsep + (col_expanded.has(col) ? col_expand : 0);
  114. }
  115. idx++;
  116. }
  117. } break;
  118. }
  119. }
  120. void GridContainer::set_columns(int p_columns) {
  121. ERR_FAIL_COND(p_columns < 1);
  122. columns = p_columns;
  123. queue_sort();
  124. minimum_size_changed();
  125. }
  126. int GridContainer::get_columns() const {
  127. return columns;
  128. }
  129. void GridContainer::_bind_methods() {
  130. ClassDB::bind_method(D_METHOD("set_columns", "columns"), &GridContainer::set_columns);
  131. ClassDB::bind_method(D_METHOD("get_columns"), &GridContainer::get_columns);
  132. ADD_PROPERTY(PropertyInfo(Variant::INT, "columns", PROPERTY_HINT_RANGE, "1,1024,1"), "set_columns", "get_columns");
  133. }
  134. Size2 GridContainer::get_minimum_size() const {
  135. Map<int, int> col_minw;
  136. Map<int, int> row_minh;
  137. int hsep = get_constant("hseparation");
  138. int vsep = get_constant("vseparation");
  139. int idx = 0;
  140. int max_row = 0;
  141. int max_col = 0;
  142. for (int i = 0; i < get_child_count(); i++) {
  143. Control *c = Object::cast_to<Control>(get_child(i));
  144. if (!c || !c->is_visible_in_tree())
  145. continue;
  146. int row = idx / columns;
  147. int col = idx % columns;
  148. Size2i ms = c->get_combined_minimum_size();
  149. if (col_minw.has(col))
  150. col_minw[col] = MAX(col_minw[col], ms.width);
  151. else
  152. col_minw[col] = ms.width;
  153. if (row_minh.has(row))
  154. row_minh[row] = MAX(row_minh[row], ms.height);
  155. else
  156. row_minh[row] = ms.height;
  157. max_col = MAX(col, max_col);
  158. max_row = MAX(row, max_row);
  159. idx++;
  160. }
  161. Size2 ms;
  162. for (Map<int, int>::Element *E = col_minw.front(); E; E = E->next()) {
  163. ms.width += E->get();
  164. }
  165. for (Map<int, int>::Element *E = row_minh.front(); E; E = E->next()) {
  166. ms.height += E->get();
  167. }
  168. ms.height += vsep * max_row;
  169. ms.width += hsep * max_col;
  170. return ms;
  171. }
  172. GridContainer::GridContainer() {
  173. set_mouse_filter(MOUSE_FILTER_PASS);
  174. columns = 1;
  175. }