flow_container.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /**************************************************************************/
  2. /* flow_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 "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. bool rtl = is_layout_rtl();
  44. HashMap<Control *, Size2i> children_minsize_cache;
  45. Vector<_LineData> lines_data;
  46. Vector2i ofs;
  47. int line_height = 0;
  48. int line_length = 0;
  49. float line_stretch_ratio_total = 0;
  50. int current_container_size = vertical ? get_rect().size.y : get_rect().size.x;
  51. int children_in_current_line = 0;
  52. // First pass for line wrapping and minimum size calculation.
  53. for (int i = 0; i < get_child_count(); i++) {
  54. Control *child = Object::cast_to<Control>(get_child(i));
  55. if (!child || !child->is_visible()) {
  56. continue;
  57. }
  58. if (child->is_set_as_top_level()) {
  59. continue;
  60. }
  61. Size2i child_msc = child->get_combined_minimum_size();
  62. if (vertical) { /* VERTICAL */
  63. if (children_in_current_line > 0) {
  64. ofs.y += theme_cache.v_separation;
  65. }
  66. if (ofs.y + child_msc.y > current_container_size) {
  67. line_length = ofs.y - theme_cache.v_separation;
  68. lines_data.push_back(_LineData{ children_in_current_line, line_height, line_length, current_container_size - line_length, line_stretch_ratio_total });
  69. // Move in new column (vertical line).
  70. ofs.x += line_height + theme_cache.h_separation;
  71. ofs.y = 0;
  72. line_height = 0;
  73. line_stretch_ratio_total = 0;
  74. children_in_current_line = 0;
  75. }
  76. line_height = MAX(line_height, child_msc.x);
  77. if (child->get_v_size_flags().has_flag(SIZE_EXPAND)) {
  78. line_stretch_ratio_total += child->get_stretch_ratio();
  79. }
  80. ofs.y += child_msc.y;
  81. } else { /* HORIZONTAL */
  82. if (children_in_current_line > 0) {
  83. ofs.x += theme_cache.h_separation;
  84. }
  85. if (ofs.x + child_msc.x > current_container_size) {
  86. line_length = ofs.x - theme_cache.h_separation;
  87. lines_data.push_back(_LineData{ children_in_current_line, line_height, line_length, current_container_size - line_length, line_stretch_ratio_total });
  88. // Move in new line.
  89. ofs.y += line_height + theme_cache.v_separation;
  90. ofs.x = 0;
  91. line_height = 0;
  92. line_stretch_ratio_total = 0;
  93. children_in_current_line = 0;
  94. }
  95. line_height = MAX(line_height, child_msc.y);
  96. if (child->get_h_size_flags().has_flag(SIZE_EXPAND)) {
  97. line_stretch_ratio_total += child->get_stretch_ratio();
  98. }
  99. ofs.x += child_msc.x;
  100. }
  101. children_minsize_cache[child] = child_msc;
  102. children_in_current_line++;
  103. }
  104. line_length = vertical ? (ofs.y) : (ofs.x);
  105. lines_data.push_back(_LineData{ children_in_current_line, line_height, line_length, current_container_size - line_length, line_stretch_ratio_total });
  106. // Second pass for in-line expansion and alignment.
  107. int current_line_idx = 0;
  108. int child_idx_in_line = 0;
  109. ofs.x = 0;
  110. ofs.y = 0;
  111. for (int i = 0; i < get_child_count(); i++) {
  112. Control *child = Object::cast_to<Control>(get_child(i));
  113. if (!child || !child->is_visible()) {
  114. continue;
  115. }
  116. if (child->is_set_as_top_level()) {
  117. continue;
  118. }
  119. Size2i child_size = children_minsize_cache[child];
  120. _LineData line_data = lines_data[current_line_idx];
  121. if (child_idx_in_line >= lines_data[current_line_idx].child_count) {
  122. current_line_idx++;
  123. child_idx_in_line = 0;
  124. if (vertical) {
  125. ofs.x += line_data.min_line_height + theme_cache.h_separation;
  126. ofs.y = 0;
  127. } else {
  128. ofs.x = 0;
  129. ofs.y += line_data.min_line_height + theme_cache.v_separation;
  130. }
  131. line_data = lines_data[current_line_idx];
  132. }
  133. // The first child of each line adds the offset caused by the alignment,
  134. // but only if the line doesn't contain a child that expands.
  135. if (child_idx_in_line == 0 && Math::is_equal_approx(line_data.stretch_ratio_total, 0)) {
  136. int alignment_ofs = 0;
  137. switch (alignment) {
  138. case ALIGNMENT_CENTER:
  139. alignment_ofs = line_data.stretch_avail / 2;
  140. break;
  141. case ALIGNMENT_END:
  142. alignment_ofs = line_data.stretch_avail;
  143. break;
  144. default:
  145. break;
  146. }
  147. if (vertical) { /* VERTICAL */
  148. ofs.y += alignment_ofs;
  149. } else { /* HORIZONTAL */
  150. ofs.x += alignment_ofs;
  151. }
  152. }
  153. if (vertical) { /* VERTICAL */
  154. if (child->get_h_size_flags().has_flag(SIZE_FILL) || child->get_h_size_flags().has_flag(SIZE_SHRINK_CENTER) || child->get_h_size_flags().has_flag(SIZE_SHRINK_END)) {
  155. child_size.width = line_data.min_line_height;
  156. }
  157. if (child->get_v_size_flags().has_flag(SIZE_EXPAND)) {
  158. int stretch = line_data.stretch_avail * child->get_stretch_ratio() / line_data.stretch_ratio_total;
  159. child_size.height += stretch;
  160. }
  161. } else { /* HORIZONTAL */
  162. if (child->get_v_size_flags().has_flag(SIZE_FILL) || child->get_v_size_flags().has_flag(SIZE_SHRINK_CENTER) || child->get_v_size_flags().has_flag(SIZE_SHRINK_END)) {
  163. child_size.height = line_data.min_line_height;
  164. }
  165. if (child->get_h_size_flags().has_flag(SIZE_EXPAND)) {
  166. int stretch = line_data.stretch_avail * child->get_stretch_ratio() / line_data.stretch_ratio_total;
  167. child_size.width += stretch;
  168. }
  169. }
  170. Rect2 child_rect = Rect2(ofs, child_size);
  171. if (rtl) {
  172. child_rect.position.x = get_rect().size.x - child_rect.position.x - child_rect.size.width;
  173. }
  174. fit_child_in_rect(child, child_rect);
  175. if (vertical) { /* VERTICAL */
  176. ofs.y += child_size.height + theme_cache.v_separation;
  177. } else { /* HORIZONTAL */
  178. ofs.x += child_size.width + theme_cache.h_separation;
  179. }
  180. child_idx_in_line++;
  181. }
  182. cached_size = (vertical ? ofs.x : ofs.y) + line_height;
  183. cached_line_count = lines_data.size();
  184. }
  185. Size2 FlowContainer::get_minimum_size() const {
  186. Size2i minimum;
  187. for (int i = 0; i < get_child_count(); i++) {
  188. Control *c = Object::cast_to<Control>(get_child(i));
  189. if (!c) {
  190. continue;
  191. }
  192. if (c->is_set_as_top_level()) {
  193. continue;
  194. }
  195. if (!c->is_visible()) {
  196. continue;
  197. }
  198. Size2i size = c->get_combined_minimum_size();
  199. if (vertical) { /* VERTICAL */
  200. minimum.height = MAX(minimum.height, size.height);
  201. minimum.width = cached_size;
  202. } else { /* HORIZONTAL */
  203. minimum.width = MAX(minimum.width, size.width);
  204. minimum.height = cached_size;
  205. }
  206. }
  207. return minimum;
  208. }
  209. Vector<int> FlowContainer::get_allowed_size_flags_horizontal() const {
  210. Vector<int> flags;
  211. flags.append(SIZE_FILL);
  212. if (!vertical) {
  213. flags.append(SIZE_EXPAND);
  214. }
  215. flags.append(SIZE_SHRINK_BEGIN);
  216. flags.append(SIZE_SHRINK_CENTER);
  217. flags.append(SIZE_SHRINK_END);
  218. return flags;
  219. }
  220. Vector<int> FlowContainer::get_allowed_size_flags_vertical() const {
  221. Vector<int> flags;
  222. flags.append(SIZE_FILL);
  223. if (vertical) {
  224. flags.append(SIZE_EXPAND);
  225. }
  226. flags.append(SIZE_SHRINK_BEGIN);
  227. flags.append(SIZE_SHRINK_CENTER);
  228. flags.append(SIZE_SHRINK_END);
  229. return flags;
  230. }
  231. void FlowContainer::_update_theme_item_cache() {
  232. Container::_update_theme_item_cache();
  233. theme_cache.h_separation = get_theme_constant(SNAME("h_separation"));
  234. theme_cache.v_separation = get_theme_constant(SNAME("v_separation"));
  235. }
  236. void FlowContainer::_notification(int p_what) {
  237. switch (p_what) {
  238. case NOTIFICATION_SORT_CHILDREN: {
  239. _resort();
  240. update_minimum_size();
  241. } break;
  242. case NOTIFICATION_THEME_CHANGED: {
  243. update_minimum_size();
  244. } break;
  245. case NOTIFICATION_TRANSLATION_CHANGED:
  246. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  247. queue_sort();
  248. } break;
  249. }
  250. }
  251. void FlowContainer::_validate_property(PropertyInfo &p_property) const {
  252. if (is_fixed && p_property.name == "vertical") {
  253. p_property.usage = PROPERTY_USAGE_NONE;
  254. }
  255. }
  256. int FlowContainer::get_line_count() const {
  257. return cached_line_count;
  258. }
  259. void FlowContainer::set_alignment(AlignmentMode p_alignment) {
  260. if (alignment == p_alignment) {
  261. return;
  262. }
  263. alignment = p_alignment;
  264. _resort();
  265. }
  266. FlowContainer::AlignmentMode FlowContainer::get_alignment() const {
  267. return alignment;
  268. }
  269. void FlowContainer::set_vertical(bool p_vertical) {
  270. ERR_FAIL_COND_MSG(is_fixed, "Can't change orientation of " + get_class() + ".");
  271. vertical = p_vertical;
  272. update_minimum_size();
  273. _resort();
  274. }
  275. bool FlowContainer::is_vertical() const {
  276. return vertical;
  277. }
  278. FlowContainer::FlowContainer(bool p_vertical) {
  279. vertical = p_vertical;
  280. }
  281. void FlowContainer::_bind_methods() {
  282. ClassDB::bind_method(D_METHOD("get_line_count"), &FlowContainer::get_line_count);
  283. ClassDB::bind_method(D_METHOD("set_alignment", "alignment"), &FlowContainer::set_alignment);
  284. ClassDB::bind_method(D_METHOD("get_alignment"), &FlowContainer::get_alignment);
  285. ClassDB::bind_method(D_METHOD("set_vertical", "vertical"), &FlowContainer::set_vertical);
  286. ClassDB::bind_method(D_METHOD("is_vertical"), &FlowContainer::is_vertical);
  287. BIND_ENUM_CONSTANT(ALIGNMENT_BEGIN);
  288. BIND_ENUM_CONSTANT(ALIGNMENT_CENTER);
  289. BIND_ENUM_CONSTANT(ALIGNMENT_END);
  290. ADD_PROPERTY(PropertyInfo(Variant::INT, "alignment", PROPERTY_HINT_ENUM, "Begin,Center,End"), "set_alignment", "get_alignment");
  291. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "vertical"), "set_vertical", "is_vertical");
  292. }