box_container.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 = 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().has_flag(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().has_flag(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) * theme_cache.separation;
  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 (alignment) {
  136. case ALIGNMENT_BEGIN:
  137. if (rtl) {
  138. ofs = stretch_diff;
  139. }
  140. break;
  141. case ALIGNMENT_CENTER:
  142. ofs = stretch_diff / 2;
  143. break;
  144. case ALIGNMENT_END:
  145. if (!rtl) {
  146. ofs = stretch_diff;
  147. }
  148. break;
  149. }
  150. } else {
  151. switch (alignment) {
  152. case ALIGNMENT_BEGIN:
  153. break;
  154. case ALIGNMENT_CENTER:
  155. ofs = stretch_diff / 2;
  156. break;
  157. case ALIGNMENT_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 += theme_cache.separation;
  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. bool first = true;
  214. for (int i = 0; i < get_child_count(); i++) {
  215. Control *c = Object::cast_to<Control>(get_child(i));
  216. if (!c) {
  217. continue;
  218. }
  219. if (c->is_set_as_top_level()) {
  220. continue;
  221. }
  222. if (!c->is_visible()) {
  223. continue;
  224. }
  225. Size2i size = c->get_combined_minimum_size();
  226. if (vertical) { /* VERTICAL */
  227. if (size.width > minimum.width) {
  228. minimum.width = size.width;
  229. }
  230. minimum.height += size.height + (first ? 0 : theme_cache.separation);
  231. } else { /* HORIZONTAL */
  232. if (size.height > minimum.height) {
  233. minimum.height = size.height;
  234. }
  235. minimum.width += size.width + (first ? 0 : theme_cache.separation);
  236. }
  237. first = false;
  238. }
  239. return minimum;
  240. }
  241. void BoxContainer::_notification(int p_what) {
  242. switch (p_what) {
  243. case NOTIFICATION_SORT_CHILDREN: {
  244. _resort();
  245. } break;
  246. case NOTIFICATION_THEME_CHANGED: {
  247. update_minimum_size();
  248. } break;
  249. case NOTIFICATION_TRANSLATION_CHANGED:
  250. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  251. queue_sort();
  252. } break;
  253. }
  254. }
  255. void BoxContainer::_validate_property(PropertyInfo &p_property) const {
  256. if (is_fixed && p_property.name == "vertical") {
  257. p_property.usage = PROPERTY_USAGE_NONE;
  258. }
  259. }
  260. void BoxContainer::set_alignment(AlignmentMode p_alignment) {
  261. if (alignment == p_alignment) {
  262. return;
  263. }
  264. alignment = p_alignment;
  265. _resort();
  266. }
  267. BoxContainer::AlignmentMode BoxContainer::get_alignment() const {
  268. return alignment;
  269. }
  270. void BoxContainer::set_vertical(bool p_vertical) {
  271. ERR_FAIL_COND_MSG(is_fixed, "Can't change orientation of " + get_class() + ".");
  272. vertical = p_vertical;
  273. update_minimum_size();
  274. _resort();
  275. }
  276. bool BoxContainer::is_vertical() const {
  277. return vertical;
  278. }
  279. Control *BoxContainer::add_spacer(bool p_begin) {
  280. Control *c = memnew(Control);
  281. c->set_mouse_filter(MOUSE_FILTER_PASS); //allow spacer to pass mouse events
  282. if (vertical) {
  283. c->set_v_size_flags(SIZE_EXPAND_FILL);
  284. } else {
  285. c->set_h_size_flags(SIZE_EXPAND_FILL);
  286. }
  287. add_child(c);
  288. if (p_begin) {
  289. move_child(c, 0);
  290. }
  291. return c;
  292. }
  293. Vector<int> BoxContainer::get_allowed_size_flags_horizontal() const {
  294. Vector<int> flags;
  295. flags.append(SIZE_FILL);
  296. if (!vertical) {
  297. flags.append(SIZE_EXPAND);
  298. }
  299. flags.append(SIZE_SHRINK_BEGIN);
  300. flags.append(SIZE_SHRINK_CENTER);
  301. flags.append(SIZE_SHRINK_END);
  302. return flags;
  303. }
  304. Vector<int> BoxContainer::get_allowed_size_flags_vertical() const {
  305. Vector<int> flags;
  306. flags.append(SIZE_FILL);
  307. if (vertical) {
  308. flags.append(SIZE_EXPAND);
  309. }
  310. flags.append(SIZE_SHRINK_BEGIN);
  311. flags.append(SIZE_SHRINK_CENTER);
  312. flags.append(SIZE_SHRINK_END);
  313. return flags;
  314. }
  315. BoxContainer::BoxContainer(bool p_vertical) {
  316. vertical = p_vertical;
  317. }
  318. void BoxContainer::_bind_methods() {
  319. ClassDB::bind_method(D_METHOD("add_spacer", "begin"), &BoxContainer::add_spacer);
  320. ClassDB::bind_method(D_METHOD("set_alignment", "alignment"), &BoxContainer::set_alignment);
  321. ClassDB::bind_method(D_METHOD("get_alignment"), &BoxContainer::get_alignment);
  322. ClassDB::bind_method(D_METHOD("set_vertical", "vertical"), &BoxContainer::set_vertical);
  323. ClassDB::bind_method(D_METHOD("is_vertical"), &BoxContainer::is_vertical);
  324. BIND_ENUM_CONSTANT(ALIGNMENT_BEGIN);
  325. BIND_ENUM_CONSTANT(ALIGNMENT_CENTER);
  326. BIND_ENUM_CONSTANT(ALIGNMENT_END);
  327. ADD_PROPERTY(PropertyInfo(Variant::INT, "alignment", PROPERTY_HINT_ENUM, "Begin,Center,End"), "set_alignment", "get_alignment");
  328. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "vertical"), "set_vertical", "is_vertical");
  329. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, BoxContainer, separation);
  330. }
  331. MarginContainer *VBoxContainer::add_margin_child(const String &p_label, Control *p_control, bool p_expand) {
  332. Label *l = memnew(Label);
  333. l->set_theme_type_variation("HeaderSmall");
  334. l->set_text(p_label);
  335. add_child(l);
  336. MarginContainer *mc = memnew(MarginContainer);
  337. mc->add_theme_constant_override("margin_left", 0);
  338. mc->add_child(p_control, true);
  339. add_child(mc);
  340. if (p_expand) {
  341. mc->set_v_size_flags(SIZE_EXPAND_FILL);
  342. }
  343. return mc;
  344. }