box_container.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /**************************************************************************/
  2. /* box_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 "box_container.h"
  31. #include "scene/gui/label.h"
  32. #include "scene/gui/margin_container.h"
  33. #include "scene/theme/theme_db.h"
  34. struct _MinSizeCache {
  35. int min_size = 0;
  36. bool will_stretch = false;
  37. int final_size = 0;
  38. };
  39. void BoxContainer::_resort() {
  40. /** First pass, determine minimum size AND amount of stretchable elements */
  41. Size2i new_size = get_size();
  42. bool rtl = is_layout_rtl();
  43. bool first = true;
  44. int children_count = 0;
  45. int stretch_min = 0;
  46. int stretch_avail = 0;
  47. float stretch_ratio_total = 0.0;
  48. HashMap<Control *, _MinSizeCache> min_size_cache;
  49. for (int i = 0; i < get_child_count(); i++) {
  50. Control *c = as_sortable_control(get_child(i));
  51. if (!c) {
  52. continue;
  53. }
  54. Size2i size = c->get_combined_minimum_size();
  55. _MinSizeCache msc;
  56. if (vertical) { /* VERTICAL */
  57. stretch_min += size.height;
  58. msc.min_size = size.height;
  59. msc.will_stretch = c->get_v_size_flags().has_flag(SIZE_EXPAND);
  60. } else { /* HORIZONTAL */
  61. stretch_min += size.width;
  62. msc.min_size = size.width;
  63. msc.will_stretch = c->get_h_size_flags().has_flag(SIZE_EXPAND);
  64. }
  65. if (msc.will_stretch) {
  66. stretch_avail += msc.min_size;
  67. stretch_ratio_total += c->get_stretch_ratio();
  68. }
  69. msc.final_size = msc.min_size;
  70. min_size_cache[c] = msc;
  71. children_count++;
  72. }
  73. if (children_count == 0) {
  74. return;
  75. }
  76. int stretch_max = (vertical ? new_size.height : new_size.width) - (children_count - 1) * theme_cache.separation;
  77. int stretch_diff = stretch_max - stretch_min;
  78. if (stretch_diff < 0) {
  79. //avoid negative stretch space
  80. stretch_diff = 0;
  81. }
  82. stretch_avail += stretch_diff; //available stretch space.
  83. /** Second, pass successively to discard elements that can't be stretched, this will run while stretchable
  84. elements exist */
  85. bool has_stretched = false;
  86. while (stretch_ratio_total > 0) { // first of all, don't even be here if no stretchable objects exist
  87. has_stretched = true;
  88. bool refit_successful = true; //assume refit-test will go well
  89. float error = 0.0; // Keep track of accumulated error in pixels
  90. for (int i = 0; i < get_child_count(); i++) {
  91. Control *c = as_sortable_control(get_child(i));
  92. if (!c) {
  93. continue;
  94. }
  95. ERR_FAIL_COND(!min_size_cache.has(c));
  96. _MinSizeCache &msc = min_size_cache[c];
  97. if (msc.will_stretch) { //wants to stretch
  98. //let's see if it can really stretch
  99. float final_pixel_size = stretch_avail * c->get_stretch_ratio() / stretch_ratio_total;
  100. // Add leftover fractional pixels to error accumulator
  101. error += final_pixel_size - (int)final_pixel_size;
  102. if (final_pixel_size < msc.min_size) {
  103. //if available stretching area is too small for widget,
  104. //then remove it from stretching area
  105. msc.will_stretch = false;
  106. stretch_ratio_total -= c->get_stretch_ratio();
  107. refit_successful = false;
  108. stretch_avail -= msc.min_size;
  109. msc.final_size = msc.min_size;
  110. break;
  111. } else {
  112. msc.final_size = final_pixel_size;
  113. // Dump accumulated error if one pixel or more
  114. if (error >= 1) {
  115. msc.final_size += 1;
  116. error -= 1;
  117. }
  118. }
  119. }
  120. }
  121. if (refit_successful) { //uf refit went well, break
  122. break;
  123. }
  124. }
  125. /** Final pass, draw and stretch elements **/
  126. int ofs = 0;
  127. if (!has_stretched) {
  128. if (!vertical) {
  129. switch (alignment) {
  130. case ALIGNMENT_BEGIN:
  131. if (rtl) {
  132. ofs = stretch_diff;
  133. }
  134. break;
  135. case ALIGNMENT_CENTER:
  136. ofs = stretch_diff / 2;
  137. break;
  138. case ALIGNMENT_END:
  139. if (!rtl) {
  140. ofs = stretch_diff;
  141. }
  142. break;
  143. }
  144. } else {
  145. switch (alignment) {
  146. case ALIGNMENT_BEGIN:
  147. break;
  148. case ALIGNMENT_CENTER:
  149. ofs = stretch_diff / 2;
  150. break;
  151. case ALIGNMENT_END:
  152. ofs = stretch_diff;
  153. break;
  154. }
  155. }
  156. }
  157. first = true;
  158. int idx = 0;
  159. int start;
  160. int end;
  161. int delta;
  162. if (!rtl || vertical) {
  163. start = 0;
  164. end = get_child_count();
  165. delta = +1;
  166. } else {
  167. start = get_child_count() - 1;
  168. end = -1;
  169. delta = -1;
  170. }
  171. for (int i = start; i != end; i += delta) {
  172. Control *c = as_sortable_control(get_child(i));
  173. if (!c) {
  174. continue;
  175. }
  176. _MinSizeCache &msc = min_size_cache[c];
  177. if (first) {
  178. first = false;
  179. } else {
  180. ofs += theme_cache.separation;
  181. }
  182. int from = ofs;
  183. int to = ofs + msc.final_size;
  184. if (msc.will_stretch && idx == children_count - 1) {
  185. //adjust so the last one always fits perfect
  186. //compensating for numerical imprecision
  187. to = vertical ? new_size.height : new_size.width;
  188. }
  189. int size = to - from;
  190. Rect2 rect;
  191. if (vertical) {
  192. rect = Rect2(0, from, new_size.width, size);
  193. } else {
  194. rect = Rect2(from, 0, size, new_size.height);
  195. }
  196. fit_child_in_rect(c, rect);
  197. ofs = to;
  198. idx++;
  199. }
  200. }
  201. Size2 BoxContainer::get_minimum_size() const {
  202. /* Calculate MINIMUM SIZE */
  203. Size2i minimum;
  204. bool first = true;
  205. for (int i = 0; i < get_child_count(); i++) {
  206. Control *c = as_sortable_control(get_child(i), SortableVisbilityMode::VISIBLE);
  207. if (!c) {
  208. continue;
  209. }
  210. Size2i size = c->get_combined_minimum_size();
  211. if (vertical) { /* VERTICAL */
  212. if (size.width > minimum.width) {
  213. minimum.width = size.width;
  214. }
  215. minimum.height += size.height + (first ? 0 : theme_cache.separation);
  216. } else { /* HORIZONTAL */
  217. if (size.height > minimum.height) {
  218. minimum.height = size.height;
  219. }
  220. minimum.width += size.width + (first ? 0 : theme_cache.separation);
  221. }
  222. first = false;
  223. }
  224. return minimum;
  225. }
  226. void BoxContainer::_notification(int p_what) {
  227. switch (p_what) {
  228. case NOTIFICATION_SORT_CHILDREN: {
  229. _resort();
  230. } break;
  231. case NOTIFICATION_THEME_CHANGED: {
  232. update_minimum_size();
  233. } break;
  234. case NOTIFICATION_TRANSLATION_CHANGED:
  235. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  236. queue_sort();
  237. } break;
  238. }
  239. }
  240. void BoxContainer::_validate_property(PropertyInfo &p_property) const {
  241. if (is_fixed && p_property.name == "vertical") {
  242. p_property.usage = PROPERTY_USAGE_NONE;
  243. }
  244. }
  245. void BoxContainer::set_alignment(AlignmentMode p_alignment) {
  246. if (alignment == p_alignment) {
  247. return;
  248. }
  249. alignment = p_alignment;
  250. _resort();
  251. }
  252. BoxContainer::AlignmentMode BoxContainer::get_alignment() const {
  253. return alignment;
  254. }
  255. void BoxContainer::set_vertical(bool p_vertical) {
  256. ERR_FAIL_COND_MSG(is_fixed, "Can't change orientation of " + get_class() + ".");
  257. vertical = p_vertical;
  258. update_minimum_size();
  259. _resort();
  260. }
  261. bool BoxContainer::is_vertical() const {
  262. return vertical;
  263. }
  264. Control *BoxContainer::add_spacer(bool p_begin) {
  265. Control *c = memnew(Control);
  266. c->set_mouse_filter(MOUSE_FILTER_PASS); //allow spacer to pass mouse events
  267. if (vertical) {
  268. c->set_v_size_flags(SIZE_EXPAND_FILL);
  269. } else {
  270. c->set_h_size_flags(SIZE_EXPAND_FILL);
  271. }
  272. add_child(c);
  273. if (p_begin) {
  274. move_child(c, 0);
  275. }
  276. return c;
  277. }
  278. Vector<int> BoxContainer::get_allowed_size_flags_horizontal() const {
  279. Vector<int> flags;
  280. flags.append(SIZE_FILL);
  281. if (!vertical) {
  282. flags.append(SIZE_EXPAND);
  283. }
  284. flags.append(SIZE_SHRINK_BEGIN);
  285. flags.append(SIZE_SHRINK_CENTER);
  286. flags.append(SIZE_SHRINK_END);
  287. return flags;
  288. }
  289. Vector<int> BoxContainer::get_allowed_size_flags_vertical() const {
  290. Vector<int> flags;
  291. flags.append(SIZE_FILL);
  292. if (vertical) {
  293. flags.append(SIZE_EXPAND);
  294. }
  295. flags.append(SIZE_SHRINK_BEGIN);
  296. flags.append(SIZE_SHRINK_CENTER);
  297. flags.append(SIZE_SHRINK_END);
  298. return flags;
  299. }
  300. BoxContainer::BoxContainer(bool p_vertical) {
  301. vertical = p_vertical;
  302. }
  303. void BoxContainer::_bind_methods() {
  304. ClassDB::bind_method(D_METHOD("add_spacer", "begin"), &BoxContainer::add_spacer);
  305. ClassDB::bind_method(D_METHOD("set_alignment", "alignment"), &BoxContainer::set_alignment);
  306. ClassDB::bind_method(D_METHOD("get_alignment"), &BoxContainer::get_alignment);
  307. ClassDB::bind_method(D_METHOD("set_vertical", "vertical"), &BoxContainer::set_vertical);
  308. ClassDB::bind_method(D_METHOD("is_vertical"), &BoxContainer::is_vertical);
  309. BIND_ENUM_CONSTANT(ALIGNMENT_BEGIN);
  310. BIND_ENUM_CONSTANT(ALIGNMENT_CENTER);
  311. BIND_ENUM_CONSTANT(ALIGNMENT_END);
  312. ADD_PROPERTY(PropertyInfo(Variant::INT, "alignment", PROPERTY_HINT_ENUM, "Begin,Center,End"), "set_alignment", "get_alignment");
  313. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "vertical"), "set_vertical", "is_vertical");
  314. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, BoxContainer, separation);
  315. }
  316. MarginContainer *VBoxContainer::add_margin_child(const String &p_label, Control *p_control, bool p_expand) {
  317. Label *l = memnew(Label);
  318. l->set_theme_type_variation("HeaderSmall");
  319. l->set_text(p_label);
  320. add_child(l);
  321. MarginContainer *mc = memnew(MarginContainer);
  322. mc->add_theme_constant_override("margin_left", 0);
  323. mc->add_child(p_control, true);
  324. add_child(mc);
  325. if (p_expand) {
  326. mc->set_v_size_flags(SIZE_EXPAND_FILL);
  327. }
  328. return mc;
  329. }