grid_container.cpp 10 KB

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