split_container.cpp 10 KB

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