2
0

split_container.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. /**************************************************************************/
  2. /* split_container.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "scene/gui/texture_rect.h"
  32. #include "scene/main/viewport.h"
  33. #include "scene/theme/theme_db.h"
  34. void SplitContainerDragger::gui_input(const Ref<InputEvent> &p_event) {
  35. ERR_FAIL_COND(p_event.is_null());
  36. SplitContainer *sc = Object::cast_to<SplitContainer>(get_parent());
  37. if (sc->collapsed || !sc->_get_sortable_child(0) || !sc->_get_sortable_child(1) || !sc->dragging_enabled) {
  38. return;
  39. }
  40. Ref<InputEventMouseButton> mb = p_event;
  41. if (mb.is_valid()) {
  42. if (mb->get_button_index() == MouseButton::LEFT) {
  43. if (mb->is_pressed()) {
  44. sc->_compute_split_offset(true);
  45. dragging = true;
  46. sc->emit_signal(SNAME("drag_started"));
  47. drag_ofs = sc->split_offset;
  48. if (sc->vertical) {
  49. drag_from = get_transform().xform(mb->get_position()).y;
  50. } else {
  51. drag_from = get_transform().xform(mb->get_position()).x;
  52. }
  53. } else {
  54. dragging = false;
  55. queue_redraw();
  56. sc->emit_signal(SNAME("drag_ended"));
  57. }
  58. }
  59. }
  60. Ref<InputEventMouseMotion> mm = p_event;
  61. if (mm.is_valid()) {
  62. if (!dragging) {
  63. return;
  64. }
  65. Vector2i in_parent_pos = get_transform().xform(mm->get_position());
  66. if (!sc->vertical && is_layout_rtl()) {
  67. sc->split_offset = drag_ofs - (in_parent_pos.x - drag_from);
  68. } else {
  69. sc->split_offset = drag_ofs + ((sc->vertical ? in_parent_pos.y : in_parent_pos.x) - drag_from);
  70. }
  71. sc->_compute_split_offset(true);
  72. sc->queue_sort();
  73. sc->emit_signal(SNAME("dragged"), sc->get_split_offset());
  74. }
  75. }
  76. Control::CursorShape SplitContainerDragger::get_cursor_shape(const Point2 &p_pos) const {
  77. SplitContainer *sc = Object::cast_to<SplitContainer>(get_parent());
  78. if (!sc->collapsed && sc->dragging_enabled) {
  79. return (sc->vertical ? CURSOR_VSPLIT : CURSOR_HSPLIT);
  80. }
  81. return Control::get_cursor_shape(p_pos);
  82. }
  83. void SplitContainerDragger::_accessibility_action_inc(const Variant &p_data) {
  84. SplitContainer *sc = Object::cast_to<SplitContainer>(get_parent());
  85. if (sc->collapsed || !sc->_get_sortable_child(0) || !sc->_get_sortable_child(1) || !sc->dragging_enabled) {
  86. return;
  87. }
  88. sc->split_offset -= 10;
  89. sc->_compute_split_offset(true);
  90. sc->queue_sort();
  91. }
  92. void SplitContainerDragger::_accessibility_action_dec(const Variant &p_data) {
  93. SplitContainer *sc = Object::cast_to<SplitContainer>(get_parent());
  94. if (sc->collapsed || !sc->_get_sortable_child(0) || !sc->_get_sortable_child(1) || !sc->dragging_enabled) {
  95. return;
  96. }
  97. sc->split_offset += 10;
  98. sc->_compute_split_offset(true);
  99. sc->queue_sort();
  100. }
  101. void SplitContainerDragger::_accessibility_action_set_value(const Variant &p_data) {
  102. SplitContainer *sc = Object::cast_to<SplitContainer>(get_parent());
  103. if (sc->collapsed || !sc->_get_sortable_child(0) || !sc->_get_sortable_child(1) || !sc->dragging_enabled) {
  104. return;
  105. }
  106. sc->split_offset = p_data;
  107. sc->_compute_split_offset(true);
  108. sc->queue_sort();
  109. }
  110. void SplitContainerDragger::_notification(int p_what) {
  111. switch (p_what) {
  112. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  113. RID ae = get_accessibility_element();
  114. ERR_FAIL_COND(ae.is_null());
  115. DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_SPLITTER);
  116. SplitContainer *sc = Object::cast_to<SplitContainer>(get_parent());
  117. if (sc->collapsed || !sc->_get_sortable_child(0) || !sc->_get_sortable_child(1) || !sc->dragging_enabled) {
  118. return;
  119. }
  120. sc->_compute_split_offset(true);
  121. DisplayServer::get_singleton()->accessibility_update_set_num_value(ae, sc->split_offset);
  122. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_DECREMENT, callable_mp(this, &SplitContainerDragger::_accessibility_action_dec));
  123. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_INCREMENT, callable_mp(this, &SplitContainerDragger::_accessibility_action_inc));
  124. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SET_VALUE, callable_mp(this, &SplitContainerDragger::_accessibility_action_set_value));
  125. } break;
  126. case NOTIFICATION_MOUSE_ENTER: {
  127. mouse_inside = true;
  128. SplitContainer *sc = Object::cast_to<SplitContainer>(get_parent());
  129. if (sc->theme_cache.autohide) {
  130. queue_redraw();
  131. }
  132. } break;
  133. case NOTIFICATION_MOUSE_EXIT: {
  134. mouse_inside = false;
  135. SplitContainer *sc = Object::cast_to<SplitContainer>(get_parent());
  136. if (sc->theme_cache.autohide) {
  137. queue_redraw();
  138. }
  139. } break;
  140. case NOTIFICATION_DRAW: {
  141. SplitContainer *sc = Object::cast_to<SplitContainer>(get_parent());
  142. draw_style_box(sc->theme_cache.split_bar_background, split_bar_rect);
  143. if (sc->dragger_visibility == sc->DRAGGER_VISIBLE && (dragging || mouse_inside || !sc->theme_cache.autohide) && !sc->touch_dragger_enabled) {
  144. Ref<Texture2D> tex = sc->_get_grabber_icon();
  145. float available_size = sc->vertical ? (sc->get_size().x - tex->get_size().x) : (sc->get_size().y - tex->get_size().y);
  146. if (available_size - sc->drag_area_margin_begin - sc->drag_area_margin_end > 0) { // Draw the grabber only if it fits.
  147. draw_texture(tex, (split_bar_rect.get_position() + (split_bar_rect.get_size() - tex->get_size()) * 0.5));
  148. }
  149. }
  150. if (sc->show_drag_area && Engine::get_singleton()->is_editor_hint()) {
  151. draw_rect(Rect2(Vector2(0, 0), get_size()), sc->dragging_enabled ? Color(1, 1, 0, 0.3) : Color(1, 0, 0, 0.3));
  152. }
  153. } break;
  154. }
  155. }
  156. SplitContainerDragger::SplitContainerDragger() {
  157. set_focus_mode(FOCUS_ACCESSIBILITY);
  158. }
  159. Control *SplitContainer::_get_sortable_child(int p_idx, SortableVisibilityMode p_visibility_mode) const {
  160. int idx = 0;
  161. for (int i = 0; i < get_child_count(false); i++) {
  162. Control *c = as_sortable_control(get_child(i, false), p_visibility_mode);
  163. if (!c) {
  164. continue;
  165. }
  166. if (idx == p_idx) {
  167. return c;
  168. }
  169. idx++;
  170. }
  171. return nullptr;
  172. }
  173. Ref<Texture2D> SplitContainer::_get_grabber_icon() const {
  174. if (is_fixed) {
  175. return theme_cache.grabber_icon;
  176. } else {
  177. if (vertical) {
  178. return theme_cache.grabber_icon_v;
  179. } else {
  180. return theme_cache.grabber_icon_h;
  181. }
  182. }
  183. }
  184. Ref<Texture2D> SplitContainer::_get_touch_dragger_icon() const {
  185. if (is_fixed) {
  186. return theme_cache.touch_dragger_icon;
  187. } else {
  188. if (vertical) {
  189. return theme_cache.touch_dragger_icon_v;
  190. } else {
  191. return theme_cache.touch_dragger_icon_h;
  192. }
  193. }
  194. }
  195. int SplitContainer::_get_separation() const {
  196. if (dragger_visibility == DRAGGER_HIDDEN_COLLAPSED) {
  197. return 0;
  198. }
  199. if (touch_dragger_enabled) {
  200. return theme_cache.separation;
  201. }
  202. // DRAGGER_VISIBLE or DRAGGER_HIDDEN.
  203. Ref<Texture2D> g = _get_grabber_icon();
  204. return MAX(theme_cache.separation, vertical ? g->get_height() : g->get_width());
  205. }
  206. void SplitContainer::_compute_split_offset(bool p_clamp) {
  207. Control *first = _get_sortable_child(0);
  208. Control *second = _get_sortable_child(1);
  209. int axis_index = vertical ? 1 : 0;
  210. int size = get_size()[axis_index];
  211. int sep = _get_separation();
  212. // Compute the wished size.
  213. int wished_size = 0;
  214. int split_offset_with_collapse = 0;
  215. if (!collapsed) {
  216. split_offset_with_collapse = split_offset;
  217. }
  218. bool first_is_expanded = (vertical ? first->get_v_size_flags() : first->get_h_size_flags()) & SIZE_EXPAND;
  219. bool second_is_expanded = (vertical ? second->get_v_size_flags() : second->get_h_size_flags()) & SIZE_EXPAND;
  220. if (first_is_expanded && second_is_expanded) {
  221. float ratio = first->get_stretch_ratio() / (first->get_stretch_ratio() + second->get_stretch_ratio());
  222. wished_size = size * ratio - sep * 0.5 + split_offset_with_collapse;
  223. } else if (first_is_expanded) {
  224. wished_size = size - sep + split_offset_with_collapse;
  225. } else {
  226. wished_size = split_offset_with_collapse;
  227. }
  228. // Clamp the split offset to acceptable values.
  229. int first_min_size = first->get_combined_minimum_size()[axis_index];
  230. int second_min_size = second->get_combined_minimum_size()[axis_index];
  231. computed_split_offset = CLAMP(wished_size, first_min_size, size - sep - second_min_size);
  232. // Clamp the split_offset if requested.
  233. if (p_clamp) {
  234. split_offset -= wished_size - computed_split_offset;
  235. }
  236. }
  237. void SplitContainer::_resort() {
  238. Control *first = _get_sortable_child(0);
  239. Control *second = _get_sortable_child(1);
  240. if (!first || !second) { // Only one child.
  241. if (first) {
  242. fit_child_in_rect(first, Rect2(Point2(), get_size()));
  243. } else if (second) {
  244. fit_child_in_rect(second, Rect2(Point2(), get_size()));
  245. }
  246. dragging_area_control->hide();
  247. return;
  248. }
  249. dragging_area_control->set_visible(!collapsed);
  250. if (touch_dragger_enabled) {
  251. touch_dragger->set_visible(dragging_enabled);
  252. }
  253. _compute_split_offset(false); // This recalculates and sets computed_split_offset.
  254. int sep = _get_separation();
  255. bool is_rtl = is_layout_rtl();
  256. // Move the children.
  257. if (vertical) {
  258. fit_child_in_rect(first, Rect2(Point2(0, 0), Size2(get_size().width, computed_split_offset)));
  259. int sofs = computed_split_offset + sep;
  260. fit_child_in_rect(second, Rect2(Point2(0, sofs), Size2(get_size().width, get_size().height - sofs)));
  261. } else {
  262. if (is_rtl) {
  263. computed_split_offset = get_size().width - computed_split_offset - sep;
  264. fit_child_in_rect(second, Rect2(Point2(0, 0), Size2(computed_split_offset, get_size().height)));
  265. int sofs = computed_split_offset + sep;
  266. fit_child_in_rect(first, Rect2(Point2(sofs, 0), Size2(get_size().width - sofs, get_size().height)));
  267. } else {
  268. fit_child_in_rect(first, Rect2(Point2(0, 0), Size2(computed_split_offset, get_size().height)));
  269. int sofs = computed_split_offset + sep;
  270. fit_child_in_rect(second, Rect2(Point2(sofs, 0), Size2(get_size().width - sofs, get_size().height)));
  271. }
  272. }
  273. dragging_area_control->set_mouse_filter(dragging_enabled ? MOUSE_FILTER_STOP : MOUSE_FILTER_IGNORE);
  274. const int dragger_ctrl_size = MAX(sep, theme_cache.minimum_grab_thickness);
  275. float split_bar_offset = (dragger_ctrl_size - sep) * 0.5;
  276. if (vertical) {
  277. Rect2 split_bar_rect = Rect2(is_rtl ? drag_area_margin_end : drag_area_margin_begin, computed_split_offset, get_size().width - drag_area_margin_begin - drag_area_margin_end, sep);
  278. dragging_area_control->set_rect(Rect2(split_bar_rect.position.x, split_bar_rect.position.y - split_bar_offset + drag_area_offset, split_bar_rect.size.x, dragger_ctrl_size));
  279. dragging_area_control->split_bar_rect = Rect2(Vector2(0.0, int(split_bar_offset) - drag_area_offset), split_bar_rect.size);
  280. } else {
  281. Rect2 split_bar_rect = Rect2(computed_split_offset, drag_area_margin_begin, sep, get_size().height - drag_area_margin_begin - drag_area_margin_end);
  282. dragging_area_control->set_rect(Rect2(split_bar_rect.position.x - split_bar_offset + drag_area_offset * (is_rtl ? -1 : 1), split_bar_rect.position.y, dragger_ctrl_size, split_bar_rect.size.y));
  283. dragging_area_control->split_bar_rect = Rect2(Vector2(int(split_bar_offset) - drag_area_offset * (is_rtl ? -1 : 1), 0.0), split_bar_rect.size);
  284. }
  285. queue_redraw();
  286. dragging_area_control->queue_redraw();
  287. }
  288. Size2 SplitContainer::get_minimum_size() const {
  289. Size2i minimum;
  290. int sep = _get_separation();
  291. for (int i = 0; i < 2; i++) {
  292. Control *child = _get_sortable_child(i, SortableVisibilityMode::VISIBLE);
  293. if (!child) {
  294. break;
  295. }
  296. if (i == 1) {
  297. if (vertical) {
  298. minimum.height += sep;
  299. } else {
  300. minimum.width += sep;
  301. }
  302. }
  303. Size2 ms = child->get_combined_minimum_size();
  304. if (vertical) {
  305. minimum.height += ms.height;
  306. minimum.width = MAX(minimum.width, ms.width);
  307. } else {
  308. minimum.width += ms.width;
  309. minimum.height = MAX(minimum.height, ms.height);
  310. }
  311. }
  312. return minimum;
  313. }
  314. void SplitContainer::_validate_property(PropertyInfo &p_property) const {
  315. if (is_fixed && p_property.name == "vertical") {
  316. p_property.usage = PROPERTY_USAGE_NONE;
  317. }
  318. }
  319. void SplitContainer::_notification(int p_what) {
  320. switch (p_what) {
  321. case NOTIFICATION_TRANSLATION_CHANGED:
  322. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  323. queue_sort();
  324. } break;
  325. case NOTIFICATION_SORT_CHILDREN: {
  326. _resort();
  327. } break;
  328. case NOTIFICATION_THEME_CHANGED: {
  329. update_minimum_size();
  330. if (touch_dragger) {
  331. touch_dragger->set_modulate(theme_cache.touch_dragger_color);
  332. touch_dragger->set_texture(_get_touch_dragger_icon());
  333. }
  334. } break;
  335. }
  336. }
  337. void SplitContainer::set_split_offset(int p_offset) {
  338. if (split_offset == p_offset) {
  339. return;
  340. }
  341. split_offset = p_offset;
  342. queue_sort();
  343. }
  344. int SplitContainer::get_split_offset() const {
  345. return split_offset;
  346. }
  347. void SplitContainer::clamp_split_offset() {
  348. if (!_get_sortable_child(0) || !_get_sortable_child(1)) {
  349. return;
  350. }
  351. _compute_split_offset(true);
  352. queue_sort();
  353. }
  354. void SplitContainer::set_collapsed(bool p_collapsed) {
  355. if (collapsed == p_collapsed) {
  356. return;
  357. }
  358. collapsed = p_collapsed;
  359. queue_sort();
  360. }
  361. void SplitContainer::set_dragger_visibility(DraggerVisibility p_visibility) {
  362. if (dragger_visibility == p_visibility) {
  363. return;
  364. }
  365. dragger_visibility = p_visibility;
  366. queue_sort();
  367. }
  368. SplitContainer::DraggerVisibility SplitContainer::get_dragger_visibility() const {
  369. return dragger_visibility;
  370. }
  371. bool SplitContainer::is_collapsed() const {
  372. return collapsed;
  373. }
  374. void SplitContainer::set_vertical(bool p_vertical) {
  375. ERR_FAIL_COND_MSG(is_fixed, "Can't change orientation of " + get_class() + ".");
  376. if (vertical == p_vertical) {
  377. return;
  378. }
  379. vertical = p_vertical;
  380. if (touch_dragger) {
  381. touch_dragger->set_texture(_get_touch_dragger_icon());
  382. touch_dragger->set_anchors_and_offsets_preset(Control::PRESET_CENTER);
  383. touch_dragger->set_default_cursor_shape(vertical ? CURSOR_VSPLIT : CURSOR_HSPLIT);
  384. }
  385. update_minimum_size();
  386. _resort();
  387. }
  388. bool SplitContainer::is_vertical() const {
  389. return vertical;
  390. }
  391. void SplitContainer::set_dragging_enabled(bool p_enabled) {
  392. if (dragging_enabled == p_enabled) {
  393. return;
  394. }
  395. dragging_enabled = p_enabled;
  396. if (!dragging_enabled && dragging_area_control->dragging) {
  397. dragging_area_control->dragging = false;
  398. // queue_redraw() is called by _resort().
  399. emit_signal(SNAME("drag_ended"));
  400. }
  401. if (get_viewport()) {
  402. get_viewport()->update_mouse_cursor_state();
  403. }
  404. _resort();
  405. }
  406. bool SplitContainer::is_dragging_enabled() const {
  407. return dragging_enabled;
  408. }
  409. Vector<int> SplitContainer::get_allowed_size_flags_horizontal() const {
  410. Vector<int> flags;
  411. flags.append(SIZE_FILL);
  412. if (!vertical) {
  413. flags.append(SIZE_EXPAND);
  414. }
  415. flags.append(SIZE_SHRINK_BEGIN);
  416. flags.append(SIZE_SHRINK_CENTER);
  417. flags.append(SIZE_SHRINK_END);
  418. return flags;
  419. }
  420. Vector<int> SplitContainer::get_allowed_size_flags_vertical() const {
  421. Vector<int> flags;
  422. flags.append(SIZE_FILL);
  423. if (vertical) {
  424. flags.append(SIZE_EXPAND);
  425. }
  426. flags.append(SIZE_SHRINK_BEGIN);
  427. flags.append(SIZE_SHRINK_CENTER);
  428. flags.append(SIZE_SHRINK_END);
  429. return flags;
  430. }
  431. void SplitContainer::set_drag_area_margin_begin(int p_margin) {
  432. if (drag_area_margin_begin == p_margin) {
  433. return;
  434. }
  435. drag_area_margin_begin = p_margin;
  436. queue_sort();
  437. }
  438. int SplitContainer::get_drag_area_margin_begin() const {
  439. return drag_area_margin_begin;
  440. }
  441. void SplitContainer::set_drag_area_margin_end(int p_margin) {
  442. if (drag_area_margin_end == p_margin) {
  443. return;
  444. }
  445. drag_area_margin_end = p_margin;
  446. queue_sort();
  447. }
  448. int SplitContainer::get_drag_area_margin_end() const {
  449. return drag_area_margin_end;
  450. }
  451. void SplitContainer::set_drag_area_offset(int p_offset) {
  452. if (drag_area_offset == p_offset) {
  453. return;
  454. }
  455. drag_area_offset = p_offset;
  456. queue_sort();
  457. }
  458. int SplitContainer::get_drag_area_offset() const {
  459. return drag_area_offset;
  460. }
  461. void SplitContainer::set_show_drag_area_enabled(bool p_enabled) {
  462. show_drag_area = p_enabled;
  463. dragging_area_control->queue_redraw();
  464. }
  465. bool SplitContainer::is_show_drag_area_enabled() const {
  466. return show_drag_area;
  467. }
  468. void SplitContainer::set_touch_dragger_enabled(bool p_enabled) {
  469. if (touch_dragger_enabled == p_enabled) {
  470. return;
  471. }
  472. touch_dragger_enabled = p_enabled;
  473. if (p_enabled) {
  474. touch_dragger = memnew(TextureRect);
  475. touch_dragger->set_texture(_get_touch_dragger_icon());
  476. touch_dragger->set_anchors_and_offsets_preset(Control::PRESET_CENTER);
  477. touch_dragger->set_default_cursor_shape(vertical ? CURSOR_VSPLIT : CURSOR_HSPLIT);
  478. touch_dragger->set_modulate(theme_cache.touch_dragger_color);
  479. touch_dragger->connect(SceneStringName(gui_input), callable_mp(this, &SplitContainer::_touch_dragger_gui_input));
  480. touch_dragger->connect(SceneStringName(mouse_exited), callable_mp(this, &SplitContainer::_touch_dragger_mouse_exited));
  481. dragging_area_control->add_child(touch_dragger, false, Node::INTERNAL_MODE_FRONT);
  482. } else {
  483. if (touch_dragger) {
  484. touch_dragger->queue_free();
  485. touch_dragger = nullptr;
  486. }
  487. }
  488. dragging_area_control->queue_redraw();
  489. }
  490. bool SplitContainer::is_touch_dragger_enabled() const {
  491. return touch_dragger_enabled;
  492. }
  493. void SplitContainer::_touch_dragger_mouse_exited() {
  494. if (!dragging_area_control->dragging) {
  495. touch_dragger->set_modulate(theme_cache.touch_dragger_color);
  496. }
  497. }
  498. void SplitContainer::_touch_dragger_gui_input(const Ref<InputEvent> &p_event) {
  499. if (!touch_dragger_enabled) {
  500. return;
  501. }
  502. Ref<InputEventMouseMotion> mm = p_event;
  503. Ref<InputEventMouseButton> mb = p_event;
  504. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) {
  505. if (mb->is_pressed()) {
  506. touch_dragger->set_modulate(theme_cache.touch_dragger_pressed_color);
  507. } else {
  508. touch_dragger->set_modulate(theme_cache.touch_dragger_color);
  509. }
  510. }
  511. if (mm.is_valid() && !dragging_area_control->dragging) {
  512. touch_dragger->set_modulate(theme_cache.touch_dragger_hover_color);
  513. }
  514. }
  515. void SplitContainer::_bind_methods() {
  516. ClassDB::bind_method(D_METHOD("set_split_offset", "offset"), &SplitContainer::set_split_offset);
  517. ClassDB::bind_method(D_METHOD("get_split_offset"), &SplitContainer::get_split_offset);
  518. ClassDB::bind_method(D_METHOD("clamp_split_offset"), &SplitContainer::clamp_split_offset);
  519. ClassDB::bind_method(D_METHOD("set_collapsed", "collapsed"), &SplitContainer::set_collapsed);
  520. ClassDB::bind_method(D_METHOD("is_collapsed"), &SplitContainer::is_collapsed);
  521. ClassDB::bind_method(D_METHOD("set_dragger_visibility", "mode"), &SplitContainer::set_dragger_visibility);
  522. ClassDB::bind_method(D_METHOD("get_dragger_visibility"), &SplitContainer::get_dragger_visibility);
  523. ClassDB::bind_method(D_METHOD("set_vertical", "vertical"), &SplitContainer::set_vertical);
  524. ClassDB::bind_method(D_METHOD("is_vertical"), &SplitContainer::is_vertical);
  525. ClassDB::bind_method(D_METHOD("set_dragging_enabled", "dragging_enabled"), &SplitContainer::set_dragging_enabled);
  526. ClassDB::bind_method(D_METHOD("is_dragging_enabled"), &SplitContainer::is_dragging_enabled);
  527. ClassDB::bind_method(D_METHOD("set_drag_area_margin_begin", "margin"), &SplitContainer::set_drag_area_margin_begin);
  528. ClassDB::bind_method(D_METHOD("get_drag_area_margin_begin"), &SplitContainer::get_drag_area_margin_begin);
  529. ClassDB::bind_method(D_METHOD("set_drag_area_margin_end", "margin"), &SplitContainer::set_drag_area_margin_end);
  530. ClassDB::bind_method(D_METHOD("get_drag_area_margin_end"), &SplitContainer::get_drag_area_margin_end);
  531. ClassDB::bind_method(D_METHOD("set_drag_area_offset", "offset"), &SplitContainer::set_drag_area_offset);
  532. ClassDB::bind_method(D_METHOD("get_drag_area_offset"), &SplitContainer::get_drag_area_offset);
  533. ClassDB::bind_method(D_METHOD("set_drag_area_highlight_in_editor", "drag_area_highlight_in_editor"), &SplitContainer::set_show_drag_area_enabled);
  534. ClassDB::bind_method(D_METHOD("is_drag_area_highlight_in_editor_enabled"), &SplitContainer::is_show_drag_area_enabled);
  535. ClassDB::bind_method(D_METHOD("get_drag_area_control"), &SplitContainer::get_drag_area_control);
  536. ClassDB::bind_method(D_METHOD("set_touch_dragger_enabled", "enabled"), &SplitContainer::set_touch_dragger_enabled);
  537. ClassDB::bind_method(D_METHOD("is_touch_dragger_enabled"), &SplitContainer::is_touch_dragger_enabled);
  538. ADD_SIGNAL(MethodInfo("dragged", PropertyInfo(Variant::INT, "offset")));
  539. ADD_SIGNAL(MethodInfo("drag_started"));
  540. ADD_SIGNAL(MethodInfo("drag_ended"));
  541. ADD_PROPERTY(PropertyInfo(Variant::INT, "split_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_split_offset", "get_split_offset");
  542. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collapsed"), "set_collapsed", "is_collapsed");
  543. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "dragging_enabled"), "set_dragging_enabled", "is_dragging_enabled");
  544. ADD_PROPERTY(PropertyInfo(Variant::INT, "dragger_visibility", PROPERTY_HINT_ENUM, "Visible,Hidden,Hidden and Collapsed"), "set_dragger_visibility", "get_dragger_visibility");
  545. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "vertical"), "set_vertical", "is_vertical");
  546. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "touch_dragger_enabled"), "set_touch_dragger_enabled", "is_touch_dragger_enabled");
  547. ADD_GROUP("Drag Area", "drag_area_");
  548. ADD_PROPERTY(PropertyInfo(Variant::INT, "drag_area_margin_begin", PROPERTY_HINT_NONE, "suffix:px"), "set_drag_area_margin_begin", "get_drag_area_margin_begin");
  549. ADD_PROPERTY(PropertyInfo(Variant::INT, "drag_area_margin_end", PROPERTY_HINT_NONE, "suffix:px"), "set_drag_area_margin_end", "get_drag_area_margin_end");
  550. ADD_PROPERTY(PropertyInfo(Variant::INT, "drag_area_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_drag_area_offset", "get_drag_area_offset");
  551. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_area_highlight_in_editor"), "set_drag_area_highlight_in_editor", "is_drag_area_highlight_in_editor_enabled");
  552. BIND_ENUM_CONSTANT(DRAGGER_VISIBLE);
  553. BIND_ENUM_CONSTANT(DRAGGER_HIDDEN);
  554. BIND_ENUM_CONSTANT(DRAGGER_HIDDEN_COLLAPSED);
  555. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, SplitContainer, touch_dragger_color);
  556. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, SplitContainer, touch_dragger_pressed_color);
  557. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, SplitContainer, touch_dragger_hover_color);
  558. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, SplitContainer, separation);
  559. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, SplitContainer, minimum_grab_thickness);
  560. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, SplitContainer, autohide);
  561. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, SplitContainer, touch_dragger_icon, "touch_dragger");
  562. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, SplitContainer, touch_dragger_icon_h, "h_touch_dragger");
  563. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, SplitContainer, touch_dragger_icon_v, "v_touch_dragger");
  564. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, SplitContainer, grabber_icon, "grabber");
  565. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, SplitContainer, grabber_icon_h, "h_grabber");
  566. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, SplitContainer, grabber_icon_v, "v_grabber");
  567. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, SplitContainer, split_bar_background, "split_bar_background");
  568. }
  569. SplitContainer::SplitContainer(bool p_vertical) {
  570. vertical = p_vertical;
  571. dragging_area_control = memnew(SplitContainerDragger);
  572. add_child(dragging_area_control, false, Node::INTERNAL_MODE_BACK);
  573. }