scroll_container.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. /*************************************************************************/
  2. /* scroll_container.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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/window.h"
  33. Size2 ScrollContainer::get_minimum_size() const {
  34. Ref<StyleBox> sb = get_theme_stylebox(SNAME("bg"));
  35. Size2 min_size;
  36. for (int i = 0; i < get_child_count(); i++) {
  37. Control *c = Object::cast_to<Control>(get_child(i));
  38. if (!c) {
  39. continue;
  40. }
  41. if (c->is_set_as_top_level()) {
  42. continue;
  43. }
  44. if (c == h_scroll || c == v_scroll) {
  45. continue;
  46. }
  47. Size2 minsize = c->get_combined_minimum_size();
  48. if (!scroll_h) {
  49. min_size.x = MAX(min_size.x, minsize.x);
  50. }
  51. if (!scroll_v) {
  52. min_size.y = MAX(min_size.y, minsize.y);
  53. }
  54. }
  55. if (h_scroll->is_visible_in_tree()) {
  56. min_size.y += h_scroll->get_minimum_size().y;
  57. }
  58. if (v_scroll->is_visible_in_tree()) {
  59. min_size.x += v_scroll->get_minimum_size().x;
  60. }
  61. min_size += sb->get_minimum_size();
  62. return min_size;
  63. }
  64. void ScrollContainer::_cancel_drag() {
  65. set_physics_process_internal(false);
  66. drag_touching_deaccel = false;
  67. drag_touching = false;
  68. drag_speed = Vector2();
  69. drag_accum = Vector2();
  70. last_drag_accum = Vector2();
  71. drag_from = Vector2();
  72. if (beyond_deadzone) {
  73. emit_signal(SNAME("scroll_ended"));
  74. propagate_notification(NOTIFICATION_SCROLL_END);
  75. beyond_deadzone = false;
  76. }
  77. }
  78. void ScrollContainer::gui_input(const Ref<InputEvent> &p_gui_input) {
  79. ERR_FAIL_COND(p_gui_input.is_null());
  80. double prev_v_scroll = v_scroll->get_value();
  81. double prev_h_scroll = h_scroll->get_value();
  82. Ref<InputEventMouseButton> mb = p_gui_input;
  83. if (mb.is_valid()) {
  84. if (mb->get_button_index() == MOUSE_BUTTON_WHEEL_UP && mb->is_pressed()) {
  85. // only horizontal is enabled, scroll horizontally
  86. if (h_scroll->is_visible() && (!v_scroll->is_visible() || mb->is_shift_pressed())) {
  87. h_scroll->set_value(h_scroll->get_value() - h_scroll->get_page() / 8 * mb->get_factor());
  88. } else if (v_scroll->is_visible_in_tree()) {
  89. v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page() / 8 * mb->get_factor());
  90. }
  91. }
  92. if (mb->get_button_index() == MOUSE_BUTTON_WHEEL_DOWN && mb->is_pressed()) {
  93. // only horizontal is enabled, scroll horizontally
  94. if (h_scroll->is_visible() && (!v_scroll->is_visible() || mb->is_shift_pressed())) {
  95. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() / 8 * mb->get_factor());
  96. } else if (v_scroll->is_visible()) {
  97. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() / 8 * mb->get_factor());
  98. }
  99. }
  100. if (mb->get_button_index() == MOUSE_BUTTON_WHEEL_LEFT && mb->is_pressed()) {
  101. if (h_scroll->is_visible_in_tree()) {
  102. h_scroll->set_value(h_scroll->get_value() - h_scroll->get_page() * mb->get_factor() / 8);
  103. }
  104. }
  105. if (mb->get_button_index() == MOUSE_BUTTON_WHEEL_RIGHT && mb->is_pressed()) {
  106. if (h_scroll->is_visible_in_tree()) {
  107. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * mb->get_factor() / 8);
  108. }
  109. }
  110. if (v_scroll->get_value() != prev_v_scroll || h_scroll->get_value() != prev_h_scroll) {
  111. accept_event(); //accept event if scroll changed
  112. }
  113. if (!DisplayServer::get_singleton()->screen_is_touchscreen(DisplayServer::get_singleton()->window_get_current_screen(get_viewport()->get_window_id()))) {
  114. return;
  115. }
  116. if (mb->get_button_index() != MOUSE_BUTTON_LEFT) {
  117. return;
  118. }
  119. if (mb->is_pressed()) {
  120. if (drag_touching) {
  121. _cancel_drag();
  122. }
  123. drag_speed = Vector2();
  124. drag_accum = Vector2();
  125. last_drag_accum = Vector2();
  126. drag_from = Vector2(h_scroll->get_value(), v_scroll->get_value());
  127. drag_touching = !DisplayServer::get_singleton()->screen_is_touchscreen(DisplayServer::get_singleton()->window_get_current_screen(get_viewport()->get_window_id()));
  128. drag_touching_deaccel = false;
  129. beyond_deadzone = false;
  130. time_since_motion = 0;
  131. if (drag_touching) {
  132. set_physics_process_internal(true);
  133. time_since_motion = 0;
  134. }
  135. } else {
  136. if (drag_touching) {
  137. if (drag_speed == Vector2()) {
  138. _cancel_drag();
  139. } else {
  140. drag_touching_deaccel = true;
  141. }
  142. }
  143. }
  144. }
  145. Ref<InputEventMouseMotion> mm = p_gui_input;
  146. if (mm.is_valid()) {
  147. if (drag_touching && !drag_touching_deaccel) {
  148. Vector2 motion = mm->get_relative();
  149. drag_accum -= motion;
  150. if (beyond_deadzone || (scroll_h && Math::abs(drag_accum.x) > deadzone) || (scroll_v && Math::abs(drag_accum.y) > deadzone)) {
  151. if (!beyond_deadzone) {
  152. propagate_notification(NOTIFICATION_SCROLL_BEGIN);
  153. emit_signal(SNAME("scroll_started"));
  154. beyond_deadzone = true;
  155. // resetting drag_accum here ensures smooth scrolling after reaching deadzone
  156. drag_accum = -motion;
  157. }
  158. Vector2 diff = drag_from + drag_accum;
  159. if (scroll_h) {
  160. h_scroll->set_value(diff.x);
  161. } else {
  162. drag_accum.x = 0;
  163. }
  164. if (scroll_v) {
  165. v_scroll->set_value(diff.y);
  166. } else {
  167. drag_accum.y = 0;
  168. }
  169. time_since_motion = 0;
  170. }
  171. }
  172. }
  173. Ref<InputEventPanGesture> pan_gesture = p_gui_input;
  174. if (pan_gesture.is_valid()) {
  175. if (h_scroll->is_visible_in_tree()) {
  176. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * pan_gesture->get_delta().x / 8);
  177. }
  178. if (v_scroll->is_visible_in_tree()) {
  179. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * pan_gesture->get_delta().y / 8);
  180. }
  181. }
  182. if (v_scroll->get_value() != prev_v_scroll || h_scroll->get_value() != prev_h_scroll) {
  183. accept_event(); //accept event if scroll changed
  184. }
  185. }
  186. void ScrollContainer::_update_scrollbar_position() {
  187. if (!_updating_scrollbars) {
  188. return;
  189. }
  190. Size2 hmin = h_scroll->get_combined_minimum_size();
  191. Size2 vmin = v_scroll->get_combined_minimum_size();
  192. h_scroll->set_anchor_and_offset(SIDE_LEFT, ANCHOR_BEGIN, 0);
  193. h_scroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0);
  194. h_scroll->set_anchor_and_offset(SIDE_TOP, ANCHOR_END, -hmin.height);
  195. h_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0);
  196. v_scroll->set_anchor_and_offset(SIDE_LEFT, ANCHOR_END, -vmin.width);
  197. v_scroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0);
  198. v_scroll->set_anchor_and_offset(SIDE_TOP, ANCHOR_BEGIN, 0);
  199. v_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0);
  200. _updating_scrollbars = false;
  201. }
  202. void ScrollContainer::_gui_focus_changed(Control *p_control) {
  203. if (follow_focus && is_ancestor_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_ancestor_of(p_control), "Must be an ancestor 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 + (!is_layout_rtl() ? right_margin : 0.0f)),
  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::_update_dimensions() {
  219. child_max_size = Size2(0, 0);
  220. Size2 size = get_size();
  221. Point2 ofs;
  222. Ref<StyleBox> sb = get_theme_stylebox(SNAME("bg"));
  223. size -= sb->get_minimum_size();
  224. ofs += sb->get_offset();
  225. bool rtl = is_layout_rtl();
  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_top_level()) {
  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(-Size2(get_h_scroll(), get_v_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. if (rtl && v_scroll->is_visible_in_tree() && v_scroll->get_parent() == this) {
  265. r.position.x += v_scroll->get_minimum_size().x;
  266. }
  267. r.position = r.position.floor();
  268. fit_child_in_rect(c, r);
  269. }
  270. update();
  271. }
  272. void ScrollContainer::_notification(int p_what) {
  273. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED || p_what == NOTIFICATION_LAYOUT_DIRECTION_CHANGED || p_what == NOTIFICATION_TRANSLATION_CHANGED) {
  274. _updating_scrollbars = true;
  275. call_deferred(SNAME("_update_scrollbar_position"));
  276. };
  277. if (p_what == NOTIFICATION_READY) {
  278. get_viewport()->connect("gui_focus_changed", callable_mp(this, &ScrollContainer::_gui_focus_changed));
  279. _update_dimensions();
  280. }
  281. if (p_what == NOTIFICATION_SORT_CHILDREN) {
  282. _update_dimensions();
  283. };
  284. if (p_what == NOTIFICATION_DRAW) {
  285. Ref<StyleBox> sb = get_theme_stylebox(SNAME("bg"));
  286. draw_style_box(sb, Rect2(Vector2(), get_size()));
  287. update_scrollbars();
  288. }
  289. if (p_what == NOTIFICATION_INTERNAL_PHYSICS_PROCESS) {
  290. if (drag_touching) {
  291. if (drag_touching_deaccel) {
  292. Vector2 pos = Vector2(h_scroll->get_value(), v_scroll->get_value());
  293. pos += drag_speed * get_physics_process_delta_time();
  294. bool turnoff_h = false;
  295. bool turnoff_v = false;
  296. if (pos.x < 0) {
  297. pos.x = 0;
  298. turnoff_h = true;
  299. }
  300. if (pos.x > (h_scroll->get_max() - h_scroll->get_page())) {
  301. pos.x = h_scroll->get_max() - h_scroll->get_page();
  302. turnoff_h = true;
  303. }
  304. if (pos.y < 0) {
  305. pos.y = 0;
  306. turnoff_v = true;
  307. }
  308. if (pos.y > (v_scroll->get_max() - v_scroll->get_page())) {
  309. pos.y = v_scroll->get_max() - v_scroll->get_page();
  310. turnoff_v = true;
  311. }
  312. if (scroll_h) {
  313. h_scroll->set_value(pos.x);
  314. }
  315. if (scroll_v) {
  316. v_scroll->set_value(pos.y);
  317. }
  318. float sgn_x = drag_speed.x < 0 ? -1 : 1;
  319. float val_x = Math::abs(drag_speed.x);
  320. val_x -= 1000 * get_physics_process_delta_time();
  321. if (val_x < 0) {
  322. turnoff_h = true;
  323. }
  324. float sgn_y = drag_speed.y < 0 ? -1 : 1;
  325. float val_y = Math::abs(drag_speed.y);
  326. val_y -= 1000 * get_physics_process_delta_time();
  327. if (val_y < 0) {
  328. turnoff_v = true;
  329. }
  330. drag_speed = Vector2(sgn_x * val_x, sgn_y * val_y);
  331. if (turnoff_h && turnoff_v) {
  332. _cancel_drag();
  333. }
  334. } else {
  335. if (time_since_motion == 0 || time_since_motion > 0.1) {
  336. Vector2 diff = drag_accum - last_drag_accum;
  337. last_drag_accum = drag_accum;
  338. drag_speed = diff / get_physics_process_delta_time();
  339. }
  340. time_since_motion += get_physics_process_delta_time();
  341. }
  342. }
  343. }
  344. };
  345. void ScrollContainer::update_scrollbars() {
  346. Size2 size = get_size();
  347. Ref<StyleBox> sb = get_theme_stylebox(SNAME("bg"));
  348. size -= sb->get_minimum_size();
  349. Size2 hmin;
  350. Size2 vmin;
  351. if (scroll_h) {
  352. hmin = h_scroll->get_combined_minimum_size();
  353. }
  354. if (scroll_v) {
  355. vmin = v_scroll->get_combined_minimum_size();
  356. }
  357. Size2 min = child_max_size;
  358. bool hide_scroll_h = !scroll_h || min.width <= size.width || !h_scroll_visible;
  359. bool hide_scroll_v = !scroll_v || min.height <= size.height || !v_scroll_visible;
  360. h_scroll->set_max(min.width);
  361. h_scroll->set_page(size.width - (hide_scroll_v ? 0 : vmin.width));
  362. h_scroll->set_visible(!hide_scroll_h);
  363. v_scroll->set_max(min.height);
  364. v_scroll->set_page(size.height - (hide_scroll_h ? 0 : hmin.height));
  365. v_scroll->set_visible(!hide_scroll_v);
  366. // Avoid scrollbar overlapping.
  367. h_scroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, hide_scroll_v ? 0 : -vmin.width);
  368. v_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, hide_scroll_h ? 0 : -hmin.height);
  369. }
  370. void ScrollContainer::_scroll_moved(float) {
  371. queue_sort();
  372. update();
  373. };
  374. void ScrollContainer::set_h_scroll(int p_pos) {
  375. h_scroll->set_value(p_pos);
  376. _cancel_drag();
  377. }
  378. int ScrollContainer::get_h_scroll() const {
  379. return h_scroll->get_value();
  380. }
  381. void ScrollContainer::set_v_scroll(int p_pos) {
  382. v_scroll->set_value(p_pos);
  383. _cancel_drag();
  384. }
  385. int ScrollContainer::get_v_scroll() const {
  386. return v_scroll->get_value();
  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. void ScrollContainer::set_h_scroll_visible(bool p_visible) {
  411. if (h_scroll_visible == p_visible) {
  412. return;
  413. }
  414. h_scroll_visible = p_visible;
  415. update_scrollbars();
  416. }
  417. bool ScrollContainer::is_h_scroll_visible() const {
  418. return h_scroll_visible;
  419. }
  420. void ScrollContainer::set_v_scroll_visible(bool p_visible) {
  421. if (v_scroll_visible == p_visible) {
  422. return;
  423. }
  424. v_scroll_visible = p_visible;
  425. update_scrollbars();
  426. }
  427. bool ScrollContainer::is_v_scroll_visible() const {
  428. return v_scroll_visible;
  429. }
  430. int ScrollContainer::get_deadzone() const {
  431. return deadzone;
  432. }
  433. void ScrollContainer::set_deadzone(int p_deadzone) {
  434. deadzone = p_deadzone;
  435. }
  436. bool ScrollContainer::is_following_focus() const {
  437. return follow_focus;
  438. }
  439. void ScrollContainer::set_follow_focus(bool p_follow) {
  440. follow_focus = p_follow;
  441. }
  442. TypedArray<String> ScrollContainer::get_configuration_warnings() const {
  443. TypedArray<String> warnings = Container::get_configuration_warnings();
  444. int found = 0;
  445. for (int i = 0; i < get_child_count(); i++) {
  446. Control *c = Object::cast_to<Control>(get_child(i));
  447. if (!c) {
  448. continue;
  449. }
  450. if (c->is_set_as_top_level()) {
  451. continue;
  452. }
  453. if (c == h_scroll || c == v_scroll) {
  454. continue;
  455. }
  456. found++;
  457. }
  458. if (found != 1) {
  459. warnings.push_back(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."));
  460. }
  461. return warnings;
  462. }
  463. HScrollBar *ScrollContainer::get_h_scrollbar() {
  464. return h_scroll;
  465. }
  466. VScrollBar *ScrollContainer::get_v_scrollbar() {
  467. return v_scroll;
  468. }
  469. void ScrollContainer::_bind_methods() {
  470. ClassDB::bind_method(D_METHOD("_update_scrollbar_position"), &ScrollContainer::_update_scrollbar_position);
  471. ClassDB::bind_method(D_METHOD("set_h_scroll", "value"), &ScrollContainer::set_h_scroll);
  472. ClassDB::bind_method(D_METHOD("get_h_scroll"), &ScrollContainer::get_h_scroll);
  473. ClassDB::bind_method(D_METHOD("set_v_scroll", "value"), &ScrollContainer::set_v_scroll);
  474. ClassDB::bind_method(D_METHOD("get_v_scroll"), &ScrollContainer::get_v_scroll);
  475. ClassDB::bind_method(D_METHOD("set_enable_h_scroll", "enable"), &ScrollContainer::set_enable_h_scroll);
  476. ClassDB::bind_method(D_METHOD("is_h_scroll_enabled"), &ScrollContainer::is_h_scroll_enabled);
  477. ClassDB::bind_method(D_METHOD("set_enable_v_scroll", "enable"), &ScrollContainer::set_enable_v_scroll);
  478. ClassDB::bind_method(D_METHOD("is_v_scroll_enabled"), &ScrollContainer::is_v_scroll_enabled);
  479. ClassDB::bind_method(D_METHOD("set_h_scroll_visible", "visible"), &ScrollContainer::set_h_scroll_visible);
  480. ClassDB::bind_method(D_METHOD("is_h_scroll_visible"), &ScrollContainer::is_h_scroll_visible);
  481. ClassDB::bind_method(D_METHOD("set_v_scroll_visible", "visible"), &ScrollContainer::set_v_scroll_visible);
  482. ClassDB::bind_method(D_METHOD("is_v_scroll_visible"), &ScrollContainer::is_v_scroll_visible);
  483. ClassDB::bind_method(D_METHOD("set_deadzone", "deadzone"), &ScrollContainer::set_deadzone);
  484. ClassDB::bind_method(D_METHOD("get_deadzone"), &ScrollContainer::get_deadzone);
  485. ClassDB::bind_method(D_METHOD("set_follow_focus", "enabled"), &ScrollContainer::set_follow_focus);
  486. ClassDB::bind_method(D_METHOD("is_following_focus"), &ScrollContainer::is_following_focus);
  487. ClassDB::bind_method(D_METHOD("get_h_scrollbar"), &ScrollContainer::get_h_scrollbar);
  488. ClassDB::bind_method(D_METHOD("get_v_scrollbar"), &ScrollContainer::get_v_scrollbar);
  489. ClassDB::bind_method(D_METHOD("ensure_control_visible", "control"), &ScrollContainer::ensure_control_visible);
  490. ADD_SIGNAL(MethodInfo("scroll_started"));
  491. ADD_SIGNAL(MethodInfo("scroll_ended"));
  492. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "follow_focus"), "set_follow_focus", "is_following_focus");
  493. ADD_GROUP("Scroll", "scroll_");
  494. ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_horizontal"), "set_h_scroll", "get_h_scroll");
  495. ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_vertical"), "set_v_scroll", "get_v_scroll");
  496. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_horizontal_enabled"), "set_enable_h_scroll", "is_h_scroll_enabled");
  497. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_vertical_enabled"), "set_enable_v_scroll", "is_v_scroll_enabled");
  498. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_horizontal_visible"), "set_h_scroll_visible", "is_h_scroll_visible");
  499. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_vertical_visible"), "set_v_scroll_visible", "is_v_scroll_visible");
  500. ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_deadzone"), "set_deadzone", "get_deadzone");
  501. GLOBAL_DEF("gui/common/default_scroll_deadzone", 0);
  502. };
  503. ScrollContainer::ScrollContainer() {
  504. h_scroll = memnew(HScrollBar);
  505. h_scroll->set_name("_h_scroll");
  506. add_child(h_scroll, false, INTERNAL_MODE_BACK);
  507. h_scroll->connect("value_changed", callable_mp(this, &ScrollContainer::_scroll_moved));
  508. v_scroll = memnew(VScrollBar);
  509. v_scroll->set_name("_v_scroll");
  510. add_child(v_scroll, false, INTERNAL_MODE_BACK);
  511. v_scroll->connect("value_changed", callable_mp(this, &ScrollContainer::_scroll_moved));
  512. deadzone = GLOBAL_GET("gui/common/default_scroll_deadzone");
  513. set_clip_contents(true);
  514. };