box_container.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*************************************************************************/
  2. /* box_container.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "box_container.h"
  30. #include "margin_container.h"
  31. #include "label.h"
  32. struct _MinSizeCache {
  33. int min_size;
  34. bool will_stretch;
  35. int final_size;
  36. };
  37. void BoxContainer::_resort() {
  38. /** First pass, determine minimum size AND amount of stretchable elements */
  39. Size2i new_size=get_size();;
  40. int sep=get_constant("separation",vertical?"VBoxContainer":"HBoxContainer");
  41. bool first=true;
  42. int children_count=0;
  43. int stretch_min=0;
  44. int stretch_avail=0;
  45. float stretch_ratio_total=0;
  46. Map<Control*,_MinSizeCache> min_size_cache;
  47. for(int i=0;i<get_child_count();i++) {
  48. Control *c=get_child(i)->cast_to<Control>();
  49. if (!c || !c->is_visible())
  50. continue;
  51. if (c->is_set_as_toplevel())
  52. continue;
  53. Size2i size=c->get_combined_minimum_size();
  54. _MinSizeCache msc;
  55. if (vertical) { /* VERTICAL */
  56. stretch_min+=size.height;
  57. msc.min_size=size.height;
  58. msc.will_stretch=c->get_v_size_flags() & SIZE_EXPAND;
  59. } else { /* HORIZONTAL */
  60. stretch_min+=size.width;
  61. msc.min_size=size.width;
  62. msc.will_stretch=c->get_h_size_flags() & SIZE_EXPAND;
  63. }
  64. if (msc.will_stretch) {
  65. stretch_avail+=msc.min_size;
  66. stretch_ratio_total+=c->get_stretch_ratio();
  67. }
  68. msc.final_size=msc.min_size;
  69. min_size_cache[c]=msc;
  70. children_count++;
  71. }
  72. if (children_count==0)
  73. return;
  74. int stretch_max = (vertical? new_size.height : new_size.width ) - (children_count-1) * sep;
  75. int stretch_diff = stretch_max - stretch_min;
  76. if (stretch_diff<0) {
  77. //avoid negative stretch space
  78. stretch_max=stretch_min;
  79. stretch_diff=0;
  80. }
  81. stretch_avail+=stretch_diff; //available stretch space.
  82. /** Second, pass sucessively to discard elements that can't be stretched, this will run while stretchable
  83. elements exist */
  84. while(stretch_ratio_total>0) { // first of all, dont even be here if no stretchable objects exist
  85. bool refit_successful=true; //assume refit-test will go well
  86. for(int i=0;i<get_child_count();i++) {
  87. Control *c=get_child(i)->cast_to<Control>();
  88. if (!c || !c->is_visible())
  89. continue;
  90. if (c->is_set_as_toplevel())
  91. continue;
  92. ERR_FAIL_COND(!min_size_cache.has(c));
  93. _MinSizeCache &msc=min_size_cache[c];
  94. if (msc.will_stretch) { //wants to stretch
  95. //let's see if it can really stretch
  96. int final_pixel_size=stretch_avail * c->get_stretch_ratio() / stretch_ratio_total;
  97. if (final_pixel_size<msc.min_size) {
  98. //if available stretching area is too small for widget,
  99. //then remove it from stretching area
  100. msc.will_stretch=false;
  101. stretch_ratio_total-=c->get_stretch_ratio();
  102. refit_successful=false;
  103. stretch_avail-=msc.min_size;
  104. msc.final_size=msc.min_size;
  105. break;
  106. } else {
  107. msc.final_size=final_pixel_size;
  108. }
  109. }
  110. }
  111. if (refit_successful) //uf refit went well, break
  112. break;
  113. }
  114. /** Final pass, draw and stretch elements **/
  115. int ofs=0;
  116. first=true;
  117. int idx=0;
  118. for(int i=0;i<get_child_count();i++) {
  119. Control *c=get_child(i)->cast_to<Control>();
  120. if (!c || !c->is_visible())
  121. continue;
  122. if (c->is_set_as_toplevel())
  123. continue;
  124. _MinSizeCache &msc=min_size_cache[c];
  125. if (first)
  126. first=false;
  127. else
  128. ofs+=sep;
  129. int from=ofs;
  130. int to=ofs+msc.final_size;
  131. if (msc.will_stretch && idx==children_count-1) {
  132. //adjust so the last one always fits perfect
  133. //compensating for numerical imprecision
  134. to=vertical?new_size.height:new_size.width;
  135. }
  136. int size=to-from;
  137. Rect2 rect;
  138. if (vertical) {
  139. rect=Rect2(0,from,new_size.width,size);
  140. } else {
  141. rect=Rect2(from,0,size,new_size.height);
  142. }
  143. fit_child_in_rect(c,rect);
  144. ofs=to;
  145. idx++;
  146. }
  147. }
  148. Size2 BoxContainer::get_minimum_size() const {
  149. /* Calculate MINIMUM SIZE */
  150. Size2i minimum;
  151. int sep=get_constant("separation",vertical?"VBoxContainer":"HBoxContainer");
  152. bool first=true;
  153. for(int i=0;i<get_child_count();i++) {
  154. Control *c=get_child(i)->cast_to<Control>();
  155. if (!c)
  156. continue;
  157. if (c->is_set_as_toplevel())
  158. continue;
  159. if (c->is_hidden()) {
  160. continue;
  161. }
  162. Size2i size=c->get_combined_minimum_size();
  163. if (vertical) { /* VERTICAL */
  164. if ( size.width > minimum.width ) {
  165. minimum.width=size.width;
  166. }
  167. minimum.height+=size.height+(first?0:sep);
  168. } else { /* HORIZONTAL */
  169. if ( size.height > minimum.height ) {
  170. minimum.height=size.height;
  171. }
  172. minimum.width+=size.width+(first?0:sep);
  173. }
  174. first=false;
  175. }
  176. return minimum;
  177. }
  178. void BoxContainer::_notification(int p_what) {
  179. switch(p_what) {
  180. case NOTIFICATION_SORT_CHILDREN: {
  181. _resort();
  182. } break;
  183. }
  184. }
  185. void BoxContainer::add_spacer(bool p_begin) {
  186. Control *c = memnew( Control );
  187. if (vertical)
  188. c->set_v_size_flags(SIZE_EXPAND_FILL);
  189. else
  190. c->set_h_size_flags(SIZE_EXPAND_FILL);
  191. add_child(c);
  192. if (p_begin)
  193. move_child(c,0);
  194. }
  195. BoxContainer::BoxContainer(bool p_vertical) {
  196. vertical=p_vertical;
  197. // set_ignore_mouse(true);
  198. set_stop_mouse(false);
  199. }
  200. MarginContainer* VBoxContainer::add_margin_child(const String& p_label,Control *p_control,bool p_expand) {
  201. Label *l = memnew( Label );
  202. l->set_text(p_label);
  203. add_child(l);
  204. MarginContainer *mc = memnew( MarginContainer );
  205. mc->add_child(p_control);
  206. add_child(mc);
  207. if (p_expand)
  208. mc->set_v_size_flags(SIZE_EXPAND_FILL);
  209. return mc;
  210. }