box_container.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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;
  35. bool will_stretch;
  36. int final_size;
  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_constant("separation"); //,vertical?"VBoxContainer":"HBoxContainer");
  42. bool first = true;
  43. int children_count = 0;
  44. int stretch_min = 0;
  45. int stretch_avail = 0;
  46. float stretch_ratio_total = 0;
  47. Map<Control *, _MinSizeCache> min_size_cache;
  48. for (int i = 0; i < get_child_count(); i++) {
  49. Control *c = Object::cast_to<Control>(get_child(i));
  50. if (!c || !c->is_visible_in_tree())
  51. continue;
  52. if (c->is_set_as_toplevel())
  53. continue;
  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() & 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() & 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. int stretch_max = (vertical ? new_size.height : new_size.width) - (children_count - 1) * sep;
  76. int stretch_diff = stretch_max - stretch_min;
  77. if (stretch_diff < 0) {
  78. //avoid negative stretch space
  79. stretch_diff = 0;
  80. }
  81. stretch_avail += stretch_diff; //available stretch space.
  82. /** Second, pass successively to discard elements that can't be stretched, this will run while stretchable
  83. elements exist */
  84. bool has_stretched = false;
  85. while (stretch_ratio_total > 0) { // first of all, don't even be here if no stretchable objects exist
  86. has_stretched = true;
  87. bool refit_successful = true; //assume refit-test will go well
  88. float error = 0; // Keep track of accumulated error in pixels
  89. for (int i = 0; i < get_child_count(); i++) {
  90. Control *c = Object::cast_to<Control>(get_child(i));
  91. if (!c || !c->is_visible_in_tree())
  92. continue;
  93. if (c->is_set_as_toplevel())
  94. continue;
  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. /** Final pass, draw and stretch elements **/
  125. int ofs = 0;
  126. if (!has_stretched) {
  127. switch (align) {
  128. case ALIGN_BEGIN:
  129. break;
  130. case ALIGN_CENTER:
  131. ofs = stretch_diff / 2;
  132. break;
  133. case ALIGN_END:
  134. ofs = stretch_diff;
  135. break;
  136. }
  137. }
  138. first = true;
  139. int idx = 0;
  140. for (int i = 0; i < get_child_count(); i++) {
  141. Control *c = Object::cast_to<Control>(get_child(i));
  142. if (!c || !c->is_visible_in_tree())
  143. continue;
  144. if (c->is_set_as_toplevel())
  145. continue;
  146. _MinSizeCache &msc = min_size_cache[c];
  147. if (first)
  148. first = false;
  149. else
  150. ofs += sep;
  151. int from = ofs;
  152. int to = ofs + msc.final_size;
  153. if (msc.will_stretch && idx == children_count - 1) {
  154. //adjust so the last one always fits perfect
  155. //compensating for numerical imprecision
  156. to = vertical ? new_size.height : new_size.width;
  157. }
  158. int size = to - from;
  159. Rect2 rect;
  160. if (vertical) {
  161. rect = Rect2(0, from, new_size.width, size);
  162. } else {
  163. rect = Rect2(from, 0, size, new_size.height);
  164. }
  165. fit_child_in_rect(c, rect);
  166. ofs = to;
  167. idx++;
  168. }
  169. }
  170. Size2 BoxContainer::get_minimum_size() const {
  171. /* Calculate MINIMUM SIZE */
  172. Size2i minimum;
  173. int sep = get_constant("separation"); //,vertical?"VBoxContainer":"HBoxContainer");
  174. bool first = true;
  175. for (int i = 0; i < get_child_count(); i++) {
  176. Control *c = Object::cast_to<Control>(get_child(i));
  177. if (!c)
  178. continue;
  179. if (c->is_set_as_toplevel())
  180. continue;
  181. if (!c->is_visible()) {
  182. continue;
  183. }
  184. Size2i size = c->get_combined_minimum_size();
  185. if (vertical) { /* VERTICAL */
  186. if (size.width > minimum.width) {
  187. minimum.width = size.width;
  188. }
  189. minimum.height += size.height + (first ? 0 : sep);
  190. } else { /* HORIZONTAL */
  191. if (size.height > minimum.height) {
  192. minimum.height = size.height;
  193. }
  194. minimum.width += size.width + (first ? 0 : sep);
  195. }
  196. first = false;
  197. }
  198. return minimum;
  199. }
  200. void BoxContainer::_notification(int p_what) {
  201. switch (p_what) {
  202. case NOTIFICATION_SORT_CHILDREN: {
  203. _resort();
  204. } break;
  205. case NOTIFICATION_THEME_CHANGED: {
  206. minimum_size_changed();
  207. } break;
  208. }
  209. }
  210. void BoxContainer::set_alignment(AlignMode p_align) {
  211. align = p_align;
  212. _resort();
  213. }
  214. BoxContainer::AlignMode BoxContainer::get_alignment() const {
  215. return align;
  216. }
  217. void BoxContainer::add_spacer(bool p_begin) {
  218. Control *c = memnew(Control);
  219. c->set_mouse_filter(MOUSE_FILTER_PASS); //allow spacer to pass mouse events
  220. if (vertical)
  221. c->set_v_size_flags(SIZE_EXPAND_FILL);
  222. else
  223. c->set_h_size_flags(SIZE_EXPAND_FILL);
  224. add_child(c);
  225. if (p_begin)
  226. move_child(c, 0);
  227. }
  228. BoxContainer::BoxContainer(bool p_vertical) {
  229. vertical = p_vertical;
  230. align = ALIGN_BEGIN;
  231. //set_ignore_mouse(true);
  232. set_mouse_filter(MOUSE_FILTER_PASS);
  233. }
  234. void BoxContainer::_bind_methods() {
  235. ClassDB::bind_method(D_METHOD("add_spacer", "begin"), &BoxContainer::add_spacer);
  236. ClassDB::bind_method(D_METHOD("get_alignment"), &BoxContainer::get_alignment);
  237. ClassDB::bind_method(D_METHOD("set_alignment", "alignment"), &BoxContainer::set_alignment);
  238. BIND_ENUM_CONSTANT(ALIGN_BEGIN);
  239. BIND_ENUM_CONSTANT(ALIGN_CENTER);
  240. BIND_ENUM_CONSTANT(ALIGN_END);
  241. ADD_PROPERTY(PropertyInfo(Variant::INT, "alignment", PROPERTY_HINT_ENUM, "Begin,Center,End"), "set_alignment", "get_alignment");
  242. }
  243. MarginContainer *VBoxContainer::add_margin_child(const String &p_label, Control *p_control, bool p_expand) {
  244. Label *l = memnew(Label);
  245. l->set_text(p_label);
  246. add_child(l);
  247. MarginContainer *mc = memnew(MarginContainer);
  248. mc->add_constant_override("margin_left", 0);
  249. mc->add_child(p_control);
  250. add_child(mc);
  251. if (p_expand)
  252. mc->set_v_size_flags(SIZE_EXPAND_FILL);
  253. return mc;
  254. }