box_container.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*************************************************************************/
  2. /* box_container.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 = get_child(i)->cast_to<Control>();
  50. if (!c || !c->is_visible())
  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_max = stretch_min;
  80. stretch_diff = 0;
  81. }
  82. stretch_avail += stretch_diff; //available stretch space.
  83. /** Second, pass sucessively 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, dont even be here if no stretchable objects exist
  87. has_stretched = true;
  88. bool refit_successful = true; //assume refit-test will go well
  89. for (int i = 0; i < get_child_count(); i++) {
  90. Control *c = get_child(i)->cast_to<Control>();
  91. if (!c || !c->is_visible())
  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. int final_pixel_size = stretch_avail * c->get_stretch_ratio() / stretch_ratio_total;
  100. if (final_pixel_size < msc.min_size) {
  101. //if available stretching area is too small for widget,
  102. //then remove it from stretching area
  103. msc.will_stretch = false;
  104. stretch_ratio_total -= c->get_stretch_ratio();
  105. refit_successful = false;
  106. stretch_avail -= msc.min_size;
  107. msc.final_size = msc.min_size;
  108. break;
  109. } else {
  110. msc.final_size = final_pixel_size;
  111. }
  112. }
  113. }
  114. if (refit_successful) //uf refit went well, break
  115. break;
  116. }
  117. /** Final pass, draw and stretch elements **/
  118. int ofs = 0;
  119. if (!has_stretched) {
  120. switch (align) {
  121. case ALIGN_BEGIN:
  122. break;
  123. case ALIGN_CENTER:
  124. ofs = stretch_diff / 2;
  125. break;
  126. case ALIGN_END:
  127. ofs = stretch_diff;
  128. break;
  129. }
  130. }
  131. first = true;
  132. int idx = 0;
  133. for (int i = 0; i < get_child_count(); i++) {
  134. Control *c = get_child(i)->cast_to<Control>();
  135. if (!c || !c->is_visible())
  136. continue;
  137. if (c->is_set_as_toplevel())
  138. continue;
  139. _MinSizeCache &msc = min_size_cache[c];
  140. if (first)
  141. first = false;
  142. else
  143. ofs += sep;
  144. int from = ofs;
  145. int to = ofs + msc.final_size;
  146. if (msc.will_stretch && idx == children_count - 1) {
  147. //adjust so the last one always fits perfect
  148. //compensating for numerical imprecision
  149. to = vertical ? new_size.height : new_size.width;
  150. }
  151. int size = to - from;
  152. Rect2 rect;
  153. if (vertical) {
  154. rect = Rect2(0, from, new_size.width, size);
  155. } else {
  156. rect = Rect2(from, 0, size, new_size.height);
  157. }
  158. fit_child_in_rect(c, rect);
  159. ofs = to;
  160. idx++;
  161. }
  162. }
  163. Size2 BoxContainer::get_minimum_size() const {
  164. /* Calculate MINIMUM SIZE */
  165. Size2i minimum;
  166. int sep = get_constant("separation"); //,vertical?"VBoxContainer":"HBoxContainer");
  167. bool first = true;
  168. for (int i = 0; i < get_child_count(); i++) {
  169. Control *c = get_child(i)->cast_to<Control>();
  170. if (!c)
  171. continue;
  172. if (c->is_set_as_toplevel())
  173. continue;
  174. if (c->is_hidden()) {
  175. continue;
  176. }
  177. Size2i size = c->get_combined_minimum_size();
  178. if (vertical) { /* VERTICAL */
  179. if (size.width > minimum.width) {
  180. minimum.width = size.width;
  181. }
  182. minimum.height += size.height + (first ? 0 : sep);
  183. } else { /* HORIZONTAL */
  184. if (size.height > minimum.height) {
  185. minimum.height = size.height;
  186. }
  187. minimum.width += size.width + (first ? 0 : sep);
  188. }
  189. first = false;
  190. }
  191. return minimum;
  192. }
  193. void BoxContainer::_notification(int p_what) {
  194. switch (p_what) {
  195. case NOTIFICATION_SORT_CHILDREN: {
  196. _resort();
  197. } break;
  198. }
  199. }
  200. void BoxContainer::set_alignment(AlignMode p_align) {
  201. align = p_align;
  202. _resort();
  203. }
  204. BoxContainer::AlignMode BoxContainer::get_alignment() const {
  205. return align;
  206. }
  207. void BoxContainer::add_spacer(bool p_begin) {
  208. Control *c = memnew(Control);
  209. c->set_stop_mouse(false);
  210. if (vertical)
  211. c->set_v_size_flags(SIZE_EXPAND_FILL);
  212. else
  213. c->set_h_size_flags(SIZE_EXPAND_FILL);
  214. add_child(c);
  215. if (p_begin)
  216. move_child(c, 0);
  217. }
  218. BoxContainer::BoxContainer(bool p_vertical) {
  219. vertical = p_vertical;
  220. align = ALIGN_BEGIN;
  221. // set_ignore_mouse(true);
  222. set_stop_mouse(false);
  223. }
  224. void BoxContainer::_bind_methods() {
  225. ObjectTypeDB::bind_method(_MD("add_spacer", "begin"), &BoxContainer::add_spacer);
  226. ObjectTypeDB::bind_method(_MD("get_alignment"), &BoxContainer::get_alignment);
  227. ObjectTypeDB::bind_method(_MD("set_alignment", "alignment"), &BoxContainer::set_alignment);
  228. BIND_CONSTANT(ALIGN_BEGIN);
  229. BIND_CONSTANT(ALIGN_CENTER);
  230. BIND_CONSTANT(ALIGN_END);
  231. ADD_PROPERTY(PropertyInfo(Variant::INT, "alignment", PROPERTY_HINT_ENUM, "Begin,Center,End"), _SCS("set_alignment"), _SCS("get_alignment"));
  232. }
  233. MarginContainer *VBoxContainer::add_margin_child(const String &p_label, Control *p_control, bool p_expand) {
  234. Label *l = memnew(Label);
  235. l->set_text(p_label);
  236. add_child(l);
  237. MarginContainer *mc = memnew(MarginContainer);
  238. mc->add_child(p_control);
  239. add_child(mc);
  240. if (p_expand)
  241. mc->set_v_size_flags(SIZE_EXPAND_FILL);
  242. return mc;
  243. }