scroll_container.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. /**************************************************************************/
  2. /* scroll_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 "scroll_container.h"
  31. #include "core/os/os.h"
  32. #include "scene/main/viewport.h"
  33. bool ScrollContainer::clips_input() const {
  34. return true;
  35. }
  36. Size2 ScrollContainer::get_minimum_size() const {
  37. Ref<StyleBox> sb = get_stylebox("bg");
  38. Size2 min_size;
  39. for (int i = 0; i < get_child_count(); i++) {
  40. Control *c = Object::cast_to<Control>(get_child(i));
  41. if (!c) {
  42. continue;
  43. }
  44. if (c->is_set_as_toplevel()) {
  45. continue;
  46. }
  47. if (c == h_scroll || c == v_scroll) {
  48. continue;
  49. }
  50. Size2 minsize = c->get_combined_minimum_size();
  51. if (!scroll_h) {
  52. min_size.x = MAX(min_size.x, minsize.x);
  53. }
  54. if (!scroll_v) {
  55. min_size.y = MAX(min_size.y, minsize.y);
  56. }
  57. }
  58. if (h_scroll->is_visible_in_tree()) {
  59. min_size.y += h_scroll->get_minimum_size().y;
  60. }
  61. if (v_scroll->is_visible_in_tree()) {
  62. min_size.x += v_scroll->get_minimum_size().x;
  63. }
  64. min_size += sb->get_minimum_size();
  65. return min_size;
  66. }
  67. void ScrollContainer::_cancel_drag() {
  68. set_physics_process_internal(false);
  69. drag_touching_deaccel = false;
  70. drag_touching = false;
  71. drag_speed = Vector2();
  72. drag_accum = Vector2();
  73. last_drag_accum = Vector2();
  74. drag_from = Vector2();
  75. if (beyond_deadzone) {
  76. emit_signal("scroll_ended");
  77. propagate_notification(NOTIFICATION_SCROLL_END);
  78. beyond_deadzone = false;
  79. }
  80. }
  81. void ScrollContainer::_gui_input(const Ref<InputEvent> &p_gui_input) {
  82. ERR_FAIL_COND(p_gui_input.is_null());
  83. double prev_v_scroll = v_scroll->get_value();
  84. double prev_h_scroll = h_scroll->get_value();
  85. Ref<InputEventMouseButton> mb = p_gui_input;
  86. if (mb.is_valid()) {
  87. if (mb->get_button_index() == BUTTON_WHEEL_UP && mb->is_pressed()) {
  88. // only horizontal is enabled, scroll horizontally
  89. if (h_scroll->is_visible() && (!v_scroll->is_visible() || mb->get_shift())) {
  90. h_scroll->set_value(h_scroll->get_value() - h_scroll->get_page() / 8 * mb->get_factor());
  91. } else if (v_scroll->is_visible_in_tree()) {
  92. v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page() / 8 * mb->get_factor());
  93. }
  94. }
  95. if (mb->get_button_index() == BUTTON_WHEEL_DOWN && mb->is_pressed()) {
  96. // only horizontal is enabled, scroll horizontally
  97. if (h_scroll->is_visible() && (!v_scroll->is_visible() || mb->get_shift())) {
  98. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() / 8 * mb->get_factor());
  99. } else if (v_scroll->is_visible()) {
  100. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() / 8 * mb->get_factor());
  101. }
  102. }
  103. if (mb->get_button_index() == BUTTON_WHEEL_LEFT && mb->is_pressed()) {
  104. if (h_scroll->is_visible_in_tree()) {
  105. h_scroll->set_value(h_scroll->get_value() - h_scroll->get_page() * mb->get_factor() / 8);
  106. }
  107. }
  108. if (mb->get_button_index() == BUTTON_WHEEL_RIGHT && mb->is_pressed()) {
  109. if (h_scroll->is_visible_in_tree()) {
  110. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * mb->get_factor() / 8);
  111. }
  112. }
  113. if (v_scroll->get_value() != prev_v_scroll || h_scroll->get_value() != prev_h_scroll) {
  114. accept_event(); //accept event if scroll changed
  115. }
  116. if (!OS::get_singleton()->has_touchscreen_ui_hint()) {
  117. return;
  118. }
  119. if (mb->get_button_index() != BUTTON_LEFT) {
  120. return;
  121. }
  122. if (mb->is_pressed()) {
  123. if (drag_touching) {
  124. _cancel_drag();
  125. }
  126. drag_speed = Vector2();
  127. drag_accum = Vector2();
  128. last_drag_accum = Vector2();
  129. drag_from = Vector2(h_scroll->get_value(), v_scroll->get_value());
  130. drag_touching = OS::get_singleton()->has_touchscreen_ui_hint();
  131. drag_touching_deaccel = false;
  132. beyond_deadzone = false;
  133. time_since_motion = 0;
  134. if (drag_touching) {
  135. set_physics_process_internal(true);
  136. time_since_motion = 0;
  137. }
  138. } else {
  139. if (drag_touching) {
  140. if (drag_speed == Vector2()) {
  141. _cancel_drag();
  142. } else {
  143. drag_touching_deaccel = true;
  144. }
  145. }
  146. }
  147. }
  148. Ref<InputEventMouseMotion> mm = p_gui_input;
  149. if (mm.is_valid()) {
  150. if (drag_touching && !drag_touching_deaccel) {
  151. Vector2 motion = Vector2(mm->get_relative().x, mm->get_relative().y);
  152. drag_accum -= motion;
  153. if (beyond_deadzone || (scroll_h && Math::abs(drag_accum.x) > deadzone) || (scroll_v && Math::abs(drag_accum.y) > deadzone)) {
  154. if (!beyond_deadzone) {
  155. propagate_notification(NOTIFICATION_SCROLL_BEGIN);
  156. emit_signal("scroll_started");
  157. beyond_deadzone = true;
  158. // resetting drag_accum here ensures smooth scrolling after reaching deadzone
  159. drag_accum = -motion;
  160. }
  161. Vector2 diff = drag_from + drag_accum;
  162. if (scroll_h) {
  163. h_scroll->set_value(diff.x);
  164. } else {
  165. drag_accum.x = 0;
  166. }
  167. if (scroll_v) {
  168. v_scroll->set_value(diff.y);
  169. } else {
  170. drag_accum.y = 0;
  171. }
  172. time_since_motion = 0;
  173. }
  174. }
  175. }
  176. Ref<InputEventPanGesture> pan_gesture = p_gui_input;
  177. if (pan_gesture.is_valid()) {
  178. if (h_scroll->is_visible_in_tree()) {
  179. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * pan_gesture->get_delta().x / 8);
  180. }
  181. if (v_scroll->is_visible_in_tree()) {
  182. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * pan_gesture->get_delta().y / 8);
  183. }
  184. }
  185. if (v_scroll->get_value() != prev_v_scroll || h_scroll->get_value() != prev_h_scroll) {
  186. accept_event(); //accept event if scroll changed
  187. }
  188. }
  189. void ScrollContainer::_update_scrollbar_position() {
  190. Size2 hmin = h_scroll->get_combined_minimum_size();
  191. Size2 vmin = v_scroll->get_combined_minimum_size();
  192. h_scroll->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_BEGIN, 0);
  193. h_scroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0);
  194. h_scroll->set_anchor_and_margin(MARGIN_TOP, ANCHOR_END, -hmin.height);
  195. h_scroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0);
  196. v_scroll->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, -vmin.width);
  197. v_scroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0);
  198. v_scroll->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 0);
  199. v_scroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0);
  200. h_scroll->raise();
  201. v_scroll->raise();
  202. }
  203. void ScrollContainer::_gui_focus_changed(Control *p_control) {
  204. if (follow_focus && is_a_parent_of(p_control)) {
  205. ensure_control_visible(p_control);
  206. }
  207. }
  208. void ScrollContainer::ensure_control_visible(Control *p_control) {
  209. ERR_FAIL_COND_MSG(!is_a_parent_of(p_control), "Must be a parent of the control.");
  210. Rect2 global_rect = get_global_rect();
  211. Rect2 other_rect = p_control->get_global_rect();
  212. float right_margin = v_scroll->is_visible() ? v_scroll->get_size().x : 0.0f;
  213. float bottom_margin = h_scroll->is_visible() ? h_scroll->get_size().y : 0.0f;
  214. Vector2 diff = Vector2(MAX(MIN(other_rect.position.x, global_rect.position.x), other_rect.position.x + other_rect.size.x - global_rect.size.x + right_margin),
  215. MAX(MIN(other_rect.position.y, global_rect.position.y), other_rect.position.y + other_rect.size.y - global_rect.size.y + bottom_margin));
  216. set_h_scroll(get_h_scroll() + (diff.x - global_rect.position.x));
  217. set_v_scroll(get_v_scroll() + (diff.y - global_rect.position.y));
  218. }
  219. void ScrollContainer::_update_dimensions() {
  220. child_max_size = Size2(0, 0);
  221. Size2 size = get_size();
  222. Point2 ofs;
  223. Ref<StyleBox> sb = get_stylebox("bg");
  224. size -= sb->get_minimum_size();
  225. ofs += sb->get_offset();
  226. if (h_scroll->is_visible_in_tree() && h_scroll->get_parent() == this) { //scrolls may have been moved out for reasons
  227. size.y -= h_scroll->get_minimum_size().y;
  228. }
  229. if (v_scroll->is_visible_in_tree() && v_scroll->get_parent() == this) { //scrolls may have been moved out for reasons
  230. size.x -= v_scroll->get_minimum_size().x;
  231. }
  232. for (int i = 0; i < get_child_count(); i++) {
  233. Control *c = Object::cast_to<Control>(get_child(i));
  234. if (!c) {
  235. continue;
  236. }
  237. if (c->is_set_as_toplevel()) {
  238. continue;
  239. }
  240. if (c == h_scroll || c == v_scroll) {
  241. continue;
  242. }
  243. Size2 minsize = c->get_combined_minimum_size();
  244. child_max_size.x = MAX(child_max_size.x, minsize.x);
  245. child_max_size.y = MAX(child_max_size.y, minsize.y);
  246. Rect2 r = Rect2(-scroll, minsize);
  247. if (!scroll_h || (!h_scroll->is_visible_in_tree() && c->get_h_size_flags() & SIZE_EXPAND)) {
  248. r.position.x = 0;
  249. if (c->get_h_size_flags() & SIZE_EXPAND) {
  250. r.size.width = MAX(size.width, minsize.width);
  251. } else {
  252. r.size.width = minsize.width;
  253. }
  254. }
  255. if (!scroll_v || (!v_scroll->is_visible_in_tree() && c->get_v_size_flags() & SIZE_EXPAND)) {
  256. r.position.y = 0;
  257. if (c->get_v_size_flags() & SIZE_EXPAND) {
  258. r.size.height = MAX(size.height, minsize.height);
  259. } else {
  260. r.size.height = minsize.height;
  261. }
  262. }
  263. r.position += ofs;
  264. fit_child_in_rect(c, r);
  265. }
  266. update();
  267. }
  268. void ScrollContainer::_notification(int p_what) {
  269. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  270. call_deferred("_update_scrollbar_position");
  271. };
  272. if (p_what == NOTIFICATION_READY) {
  273. Viewport *viewport = get_viewport();
  274. ERR_FAIL_COND(!viewport);
  275. viewport->connect("gui_focus_changed", this, "_gui_focus_changed");
  276. _update_dimensions();
  277. }
  278. if (p_what == NOTIFICATION_SORT_CHILDREN) {
  279. _update_dimensions();
  280. };
  281. if (p_what == NOTIFICATION_DRAW) {
  282. Ref<StyleBox> sb = get_stylebox("bg");
  283. draw_style_box(sb, Rect2(Vector2(), get_size()));
  284. update_scrollbars();
  285. }
  286. if (p_what == NOTIFICATION_INTERNAL_PHYSICS_PROCESS) {
  287. if (drag_touching) {
  288. if (drag_touching_deaccel) {
  289. Vector2 pos = Vector2(h_scroll->get_value(), v_scroll->get_value());
  290. pos += drag_speed * get_physics_process_delta_time();
  291. bool turnoff_h = false;
  292. bool turnoff_v = false;
  293. if (pos.x < 0) {
  294. pos.x = 0;
  295. turnoff_h = true;
  296. }
  297. if (pos.x > (h_scroll->get_max() - h_scroll->get_page())) {
  298. pos.x = h_scroll->get_max() - h_scroll->get_page();
  299. turnoff_h = true;
  300. }
  301. if (pos.y < 0) {
  302. pos.y = 0;
  303. turnoff_v = true;
  304. }
  305. if (pos.y > (v_scroll->get_max() - v_scroll->get_page())) {
  306. pos.y = v_scroll->get_max() - v_scroll->get_page();
  307. turnoff_v = true;
  308. }
  309. if (scroll_h) {
  310. h_scroll->set_value(pos.x);
  311. }
  312. if (scroll_v) {
  313. v_scroll->set_value(pos.y);
  314. }
  315. float sgn_x = drag_speed.x < 0 ? -1 : 1;
  316. float val_x = Math::abs(drag_speed.x);
  317. val_x -= 1000 * get_physics_process_delta_time();
  318. if (val_x < 0) {
  319. turnoff_h = true;
  320. }
  321. float sgn_y = drag_speed.y < 0 ? -1 : 1;
  322. float val_y = Math::abs(drag_speed.y);
  323. val_y -= 1000 * get_physics_process_delta_time();
  324. if (val_y < 0) {
  325. turnoff_v = true;
  326. }
  327. drag_speed = Vector2(sgn_x * val_x, sgn_y * val_y);
  328. if (turnoff_h && turnoff_v) {
  329. _cancel_drag();
  330. }
  331. } else {
  332. if (time_since_motion == 0 || time_since_motion > 0.1) {
  333. Vector2 diff = drag_accum - last_drag_accum;
  334. last_drag_accum = drag_accum;
  335. drag_speed = diff / get_physics_process_delta_time();
  336. }
  337. time_since_motion += get_physics_process_delta_time();
  338. }
  339. }
  340. }
  341. };
  342. void ScrollContainer::update_scrollbars() {
  343. Size2 size = get_size();
  344. Ref<StyleBox> sb = get_stylebox("bg");
  345. size -= sb->get_minimum_size();
  346. Size2 hmin;
  347. Size2 vmin;
  348. if (scroll_h) {
  349. hmin = h_scroll->get_combined_minimum_size();
  350. }
  351. if (scroll_v) {
  352. vmin = v_scroll->get_combined_minimum_size();
  353. }
  354. Size2 min = child_max_size;
  355. bool hide_scroll_v = !scroll_v || min.height <= size.height;
  356. bool hide_scroll_h = !scroll_h || min.width <= size.width;
  357. v_scroll->set_max(min.height);
  358. if (hide_scroll_v) {
  359. v_scroll->set_page(size.height);
  360. v_scroll->hide();
  361. scroll.y = 0;
  362. } else {
  363. v_scroll->show();
  364. if (hide_scroll_h) {
  365. v_scroll->set_page(size.height);
  366. } else {
  367. v_scroll->set_page(size.height - hmin.height);
  368. }
  369. scroll.y = v_scroll->get_value();
  370. }
  371. h_scroll->set_max(min.width);
  372. if (hide_scroll_h) {
  373. h_scroll->set_page(size.width);
  374. h_scroll->hide();
  375. scroll.x = 0;
  376. } else {
  377. h_scroll->show();
  378. if (hide_scroll_v) {
  379. h_scroll->set_page(size.width);
  380. } else {
  381. h_scroll->set_page(size.width - vmin.width);
  382. }
  383. scroll.x = h_scroll->get_value();
  384. }
  385. // Avoid scrollbar overlapping.
  386. h_scroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, hide_scroll_v ? 0 : -vmin.width);
  387. v_scroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, hide_scroll_h ? 0 : -hmin.height);
  388. }
  389. void ScrollContainer::_scroll_moved(float) {
  390. scroll.x = h_scroll->get_value();
  391. scroll.y = v_scroll->get_value();
  392. queue_sort();
  393. update();
  394. };
  395. void ScrollContainer::set_enable_h_scroll(bool p_enable) {
  396. if (scroll_h == p_enable) {
  397. return;
  398. }
  399. scroll_h = p_enable;
  400. minimum_size_changed();
  401. queue_sort();
  402. }
  403. bool ScrollContainer::is_h_scroll_enabled() const {
  404. return scroll_h;
  405. }
  406. void ScrollContainer::set_enable_v_scroll(bool p_enable) {
  407. if (scroll_v == p_enable) {
  408. return;
  409. }
  410. scroll_v = p_enable;
  411. minimum_size_changed();
  412. queue_sort();
  413. }
  414. bool ScrollContainer::is_v_scroll_enabled() const {
  415. return scroll_v;
  416. }
  417. int ScrollContainer::get_v_scroll() const {
  418. return v_scroll->get_value();
  419. }
  420. void ScrollContainer::set_v_scroll(int p_pos) {
  421. v_scroll->set_value(p_pos);
  422. _cancel_drag();
  423. }
  424. int ScrollContainer::get_h_scroll() const {
  425. return h_scroll->get_value();
  426. }
  427. void ScrollContainer::set_h_scroll(int p_pos) {
  428. h_scroll->set_value(p_pos);
  429. _cancel_drag();
  430. }
  431. int ScrollContainer::get_deadzone() const {
  432. return deadzone;
  433. }
  434. void ScrollContainer::set_deadzone(int p_deadzone) {
  435. deadzone = p_deadzone;
  436. }
  437. bool ScrollContainer::is_following_focus() const {
  438. return follow_focus;
  439. }
  440. void ScrollContainer::set_follow_focus(bool p_follow) {
  441. follow_focus = p_follow;
  442. }
  443. String ScrollContainer::get_configuration_warning() const {
  444. String warning = Control::get_configuration_warning();
  445. int found = 0;
  446. for (int i = 0; i < get_child_count(); i++) {
  447. Control *c = Object::cast_to<Control>(get_child(i));
  448. if (!c) {
  449. continue;
  450. }
  451. if (c->is_set_as_toplevel()) {
  452. continue;
  453. }
  454. if (c == h_scroll || c == v_scroll) {
  455. continue;
  456. }
  457. found++;
  458. }
  459. if (found != 1) {
  460. if (warning != String()) {
  461. warning += "\n\n";
  462. }
  463. warning += TTR("ScrollContainer is intended to work with a single child control.\nUse a container as child (VBox, HBox, etc.), or a Control and set the custom minimum size manually.");
  464. }
  465. return warning;
  466. }
  467. HScrollBar *ScrollContainer::get_h_scrollbar() {
  468. return h_scroll;
  469. }
  470. VScrollBar *ScrollContainer::get_v_scrollbar() {
  471. return v_scroll;
  472. }
  473. void ScrollContainer::_bind_methods() {
  474. ClassDB::bind_method(D_METHOD("_scroll_moved"), &ScrollContainer::_scroll_moved);
  475. ClassDB::bind_method(D_METHOD("_gui_input"), &ScrollContainer::_gui_input);
  476. ClassDB::bind_method(D_METHOD("_gui_focus_changed"), &ScrollContainer::_gui_focus_changed);
  477. ClassDB::bind_method(D_METHOD("set_enable_h_scroll", "enable"), &ScrollContainer::set_enable_h_scroll);
  478. ClassDB::bind_method(D_METHOD("is_h_scroll_enabled"), &ScrollContainer::is_h_scroll_enabled);
  479. ClassDB::bind_method(D_METHOD("set_enable_v_scroll", "enable"), &ScrollContainer::set_enable_v_scroll);
  480. ClassDB::bind_method(D_METHOD("is_v_scroll_enabled"), &ScrollContainer::is_v_scroll_enabled);
  481. ClassDB::bind_method(D_METHOD("_update_scrollbar_position"), &ScrollContainer::_update_scrollbar_position);
  482. ClassDB::bind_method(D_METHOD("set_h_scroll", "value"), &ScrollContainer::set_h_scroll);
  483. ClassDB::bind_method(D_METHOD("get_h_scroll"), &ScrollContainer::get_h_scroll);
  484. ClassDB::bind_method(D_METHOD("set_v_scroll", "value"), &ScrollContainer::set_v_scroll);
  485. ClassDB::bind_method(D_METHOD("get_v_scroll"), &ScrollContainer::get_v_scroll);
  486. ClassDB::bind_method(D_METHOD("set_deadzone", "deadzone"), &ScrollContainer::set_deadzone);
  487. ClassDB::bind_method(D_METHOD("get_deadzone"), &ScrollContainer::get_deadzone);
  488. ClassDB::bind_method(D_METHOD("set_follow_focus", "enabled"), &ScrollContainer::set_follow_focus);
  489. ClassDB::bind_method(D_METHOD("is_following_focus"), &ScrollContainer::is_following_focus);
  490. ClassDB::bind_method(D_METHOD("get_h_scrollbar"), &ScrollContainer::get_h_scrollbar);
  491. ClassDB::bind_method(D_METHOD("get_v_scrollbar"), &ScrollContainer::get_v_scrollbar);
  492. ClassDB::bind_method(D_METHOD("ensure_control_visible", "control"), &ScrollContainer::ensure_control_visible);
  493. ADD_SIGNAL(MethodInfo("scroll_started"));
  494. ADD_SIGNAL(MethodInfo("scroll_ended"));
  495. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "follow_focus"), "set_follow_focus", "is_following_focus");
  496. ADD_GROUP("Scroll", "scroll_");
  497. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_horizontal_enabled"), "set_enable_h_scroll", "is_h_scroll_enabled");
  498. ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_horizontal"), "set_h_scroll", "get_h_scroll");
  499. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_vertical_enabled"), "set_enable_v_scroll", "is_v_scroll_enabled");
  500. ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_vertical"), "set_v_scroll", "get_v_scroll");
  501. ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_deadzone"), "set_deadzone", "get_deadzone");
  502. GLOBAL_DEF("gui/common/default_scroll_deadzone", 0);
  503. };
  504. ScrollContainer::ScrollContainer() {
  505. h_scroll = memnew(HScrollBar);
  506. h_scroll->set_name("_h_scroll");
  507. add_child(h_scroll);
  508. h_scroll->connect("value_changed", this, "_scroll_moved");
  509. v_scroll = memnew(VScrollBar);
  510. v_scroll->set_name("_v_scroll");
  511. add_child(v_scroll);
  512. v_scroll->connect("value_changed", this, "_scroll_moved");
  513. drag_speed = Vector2();
  514. drag_touching = false;
  515. drag_touching_deaccel = false;
  516. beyond_deadzone = false;
  517. scroll_h = true;
  518. scroll_v = true;
  519. deadzone = GLOBAL_GET("gui/common/default_scroll_deadzone");
  520. follow_focus = false;
  521. set_clip_contents(true);
  522. };