scroll_container.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. /**************************************************************************/
  2. /* scroll_container.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "scroll_container.h"
  31. #include "core/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 is_touchscreen_available = DisplayServer::get_singleton()->is_touchscreen_available();
  145. if (!is_touchscreen_available) {
  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 = true;
  160. drag_touching_deaccel = false;
  161. beyond_deadzone = false;
  162. time_since_motion = 0;
  163. set_physics_process_internal(true);
  164. time_since_motion = 0;
  165. } else {
  166. if (drag_touching) {
  167. if (drag_speed == Vector2()) {
  168. _cancel_drag();
  169. } else {
  170. drag_touching_deaccel = true;
  171. }
  172. }
  173. }
  174. return;
  175. }
  176. Ref<InputEventMouseMotion> mm = p_gui_input;
  177. if (mm.is_valid()) {
  178. if (drag_touching && !drag_touching_deaccel) {
  179. Vector2 motion = mm->get_relative();
  180. drag_accum -= motion;
  181. if (beyond_deadzone || (h_scroll_enabled && Math::abs(drag_accum.x) > deadzone) || (v_scroll_enabled && Math::abs(drag_accum.y) > deadzone)) {
  182. if (!beyond_deadzone) {
  183. propagate_notification(NOTIFICATION_SCROLL_BEGIN);
  184. emit_signal(SNAME("scroll_started"));
  185. beyond_deadzone = true;
  186. // Resetting drag_accum here ensures smooth scrolling after reaching deadzone.
  187. drag_accum = -motion;
  188. }
  189. Vector2 diff = drag_from + drag_accum;
  190. if (h_scroll_enabled) {
  191. h_scroll->set_value(diff.x);
  192. } else {
  193. drag_accum.x = 0;
  194. }
  195. if (v_scroll_enabled) {
  196. v_scroll->set_value(diff.y);
  197. } else {
  198. drag_accum.y = 0;
  199. }
  200. time_since_motion = 0;
  201. }
  202. }
  203. if (v_scroll->get_value() != prev_v_scroll || h_scroll->get_value() != prev_h_scroll) {
  204. accept_event(); // Accept event if scroll changed.
  205. }
  206. return;
  207. }
  208. Ref<InputEventPanGesture> pan_gesture = p_gui_input;
  209. if (pan_gesture.is_valid()) {
  210. if (h_scroll_enabled) {
  211. h_scroll->set_value(prev_h_scroll + h_scroll->get_page() * pan_gesture->get_delta().x / 8);
  212. }
  213. if (v_scroll_enabled) {
  214. v_scroll->set_value(prev_v_scroll + v_scroll->get_page() * pan_gesture->get_delta().y / 8);
  215. }
  216. if (v_scroll->get_value() != prev_v_scroll || h_scroll->get_value() != prev_h_scroll) {
  217. accept_event(); // Accept event if scroll changed.
  218. }
  219. return;
  220. }
  221. }
  222. void ScrollContainer::_update_scrollbar_position() {
  223. if (!_updating_scrollbars) {
  224. return;
  225. }
  226. Size2 hmin = h_scroll->get_combined_minimum_size();
  227. Size2 vmin = v_scroll->get_combined_minimum_size();
  228. h_scroll->set_anchor_and_offset(SIDE_LEFT, ANCHOR_BEGIN, 0);
  229. h_scroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0);
  230. h_scroll->set_anchor_and_offset(SIDE_TOP, ANCHOR_END, -hmin.height);
  231. h_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0);
  232. v_scroll->set_anchor_and_offset(SIDE_LEFT, ANCHOR_END, -vmin.width);
  233. v_scroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0);
  234. v_scroll->set_anchor_and_offset(SIDE_TOP, ANCHOR_BEGIN, 0);
  235. v_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0);
  236. _updating_scrollbars = false;
  237. }
  238. void ScrollContainer::_gui_focus_changed(Control *p_control) {
  239. if (follow_focus && is_ancestor_of(p_control)) {
  240. ensure_control_visible(p_control);
  241. }
  242. }
  243. void ScrollContainer::ensure_control_visible(Control *p_control) {
  244. ERR_FAIL_COND_MSG(!is_ancestor_of(p_control), "Must be an ancestor of the control.");
  245. Rect2 global_rect = get_global_rect();
  246. Rect2 other_rect = p_control->get_global_rect();
  247. float right_margin = v_scroll->is_visible() ? v_scroll->get_size().x : 0.0f;
  248. float bottom_margin = h_scroll->is_visible() ? h_scroll->get_size().y : 0.0f;
  249. 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)),
  250. MAX(MIN(other_rect.position.y, global_rect.position.y), other_rect.position.y + other_rect.size.y - global_rect.size.y + bottom_margin));
  251. set_h_scroll(get_h_scroll() + (diff.x - global_rect.position.x));
  252. set_v_scroll(get_v_scroll() + (diff.y - global_rect.position.y));
  253. }
  254. void ScrollContainer::_reposition_children() {
  255. update_scrollbars();
  256. Size2 size = get_size();
  257. Point2 ofs;
  258. size -= theme_cache.panel_style->get_minimum_size();
  259. ofs += theme_cache.panel_style->get_offset();
  260. bool rtl = is_layout_rtl();
  261. if (h_scroll->is_visible_in_tree() && h_scroll->get_parent() == this) { //scrolls may have been moved out for reasons
  262. size.y -= h_scroll->get_minimum_size().y;
  263. }
  264. if (v_scroll->is_visible_in_tree() && v_scroll->get_parent() == this) { //scrolls may have been moved out for reasons
  265. size.x -= v_scroll->get_minimum_size().x;
  266. }
  267. for (int i = 0; i < get_child_count(); i++) {
  268. Control *c = Object::cast_to<Control>(get_child(i));
  269. if (!c || !c->is_visible()) {
  270. continue;
  271. }
  272. if (c->is_set_as_top_level()) {
  273. continue;
  274. }
  275. if (c == h_scroll || c == v_scroll) {
  276. continue;
  277. }
  278. Size2 minsize = c->get_combined_minimum_size();
  279. Rect2 r = Rect2(-Size2(get_h_scroll(), get_v_scroll()), minsize);
  280. if (c->get_h_size_flags().has_flag(SIZE_EXPAND)) {
  281. r.size.width = MAX(size.width, minsize.width);
  282. }
  283. if (c->get_v_size_flags().has_flag(SIZE_EXPAND)) {
  284. r.size.height = MAX(size.height, minsize.height);
  285. }
  286. r.position += ofs;
  287. if (rtl && v_scroll->is_visible_in_tree() && v_scroll->get_parent() == this) {
  288. r.position.x += v_scroll->get_minimum_size().x;
  289. }
  290. r.position = r.position.floor();
  291. fit_child_in_rect(c, r);
  292. }
  293. queue_redraw();
  294. }
  295. void ScrollContainer::_notification(int p_what) {
  296. switch (p_what) {
  297. case NOTIFICATION_ENTER_TREE:
  298. case NOTIFICATION_THEME_CHANGED:
  299. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  300. case NOTIFICATION_TRANSLATION_CHANGED: {
  301. _updating_scrollbars = true;
  302. call_deferred(SNAME("_update_scrollbar_position"));
  303. } break;
  304. case NOTIFICATION_READY: {
  305. Viewport *viewport = get_viewport();
  306. ERR_FAIL_COND(!viewport);
  307. viewport->connect("gui_focus_changed", callable_mp(this, &ScrollContainer::_gui_focus_changed));
  308. _reposition_children();
  309. } break;
  310. case NOTIFICATION_SORT_CHILDREN: {
  311. _reposition_children();
  312. } break;
  313. case NOTIFICATION_DRAW: {
  314. draw_style_box(theme_cache.panel_style, Rect2(Vector2(), get_size()));
  315. } break;
  316. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  317. if (drag_touching) {
  318. if (drag_touching_deaccel) {
  319. Vector2 pos = Vector2(h_scroll->get_value(), v_scroll->get_value());
  320. pos += drag_speed * get_physics_process_delta_time();
  321. bool turnoff_h = false;
  322. bool turnoff_v = false;
  323. if (pos.x < 0) {
  324. pos.x = 0;
  325. turnoff_h = true;
  326. }
  327. if (pos.x > (h_scroll->get_max() - h_scroll->get_page())) {
  328. pos.x = h_scroll->get_max() - h_scroll->get_page();
  329. turnoff_h = true;
  330. }
  331. if (pos.y < 0) {
  332. pos.y = 0;
  333. turnoff_v = true;
  334. }
  335. if (pos.y > (v_scroll->get_max() - v_scroll->get_page())) {
  336. pos.y = v_scroll->get_max() - v_scroll->get_page();
  337. turnoff_v = true;
  338. }
  339. if (horizontal_scroll_mode != SCROLL_MODE_DISABLED) {
  340. h_scroll->set_value(pos.x);
  341. }
  342. if (vertical_scroll_mode != SCROLL_MODE_DISABLED) {
  343. v_scroll->set_value(pos.y);
  344. }
  345. float sgn_x = drag_speed.x < 0 ? -1 : 1;
  346. float val_x = Math::abs(drag_speed.x);
  347. val_x -= 1000 * get_physics_process_delta_time();
  348. if (val_x < 0) {
  349. turnoff_h = true;
  350. }
  351. float sgn_y = drag_speed.y < 0 ? -1 : 1;
  352. float val_y = Math::abs(drag_speed.y);
  353. val_y -= 1000 * get_physics_process_delta_time();
  354. if (val_y < 0) {
  355. turnoff_v = true;
  356. }
  357. drag_speed = Vector2(sgn_x * val_x, sgn_y * val_y);
  358. if (turnoff_h && turnoff_v) {
  359. _cancel_drag();
  360. }
  361. } else {
  362. if (time_since_motion == 0 || time_since_motion > 0.1) {
  363. Vector2 diff = drag_accum - last_drag_accum;
  364. last_drag_accum = drag_accum;
  365. drag_speed = diff / get_physics_process_delta_time();
  366. }
  367. time_since_motion += get_physics_process_delta_time();
  368. }
  369. }
  370. } break;
  371. }
  372. }
  373. void ScrollContainer::update_scrollbars() {
  374. Size2 size = get_size();
  375. size -= theme_cache.panel_style->get_minimum_size();
  376. Size2 hmin = h_scroll->get_combined_minimum_size();
  377. Size2 vmin = v_scroll->get_combined_minimum_size();
  378. h_scroll->set_visible(horizontal_scroll_mode == SCROLL_MODE_SHOW_ALWAYS || (horizontal_scroll_mode == SCROLL_MODE_AUTO && largest_child_min_size.width > size.width));
  379. v_scroll->set_visible(vertical_scroll_mode == SCROLL_MODE_SHOW_ALWAYS || (vertical_scroll_mode == SCROLL_MODE_AUTO && largest_child_min_size.height > size.height));
  380. h_scroll->set_max(largest_child_min_size.width);
  381. h_scroll->set_page((v_scroll->is_visible() && v_scroll->get_parent() == this) ? size.width - vmin.width : size.width);
  382. v_scroll->set_max(largest_child_min_size.height);
  383. v_scroll->set_page((h_scroll->is_visible() && h_scroll->get_parent() == this) ? size.height - hmin.height : size.height);
  384. // Avoid scrollbar overlapping.
  385. h_scroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, (v_scroll->is_visible() && v_scroll->get_parent() == this) ? -vmin.width : 0);
  386. v_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, (h_scroll->is_visible() && h_scroll->get_parent() == this) ? -hmin.height : 0);
  387. }
  388. void ScrollContainer::_scroll_moved(float) {
  389. queue_sort();
  390. };
  391. void ScrollContainer::set_h_scroll(int p_pos) {
  392. h_scroll->set_value(p_pos);
  393. _cancel_drag();
  394. }
  395. int ScrollContainer::get_h_scroll() const {
  396. return h_scroll->get_value();
  397. }
  398. void ScrollContainer::set_v_scroll(int p_pos) {
  399. v_scroll->set_value(p_pos);
  400. _cancel_drag();
  401. }
  402. int ScrollContainer::get_v_scroll() const {
  403. return v_scroll->get_value();
  404. }
  405. void ScrollContainer::set_horizontal_scroll_mode(ScrollMode p_mode) {
  406. if (horizontal_scroll_mode == p_mode) {
  407. return;
  408. }
  409. horizontal_scroll_mode = p_mode;
  410. update_minimum_size();
  411. queue_sort();
  412. }
  413. ScrollContainer::ScrollMode ScrollContainer::get_horizontal_scroll_mode() const {
  414. return horizontal_scroll_mode;
  415. }
  416. void ScrollContainer::set_vertical_scroll_mode(ScrollMode p_mode) {
  417. if (vertical_scroll_mode == p_mode) {
  418. return;
  419. }
  420. vertical_scroll_mode = p_mode;
  421. update_minimum_size();
  422. queue_sort();
  423. }
  424. ScrollContainer::ScrollMode ScrollContainer::get_vertical_scroll_mode() const {
  425. return vertical_scroll_mode;
  426. }
  427. int ScrollContainer::get_deadzone() const {
  428. return deadzone;
  429. }
  430. void ScrollContainer::set_deadzone(int p_deadzone) {
  431. deadzone = p_deadzone;
  432. }
  433. bool ScrollContainer::is_following_focus() const {
  434. return follow_focus;
  435. }
  436. void ScrollContainer::set_follow_focus(bool p_follow) {
  437. follow_focus = p_follow;
  438. }
  439. PackedStringArray ScrollContainer::get_configuration_warnings() const {
  440. PackedStringArray warnings = Container::get_configuration_warnings();
  441. int found = 0;
  442. for (int i = 0; i < get_child_count(); i++) {
  443. Control *c = Object::cast_to<Control>(get_child(i));
  444. if (!c) {
  445. continue;
  446. }
  447. if (c->is_set_as_top_level()) {
  448. continue;
  449. }
  450. if (c == h_scroll || c == v_scroll) {
  451. continue;
  452. }
  453. found++;
  454. }
  455. if (found != 1) {
  456. 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."));
  457. }
  458. return warnings;
  459. }
  460. HScrollBar *ScrollContainer::get_h_scroll_bar() {
  461. return h_scroll;
  462. }
  463. VScrollBar *ScrollContainer::get_v_scroll_bar() {
  464. return v_scroll;
  465. }
  466. void ScrollContainer::_bind_methods() {
  467. ClassDB::bind_method(D_METHOD("_update_scrollbar_position"), &ScrollContainer::_update_scrollbar_position);
  468. ClassDB::bind_method(D_METHOD("set_h_scroll", "value"), &ScrollContainer::set_h_scroll);
  469. ClassDB::bind_method(D_METHOD("get_h_scroll"), &ScrollContainer::get_h_scroll);
  470. ClassDB::bind_method(D_METHOD("set_v_scroll", "value"), &ScrollContainer::set_v_scroll);
  471. ClassDB::bind_method(D_METHOD("get_v_scroll"), &ScrollContainer::get_v_scroll);
  472. ClassDB::bind_method(D_METHOD("set_horizontal_scroll_mode", "enable"), &ScrollContainer::set_horizontal_scroll_mode);
  473. ClassDB::bind_method(D_METHOD("get_horizontal_scroll_mode"), &ScrollContainer::get_horizontal_scroll_mode);
  474. ClassDB::bind_method(D_METHOD("set_vertical_scroll_mode", "enable"), &ScrollContainer::set_vertical_scroll_mode);
  475. ClassDB::bind_method(D_METHOD("get_vertical_scroll_mode"), &ScrollContainer::get_vertical_scroll_mode);
  476. ClassDB::bind_method(D_METHOD("set_deadzone", "deadzone"), &ScrollContainer::set_deadzone);
  477. ClassDB::bind_method(D_METHOD("get_deadzone"), &ScrollContainer::get_deadzone);
  478. ClassDB::bind_method(D_METHOD("set_follow_focus", "enabled"), &ScrollContainer::set_follow_focus);
  479. ClassDB::bind_method(D_METHOD("is_following_focus"), &ScrollContainer::is_following_focus);
  480. ClassDB::bind_method(D_METHOD("get_h_scroll_bar"), &ScrollContainer::get_h_scroll_bar);
  481. ClassDB::bind_method(D_METHOD("get_v_scroll_bar"), &ScrollContainer::get_v_scroll_bar);
  482. ClassDB::bind_method(D_METHOD("ensure_control_visible", "control"), &ScrollContainer::ensure_control_visible);
  483. ADD_SIGNAL(MethodInfo("scroll_started"));
  484. ADD_SIGNAL(MethodInfo("scroll_ended"));
  485. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "follow_focus"), "set_follow_focus", "is_following_focus");
  486. ADD_GROUP("Scroll", "scroll_");
  487. ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_horizontal", PROPERTY_HINT_NONE, "suffix:px"), "set_h_scroll", "get_h_scroll");
  488. ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_vertical", PROPERTY_HINT_NONE, "suffix:px"), "set_v_scroll", "get_v_scroll");
  489. 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");
  490. 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");
  491. ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_deadzone"), "set_deadzone", "get_deadzone");
  492. BIND_ENUM_CONSTANT(SCROLL_MODE_DISABLED);
  493. BIND_ENUM_CONSTANT(SCROLL_MODE_AUTO);
  494. BIND_ENUM_CONSTANT(SCROLL_MODE_SHOW_ALWAYS);
  495. BIND_ENUM_CONSTANT(SCROLL_MODE_SHOW_NEVER);
  496. GLOBAL_DEF("gui/common/default_scroll_deadzone", 0);
  497. };
  498. ScrollContainer::ScrollContainer() {
  499. h_scroll = memnew(HScrollBar);
  500. h_scroll->set_name("_h_scroll");
  501. add_child(h_scroll, false, INTERNAL_MODE_BACK);
  502. h_scroll->connect("value_changed", callable_mp(this, &ScrollContainer::_scroll_moved));
  503. v_scroll = memnew(VScrollBar);
  504. v_scroll->set_name("_v_scroll");
  505. add_child(v_scroll, false, INTERNAL_MODE_BACK);
  506. v_scroll->connect("value_changed", callable_mp(this, &ScrollContainer::_scroll_moved));
  507. deadzone = GLOBAL_GET("gui/common/default_scroll_deadzone");
  508. set_clip_contents(true);
  509. };