scroll_container.cpp 19 KB

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