scroll_container.cpp 22 KB

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