box_container.cpp 11 KB

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