box_container.cpp 9.7 KB

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