scroll_container.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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 "scene/main/window.h"
  33. #include "scene/theme/theme_db.h"
  34. Size2 ScrollContainer::get_minimum_size() const {
  35. // Calculated in this function, as it needs to traverse all child controls once to calculate;
  36. // and needs to be calculated before being used by update_scrollbars().
  37. largest_child_min_size = Size2();
  38. for (int i = 0; i < get_child_count(); i++) {
  39. Control *c = as_sortable_control(get_child(i), SortableVisbilityMode::VISIBLE);
  40. if (!c) {
  41. continue;
  42. }
  43. if (c == h_scroll || c == v_scroll) {
  44. continue;
  45. }
  46. Size2 child_min_size = c->get_combined_minimum_size();
  47. largest_child_min_size = largest_child_min_size.max(child_min_size);
  48. }
  49. Size2 min_size;
  50. const Size2 size = get_size();
  51. if (horizontal_scroll_mode == SCROLL_MODE_DISABLED) {
  52. min_size.x = largest_child_min_size.x;
  53. bool v_scroll_show = vertical_scroll_mode == SCROLL_MODE_SHOW_ALWAYS || vertical_scroll_mode == SCROLL_MODE_RESERVE || (vertical_scroll_mode == SCROLL_MODE_AUTO && largest_child_min_size.y > size.y);
  54. if (v_scroll_show && v_scroll->get_parent() == this) {
  55. min_size.x += v_scroll->get_minimum_size().x;
  56. }
  57. }
  58. if (vertical_scroll_mode == SCROLL_MODE_DISABLED) {
  59. min_size.y = largest_child_min_size.y;
  60. bool h_scroll_show = horizontal_scroll_mode == SCROLL_MODE_SHOW_ALWAYS || horizontal_scroll_mode == SCROLL_MODE_RESERVE || (horizontal_scroll_mode == SCROLL_MODE_AUTO && largest_child_min_size.x > size.x);
  61. if (h_scroll_show && h_scroll->get_parent() == this) {
  62. min_size.y += h_scroll->get_minimum_size().y;
  63. }
  64. }
  65. min_size += theme_cache.panel_style->get_minimum_size();
  66. return min_size;
  67. }
  68. void ScrollContainer::_cancel_drag() {
  69. set_physics_process_internal(false);
  70. drag_touching_deaccel = false;
  71. drag_touching = false;
  72. drag_speed = Vector2();
  73. drag_accum = Vector2();
  74. last_drag_accum = Vector2();
  75. drag_from = Vector2();
  76. if (beyond_deadzone) {
  77. emit_signal(SNAME("scroll_ended"));
  78. propagate_notification(NOTIFICATION_SCROLL_END);
  79. beyond_deadzone = false;
  80. }
  81. }
  82. bool ScrollContainer::_is_h_scroll_visible() const {
  83. // Scrolls may have been moved out for reasons.
  84. return h_scroll->is_visible() && h_scroll->get_parent() == this;
  85. }
  86. bool ScrollContainer::_is_v_scroll_visible() const {
  87. return v_scroll->is_visible() && v_scroll->get_parent() == this;
  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->scroll(-h_scroll->get_page() / 8 * mb->get_factor());
  104. scroll_value_modified = true;
  105. } else if (v_scroll_enabled) {
  106. v_scroll->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->scroll(h_scroll->get_page() / 8 * mb->get_factor());
  113. scroll_value_modified = true;
  114. } else if (v_scroll_enabled) {
  115. v_scroll->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->scroll(-v_scroll->get_page() / 8 * mb->get_factor());
  124. scroll_value_modified = true;
  125. } else if (h_scroll_enabled) {
  126. h_scroll->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->scroll(v_scroll->get_page() / 8 * mb->get_factor());
  133. scroll_value_modified = true;
  134. } else if (h_scroll_enabled) {
  135. h_scroll->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->scroll_to(diff.x);
  192. } else {
  193. drag_accum.x = 0;
  194. }
  195. if (v_scroll_enabled) {
  196. v_scroll->scroll_to(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->scroll(h_scroll->get_page() * pan_gesture->get_delta().x / 8);
  212. }
  213. if (v_scroll_enabled) {
  214. v_scroll->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->is_visible() ? h_scroll->get_combined_minimum_size() : Size2();
  227. Size2 vmin = v_scroll->is_visible() ? v_scroll->get_combined_minimum_size() : Size2();
  228. int lmar = is_layout_rtl() ? theme_cache.panel_style->get_margin(SIDE_RIGHT) : theme_cache.panel_style->get_margin(SIDE_LEFT);
  229. int rmar = is_layout_rtl() ? theme_cache.panel_style->get_margin(SIDE_LEFT) : theme_cache.panel_style->get_margin(SIDE_RIGHT);
  230. h_scroll->set_anchor_and_offset(SIDE_LEFT, ANCHOR_BEGIN, lmar);
  231. h_scroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, -rmar - vmin.width);
  232. h_scroll->set_anchor_and_offset(SIDE_TOP, ANCHOR_END, -hmin.height - theme_cache.panel_style->get_margin(SIDE_BOTTOM));
  233. h_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, -theme_cache.panel_style->get_margin(SIDE_BOTTOM));
  234. v_scroll->set_anchor_and_offset(SIDE_LEFT, ANCHOR_END, -vmin.width - rmar);
  235. v_scroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, -rmar);
  236. v_scroll->set_anchor_and_offset(SIDE_TOP, ANCHOR_BEGIN, theme_cache.panel_style->get_margin(SIDE_TOP));
  237. v_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, -hmin.height - theme_cache.panel_style->get_margin(SIDE_BOTTOM));
  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 side_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 - (is_layout_rtl() ? side_margin : 0.0f), global_rect.position.x), other_rect.position.x + other_rect.size.x - global_rect.size.x + (!is_layout_rtl() ? side_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 (_is_h_scroll_visible() || horizontal_scroll_mode == SCROLL_MODE_RESERVE) {
  264. size.y -= h_scroll->get_minimum_size().y;
  265. }
  266. if (_is_v_scroll_visible() || vertical_scroll_mode == SCROLL_MODE_RESERVE) {
  267. size.x -= v_scroll->get_minimum_size().x;
  268. }
  269. for (int i = 0; i < get_child_count(); i++) {
  270. Control *c = as_sortable_control(get_child(i));
  271. if (!c) {
  272. continue;
  273. }
  274. if (c == h_scroll || c == v_scroll) {
  275. continue;
  276. }
  277. Size2 minsize = c->get_combined_minimum_size();
  278. Rect2 r = Rect2(-Size2(get_h_scroll(), get_v_scroll()), minsize);
  279. if (c->get_h_size_flags().has_flag(SIZE_EXPAND)) {
  280. r.size.width = MAX(size.width, minsize.width);
  281. }
  282. if (c->get_v_size_flags().has_flag(SIZE_EXPAND)) {
  283. r.size.height = MAX(size.height, minsize.height);
  284. }
  285. r.position += ofs;
  286. if (rtl && _is_v_scroll_visible()) {
  287. r.position.x += v_scroll->get_minimum_size().x;
  288. }
  289. r.position = r.position.floor();
  290. fit_child_in_rect(c, r);
  291. }
  292. queue_redraw();
  293. }
  294. void ScrollContainer::_notification(int p_what) {
  295. switch (p_what) {
  296. case NOTIFICATION_ENTER_TREE:
  297. case NOTIFICATION_THEME_CHANGED:
  298. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  299. case NOTIFICATION_TRANSLATION_CHANGED: {
  300. _updating_scrollbars = true;
  301. callable_mp(this, is_ready() ? &ScrollContainer::_reposition_children : &ScrollContainer::_update_scrollbar_position).call_deferred();
  302. } break;
  303. case NOTIFICATION_READY: {
  304. Viewport *viewport = get_viewport();
  305. ERR_FAIL_NULL(viewport);
  306. viewport->connect("gui_focus_changed", callable_mp(this, &ScrollContainer::_gui_focus_changed));
  307. _reposition_children();
  308. } break;
  309. case NOTIFICATION_SORT_CHILDREN: {
  310. _reposition_children();
  311. } break;
  312. case NOTIFICATION_DRAW: {
  313. draw_style_box(theme_cache.panel_style, Rect2(Vector2(), get_size()));
  314. } break;
  315. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  316. if (drag_touching) {
  317. if (drag_touching_deaccel) {
  318. Vector2 pos = Vector2(h_scroll->get_value(), v_scroll->get_value());
  319. pos += drag_speed * get_physics_process_delta_time();
  320. bool turnoff_h = false;
  321. bool turnoff_v = false;
  322. if (pos.x < 0) {
  323. pos.x = 0;
  324. turnoff_h = true;
  325. }
  326. if (pos.x > (h_scroll->get_max() - h_scroll->get_page())) {
  327. pos.x = h_scroll->get_max() - h_scroll->get_page();
  328. turnoff_h = true;
  329. }
  330. if (pos.y < 0) {
  331. pos.y = 0;
  332. turnoff_v = true;
  333. }
  334. if (pos.y > (v_scroll->get_max() - v_scroll->get_page())) {
  335. pos.y = v_scroll->get_max() - v_scroll->get_page();
  336. turnoff_v = true;
  337. }
  338. if (horizontal_scroll_mode != SCROLL_MODE_DISABLED) {
  339. h_scroll->scroll_to(pos.x);
  340. }
  341. if (vertical_scroll_mode != SCROLL_MODE_DISABLED) {
  342. v_scroll->scroll_to(pos.y);
  343. }
  344. float sgn_x = drag_speed.x < 0 ? -1 : 1;
  345. float val_x = Math::abs(drag_speed.x);
  346. val_x -= 1000 * get_physics_process_delta_time();
  347. if (val_x < 0) {
  348. turnoff_h = true;
  349. }
  350. float sgn_y = drag_speed.y < 0 ? -1 : 1;
  351. float val_y = Math::abs(drag_speed.y);
  352. val_y -= 1000 * get_physics_process_delta_time();
  353. if (val_y < 0) {
  354. turnoff_v = true;
  355. }
  356. drag_speed = Vector2(sgn_x * val_x, sgn_y * val_y);
  357. if (turnoff_h && turnoff_v) {
  358. _cancel_drag();
  359. }
  360. } else {
  361. if (time_since_motion == 0 || time_since_motion > 0.1) {
  362. Vector2 diff = drag_accum - last_drag_accum;
  363. last_drag_accum = drag_accum;
  364. drag_speed = diff / get_physics_process_delta_time();
  365. }
  366. time_since_motion += get_physics_process_delta_time();
  367. }
  368. }
  369. } break;
  370. }
  371. }
  372. void ScrollContainer::update_scrollbars() {
  373. Size2 size = get_size();
  374. size -= theme_cache.panel_style->get_minimum_size();
  375. Size2 hmin = h_scroll->get_combined_minimum_size();
  376. Size2 vmin = v_scroll->get_combined_minimum_size();
  377. h_scroll->set_visible(horizontal_scroll_mode == SCROLL_MODE_SHOW_ALWAYS || ((horizontal_scroll_mode == SCROLL_MODE_AUTO || horizontal_scroll_mode == SCROLL_MODE_RESERVE) && largest_child_min_size.width > size.width));
  378. v_scroll->set_visible(vertical_scroll_mode == SCROLL_MODE_SHOW_ALWAYS || ((vertical_scroll_mode == SCROLL_MODE_AUTO || vertical_scroll_mode == SCROLL_MODE_RESERVE) && largest_child_min_size.height > size.height));
  379. h_scroll->set_max(largest_child_min_size.width);
  380. h_scroll->set_page(_is_v_scroll_visible() ? size.width - vmin.width : size.width);
  381. v_scroll->set_max(largest_child_min_size.height);
  382. v_scroll->set_page(_is_h_scroll_visible() ? size.height - hmin.height : size.height);
  383. // Avoid scrollbar overlapping.
  384. _updating_scrollbars = true;
  385. callable_mp(this, &ScrollContainer::_update_scrollbar_position).call_deferred();
  386. }
  387. void ScrollContainer::_scroll_moved(float) {
  388. queue_sort();
  389. }
  390. void ScrollContainer::set_h_scroll(int p_pos) {
  391. h_scroll->set_value(p_pos);
  392. _cancel_drag();
  393. }
  394. int ScrollContainer::get_h_scroll() const {
  395. return h_scroll->get_value();
  396. }
  397. void ScrollContainer::set_v_scroll(int p_pos) {
  398. v_scroll->set_value(p_pos);
  399. _cancel_drag();
  400. }
  401. int ScrollContainer::get_v_scroll() const {
  402. return v_scroll->get_value();
  403. }
  404. void ScrollContainer::set_horizontal_custom_step(float p_custom_step) {
  405. h_scroll->set_custom_step(p_custom_step);
  406. }
  407. float ScrollContainer::get_horizontal_custom_step() const {
  408. return h_scroll->get_custom_step();
  409. }
  410. void ScrollContainer::set_vertical_custom_step(float p_custom_step) {
  411. v_scroll->set_custom_step(p_custom_step);
  412. }
  413. float ScrollContainer::get_vertical_custom_step() const {
  414. return v_scroll->get_custom_step();
  415. }
  416. void ScrollContainer::set_horizontal_scroll_mode(ScrollMode p_mode) {
  417. if (horizontal_scroll_mode == p_mode) {
  418. return;
  419. }
  420. horizontal_scroll_mode = p_mode;
  421. update_minimum_size();
  422. queue_sort();
  423. }
  424. ScrollContainer::ScrollMode ScrollContainer::get_horizontal_scroll_mode() const {
  425. return horizontal_scroll_mode;
  426. }
  427. void ScrollContainer::set_vertical_scroll_mode(ScrollMode p_mode) {
  428. if (vertical_scroll_mode == p_mode) {
  429. return;
  430. }
  431. vertical_scroll_mode = p_mode;
  432. update_minimum_size();
  433. queue_sort();
  434. }
  435. ScrollContainer::ScrollMode ScrollContainer::get_vertical_scroll_mode() const {
  436. return vertical_scroll_mode;
  437. }
  438. int ScrollContainer::get_deadzone() const {
  439. return deadzone;
  440. }
  441. void ScrollContainer::set_deadzone(int p_deadzone) {
  442. deadzone = p_deadzone;
  443. }
  444. bool ScrollContainer::is_following_focus() const {
  445. return follow_focus;
  446. }
  447. void ScrollContainer::set_follow_focus(bool p_follow) {
  448. follow_focus = p_follow;
  449. }
  450. PackedStringArray ScrollContainer::get_configuration_warnings() const {
  451. PackedStringArray warnings = Container::get_configuration_warnings();
  452. int found = 0;
  453. for (int i = 0; i < get_child_count(); i++) {
  454. Control *c = as_sortable_control(get_child(i), SortableVisbilityMode::VISIBLE);
  455. if (!c) {
  456. continue;
  457. }
  458. if (c == h_scroll || c == v_scroll) {
  459. continue;
  460. }
  461. found++;
  462. }
  463. if (found != 1) {
  464. 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."));
  465. }
  466. return warnings;
  467. }
  468. HScrollBar *ScrollContainer::get_h_scroll_bar() {
  469. return h_scroll;
  470. }
  471. VScrollBar *ScrollContainer::get_v_scroll_bar() {
  472. return v_scroll;
  473. }
  474. void ScrollContainer::_bind_methods() {
  475. ClassDB::bind_method(D_METHOD("set_h_scroll", "value"), &ScrollContainer::set_h_scroll);
  476. ClassDB::bind_method(D_METHOD("get_h_scroll"), &ScrollContainer::get_h_scroll);
  477. ClassDB::bind_method(D_METHOD("set_v_scroll", "value"), &ScrollContainer::set_v_scroll);
  478. ClassDB::bind_method(D_METHOD("get_v_scroll"), &ScrollContainer::get_v_scroll);
  479. ClassDB::bind_method(D_METHOD("set_horizontal_custom_step", "value"), &ScrollContainer::set_horizontal_custom_step);
  480. ClassDB::bind_method(D_METHOD("get_horizontal_custom_step"), &ScrollContainer::get_horizontal_custom_step);
  481. ClassDB::bind_method(D_METHOD("set_vertical_custom_step", "value"), &ScrollContainer::set_vertical_custom_step);
  482. ClassDB::bind_method(D_METHOD("get_vertical_custom_step"), &ScrollContainer::get_vertical_custom_step);
  483. ClassDB::bind_method(D_METHOD("set_horizontal_scroll_mode", "enable"), &ScrollContainer::set_horizontal_scroll_mode);
  484. ClassDB::bind_method(D_METHOD("get_horizontal_scroll_mode"), &ScrollContainer::get_horizontal_scroll_mode);
  485. ClassDB::bind_method(D_METHOD("set_vertical_scroll_mode", "enable"), &ScrollContainer::set_vertical_scroll_mode);
  486. ClassDB::bind_method(D_METHOD("get_vertical_scroll_mode"), &ScrollContainer::get_vertical_scroll_mode);
  487. ClassDB::bind_method(D_METHOD("set_deadzone", "deadzone"), &ScrollContainer::set_deadzone);
  488. ClassDB::bind_method(D_METHOD("get_deadzone"), &ScrollContainer::get_deadzone);
  489. ClassDB::bind_method(D_METHOD("set_follow_focus", "enabled"), &ScrollContainer::set_follow_focus);
  490. ClassDB::bind_method(D_METHOD("is_following_focus"), &ScrollContainer::is_following_focus);
  491. ClassDB::bind_method(D_METHOD("get_h_scroll_bar"), &ScrollContainer::get_h_scroll_bar);
  492. ClassDB::bind_method(D_METHOD("get_v_scroll_bar"), &ScrollContainer::get_v_scroll_bar);
  493. ClassDB::bind_method(D_METHOD("ensure_control_visible", "control"), &ScrollContainer::ensure_control_visible);
  494. ADD_SIGNAL(MethodInfo("scroll_started"));
  495. ADD_SIGNAL(MethodInfo("scroll_ended"));
  496. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "follow_focus"), "set_follow_focus", "is_following_focus");
  497. ADD_GROUP("Scroll", "scroll_");
  498. ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_horizontal", PROPERTY_HINT_NONE, "suffix:px"), "set_h_scroll", "get_h_scroll");
  499. ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_vertical", PROPERTY_HINT_NONE, "suffix:px"), "set_v_scroll", "get_v_scroll");
  500. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "scroll_horizontal_custom_step", PROPERTY_HINT_RANGE, "-1,4096,suffix:px"), "set_horizontal_custom_step", "get_horizontal_custom_step");
  501. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "scroll_vertical_custom_step", PROPERTY_HINT_RANGE, "-1,4096,suffix:px"), "set_vertical_custom_step", "get_vertical_custom_step");
  502. ADD_PROPERTY(PropertyInfo(Variant::INT, "horizontal_scroll_mode", PROPERTY_HINT_ENUM, "Disabled,Auto,Always Show,Never Show,Reserve"), "set_horizontal_scroll_mode", "get_horizontal_scroll_mode");
  503. ADD_PROPERTY(PropertyInfo(Variant::INT, "vertical_scroll_mode", PROPERTY_HINT_ENUM, "Disabled,Auto,Always Show,Never Show,Reserve"), "set_vertical_scroll_mode", "get_vertical_scroll_mode");
  504. ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_deadzone"), "set_deadzone", "get_deadzone");
  505. BIND_ENUM_CONSTANT(SCROLL_MODE_DISABLED);
  506. BIND_ENUM_CONSTANT(SCROLL_MODE_AUTO);
  507. BIND_ENUM_CONSTANT(SCROLL_MODE_SHOW_ALWAYS);
  508. BIND_ENUM_CONSTANT(SCROLL_MODE_SHOW_NEVER);
  509. BIND_ENUM_CONSTANT(SCROLL_MODE_RESERVE);
  510. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ScrollContainer, panel_style, "panel");
  511. GLOBAL_DEF("gui/common/default_scroll_deadzone", 0);
  512. }
  513. ScrollContainer::ScrollContainer() {
  514. h_scroll = memnew(HScrollBar);
  515. h_scroll->set_name("_h_scroll");
  516. add_child(h_scroll, false, INTERNAL_MODE_BACK);
  517. h_scroll->connect(SceneStringName(value_changed), callable_mp(this, &ScrollContainer::_scroll_moved));
  518. v_scroll = memnew(VScrollBar);
  519. v_scroll->set_name("_v_scroll");
  520. add_child(v_scroll, false, INTERNAL_MODE_BACK);
  521. v_scroll->connect(SceneStringName(value_changed), callable_mp(this, &ScrollContainer::_scroll_moved));
  522. deadzone = GLOBAL_GET("gui/common/default_scroll_deadzone");
  523. set_clip_contents(true);
  524. }