grid_container.cpp 10 KB

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