grid_container.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. #include "core/templates/rb_set.h"
  32. void GridContainer::_notification(int p_what) {
  33. switch (p_what) {
  34. case NOTIFICATION_SORT_CHILDREN: {
  35. RBMap<int, int> col_minw; // Max of min_width of all controls in each col (indexed by col).
  36. RBMap<int, int> row_minh; // Max of min_height of all controls in each row (indexed by row).
  37. RBSet<int> col_expanded; // Columns which have the SIZE_EXPAND flag set.
  38. RBSet<int> row_expanded; // Rows which have the SIZE_EXPAND flag set.
  39. int hsep = get_theme_constant(SNAME("h_separation"));
  40. int vsep = get_theme_constant(SNAME("v_separation"));
  41. // Compute the per-column/per-row data.
  42. int valid_controls_index = 0;
  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. }
  48. if (c->is_set_as_top_level()) {
  49. continue;
  50. }
  51. int row = valid_controls_index / columns;
  52. int col = valid_controls_index % columns;
  53. valid_controls_index++;
  54. Size2i ms = c->get_combined_minimum_size();
  55. if (col_minw.has(col)) {
  56. col_minw[col] = MAX(col_minw[col], ms.width);
  57. } else {
  58. col_minw[col] = ms.width;
  59. }
  60. if (row_minh.has(row)) {
  61. row_minh[row] = MAX(row_minh[row], ms.height);
  62. } else {
  63. row_minh[row] = ms.height;
  64. }
  65. if (c->get_h_size_flags() & SIZE_EXPAND) {
  66. col_expanded.insert(col);
  67. }
  68. if (c->get_v_size_flags() & SIZE_EXPAND) {
  69. row_expanded.insert(row);
  70. }
  71. }
  72. int max_col = MIN(valid_controls_index, columns);
  73. int max_row = ceil((float)valid_controls_index / (float)columns);
  74. // Consider all empty columns expanded.
  75. for (int i = valid_controls_index; i < columns; i++) {
  76. col_expanded.insert(i);
  77. }
  78. // Evaluate the remaining space for expanded columns/rows.
  79. Size2 remaining_space = get_size();
  80. for (const KeyValue<int, int> &E : col_minw) {
  81. if (!col_expanded.has(E.key)) {
  82. remaining_space.width -= E.value;
  83. }
  84. }
  85. for (const KeyValue<int, int> &E : row_minh) {
  86. if (!row_expanded.has(E.key)) {
  87. remaining_space.height -= E.value;
  88. }
  89. }
  90. remaining_space.height -= vsep * MAX(max_row - 1, 0);
  91. remaining_space.width -= hsep * MAX(max_col - 1, 0);
  92. bool can_fit = false;
  93. while (!can_fit && col_expanded.size() > 0) {
  94. // Check if all minwidth constraints are OK if we use the remaining space.
  95. can_fit = true;
  96. int max_index = col_expanded.front()->get();
  97. for (const int &E : col_expanded) {
  98. if (col_minw[E] > col_minw[max_index]) {
  99. max_index = E;
  100. }
  101. if (can_fit && (remaining_space.width / col_expanded.size()) < col_minw[E]) {
  102. can_fit = false;
  103. }
  104. }
  105. // If not, the column with maximum minwidth is not expanded.
  106. if (!can_fit) {
  107. col_expanded.erase(max_index);
  108. remaining_space.width -= col_minw[max_index];
  109. }
  110. }
  111. can_fit = false;
  112. while (!can_fit && row_expanded.size() > 0) {
  113. // Check if all minheight constraints are OK if we use the remaining space.
  114. can_fit = true;
  115. int max_index = row_expanded.front()->get();
  116. for (const int &E : row_expanded) {
  117. if (row_minh[E] > row_minh[max_index]) {
  118. max_index = E;
  119. }
  120. if (can_fit && (remaining_space.height / row_expanded.size()) < row_minh[E]) {
  121. can_fit = false;
  122. }
  123. }
  124. // If not, the row with maximum minheight is not expanded.
  125. if (!can_fit) {
  126. row_expanded.erase(max_index);
  127. remaining_space.height -= row_minh[max_index];
  128. }
  129. }
  130. // Finally, fit the nodes.
  131. int col_remaining_pixel = 0;
  132. int col_expand = 0;
  133. if (col_expanded.size() > 0) {
  134. col_expand = remaining_space.width / col_expanded.size();
  135. col_remaining_pixel = remaining_space.width - col_expanded.size() * col_expand;
  136. }
  137. int row_remaining_pixel = 0;
  138. int row_expand = 0;
  139. if (row_expanded.size() > 0) {
  140. row_expand = remaining_space.height / row_expanded.size();
  141. row_remaining_pixel = remaining_space.height - row_expanded.size() * row_expand;
  142. }
  143. bool rtl = is_layout_rtl();
  144. int col_ofs = 0;
  145. int row_ofs = 0;
  146. // Calculate the index of rows and columns that receive the remaining pixel.
  147. int col_remaining_pixel_index = 0;
  148. for (int i = 0; i < max_col; i++) {
  149. if (col_remaining_pixel == 0) {
  150. break;
  151. }
  152. if (col_expanded.has(i)) {
  153. col_remaining_pixel_index = i + 1;
  154. col_remaining_pixel--;
  155. }
  156. }
  157. int row_remaining_pixel_index = 0;
  158. for (int i = 0; i < max_row; i++) {
  159. if (row_remaining_pixel == 0) {
  160. break;
  161. }
  162. if (row_expanded.has(i)) {
  163. row_remaining_pixel_index = i + 1;
  164. row_remaining_pixel--;
  165. }
  166. }
  167. valid_controls_index = 0;
  168. for (int i = 0; i < get_child_count(); i++) {
  169. Control *c = Object::cast_to<Control>(get_child(i));
  170. if (!c || !c->is_visible_in_tree()) {
  171. continue;
  172. }
  173. int row = valid_controls_index / columns;
  174. int col = valid_controls_index % columns;
  175. valid_controls_index++;
  176. if (col == 0) {
  177. if (rtl) {
  178. col_ofs = get_size().width;
  179. } else {
  180. col_ofs = 0;
  181. }
  182. if (row > 0) {
  183. row_ofs += (row_expanded.has(row - 1) ? row_expand : row_minh[row - 1]) + vsep;
  184. if (row_expanded.has(row - 1) && row - 1 < row_remaining_pixel_index) {
  185. // Apply the remaining pixel of the previous row.
  186. row_ofs++;
  187. }
  188. }
  189. }
  190. Size2 s(col_expanded.has(col) ? col_expand : col_minw[col], row_expanded.has(row) ? row_expand : row_minh[row]);
  191. // Add the remaining pixel to the expanding columns and rows, starting from left and top.
  192. if (col_expanded.has(col) && col < col_remaining_pixel_index) {
  193. s.x++;
  194. }
  195. if (row_expanded.has(row) && row < row_remaining_pixel_index) {
  196. s.y++;
  197. }
  198. if (rtl) {
  199. Point2 p(col_ofs - s.width, row_ofs);
  200. fit_child_in_rect(c, Rect2(p, s));
  201. col_ofs -= s.width + hsep;
  202. } else {
  203. Point2 p(col_ofs, row_ofs);
  204. fit_child_in_rect(c, Rect2(p, s));
  205. col_ofs += s.width + hsep;
  206. }
  207. }
  208. } break;
  209. case NOTIFICATION_THEME_CHANGED: {
  210. update_minimum_size();
  211. } break;
  212. case NOTIFICATION_TRANSLATION_CHANGED:
  213. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  214. queue_sort();
  215. } break;
  216. }
  217. }
  218. void GridContainer::set_columns(int p_columns) {
  219. ERR_FAIL_COND(p_columns < 1);
  220. if (columns == p_columns) {
  221. return;
  222. }
  223. columns = p_columns;
  224. queue_sort();
  225. update_minimum_size();
  226. }
  227. int GridContainer::get_columns() const {
  228. return columns;
  229. }
  230. void GridContainer::_bind_methods() {
  231. ClassDB::bind_method(D_METHOD("set_columns", "columns"), &GridContainer::set_columns);
  232. ClassDB::bind_method(D_METHOD("get_columns"), &GridContainer::get_columns);
  233. ADD_PROPERTY(PropertyInfo(Variant::INT, "columns", PROPERTY_HINT_RANGE, "1,1024,1"), "set_columns", "get_columns");
  234. }
  235. Size2 GridContainer::get_minimum_size() const {
  236. RBMap<int, int> col_minw;
  237. RBMap<int, int> row_minh;
  238. int hsep = get_theme_constant(SNAME("h_separation"));
  239. int vsep = get_theme_constant(SNAME("v_separation"));
  240. int max_row = 0;
  241. int max_col = 0;
  242. int valid_controls_index = 0;
  243. for (int i = 0; i < get_child_count(); i++) {
  244. Control *c = Object::cast_to<Control>(get_child(i));
  245. if (!c || !c->is_visible()) {
  246. continue;
  247. }
  248. int row = valid_controls_index / columns;
  249. int col = valid_controls_index % columns;
  250. valid_controls_index++;
  251. Size2i ms = c->get_combined_minimum_size();
  252. if (col_minw.has(col)) {
  253. col_minw[col] = MAX(col_minw[col], ms.width);
  254. } else {
  255. col_minw[col] = ms.width;
  256. }
  257. if (row_minh.has(row)) {
  258. row_minh[row] = MAX(row_minh[row], ms.height);
  259. } else {
  260. row_minh[row] = ms.height;
  261. }
  262. max_col = MAX(col, max_col);
  263. max_row = MAX(row, max_row);
  264. }
  265. Size2 ms;
  266. for (const KeyValue<int, int> &E : col_minw) {
  267. ms.width += E.value;
  268. }
  269. for (const KeyValue<int, int> &E : row_minh) {
  270. ms.height += E.value;
  271. }
  272. ms.height += vsep * max_row;
  273. ms.width += hsep * max_col;
  274. return ms;
  275. }
  276. GridContainer::GridContainer() {}