flow_container.cpp 11 KB

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