split_container.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*************************************************************************/
  2. /* split_container.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 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 "split_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. Control *SplitContainer::_getch(int p_idx) const {
  38. int idx=0;
  39. for(int i=0;i<get_child_count();i++) {
  40. Control *c=get_child(i)->cast_to<Control>();
  41. if (!c || !c->is_visible())
  42. continue;
  43. if (c->is_set_as_toplevel())
  44. continue;
  45. if (idx==p_idx)
  46. return c;
  47. idx++;
  48. }
  49. return NULL;
  50. }
  51. void SplitContainer::_resort() {
  52. /** First pass, determine minimum size AND amount of stretchable elements */
  53. int axis = vertical?1:0;
  54. bool has_first=_getch(0);
  55. bool has_second=_getch(1);
  56. if (!has_first && !has_second) {
  57. return;
  58. } else if (! (has_first && has_second)) {
  59. if (has_first)
  60. fit_child_in_rect(_getch(0),Rect2(Point2(),get_size()));
  61. else
  62. fit_child_in_rect(_getch(1),Rect2(Point2(),get_size()));
  63. return;
  64. }
  65. Control *first=_getch(0);
  66. Control *second=_getch(1);
  67. bool ratiomode=false;
  68. bool expand_first_mode=false;
  69. if (vertical) {
  70. ratiomode=first->get_v_size_flags()&SIZE_EXPAND && second->get_v_size_flags()&SIZE_EXPAND;
  71. expand_first_mode=first->get_v_size_flags()&SIZE_EXPAND && !(second->get_v_size_flags()&SIZE_EXPAND);
  72. } else {
  73. ratiomode=first->get_h_size_flags()&SIZE_EXPAND && second->get_h_size_flags()&SIZE_EXPAND;
  74. expand_first_mode=first->get_h_size_flags()&SIZE_EXPAND && !(second->get_h_size_flags()&SIZE_EXPAND);
  75. }
  76. int sep=get_constant("separation");
  77. Ref<Texture> g = get_icon("grabber");
  78. if (dragger_visibility==DRAGGER_HIDDEN_COLLAPSED) {
  79. sep=0;
  80. } else {
  81. sep=MAX(sep,vertical?g->get_height():g->get_width());
  82. }
  83. int total = vertical?get_size().height:get_size().width;
  84. total-=sep;
  85. int minimum=0;
  86. Size2 ms_first = first->get_combined_minimum_size();
  87. Size2 ms_second = second->get_combined_minimum_size();
  88. if (vertical) {
  89. minimum=ms_first.height+ms_second.height;
  90. } else {
  91. minimum=ms_first.width+ms_second.width;
  92. }
  93. int available=total-minimum;
  94. if (available<0)
  95. available=0;
  96. middle_sep=0;
  97. if (collapsed) {
  98. if (ratiomode) {
  99. middle_sep=ms_first[axis]+available/2;
  100. } else if (expand_first_mode) {
  101. middle_sep=get_size()[axis]-ms_second[axis]-sep;
  102. } else {
  103. middle_sep=ms_first[axis];
  104. }
  105. } else if (ratiomode) {
  106. if (expand_ofs<-(available/2))
  107. expand_ofs=-(available/2);
  108. else if (expand_ofs>(available/2))
  109. expand_ofs=(available/2);
  110. middle_sep=ms_first[axis]+available/2+expand_ofs;
  111. } else if (expand_first_mode) {
  112. if (expand_ofs>0)
  113. expand_ofs=0;
  114. if (expand_ofs < -available)
  115. expand_ofs=-available;
  116. middle_sep=get_size()[axis]-ms_second[axis]-sep+expand_ofs;
  117. } else {
  118. if (expand_ofs<0)
  119. expand_ofs=0;
  120. if (expand_ofs > available)
  121. expand_ofs=available;
  122. middle_sep=ms_first[axis]+expand_ofs;
  123. }
  124. if (vertical) {
  125. fit_child_in_rect(first,Rect2(Point2(0,0),Size2(get_size().width,middle_sep)));
  126. int sofs=middle_sep+sep;
  127. fit_child_in_rect(second,Rect2(Point2(0,sofs),Size2(get_size().width,get_size().height-sofs)));
  128. } else {
  129. fit_child_in_rect(first,Rect2(Point2(0,0),Size2(middle_sep,get_size().height)));
  130. int sofs=middle_sep+sep;
  131. fit_child_in_rect(second,Rect2(Point2(sofs,0),Size2(get_size().width-sofs,get_size().height)));
  132. }
  133. update();
  134. _change_notify("split/offset");
  135. }
  136. Size2 SplitContainer::get_minimum_size() const {
  137. /* Calculate MINIMUM SIZE */
  138. Size2i minimum;
  139. int sep=get_constant("separation");
  140. Ref<Texture> g = get_icon("grabber");
  141. sep=(dragger_visibility!=DRAGGER_HIDDEN_COLLAPSED)?MAX(sep,vertical?g->get_height():g->get_width()):0;
  142. for(int i=0;i<2;i++) {
  143. if (!_getch(i))
  144. break;
  145. if (i==1) {
  146. if (vertical)
  147. minimum.height+=sep;
  148. else
  149. minimum.width+=sep;
  150. }
  151. Size2 ms = _getch(i)->get_combined_minimum_size();
  152. if (vertical) {
  153. minimum.height+=ms.height;
  154. minimum.width=MAX(minimum.width,ms.width);
  155. } else {
  156. minimum.width+=ms.width;
  157. minimum.height=MAX(minimum.height,ms.height);
  158. }
  159. }
  160. return minimum;
  161. }
  162. void SplitContainer::_notification(int p_what) {
  163. switch(p_what) {
  164. case NOTIFICATION_SORT_CHILDREN: {
  165. _resort();
  166. } break;
  167. case NOTIFICATION_MOUSE_ENTER: {
  168. mouse_inside=true;
  169. update();
  170. } break;
  171. case NOTIFICATION_MOUSE_EXIT: {
  172. mouse_inside=false;
  173. update();
  174. } break;
  175. case NOTIFICATION_DRAW: {
  176. if (!_getch(0) || !_getch(1))
  177. return;
  178. if (collapsed || (!mouse_inside && get_constant("autohide")))
  179. return;
  180. int sep=dragger_visibility!=DRAGGER_HIDDEN_COLLAPSED?get_constant("separation"):0;
  181. Ref<Texture> tex = get_icon("grabber");
  182. Size2 size=get_size();
  183. if (vertical) {
  184. //draw_style_box( get_stylebox("bg"), Rect2(0,middle_sep,get_size().width,sep));
  185. if (dragger_visibility==DRAGGER_VISIBLE)
  186. draw_texture(tex,Point2i((size.x-tex->get_width())/2,middle_sep+(sep-tex->get_height())/2));
  187. } else {
  188. //draw_style_box( get_stylebox("bg"), Rect2(middle_sep,0,sep,get_size().height));
  189. if (dragger_visibility==DRAGGER_VISIBLE)
  190. draw_texture(tex,Point2i(middle_sep+(sep-tex->get_width())/2,(size.y-tex->get_height())/2));
  191. }
  192. } break;
  193. }
  194. }
  195. void SplitContainer::_input_event(const InputEvent& p_event) {
  196. if (collapsed || !_getch(0) || !_getch(1) || dragger_visibility!=DRAGGER_VISIBLE)
  197. return;
  198. if (p_event.type==InputEvent::MOUSE_BUTTON) {
  199. const InputEventMouseButton &mb=p_event.mouse_button;
  200. if (mb.button_index==BUTTON_LEFT) {
  201. if (mb.pressed) {
  202. int sep=get_constant("separation");
  203. if (vertical) {
  204. if (mb.y > middle_sep && mb.y < middle_sep+sep) {
  205. dragging=true;
  206. drag_from=mb.y;
  207. drag_ofs=expand_ofs;
  208. }
  209. } else {
  210. if (mb.x > middle_sep && mb.x < middle_sep+sep) {
  211. dragging=true;
  212. drag_from=mb.x;
  213. drag_ofs=expand_ofs;
  214. }
  215. }
  216. } else {
  217. dragging=false;
  218. }
  219. }
  220. }
  221. if (p_event.type==InputEvent::MOUSE_MOTION) {
  222. const InputEventMouseMotion &mm=p_event.mouse_motion;
  223. if (dragging) {
  224. expand_ofs=drag_ofs+((vertical?mm.y:mm.x)-drag_from);
  225. queue_sort();
  226. emit_signal("dragged",get_split_offset());
  227. }
  228. }
  229. }
  230. Control::CursorShape SplitContainer::get_cursor_shape(const Point2& p_pos) {
  231. if (collapsed)
  232. return Control::get_cursor_shape(p_pos);
  233. if (dragging)
  234. return (vertical?CURSOR_VSIZE:CURSOR_HSIZE);
  235. int sep=get_constant("separation");
  236. if (vertical) {
  237. if (p_pos.y > middle_sep && p_pos.y < middle_sep+sep) {
  238. return CURSOR_VSIZE;
  239. }
  240. } else {
  241. if (p_pos.x > middle_sep && p_pos.x < middle_sep+sep) {
  242. return CURSOR_HSIZE;
  243. }
  244. }
  245. return Control::get_cursor_shape(p_pos);
  246. }
  247. void SplitContainer::set_split_offset(int p_offset) {
  248. if (expand_ofs==p_offset)
  249. return;
  250. expand_ofs=p_offset;
  251. queue_sort();
  252. }
  253. int SplitContainer::get_split_offset() const {
  254. return expand_ofs;
  255. }
  256. void SplitContainer::set_collapsed(bool p_collapsed) {
  257. if (collapsed==p_collapsed)
  258. return;
  259. collapsed=p_collapsed;
  260. queue_sort();
  261. }
  262. void SplitContainer::set_dragger_visibility(DraggerVisibility p_visibility) {
  263. dragger_visibility=p_visibility;
  264. queue_sort();
  265. update();
  266. }
  267. SplitContainer::DraggerVisibility SplitContainer::get_dragger_visibility() const {
  268. return dragger_visibility;
  269. }
  270. bool SplitContainer::is_collapsed() const {
  271. return collapsed;
  272. }
  273. void SplitContainer::_bind_methods() {
  274. ObjectTypeDB::bind_method(_MD("_input_event"),&SplitContainer::_input_event);
  275. ObjectTypeDB::bind_method(_MD("set_split_offset","offset"),&SplitContainer::set_split_offset);
  276. ObjectTypeDB::bind_method(_MD("get_split_offset"),&SplitContainer::get_split_offset);
  277. ObjectTypeDB::bind_method(_MD("set_collapsed","collapsed"),&SplitContainer::set_collapsed);
  278. ObjectTypeDB::bind_method(_MD("is_collapsed"),&SplitContainer::is_collapsed);
  279. ObjectTypeDB::bind_method(_MD("set_dragger_visibility","mode"),&SplitContainer::set_dragger_visibility);
  280. ObjectTypeDB::bind_method(_MD("get_dragger_visibility"),&SplitContainer::get_dragger_visibility);
  281. ADD_SIGNAL( MethodInfo("dragged",PropertyInfo(Variant::INT,"offset")));
  282. ADD_PROPERTY( PropertyInfo(Variant::INT,"split/offset"),_SCS("set_split_offset"),_SCS("get_split_offset"));
  283. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"split/collapsed"),_SCS("set_collapsed"),_SCS("is_collapsed"));
  284. ADD_PROPERTY( PropertyInfo(Variant::INT,"split/dragger_visibility",PROPERTY_HINT_ENUM,"Visible,Hidden,Hidden & Collapsed"),_SCS("set_dragger_visibility"),_SCS("get_dragger_visibility"));
  285. BIND_CONSTANT( DRAGGER_VISIBLE );
  286. BIND_CONSTANT( DRAGGER_HIDDEN );
  287. BIND_CONSTANT( DRAGGER_HIDDEN_COLLAPSED );
  288. }
  289. SplitContainer::SplitContainer(bool p_vertical) {
  290. mouse_inside=false;
  291. expand_ofs=0;
  292. middle_sep=0;
  293. vertical=p_vertical;
  294. dragging=false;
  295. collapsed=false;
  296. dragger_visibility=DRAGGER_VISIBLE;
  297. }