split_container.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. }
  157. }
  158. void SplitContainer::_gui_input(const Ref<InputEvent> &p_event) {
  159. if (collapsed || !_getch(0) || !_getch(1) || dragger_visibility != DRAGGER_VISIBLE)
  160. return;
  161. Ref<InputEventMouseButton> mb = p_event;
  162. if (mb.is_valid()) {
  163. if (mb->get_button_index() == BUTTON_LEFT) {
  164. if (mb->is_pressed()) {
  165. int sep = get_constant("separation");
  166. if (vertical) {
  167. if (mb->get_position().y > middle_sep && mb->get_position().y < middle_sep + sep) {
  168. dragging = true;
  169. drag_from = mb->get_position().y;
  170. drag_ofs = split_offset;
  171. }
  172. } else {
  173. if (mb->get_position().x > middle_sep && mb->get_position().x < middle_sep + sep) {
  174. dragging = true;
  175. drag_from = mb->get_position().x;
  176. drag_ofs = split_offset;
  177. }
  178. }
  179. } else {
  180. dragging = false;
  181. }
  182. }
  183. }
  184. Ref<InputEventMouseMotion> mm = p_event;
  185. if (mm.is_valid() && dragging) {
  186. split_offset = drag_ofs + ((vertical ? mm->get_position().y : mm->get_position().x) - drag_from);
  187. should_clamp_split_offset = true;
  188. queue_sort();
  189. emit_signal("dragged", get_split_offset());
  190. }
  191. }
  192. Control::CursorShape SplitContainer::get_cursor_shape(const Point2 &p_pos) const {
  193. if (dragging)
  194. return (vertical ? CURSOR_VSIZE : CURSOR_HSIZE);
  195. if (!collapsed && _getch(0) && _getch(1) && dragger_visibility == DRAGGER_VISIBLE) {
  196. int sep = get_constant("separation");
  197. if (vertical) {
  198. if (p_pos.y > middle_sep && p_pos.y < middle_sep + sep)
  199. return CURSOR_VSIZE;
  200. } else {
  201. if (p_pos.x > middle_sep && p_pos.x < middle_sep + sep)
  202. return CURSOR_HSIZE;
  203. }
  204. }
  205. return Control::get_cursor_shape(p_pos);
  206. }
  207. void SplitContainer::set_split_offset(int p_offset) {
  208. if (split_offset == p_offset)
  209. return;
  210. split_offset = p_offset;
  211. queue_sort();
  212. }
  213. int SplitContainer::get_split_offset() const {
  214. return split_offset;
  215. }
  216. void SplitContainer::clamp_split_offset() {
  217. should_clamp_split_offset = true;
  218. queue_sort();
  219. }
  220. void SplitContainer::set_collapsed(bool p_collapsed) {
  221. if (collapsed == p_collapsed)
  222. return;
  223. collapsed = p_collapsed;
  224. queue_sort();
  225. }
  226. void SplitContainer::set_dragger_visibility(DraggerVisibility p_visibility) {
  227. dragger_visibility = p_visibility;
  228. queue_sort();
  229. update();
  230. }
  231. SplitContainer::DraggerVisibility SplitContainer::get_dragger_visibility() const {
  232. return dragger_visibility;
  233. }
  234. bool SplitContainer::is_collapsed() const {
  235. return collapsed;
  236. }
  237. void SplitContainer::_bind_methods() {
  238. ClassDB::bind_method(D_METHOD("_gui_input"), &SplitContainer::_gui_input);
  239. ClassDB::bind_method(D_METHOD("set_split_offset", "offset"), &SplitContainer::set_split_offset);
  240. ClassDB::bind_method(D_METHOD("get_split_offset"), &SplitContainer::get_split_offset);
  241. ClassDB::bind_method(D_METHOD("clamp_split_offset"), &SplitContainer::clamp_split_offset);
  242. ClassDB::bind_method(D_METHOD("set_collapsed", "collapsed"), &SplitContainer::set_collapsed);
  243. ClassDB::bind_method(D_METHOD("is_collapsed"), &SplitContainer::is_collapsed);
  244. ClassDB::bind_method(D_METHOD("set_dragger_visibility", "mode"), &SplitContainer::set_dragger_visibility);
  245. ClassDB::bind_method(D_METHOD("get_dragger_visibility"), &SplitContainer::get_dragger_visibility);
  246. ADD_SIGNAL(MethodInfo("dragged", PropertyInfo(Variant::INT, "offset")));
  247. ADD_PROPERTY(PropertyInfo(Variant::INT, "split_offset"), "set_split_offset", "get_split_offset");
  248. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collapsed"), "set_collapsed", "is_collapsed");
  249. ADD_PROPERTY(PropertyInfo(Variant::INT, "dragger_visibility", PROPERTY_HINT_ENUM, "Visible,Hidden,Hidden & Collapsed"), "set_dragger_visibility", "get_dragger_visibility");
  250. BIND_ENUM_CONSTANT(DRAGGER_VISIBLE);
  251. BIND_ENUM_CONSTANT(DRAGGER_HIDDEN);
  252. BIND_ENUM_CONSTANT(DRAGGER_HIDDEN_COLLAPSED);
  253. }
  254. SplitContainer::SplitContainer(bool p_vertical) {
  255. mouse_inside = false;
  256. split_offset = 0;
  257. should_clamp_split_offset = false;
  258. middle_sep = 0;
  259. vertical = p_vertical;
  260. dragging = false;
  261. collapsed = false;
  262. dragger_visibility = DRAGGER_VISIBLE;
  263. }