flow_container.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. #include "scene/gui/texture_rect.h"
  32. #include "scene/theme/theme_db.h"
  33. struct _LineData {
  34. int child_count = 0;
  35. int min_line_height = 0;
  36. int min_line_length = 0;
  37. int stretch_avail = 0;
  38. float stretch_ratio_total = 0;
  39. bool is_filled = false;
  40. };
  41. void FlowContainer::_resort() {
  42. // Avoid resorting if invisible.
  43. if (!is_visible_in_tree()) {
  44. return;
  45. }
  46. bool rtl = is_layout_rtl();
  47. HashMap<Control *, Size2i> children_minsize_cache;
  48. Vector<_LineData> lines_data;
  49. Vector2i ofs;
  50. int line_height = 0;
  51. int line_length = 0;
  52. float line_stretch_ratio_total = 0;
  53. int current_container_size = vertical ? get_size().y : get_size().x;
  54. int children_in_current_line = 0;
  55. Control *last_child = nullptr;
  56. // First pass for line wrapping and minimum size calculation.
  57. for (int i = 0; i < get_child_count(); i++) {
  58. Control *child = as_sortable_control(get_child(i));
  59. if (!child) {
  60. continue;
  61. }
  62. Size2i child_msc = child->get_combined_minimum_size();
  63. if (vertical) { /* VERTICAL */
  64. if (children_in_current_line > 0) {
  65. ofs.y += theme_cache.v_separation;
  66. }
  67. if (ofs.y + child_msc.y > current_container_size) {
  68. line_length = ofs.y - theme_cache.v_separation;
  69. lines_data.push_back(_LineData{ children_in_current_line, line_height, line_length, current_container_size - line_length, line_stretch_ratio_total, true });
  70. // Move in new column (vertical line).
  71. ofs.x += line_height + theme_cache.h_separation;
  72. ofs.y = 0;
  73. line_height = 0;
  74. line_stretch_ratio_total = 0;
  75. children_in_current_line = 0;
  76. }
  77. line_height = MAX(line_height, child_msc.x);
  78. if (child->get_v_size_flags().has_flag(SIZE_EXPAND)) {
  79. line_stretch_ratio_total += child->get_stretch_ratio();
  80. }
  81. ofs.y += child_msc.y;
  82. } else { /* HORIZONTAL */
  83. if (children_in_current_line > 0) {
  84. ofs.x += theme_cache.h_separation;
  85. }
  86. if (ofs.x + child_msc.x > current_container_size) {
  87. line_length = ofs.x - theme_cache.h_separation;
  88. lines_data.push_back(_LineData{ children_in_current_line, line_height, line_length, current_container_size - line_length, line_stretch_ratio_total, true });
  89. // Move in new line.
  90. ofs.y += line_height + theme_cache.v_separation;
  91. ofs.x = 0;
  92. line_height = 0;
  93. line_stretch_ratio_total = 0;
  94. children_in_current_line = 0;
  95. }
  96. line_height = MAX(line_height, child_msc.y);
  97. if (child->get_h_size_flags().has_flag(SIZE_EXPAND)) {
  98. line_stretch_ratio_total += child->get_stretch_ratio();
  99. }
  100. ofs.x += child_msc.x;
  101. }
  102. last_child = child;
  103. children_minsize_cache[child] = child_msc;
  104. children_in_current_line++;
  105. }
  106. line_length = vertical ? (ofs.y) : (ofs.x);
  107. bool is_filled = false;
  108. if (last_child != nullptr) {
  109. is_filled = vertical ? (ofs.y + last_child->get_combined_minimum_size().y > current_container_size ? true : false) : (ofs.x + last_child->get_combined_minimum_size().x > current_container_size ? true : false);
  110. }
  111. lines_data.push_back(_LineData{ children_in_current_line, line_height, line_length, current_container_size - line_length, line_stretch_ratio_total, is_filled });
  112. // Second pass for in-line expansion and alignment.
  113. int current_line_idx = 0;
  114. int child_idx_in_line = 0;
  115. ofs.x = 0;
  116. ofs.y = 0;
  117. for (int i = 0; i < get_child_count(); i++) {
  118. Control *child = as_sortable_control(get_child(i));
  119. if (!child) {
  120. continue;
  121. }
  122. Size2i child_size = children_minsize_cache[child];
  123. _LineData line_data = lines_data[current_line_idx];
  124. if (child_idx_in_line >= lines_data[current_line_idx].child_count) {
  125. current_line_idx++;
  126. child_idx_in_line = 0;
  127. if (vertical) {
  128. ofs.x += line_data.min_line_height + theme_cache.h_separation;
  129. ofs.y = 0;
  130. } else {
  131. ofs.x = 0;
  132. ofs.y += line_data.min_line_height + theme_cache.v_separation;
  133. }
  134. line_data = lines_data[current_line_idx];
  135. }
  136. // The first child of each line adds the offset caused by the alignment,
  137. // but only if the line doesn't contain a child that expands.
  138. if (child_idx_in_line == 0 && Math::is_equal_approx(line_data.stretch_ratio_total, 0)) {
  139. int alignment_ofs = 0;
  140. bool is_not_first_line_and_not_filled = current_line_idx != 0 && !line_data.is_filled;
  141. float prior_stretch_avail = is_not_first_line_and_not_filled ? lines_data[current_line_idx - 1].stretch_avail : 0.0;
  142. switch (alignment) {
  143. case ALIGNMENT_BEGIN: {
  144. if (last_wrap_alignment != LAST_WRAP_ALIGNMENT_INHERIT && is_not_first_line_and_not_filled) {
  145. if (last_wrap_alignment == LAST_WRAP_ALIGNMENT_END) {
  146. alignment_ofs = line_data.stretch_avail - prior_stretch_avail;
  147. } else if (last_wrap_alignment == LAST_WRAP_ALIGNMENT_CENTER) {
  148. alignment_ofs = (line_data.stretch_avail - prior_stretch_avail) * 0.5;
  149. }
  150. }
  151. } break;
  152. case ALIGNMENT_CENTER: {
  153. if (last_wrap_alignment != LAST_WRAP_ALIGNMENT_INHERIT && last_wrap_alignment != LAST_WRAP_ALIGNMENT_CENTER && is_not_first_line_and_not_filled) {
  154. if (last_wrap_alignment == LAST_WRAP_ALIGNMENT_END) {
  155. alignment_ofs = line_data.stretch_avail - (prior_stretch_avail * 0.5);
  156. } else { // Is LAST_WRAP_ALIGNMENT_BEGIN
  157. alignment_ofs = prior_stretch_avail * 0.5;
  158. }
  159. } else {
  160. alignment_ofs = line_data.stretch_avail * 0.5;
  161. }
  162. } break;
  163. case ALIGNMENT_END: {
  164. if (last_wrap_alignment != LAST_WRAP_ALIGNMENT_INHERIT && last_wrap_alignment != LAST_WRAP_ALIGNMENT_END && is_not_first_line_and_not_filled) {
  165. if (last_wrap_alignment == LAST_WRAP_ALIGNMENT_BEGIN) {
  166. alignment_ofs = prior_stretch_avail;
  167. } else { // Is LAST_WRAP_ALIGNMENT_CENTER
  168. alignment_ofs = prior_stretch_avail + (line_data.stretch_avail - prior_stretch_avail) * 0.5;
  169. }
  170. } else {
  171. alignment_ofs = line_data.stretch_avail;
  172. }
  173. } break;
  174. default:
  175. break;
  176. }
  177. if (vertical) { /* VERTICAL */
  178. ofs.y += alignment_ofs;
  179. } else { /* HORIZONTAL */
  180. ofs.x += alignment_ofs;
  181. }
  182. }
  183. bool is_unsupported_texture_rect = false;
  184. if (lines_data.size() > 1) {
  185. TextureRect *trect = Object::cast_to<TextureRect>(child);
  186. if (trect) {
  187. TextureRect::ExpandMode mode = trect->get_expand_mode();
  188. if (mode == TextureRect::EXPAND_FIT_WIDTH || mode == TextureRect::EXPAND_FIT_WIDTH_PROPORTIONAL ||
  189. mode == TextureRect::EXPAND_FIT_HEIGHT || mode == TextureRect::EXPAND_FIT_HEIGHT_PROPORTIONAL) {
  190. is_unsupported_texture_rect = true;
  191. }
  192. }
  193. }
  194. if (is_unsupported_texture_rect) {
  195. // Temporary fix for editor crash. Changing size of TextureRect with EXPAND_FIT_* ExpandModes can lead to infinite loop if child items are moved between lines.
  196. WARN_PRINT_ONCE("TextureRects with Fit Expand Modes are currently not supported inside FlowContainers with multiple lines");
  197. child_size = child->get_size();
  198. } else if (vertical) { /* VERTICAL */
  199. 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)) {
  200. child_size.width = line_data.min_line_height;
  201. }
  202. if (child->get_v_size_flags().has_flag(SIZE_EXPAND)) {
  203. int stretch = line_data.stretch_avail * child->get_stretch_ratio() / line_data.stretch_ratio_total;
  204. child_size.height += stretch;
  205. }
  206. } else { /* HORIZONTAL */
  207. 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)) {
  208. child_size.height = line_data.min_line_height;
  209. }
  210. if (child->get_h_size_flags().has_flag(SIZE_EXPAND)) {
  211. int stretch = line_data.stretch_avail * child->get_stretch_ratio() / line_data.stretch_ratio_total;
  212. child_size.width += stretch;
  213. }
  214. }
  215. Rect2 child_rect = Rect2(ofs, child_size);
  216. if (reverse_fill && !vertical) {
  217. child_rect.position.y = get_rect().size.y - child_rect.position.y - child_rect.size.height;
  218. }
  219. if ((rtl && !vertical) || ((rtl != reverse_fill) && vertical)) {
  220. child_rect.position.x = get_rect().size.x - child_rect.position.x - child_rect.size.width;
  221. }
  222. fit_child_in_rect(child, child_rect);
  223. if (vertical) { /* VERTICAL */
  224. ofs.y += child_size.height + theme_cache.v_separation;
  225. } else { /* HORIZONTAL */
  226. ofs.x += child_size.width + theme_cache.h_separation;
  227. }
  228. child_idx_in_line++;
  229. }
  230. cached_size = (vertical ? ofs.x : ofs.y) + line_height;
  231. cached_line_count = lines_data.size();
  232. cached_line_max_child_count = lines_data.size() > 0 ? lines_data[0].child_count : 0;
  233. }
  234. Size2 FlowContainer::get_minimum_size() const {
  235. Size2i minimum;
  236. for (int i = 0; i < get_child_count(); i++) {
  237. Control *c = as_sortable_control(get_child(i), SortableVisbilityMode::VISIBLE);
  238. if (!c) {
  239. continue;
  240. }
  241. Size2i size = c->get_combined_minimum_size();
  242. if (vertical) { /* VERTICAL */
  243. minimum.height = MAX(minimum.height, size.height);
  244. minimum.width = cached_size;
  245. } else { /* HORIZONTAL */
  246. minimum.width = MAX(minimum.width, size.width);
  247. minimum.height = cached_size;
  248. }
  249. }
  250. return minimum;
  251. }
  252. Vector<int> FlowContainer::get_allowed_size_flags_horizontal() const {
  253. Vector<int> flags;
  254. flags.append(SIZE_FILL);
  255. if (!vertical) {
  256. flags.append(SIZE_EXPAND);
  257. }
  258. flags.append(SIZE_SHRINK_BEGIN);
  259. flags.append(SIZE_SHRINK_CENTER);
  260. flags.append(SIZE_SHRINK_END);
  261. return flags;
  262. }
  263. Vector<int> FlowContainer::get_allowed_size_flags_vertical() const {
  264. Vector<int> flags;
  265. flags.append(SIZE_FILL);
  266. if (vertical) {
  267. flags.append(SIZE_EXPAND);
  268. }
  269. flags.append(SIZE_SHRINK_BEGIN);
  270. flags.append(SIZE_SHRINK_CENTER);
  271. flags.append(SIZE_SHRINK_END);
  272. return flags;
  273. }
  274. void FlowContainer::_notification(int p_what) {
  275. switch (p_what) {
  276. case NOTIFICATION_SORT_CHILDREN: {
  277. _resort();
  278. update_minimum_size();
  279. } break;
  280. case NOTIFICATION_THEME_CHANGED: {
  281. update_minimum_size();
  282. } break;
  283. case NOTIFICATION_TRANSLATION_CHANGED:
  284. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  285. queue_sort();
  286. } break;
  287. }
  288. }
  289. void FlowContainer::_validate_property(PropertyInfo &p_property) const {
  290. if (is_fixed && p_property.name == "vertical") {
  291. p_property.usage = PROPERTY_USAGE_NONE;
  292. }
  293. }
  294. int FlowContainer::get_line_count() const {
  295. return cached_line_count;
  296. }
  297. int FlowContainer::get_line_max_child_count() const {
  298. return cached_line_max_child_count;
  299. }
  300. void FlowContainer::set_alignment(AlignmentMode p_alignment) {
  301. if (alignment == p_alignment) {
  302. return;
  303. }
  304. alignment = p_alignment;
  305. _resort();
  306. }
  307. FlowContainer::AlignmentMode FlowContainer::get_alignment() const {
  308. return alignment;
  309. }
  310. void FlowContainer::set_last_wrap_alignment(LastWrapAlignmentMode p_last_wrap_alignment) {
  311. if (last_wrap_alignment == p_last_wrap_alignment) {
  312. return;
  313. }
  314. last_wrap_alignment = p_last_wrap_alignment;
  315. _resort();
  316. }
  317. FlowContainer::LastWrapAlignmentMode FlowContainer::get_last_wrap_alignment() const {
  318. return last_wrap_alignment;
  319. }
  320. void FlowContainer::set_vertical(bool p_vertical) {
  321. ERR_FAIL_COND_MSG(is_fixed, "Can't change orientation of " + get_class() + ".");
  322. vertical = p_vertical;
  323. update_minimum_size();
  324. _resort();
  325. }
  326. bool FlowContainer::is_vertical() const {
  327. return vertical;
  328. }
  329. void FlowContainer::set_reverse_fill(bool p_reverse_fill) {
  330. if (reverse_fill == p_reverse_fill) {
  331. return;
  332. }
  333. reverse_fill = p_reverse_fill;
  334. _resort();
  335. }
  336. bool FlowContainer::is_reverse_fill() const {
  337. return reverse_fill;
  338. }
  339. FlowContainer::FlowContainer(bool p_vertical) {
  340. vertical = p_vertical;
  341. }
  342. void FlowContainer::_bind_methods() {
  343. ClassDB::bind_method(D_METHOD("get_line_count"), &FlowContainer::get_line_count);
  344. ClassDB::bind_method(D_METHOD("set_alignment", "alignment"), &FlowContainer::set_alignment);
  345. ClassDB::bind_method(D_METHOD("get_alignment"), &FlowContainer::get_alignment);
  346. ClassDB::bind_method(D_METHOD("set_last_wrap_alignment", "last_wrap_alignment"), &FlowContainer::set_last_wrap_alignment);
  347. ClassDB::bind_method(D_METHOD("get_last_wrap_alignment"), &FlowContainer::get_last_wrap_alignment);
  348. ClassDB::bind_method(D_METHOD("set_vertical", "vertical"), &FlowContainer::set_vertical);
  349. ClassDB::bind_method(D_METHOD("is_vertical"), &FlowContainer::is_vertical);
  350. ClassDB::bind_method(D_METHOD("set_reverse_fill", "reverse_fill"), &FlowContainer::set_reverse_fill);
  351. ClassDB::bind_method(D_METHOD("is_reverse_fill"), &FlowContainer::is_reverse_fill);
  352. BIND_ENUM_CONSTANT(ALIGNMENT_BEGIN);
  353. BIND_ENUM_CONSTANT(ALIGNMENT_CENTER);
  354. BIND_ENUM_CONSTANT(ALIGNMENT_END);
  355. BIND_ENUM_CONSTANT(LAST_WRAP_ALIGNMENT_INHERIT);
  356. BIND_ENUM_CONSTANT(LAST_WRAP_ALIGNMENT_BEGIN);
  357. BIND_ENUM_CONSTANT(LAST_WRAP_ALIGNMENT_CENTER);
  358. BIND_ENUM_CONSTANT(LAST_WRAP_ALIGNMENT_END);
  359. ADD_PROPERTY(PropertyInfo(Variant::INT, "alignment", PROPERTY_HINT_ENUM, "Begin,Center,End"), "set_alignment", "get_alignment");
  360. ADD_PROPERTY(PropertyInfo(Variant::INT, "last_wrap_alignment", PROPERTY_HINT_ENUM, "Inherit,Begin,Center,End"), "set_last_wrap_alignment", "get_last_wrap_alignment");
  361. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "vertical"), "set_vertical", "is_vertical");
  362. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "reverse_fill"), "set_reverse_fill", "is_reverse_fill");
  363. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, FlowContainer, h_separation);
  364. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, FlowContainer, v_separation);
  365. }