split_container.cpp 11 KB

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