scroll_container.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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/config/project_settings.h"
  32. #include "core/os/os.h"
  33. #include "scene/main/window.h"
  34. Size2 ScrollContainer::get_minimum_size() const {
  35. Ref<StyleBox> sb = get_theme_stylebox(SNAME("bg"));
  36. Size2 min_size;
  37. // Calculated in this function, as it needs to traverse all child controls once to calculate;
  38. // and needs to be calculated before being used by update_scrollbars().
  39. largest_child_min_size = Size2();
  40. for (int i = 0; i < get_child_count(); i++) {
  41. Control *c = Object::cast_to<Control>(get_child(i));
  42. if (!c || !c->is_visible()) {
  43. continue;
  44. }
  45. if (c->is_set_as_top_level()) {
  46. continue;
  47. }
  48. if (c == h_scroll || c == v_scroll) {
  49. continue;
  50. }
  51. Size2 child_min_size = c->get_combined_minimum_size();
  52. largest_child_min_size.x = MAX(largest_child_min_size.x, child_min_size.x);
  53. largest_child_min_size.y = MAX(largest_child_min_size.y, child_min_size.y);
  54. }
  55. if (horizontal_scroll_mode == SCROLL_MODE_DISABLED) {
  56. min_size.x = MAX(min_size.x, largest_child_min_size.x);
  57. }
  58. if (vertical_scroll_mode == SCROLL_MODE_DISABLED) {
  59. min_size.y = MAX(min_size.y, largest_child_min_size.y);
  60. }
  61. bool h_scroll_show = horizontal_scroll_mode == SCROLL_MODE_SHOW_ALWAYS || (horizontal_scroll_mode == SCROLL_MODE_AUTO && largest_child_min_size.x > min_size.x);
  62. bool v_scroll_show = vertical_scroll_mode == SCROLL_MODE_SHOW_ALWAYS || (vertical_scroll_mode == SCROLL_MODE_AUTO && largest_child_min_size.y > min_size.y);
  63. if (h_scroll_show && h_scroll->get_parent() == this) {
  64. min_size.y += h_scroll->get_minimum_size().y;
  65. }
  66. if (v_scroll_show && v_scroll->get_parent() == this) {
  67. min_size.x += v_scroll->get_minimum_size().x;
  68. }
  69. min_size += sb->get_minimum_size();
  70. return min_size;
  71. }
  72. void ScrollContainer::_cancel_drag() {
  73. set_physics_process_internal(false);
  74. drag_touching_deaccel = false;
  75. drag_touching = false;
  76. drag_speed = Vector2();
  77. drag_accum = Vector2();
  78. last_drag_accum = Vector2();
  79. drag_from = Vector2();
  80. if (beyond_deadzone) {
  81. emit_signal(SNAME("scroll_ended"));
  82. propagate_notification(NOTIFICATION_SCROLL_END);
  83. beyond_deadzone = false;
  84. }
  85. }
  86. void ScrollContainer::gui_input(const Ref<InputEvent> &p_gui_input) {
  87. ERR_FAIL_COND(p_gui_input.is_null());
  88. double prev_v_scroll = v_scroll->get_value();
  89. double prev_h_scroll = h_scroll->get_value();
  90. Ref<InputEventMouseButton> mb = p_gui_input;
  91. if (mb.is_valid()) {
  92. if (mb->get_button_index() == MouseButton::WHEEL_UP && 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_in_tree()) {
  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() == MouseButton::WHEEL_DOWN && mb->is_pressed()) {
  101. // only horizontal is enabled, scroll horizontally
  102. if (h_scroll->is_visible() && (!v_scroll->is_visible() || mb->is_shift_pressed())) {
  103. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() / 8 * mb->get_factor());
  104. } else if (v_scroll->is_visible()) {
  105. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() / 8 * mb->get_factor());
  106. }
  107. }
  108. if (mb->get_button_index() == MouseButton::WHEEL_LEFT && 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 (mb->get_button_index() == MouseButton::WHEEL_RIGHT && mb->is_pressed()) {
  114. if (h_scroll->is_visible_in_tree()) {
  115. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * mb->get_factor() / 8);
  116. }
  117. }
  118. if (v_scroll->get_value() != prev_v_scroll || h_scroll->get_value() != prev_h_scroll) {
  119. accept_event(); //accept event if scroll changed
  120. }
  121. if (!DisplayServer::get_singleton()->screen_is_touchscreen(DisplayServer::get_singleton()->window_get_current_screen(get_viewport()->get_window_id()))) {
  122. return;
  123. }
  124. if (mb->get_button_index() != MouseButton::LEFT) {
  125. return;
  126. }
  127. if (mb->is_pressed()) {
  128. if (drag_touching) {
  129. _cancel_drag();
  130. }
  131. drag_speed = Vector2();
  132. drag_accum = Vector2();
  133. last_drag_accum = Vector2();
  134. drag_from = Vector2(h_scroll->get_value(), v_scroll->get_value());
  135. drag_touching = !DisplayServer::get_singleton()->screen_is_touchscreen(DisplayServer::get_singleton()->window_get_current_screen(get_viewport()->get_window_id()));
  136. drag_touching_deaccel = false;
  137. beyond_deadzone = false;
  138. time_since_motion = 0;
  139. if (drag_touching) {
  140. set_physics_process_internal(true);
  141. time_since_motion = 0;
  142. }
  143. } else {
  144. if (drag_touching) {
  145. if (drag_speed == Vector2()) {
  146. _cancel_drag();
  147. } else {
  148. drag_touching_deaccel = true;
  149. }
  150. }
  151. }
  152. }
  153. Ref<InputEventMouseMotion> mm = p_gui_input;
  154. if (mm.is_valid()) {
  155. if (drag_touching && !drag_touching_deaccel) {
  156. Vector2 motion = mm->get_relative();
  157. drag_accum -= motion;
  158. if (beyond_deadzone || (horizontal_scroll_mode != SCROLL_MODE_DISABLED && Math::abs(drag_accum.x) > deadzone) || (vertical_scroll_mode != SCROLL_MODE_DISABLED && Math::abs(drag_accum.y) > deadzone)) {
  159. if (!beyond_deadzone) {
  160. propagate_notification(NOTIFICATION_SCROLL_BEGIN);
  161. emit_signal(SNAME("scroll_started"));
  162. beyond_deadzone = true;
  163. // resetting drag_accum here ensures smooth scrolling after reaching deadzone
  164. drag_accum = -motion;
  165. }
  166. Vector2 diff = drag_from + drag_accum;
  167. if (horizontal_scroll_mode != SCROLL_MODE_DISABLED) {
  168. h_scroll->set_value(diff.x);
  169. } else {
  170. drag_accum.x = 0;
  171. }
  172. if (vertical_scroll_mode != SCROLL_MODE_DISABLED) {
  173. v_scroll->set_value(diff.y);
  174. } else {
  175. drag_accum.y = 0;
  176. }
  177. time_since_motion = 0;
  178. }
  179. }
  180. }
  181. Ref<InputEventPanGesture> pan_gesture = p_gui_input;
  182. if (pan_gesture.is_valid()) {
  183. if (h_scroll->is_visible_in_tree()) {
  184. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * pan_gesture->get_delta().x / 8);
  185. }
  186. if (v_scroll->is_visible_in_tree()) {
  187. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * pan_gesture->get_delta().y / 8);
  188. }
  189. }
  190. if (v_scroll->get_value() != prev_v_scroll || h_scroll->get_value() != prev_h_scroll) {
  191. accept_event(); //accept event if scroll changed
  192. }
  193. }
  194. void ScrollContainer::_update_scrollbar_position() {
  195. if (!_updating_scrollbars) {
  196. return;
  197. }
  198. Size2 hmin = h_scroll->get_combined_minimum_size();
  199. Size2 vmin = v_scroll->get_combined_minimum_size();
  200. h_scroll->set_anchor_and_offset(SIDE_LEFT, ANCHOR_BEGIN, 0);
  201. h_scroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0);
  202. h_scroll->set_anchor_and_offset(SIDE_TOP, ANCHOR_END, -hmin.height);
  203. h_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0);
  204. v_scroll->set_anchor_and_offset(SIDE_LEFT, ANCHOR_END, -vmin.width);
  205. v_scroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0);
  206. v_scroll->set_anchor_and_offset(SIDE_TOP, ANCHOR_BEGIN, 0);
  207. v_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0);
  208. _updating_scrollbars = false;
  209. }
  210. void ScrollContainer::_gui_focus_changed(Control *p_control) {
  211. if (follow_focus && is_ancestor_of(p_control)) {
  212. ensure_control_visible(p_control);
  213. }
  214. }
  215. void ScrollContainer::ensure_control_visible(Control *p_control) {
  216. ERR_FAIL_COND_MSG(!is_ancestor_of(p_control), "Must be an ancestor of the control.");
  217. Rect2 global_rect = get_global_rect();
  218. Rect2 other_rect = p_control->get_global_rect();
  219. float right_margin = v_scroll->is_visible() ? v_scroll->get_size().x : 0.0f;
  220. float bottom_margin = h_scroll->is_visible() ? h_scroll->get_size().y : 0.0f;
  221. 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)),
  222. MAX(MIN(other_rect.position.y, global_rect.position.y), other_rect.position.y + other_rect.size.y - global_rect.size.y + bottom_margin));
  223. set_h_scroll(get_h_scroll() + (diff.x - global_rect.position.x));
  224. set_v_scroll(get_v_scroll() + (diff.y - global_rect.position.y));
  225. }
  226. void ScrollContainer::_reposition_children() {
  227. update_scrollbars();
  228. Size2 size = get_size();
  229. Point2 ofs;
  230. Ref<StyleBox> sb = get_theme_stylebox(SNAME("bg"));
  231. size -= sb->get_minimum_size();
  232. ofs += sb->get_offset();
  233. bool rtl = is_layout_rtl();
  234. if (h_scroll->is_visible_in_tree() && h_scroll->get_parent() == this) { //scrolls may have been moved out for reasons
  235. size.y -= h_scroll->get_minimum_size().y;
  236. }
  237. if (v_scroll->is_visible_in_tree() && v_scroll->get_parent() == this) { //scrolls may have been moved out for reasons
  238. size.x -= v_scroll->get_minimum_size().x;
  239. }
  240. for (int i = 0; i < get_child_count(); i++) {
  241. Control *c = Object::cast_to<Control>(get_child(i));
  242. if (!c || !c->is_visible()) {
  243. continue;
  244. }
  245. if (c->is_set_as_top_level()) {
  246. continue;
  247. }
  248. if (c == h_scroll || c == v_scroll) {
  249. continue;
  250. }
  251. Size2 minsize = c->get_combined_minimum_size();
  252. Rect2 r = Rect2(-Size2(get_h_scroll(), get_v_scroll()), minsize);
  253. if (c->get_h_size_flags() & SIZE_EXPAND) {
  254. r.size.width = MAX(size.width, minsize.width);
  255. }
  256. if (c->get_v_size_flags() & SIZE_EXPAND) {
  257. r.size.height = MAX(size.height, minsize.height);
  258. }
  259. r.position += ofs;
  260. if (rtl && v_scroll->is_visible_in_tree() && v_scroll->get_parent() == this) {
  261. r.position.x += v_scroll->get_minimum_size().x;
  262. }
  263. r.position = r.position.floor();
  264. fit_child_in_rect(c, r);
  265. }
  266. queue_redraw();
  267. }
  268. void ScrollContainer::_notification(int p_what) {
  269. switch (p_what) {
  270. case NOTIFICATION_ENTER_TREE:
  271. case NOTIFICATION_THEME_CHANGED:
  272. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  273. case NOTIFICATION_TRANSLATION_CHANGED: {
  274. _updating_scrollbars = true;
  275. call_deferred(SNAME("_update_scrollbar_position"));
  276. } break;
  277. case NOTIFICATION_READY: {
  278. Viewport *viewport = get_viewport();
  279. ERR_FAIL_COND(!viewport);
  280. viewport->connect("gui_focus_changed", callable_mp(this, &ScrollContainer::_gui_focus_changed));
  281. _reposition_children();
  282. } break;
  283. case NOTIFICATION_SORT_CHILDREN: {
  284. _reposition_children();
  285. } break;
  286. case NOTIFICATION_DRAW: {
  287. Ref<StyleBox> sb = get_theme_stylebox(SNAME("bg"));
  288. draw_style_box(sb, Rect2(Vector2(), get_size()));
  289. } break;
  290. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  291. if (drag_touching) {
  292. if (drag_touching_deaccel) {
  293. Vector2 pos = Vector2(h_scroll->get_value(), v_scroll->get_value());
  294. pos += drag_speed * get_physics_process_delta_time();
  295. bool turnoff_h = false;
  296. bool turnoff_v = false;
  297. if (pos.x < 0) {
  298. pos.x = 0;
  299. turnoff_h = true;
  300. }
  301. if (pos.x > (h_scroll->get_max() - h_scroll->get_page())) {
  302. pos.x = h_scroll->get_max() - h_scroll->get_page();
  303. turnoff_h = true;
  304. }
  305. if (pos.y < 0) {
  306. pos.y = 0;
  307. turnoff_v = true;
  308. }
  309. if (pos.y > (v_scroll->get_max() - v_scroll->get_page())) {
  310. pos.y = v_scroll->get_max() - v_scroll->get_page();
  311. turnoff_v = true;
  312. }
  313. if (horizontal_scroll_mode != SCROLL_MODE_DISABLED) {
  314. h_scroll->set_value(pos.x);
  315. }
  316. if (vertical_scroll_mode != SCROLL_MODE_DISABLED) {
  317. v_scroll->set_value(pos.y);
  318. }
  319. float sgn_x = drag_speed.x < 0 ? -1 : 1;
  320. float val_x = Math::abs(drag_speed.x);
  321. val_x -= 1000 * get_physics_process_delta_time();
  322. if (val_x < 0) {
  323. turnoff_h = true;
  324. }
  325. float sgn_y = drag_speed.y < 0 ? -1 : 1;
  326. float val_y = Math::abs(drag_speed.y);
  327. val_y -= 1000 * get_physics_process_delta_time();
  328. if (val_y < 0) {
  329. turnoff_v = true;
  330. }
  331. drag_speed = Vector2(sgn_x * val_x, sgn_y * val_y);
  332. if (turnoff_h && turnoff_v) {
  333. _cancel_drag();
  334. }
  335. } else {
  336. if (time_since_motion == 0 || time_since_motion > 0.1) {
  337. Vector2 diff = drag_accum - last_drag_accum;
  338. last_drag_accum = drag_accum;
  339. drag_speed = diff / get_physics_process_delta_time();
  340. }
  341. time_since_motion += get_physics_process_delta_time();
  342. }
  343. }
  344. } break;
  345. }
  346. }
  347. void ScrollContainer::update_scrollbars() {
  348. Size2 size = get_size();
  349. Ref<StyleBox> sb = get_theme_stylebox(SNAME("bg"));
  350. size -= sb->get_minimum_size();
  351. Size2 hmin = h_scroll->get_combined_minimum_size();
  352. Size2 vmin = v_scroll->get_combined_minimum_size();
  353. h_scroll->set_visible(horizontal_scroll_mode == SCROLL_MODE_SHOW_ALWAYS || (horizontal_scroll_mode == SCROLL_MODE_AUTO && largest_child_min_size.width > size.width));
  354. v_scroll->set_visible(vertical_scroll_mode == SCROLL_MODE_SHOW_ALWAYS || (vertical_scroll_mode == SCROLL_MODE_AUTO && largest_child_min_size.height > size.height));
  355. h_scroll->set_max(largest_child_min_size.width);
  356. h_scroll->set_page((v_scroll->is_visible() && v_scroll->get_parent() == this) ? size.width - vmin.width : size.width);
  357. v_scroll->set_max(largest_child_min_size.height);
  358. v_scroll->set_page((h_scroll->is_visible() && h_scroll->get_parent() == this) ? size.height - hmin.height : size.height);
  359. // Avoid scrollbar overlapping.
  360. h_scroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, (v_scroll->is_visible() && v_scroll->get_parent() == this) ? -vmin.width : 0);
  361. v_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, (h_scroll->is_visible() && h_scroll->get_parent() == this) ? -hmin.height : 0);
  362. }
  363. void ScrollContainer::_scroll_moved(float) {
  364. queue_sort();
  365. };
  366. void ScrollContainer::set_h_scroll(int p_pos) {
  367. h_scroll->set_value(p_pos);
  368. _cancel_drag();
  369. }
  370. int ScrollContainer::get_h_scroll() const {
  371. return h_scroll->get_value();
  372. }
  373. void ScrollContainer::set_v_scroll(int p_pos) {
  374. v_scroll->set_value(p_pos);
  375. _cancel_drag();
  376. }
  377. int ScrollContainer::get_v_scroll() const {
  378. return v_scroll->get_value();
  379. }
  380. void ScrollContainer::set_horizontal_scroll_mode(ScrollMode p_mode) {
  381. if (horizontal_scroll_mode == p_mode) {
  382. return;
  383. }
  384. horizontal_scroll_mode = p_mode;
  385. update_minimum_size();
  386. queue_sort();
  387. }
  388. ScrollContainer::ScrollMode ScrollContainer::get_horizontal_scroll_mode() const {
  389. return horizontal_scroll_mode;
  390. }
  391. void ScrollContainer::set_vertical_scroll_mode(ScrollMode p_mode) {
  392. if (vertical_scroll_mode == p_mode) {
  393. return;
  394. }
  395. vertical_scroll_mode = p_mode;
  396. update_minimum_size();
  397. queue_sort();
  398. }
  399. ScrollContainer::ScrollMode ScrollContainer::get_vertical_scroll_mode() const {
  400. return vertical_scroll_mode;
  401. }
  402. int ScrollContainer::get_deadzone() const {
  403. return deadzone;
  404. }
  405. void ScrollContainer::set_deadzone(int p_deadzone) {
  406. deadzone = p_deadzone;
  407. }
  408. bool ScrollContainer::is_following_focus() const {
  409. return follow_focus;
  410. }
  411. void ScrollContainer::set_follow_focus(bool p_follow) {
  412. follow_focus = p_follow;
  413. }
  414. TypedArray<String> ScrollContainer::get_configuration_warnings() const {
  415. TypedArray<String> warnings = Container::get_configuration_warnings();
  416. int found = 0;
  417. for (int i = 0; i < get_child_count(); i++) {
  418. Control *c = Object::cast_to<Control>(get_child(i));
  419. if (!c) {
  420. continue;
  421. }
  422. if (c->is_set_as_top_level()) {
  423. continue;
  424. }
  425. if (c == h_scroll || c == v_scroll) {
  426. continue;
  427. }
  428. found++;
  429. }
  430. if (found != 1) {
  431. warnings.push_back(RTR("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."));
  432. }
  433. return warnings;
  434. }
  435. HScrollBar *ScrollContainer::get_h_scroll_bar() {
  436. return h_scroll;
  437. }
  438. VScrollBar *ScrollContainer::get_v_scroll_bar() {
  439. return v_scroll;
  440. }
  441. void ScrollContainer::_bind_methods() {
  442. ClassDB::bind_method(D_METHOD("_update_scrollbar_position"), &ScrollContainer::_update_scrollbar_position);
  443. ClassDB::bind_method(D_METHOD("set_h_scroll", "value"), &ScrollContainer::set_h_scroll);
  444. ClassDB::bind_method(D_METHOD("get_h_scroll"), &ScrollContainer::get_h_scroll);
  445. ClassDB::bind_method(D_METHOD("set_v_scroll", "value"), &ScrollContainer::set_v_scroll);
  446. ClassDB::bind_method(D_METHOD("get_v_scroll"), &ScrollContainer::get_v_scroll);
  447. ClassDB::bind_method(D_METHOD("set_horizontal_scroll_mode", "enable"), &ScrollContainer::set_horizontal_scroll_mode);
  448. ClassDB::bind_method(D_METHOD("get_horizontal_scroll_mode"), &ScrollContainer::get_horizontal_scroll_mode);
  449. ClassDB::bind_method(D_METHOD("set_vertical_scroll_mode", "enable"), &ScrollContainer::set_vertical_scroll_mode);
  450. ClassDB::bind_method(D_METHOD("get_vertical_scroll_mode"), &ScrollContainer::get_vertical_scroll_mode);
  451. ClassDB::bind_method(D_METHOD("set_deadzone", "deadzone"), &ScrollContainer::set_deadzone);
  452. ClassDB::bind_method(D_METHOD("get_deadzone"), &ScrollContainer::get_deadzone);
  453. ClassDB::bind_method(D_METHOD("set_follow_focus", "enabled"), &ScrollContainer::set_follow_focus);
  454. ClassDB::bind_method(D_METHOD("is_following_focus"), &ScrollContainer::is_following_focus);
  455. ClassDB::bind_method(D_METHOD("get_h_scroll_bar"), &ScrollContainer::get_h_scroll_bar);
  456. ClassDB::bind_method(D_METHOD("get_v_scroll_bar"), &ScrollContainer::get_v_scroll_bar);
  457. ClassDB::bind_method(D_METHOD("ensure_control_visible", "control"), &ScrollContainer::ensure_control_visible);
  458. ADD_SIGNAL(MethodInfo("scroll_started"));
  459. ADD_SIGNAL(MethodInfo("scroll_ended"));
  460. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "follow_focus"), "set_follow_focus", "is_following_focus");
  461. ADD_GROUP("Scroll", "scroll_");
  462. ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_horizontal", PROPERTY_HINT_NONE, "suffix:px"), "set_h_scroll", "get_h_scroll");
  463. ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_vertical", PROPERTY_HINT_NONE, "suffix:px"), "set_v_scroll", "get_v_scroll");
  464. ADD_PROPERTY(PropertyInfo(Variant::INT, "horizontal_scroll_mode", PROPERTY_HINT_ENUM, "Disabled,Auto,Always Show,Never Show"), "set_horizontal_scroll_mode", "get_horizontal_scroll_mode");
  465. ADD_PROPERTY(PropertyInfo(Variant::INT, "vertical_scroll_mode", PROPERTY_HINT_ENUM, "Disabled,Auto,Always Show,Never Show"), "set_vertical_scroll_mode", "get_vertical_scroll_mode");
  466. ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_deadzone"), "set_deadzone", "get_deadzone");
  467. BIND_ENUM_CONSTANT(SCROLL_MODE_DISABLED);
  468. BIND_ENUM_CONSTANT(SCROLL_MODE_AUTO);
  469. BIND_ENUM_CONSTANT(SCROLL_MODE_SHOW_ALWAYS);
  470. BIND_ENUM_CONSTANT(SCROLL_MODE_SHOW_NEVER);
  471. GLOBAL_DEF("gui/common/default_scroll_deadzone", 0);
  472. };
  473. ScrollContainer::ScrollContainer() {
  474. h_scroll = memnew(HScrollBar);
  475. h_scroll->set_name("_h_scroll");
  476. add_child(h_scroll, false, INTERNAL_MODE_BACK);
  477. h_scroll->connect("value_changed", callable_mp(this, &ScrollContainer::_scroll_moved));
  478. v_scroll = memnew(VScrollBar);
  479. v_scroll->set_name("_v_scroll");
  480. add_child(v_scroll, false, INTERNAL_MODE_BACK);
  481. v_scroll->connect("value_changed", callable_mp(this, &ScrollContainer::_scroll_moved));
  482. deadzone = GLOBAL_GET("gui/common/default_scroll_deadzone");
  483. set_clip_contents(true);
  484. };