split_container.cpp 11 KB

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