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