split_container.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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. } break;
  331. }
  332. }
  333. void SplitContainer::set_split_offset(int p_offset) {
  334. if (split_offset == p_offset) {
  335. return;
  336. }
  337. split_offset = p_offset;
  338. queue_sort();
  339. }
  340. int SplitContainer::get_split_offset() const {
  341. return split_offset;
  342. }
  343. void SplitContainer::clamp_split_offset() {
  344. if (!_get_sortable_child(0) || !_get_sortable_child(1)) {
  345. return;
  346. }
  347. _compute_split_offset(true);
  348. queue_sort();
  349. }
  350. void SplitContainer::set_collapsed(bool p_collapsed) {
  351. if (collapsed == p_collapsed) {
  352. return;
  353. }
  354. collapsed = p_collapsed;
  355. queue_sort();
  356. }
  357. void SplitContainer::set_dragger_visibility(DraggerVisibility p_visibility) {
  358. if (dragger_visibility == p_visibility) {
  359. return;
  360. }
  361. dragger_visibility = p_visibility;
  362. queue_sort();
  363. }
  364. SplitContainer::DraggerVisibility SplitContainer::get_dragger_visibility() const {
  365. return dragger_visibility;
  366. }
  367. bool SplitContainer::is_collapsed() const {
  368. return collapsed;
  369. }
  370. void SplitContainer::set_vertical(bool p_vertical) {
  371. ERR_FAIL_COND_MSG(is_fixed, "Can't change orientation of " + get_class() + ".");
  372. if (vertical == p_vertical) {
  373. return;
  374. }
  375. vertical = p_vertical;
  376. if (touch_dragger) {
  377. touch_dragger->set_texture(_get_touch_dragger_icon());
  378. touch_dragger->set_anchors_and_offsets_preset(Control::PRESET_CENTER);
  379. touch_dragger->set_default_cursor_shape(vertical ? CURSOR_VSPLIT : CURSOR_HSPLIT);
  380. }
  381. update_minimum_size();
  382. _resort();
  383. }
  384. bool SplitContainer::is_vertical() const {
  385. return vertical;
  386. }
  387. void SplitContainer::set_dragging_enabled(bool p_enabled) {
  388. if (dragging_enabled == p_enabled) {
  389. return;
  390. }
  391. dragging_enabled = p_enabled;
  392. if (!dragging_enabled && dragging_area_control->dragging) {
  393. dragging_area_control->dragging = false;
  394. // queue_redraw() is called by _resort().
  395. emit_signal(SNAME("drag_ended"));
  396. }
  397. if (get_viewport()) {
  398. get_viewport()->update_mouse_cursor_state();
  399. }
  400. _resort();
  401. }
  402. bool SplitContainer::is_dragging_enabled() const {
  403. return dragging_enabled;
  404. }
  405. Vector<int> SplitContainer::get_allowed_size_flags_horizontal() const {
  406. Vector<int> flags;
  407. flags.append(SIZE_FILL);
  408. if (!vertical) {
  409. flags.append(SIZE_EXPAND);
  410. }
  411. flags.append(SIZE_SHRINK_BEGIN);
  412. flags.append(SIZE_SHRINK_CENTER);
  413. flags.append(SIZE_SHRINK_END);
  414. return flags;
  415. }
  416. Vector<int> SplitContainer::get_allowed_size_flags_vertical() const {
  417. Vector<int> flags;
  418. flags.append(SIZE_FILL);
  419. if (vertical) {
  420. flags.append(SIZE_EXPAND);
  421. }
  422. flags.append(SIZE_SHRINK_BEGIN);
  423. flags.append(SIZE_SHRINK_CENTER);
  424. flags.append(SIZE_SHRINK_END);
  425. return flags;
  426. }
  427. void SplitContainer::set_drag_area_margin_begin(int p_margin) {
  428. if (drag_area_margin_begin == p_margin) {
  429. return;
  430. }
  431. drag_area_margin_begin = p_margin;
  432. queue_sort();
  433. }
  434. int SplitContainer::get_drag_area_margin_begin() const {
  435. return drag_area_margin_begin;
  436. }
  437. void SplitContainer::set_drag_area_margin_end(int p_margin) {
  438. if (drag_area_margin_end == p_margin) {
  439. return;
  440. }
  441. drag_area_margin_end = p_margin;
  442. queue_sort();
  443. }
  444. int SplitContainer::get_drag_area_margin_end() const {
  445. return drag_area_margin_end;
  446. }
  447. void SplitContainer::set_drag_area_offset(int p_offset) {
  448. if (drag_area_offset == p_offset) {
  449. return;
  450. }
  451. drag_area_offset = p_offset;
  452. queue_sort();
  453. }
  454. int SplitContainer::get_drag_area_offset() const {
  455. return drag_area_offset;
  456. }
  457. void SplitContainer::set_show_drag_area_enabled(bool p_enabled) {
  458. show_drag_area = p_enabled;
  459. dragging_area_control->queue_redraw();
  460. }
  461. bool SplitContainer::is_show_drag_area_enabled() const {
  462. return show_drag_area;
  463. }
  464. void SplitContainer::set_touch_dragger_enabled(bool p_enabled) {
  465. if (touch_dragger_enabled == p_enabled) {
  466. return;
  467. }
  468. touch_dragger_enabled = p_enabled;
  469. if (p_enabled) {
  470. touch_dragger = memnew(TextureRect);
  471. touch_dragger->set_texture(_get_touch_dragger_icon());
  472. touch_dragger->set_anchors_and_offsets_preset(Control::PRESET_CENTER);
  473. touch_dragger->set_default_cursor_shape(vertical ? CURSOR_VSPLIT : CURSOR_HSPLIT);
  474. touch_dragger->set_modulate(Color(1, 1, 1, 0.3));
  475. touch_dragger->connect(SceneStringName(gui_input), callable_mp(this, &SplitContainer::_touch_dragger_gui_input));
  476. touch_dragger->connect(SceneStringName(mouse_exited), callable_mp(this, &SplitContainer::_touch_dragger_mouse_exited));
  477. dragging_area_control->add_child(touch_dragger, false, Node::INTERNAL_MODE_FRONT);
  478. } else {
  479. if (touch_dragger) {
  480. touch_dragger->queue_free();
  481. touch_dragger = nullptr;
  482. }
  483. }
  484. dragging_area_control->queue_redraw();
  485. }
  486. bool SplitContainer::is_touch_dragger_enabled() const {
  487. return touch_dragger_enabled;
  488. }
  489. void SplitContainer::_touch_dragger_mouse_exited() {
  490. if (!dragging_area_control->dragging) {
  491. touch_dragger->set_modulate(Color(1, 1, 1, 0.3));
  492. }
  493. }
  494. void SplitContainer::_touch_dragger_gui_input(const Ref<InputEvent> &p_event) {
  495. if (!touch_dragger_enabled) {
  496. return;
  497. }
  498. Ref<InputEventMouseMotion> mm = p_event;
  499. Ref<InputEventMouseButton> mb = p_event;
  500. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) {
  501. if (mb->is_pressed()) {
  502. touch_dragger->set_modulate(Color(1, 1, 1, 1));
  503. } else {
  504. touch_dragger->set_modulate(Color(1, 1, 1, 0.3));
  505. }
  506. }
  507. if (mm.is_valid() && !dragging_area_control->dragging) {
  508. touch_dragger->set_modulate(Color(1, 1, 1, 0.6));
  509. }
  510. }
  511. void SplitContainer::_bind_methods() {
  512. ClassDB::bind_method(D_METHOD("set_split_offset", "offset"), &SplitContainer::set_split_offset);
  513. ClassDB::bind_method(D_METHOD("get_split_offset"), &SplitContainer::get_split_offset);
  514. ClassDB::bind_method(D_METHOD("clamp_split_offset"), &SplitContainer::clamp_split_offset);
  515. ClassDB::bind_method(D_METHOD("set_collapsed", "collapsed"), &SplitContainer::set_collapsed);
  516. ClassDB::bind_method(D_METHOD("is_collapsed"), &SplitContainer::is_collapsed);
  517. ClassDB::bind_method(D_METHOD("set_dragger_visibility", "mode"), &SplitContainer::set_dragger_visibility);
  518. ClassDB::bind_method(D_METHOD("get_dragger_visibility"), &SplitContainer::get_dragger_visibility);
  519. ClassDB::bind_method(D_METHOD("set_vertical", "vertical"), &SplitContainer::set_vertical);
  520. ClassDB::bind_method(D_METHOD("is_vertical"), &SplitContainer::is_vertical);
  521. ClassDB::bind_method(D_METHOD("set_dragging_enabled", "dragging_enabled"), &SplitContainer::set_dragging_enabled);
  522. ClassDB::bind_method(D_METHOD("is_dragging_enabled"), &SplitContainer::is_dragging_enabled);
  523. ClassDB::bind_method(D_METHOD("set_drag_area_margin_begin", "margin"), &SplitContainer::set_drag_area_margin_begin);
  524. ClassDB::bind_method(D_METHOD("get_drag_area_margin_begin"), &SplitContainer::get_drag_area_margin_begin);
  525. ClassDB::bind_method(D_METHOD("set_drag_area_margin_end", "margin"), &SplitContainer::set_drag_area_margin_end);
  526. ClassDB::bind_method(D_METHOD("get_drag_area_margin_end"), &SplitContainer::get_drag_area_margin_end);
  527. ClassDB::bind_method(D_METHOD("set_drag_area_offset", "offset"), &SplitContainer::set_drag_area_offset);
  528. ClassDB::bind_method(D_METHOD("get_drag_area_offset"), &SplitContainer::get_drag_area_offset);
  529. ClassDB::bind_method(D_METHOD("set_drag_area_highlight_in_editor", "drag_area_highlight_in_editor"), &SplitContainer::set_show_drag_area_enabled);
  530. ClassDB::bind_method(D_METHOD("is_drag_area_highlight_in_editor_enabled"), &SplitContainer::is_show_drag_area_enabled);
  531. ClassDB::bind_method(D_METHOD("get_drag_area_control"), &SplitContainer::get_drag_area_control);
  532. ClassDB::bind_method(D_METHOD("set_touch_dragger_enabled", "enabled"), &SplitContainer::set_touch_dragger_enabled);
  533. ClassDB::bind_method(D_METHOD("is_touch_dragger_enabled"), &SplitContainer::is_touch_dragger_enabled);
  534. ADD_SIGNAL(MethodInfo("dragged", PropertyInfo(Variant::INT, "offset")));
  535. ADD_SIGNAL(MethodInfo("drag_started"));
  536. ADD_SIGNAL(MethodInfo("drag_ended"));
  537. ADD_PROPERTY(PropertyInfo(Variant::INT, "split_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_split_offset", "get_split_offset");
  538. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collapsed"), "set_collapsed", "is_collapsed");
  539. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "dragging_enabled"), "set_dragging_enabled", "is_dragging_enabled");
  540. ADD_PROPERTY(PropertyInfo(Variant::INT, "dragger_visibility", PROPERTY_HINT_ENUM, "Visible,Hidden,Hidden and Collapsed"), "set_dragger_visibility", "get_dragger_visibility");
  541. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "vertical"), "set_vertical", "is_vertical");
  542. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "touch_dragger_enabled"), "set_touch_dragger_enabled", "is_touch_dragger_enabled");
  543. ADD_GROUP("Drag Area", "drag_area_");
  544. ADD_PROPERTY(PropertyInfo(Variant::INT, "drag_area_margin_begin", PROPERTY_HINT_NONE, "suffix:px"), "set_drag_area_margin_begin", "get_drag_area_margin_begin");
  545. ADD_PROPERTY(PropertyInfo(Variant::INT, "drag_area_margin_end", PROPERTY_HINT_NONE, "suffix:px"), "set_drag_area_margin_end", "get_drag_area_margin_end");
  546. ADD_PROPERTY(PropertyInfo(Variant::INT, "drag_area_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_drag_area_offset", "get_drag_area_offset");
  547. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_area_highlight_in_editor"), "set_drag_area_highlight_in_editor", "is_drag_area_highlight_in_editor_enabled");
  548. BIND_ENUM_CONSTANT(DRAGGER_VISIBLE);
  549. BIND_ENUM_CONSTANT(DRAGGER_HIDDEN);
  550. BIND_ENUM_CONSTANT(DRAGGER_HIDDEN_COLLAPSED);
  551. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, SplitContainer, separation);
  552. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, SplitContainer, minimum_grab_thickness);
  553. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, SplitContainer, autohide);
  554. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, SplitContainer, touch_dragger_icon, "touch_dragger");
  555. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, SplitContainer, touch_dragger_icon_h, "h_touch_dragger");
  556. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, SplitContainer, touch_dragger_icon_v, "v_touch_dragger");
  557. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, SplitContainer, grabber_icon, "grabber");
  558. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, SplitContainer, grabber_icon_h, "h_grabber");
  559. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, SplitContainer, grabber_icon_v, "v_grabber");
  560. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, SplitContainer, split_bar_background, "split_bar_background");
  561. }
  562. SplitContainer::SplitContainer(bool p_vertical) {
  563. vertical = p_vertical;
  564. dragging_area_control = memnew(SplitContainerDragger);
  565. add_child(dragging_area_control, false, Node::INTERNAL_MODE_BACK);
  566. }