flow_container.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*************************************************************************/
  2. /* flow_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 "flow_container.h"
  31. struct _LineData {
  32. int child_count = 0;
  33. int min_line_height = 0;
  34. int min_line_length = 0;
  35. int stretch_avail = 0;
  36. float stretch_ratio_total = 0;
  37. };
  38. void FlowContainer::_resort() {
  39. // Avoid resorting if invisible.
  40. if (!is_visible_in_tree()) {
  41. return;
  42. }
  43. int separation_horizontal = get_theme_constant(SNAME("h_separation"));
  44. int separation_vertical = get_theme_constant(SNAME("v_separation"));
  45. bool rtl = is_layout_rtl();
  46. HashMap<Control *, Size2i> children_minsize_cache;
  47. Vector<_LineData> lines_data;
  48. Vector2i ofs;
  49. int line_height = 0;
  50. int line_length = 0;
  51. float line_stretch_ratio_total = 0;
  52. int current_container_size = vertical ? get_rect().size.y : get_rect().size.x;
  53. int children_in_current_line = 0;
  54. // First pass for line wrapping and minimum size calculation.
  55. for (int i = 0; i < get_child_count(); i++) {
  56. Control *child = Object::cast_to<Control>(get_child(i));
  57. if (!child || !child->is_visible()) {
  58. continue;
  59. }
  60. if (child->is_set_as_top_level()) {
  61. continue;
  62. }
  63. Size2i child_msc = child->get_combined_minimum_size();
  64. if (vertical) { /* VERTICAL */
  65. if (children_in_current_line > 0) {
  66. ofs.y += separation_vertical;
  67. }
  68. if (ofs.y + child_msc.y > current_container_size) {
  69. line_length = ofs.y - separation_vertical;
  70. lines_data.push_back(_LineData{ children_in_current_line, line_height, line_length, current_container_size - line_length, line_stretch_ratio_total });
  71. // Move in new column (vertical line).
  72. ofs.x += line_height + separation_horizontal;
  73. ofs.y = 0;
  74. line_height = 0;
  75. line_stretch_ratio_total = 0;
  76. children_in_current_line = 0;
  77. }
  78. line_height = MAX(line_height, child_msc.x);
  79. if (child->get_v_size_flags() & SIZE_EXPAND) {
  80. line_stretch_ratio_total += child->get_stretch_ratio();
  81. }
  82. ofs.y += child_msc.y;
  83. } else { /* HORIZONTAL */
  84. if (children_in_current_line > 0) {
  85. ofs.x += separation_horizontal;
  86. }
  87. if (ofs.x + child_msc.x > current_container_size) {
  88. line_length = ofs.x - separation_horizontal;
  89. lines_data.push_back(_LineData{ children_in_current_line, line_height, line_length, current_container_size - line_length, line_stretch_ratio_total });
  90. // Move in new line.
  91. ofs.y += line_height + separation_vertical;
  92. ofs.x = 0;
  93. line_height = 0;
  94. line_stretch_ratio_total = 0;
  95. children_in_current_line = 0;
  96. }
  97. line_height = MAX(line_height, child_msc.y);
  98. if (child->get_h_size_flags() & SIZE_EXPAND) {
  99. line_stretch_ratio_total += child->get_stretch_ratio();
  100. }
  101. ofs.x += child_msc.x;
  102. }
  103. children_minsize_cache[child] = child_msc;
  104. children_in_current_line++;
  105. }
  106. line_length = vertical ? (ofs.y) : (ofs.x);
  107. lines_data.push_back(_LineData{ children_in_current_line, line_height, line_length, current_container_size - line_length, line_stretch_ratio_total });
  108. // Second pass for in-line expansion and alignment.
  109. int current_line_idx = 0;
  110. int child_idx_in_line = 0;
  111. ofs.x = 0;
  112. ofs.y = 0;
  113. for (int i = 0; i < get_child_count(); i++) {
  114. Control *child = Object::cast_to<Control>(get_child(i));
  115. if (!child || !child->is_visible()) {
  116. continue;
  117. }
  118. if (child->is_set_as_top_level()) {
  119. continue;
  120. }
  121. Size2i child_size = children_minsize_cache[child];
  122. _LineData line_data = lines_data[current_line_idx];
  123. if (child_idx_in_line >= lines_data[current_line_idx].child_count) {
  124. current_line_idx++;
  125. child_idx_in_line = 0;
  126. if (vertical) {
  127. ofs.x += line_data.min_line_height + separation_horizontal;
  128. ofs.y = 0;
  129. } else {
  130. ofs.x = 0;
  131. ofs.y += line_data.min_line_height + separation_vertical;
  132. }
  133. line_data = lines_data[current_line_idx];
  134. }
  135. if (vertical) { /* VERTICAL */
  136. if (child->get_h_size_flags() & (SIZE_FILL | SIZE_SHRINK_CENTER | SIZE_SHRINK_END)) {
  137. child_size.width = line_data.min_line_height;
  138. }
  139. if (child->get_v_size_flags() & SIZE_EXPAND) {
  140. int stretch = line_data.stretch_avail * child->get_stretch_ratio() / line_data.stretch_ratio_total;
  141. child_size.height += stretch;
  142. }
  143. } else { /* HORIZONTAL */
  144. if (child->get_v_size_flags() & (SIZE_FILL | SIZE_SHRINK_CENTER | SIZE_SHRINK_END)) {
  145. child_size.height = line_data.min_line_height;
  146. }
  147. if (child->get_h_size_flags() & SIZE_EXPAND) {
  148. int stretch = line_data.stretch_avail * child->get_stretch_ratio() / line_data.stretch_ratio_total;
  149. child_size.width += stretch;
  150. }
  151. }
  152. Rect2 child_rect = Rect2(ofs, child_size);
  153. if (rtl) {
  154. child_rect.position.x = get_rect().size.x - child_rect.position.x - child_rect.size.width;
  155. }
  156. fit_child_in_rect(child, child_rect);
  157. if (vertical) { /* VERTICAL */
  158. ofs.y += child_size.height + separation_vertical;
  159. } else { /* HORIZONTAL */
  160. ofs.x += child_size.width + separation_horizontal;
  161. }
  162. child_idx_in_line++;
  163. }
  164. cached_size = (vertical ? ofs.x : ofs.y) + line_height;
  165. cached_line_count = lines_data.size();
  166. }
  167. Size2 FlowContainer::get_minimum_size() const {
  168. Size2i minimum;
  169. for (int i = 0; i < get_child_count(); i++) {
  170. Control *c = Object::cast_to<Control>(get_child(i));
  171. if (!c) {
  172. continue;
  173. }
  174. if (c->is_set_as_top_level()) {
  175. continue;
  176. }
  177. if (!c->is_visible()) {
  178. continue;
  179. }
  180. Size2i size = c->get_combined_minimum_size();
  181. if (vertical) { /* VERTICAL */
  182. minimum.height = MAX(minimum.height, size.height);
  183. minimum.width = cached_size;
  184. } else { /* HORIZONTAL */
  185. minimum.width = MAX(minimum.width, size.width);
  186. minimum.height = cached_size;
  187. }
  188. }
  189. return minimum;
  190. }
  191. Vector<int> FlowContainer::get_allowed_size_flags_horizontal() const {
  192. Vector<int> flags;
  193. flags.append(SIZE_FILL);
  194. if (!vertical) {
  195. flags.append(SIZE_EXPAND);
  196. }
  197. flags.append(SIZE_SHRINK_BEGIN);
  198. flags.append(SIZE_SHRINK_CENTER);
  199. flags.append(SIZE_SHRINK_END);
  200. return flags;
  201. }
  202. Vector<int> FlowContainer::get_allowed_size_flags_vertical() const {
  203. Vector<int> flags;
  204. flags.append(SIZE_FILL);
  205. if (vertical) {
  206. flags.append(SIZE_EXPAND);
  207. }
  208. flags.append(SIZE_SHRINK_BEGIN);
  209. flags.append(SIZE_SHRINK_CENTER);
  210. flags.append(SIZE_SHRINK_END);
  211. return flags;
  212. }
  213. void FlowContainer::_notification(int p_what) {
  214. switch (p_what) {
  215. case NOTIFICATION_SORT_CHILDREN: {
  216. _resort();
  217. update_minimum_size();
  218. } break;
  219. case NOTIFICATION_THEME_CHANGED: {
  220. update_minimum_size();
  221. } break;
  222. case NOTIFICATION_TRANSLATION_CHANGED:
  223. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  224. queue_sort();
  225. } break;
  226. }
  227. }
  228. int FlowContainer::get_line_count() const {
  229. return cached_line_count;
  230. }
  231. FlowContainer::FlowContainer(bool p_vertical) {
  232. vertical = p_vertical;
  233. }
  234. void FlowContainer::_bind_methods() {
  235. ClassDB::bind_method(D_METHOD("get_line_count"), &FlowContainer::get_line_count);
  236. }