scroll_container.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  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/gui/panel_container.h"
  33. #include "scene/main/window.h"
  34. #include "scene/theme/theme_db.h"
  35. Size2 ScrollContainer::get_minimum_size() const {
  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 = as_sortable_control(get_child(i), SortableVisibilityMode::VISIBLE);
  41. if (!c) {
  42. continue;
  43. }
  44. if (c == h_scroll || c == v_scroll || c == focus_panel) {
  45. continue;
  46. }
  47. Size2 child_min_size = c->get_combined_minimum_size();
  48. largest_child_min_size = largest_child_min_size.max(child_min_size);
  49. }
  50. Size2 min_size;
  51. const Size2 size = get_size();
  52. if (horizontal_scroll_mode == SCROLL_MODE_DISABLED) {
  53. min_size.x = largest_child_min_size.x;
  54. 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);
  55. if (v_scroll_show && v_scroll->get_parent() == this) {
  56. min_size.x += v_scroll->get_minimum_size().x;
  57. }
  58. }
  59. if (vertical_scroll_mode == SCROLL_MODE_DISABLED) {
  60. min_size.y = largest_child_min_size.y;
  61. 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);
  62. if (h_scroll_show && h_scroll->get_parent() == this) {
  63. min_size.y += h_scroll->get_minimum_size().y;
  64. }
  65. }
  66. Size2 panel_size = theme_cache.panel_style->get_minimum_size();
  67. min_size += panel_size;
  68. if (draw_focus_border) {
  69. Size2 focus_size = theme_cache.focus_style->get_minimum_size();
  70. // Only update the minimum size if the focus style's minimum size doesn't fit into the panel style's minimum size.
  71. if (focus_size > panel_size) {
  72. min_size += focus_size - panel_size;
  73. }
  74. }
  75. return min_size;
  76. }
  77. void ScrollContainer::_cancel_drag() {
  78. set_process_internal(false);
  79. drag_touching_deaccel = false;
  80. drag_touching = false;
  81. drag_speed = Vector2();
  82. drag_accum = Vector2();
  83. last_drag_accum = Vector2();
  84. drag_from = Vector2();
  85. if (beyond_deadzone) {
  86. emit_signal(SNAME("scroll_ended"));
  87. propagate_notification(NOTIFICATION_SCROLL_END);
  88. beyond_deadzone = false;
  89. }
  90. }
  91. bool ScrollContainer::_is_h_scroll_visible() const {
  92. // Scrolls may have been moved out for reasons.
  93. return h_scroll->is_visible() && h_scroll->get_parent() == this;
  94. }
  95. bool ScrollContainer::_is_v_scroll_visible() const {
  96. return v_scroll->is_visible() && v_scroll->get_parent() == this;
  97. }
  98. void ScrollContainer::gui_input(const Ref<InputEvent> &p_gui_input) {
  99. ERR_FAIL_COND(p_gui_input.is_null());
  100. double prev_v_scroll = v_scroll->get_value();
  101. double prev_h_scroll = h_scroll->get_value();
  102. bool h_scroll_enabled = horizontal_scroll_mode != SCROLL_MODE_DISABLED;
  103. bool v_scroll_enabled = vertical_scroll_mode != SCROLL_MODE_DISABLED;
  104. Ref<InputEventMouseButton> mb = p_gui_input;
  105. if (mb.is_valid()) {
  106. if (mb->is_pressed()) {
  107. bool scroll_value_modified = false;
  108. bool v_scroll_hidden = !v_scroll->is_visible() && vertical_scroll_mode != SCROLL_MODE_SHOW_NEVER;
  109. if (mb->get_button_index() == MouseButton::WHEEL_UP) {
  110. // By default, the vertical orientation takes precedence. This is an exception.
  111. if ((h_scroll_enabled && mb->is_shift_pressed()) || v_scroll_hidden) {
  112. h_scroll->scroll(-h_scroll->get_page() / ScrollBar::PAGE_DIVISOR * mb->get_factor());
  113. scroll_value_modified = true;
  114. } else if (v_scroll_enabled) {
  115. v_scroll->scroll(-v_scroll->get_page() / ScrollBar::PAGE_DIVISOR * mb->get_factor());
  116. scroll_value_modified = true;
  117. }
  118. }
  119. if (mb->get_button_index() == MouseButton::WHEEL_DOWN) {
  120. if ((h_scroll_enabled && mb->is_shift_pressed()) || v_scroll_hidden) {
  121. h_scroll->scroll(h_scroll->get_page() / ScrollBar::PAGE_DIVISOR * mb->get_factor());
  122. scroll_value_modified = true;
  123. } else if (v_scroll_enabled) {
  124. v_scroll->scroll(v_scroll->get_page() / ScrollBar::PAGE_DIVISOR * mb->get_factor());
  125. scroll_value_modified = true;
  126. }
  127. }
  128. bool h_scroll_hidden = !h_scroll->is_visible() && horizontal_scroll_mode != SCROLL_MODE_SHOW_NEVER;
  129. if (mb->get_button_index() == MouseButton::WHEEL_LEFT) {
  130. // By default, the horizontal orientation takes precedence. This is an exception.
  131. if ((v_scroll_enabled && mb->is_shift_pressed()) || h_scroll_hidden) {
  132. v_scroll->scroll(-v_scroll->get_page() / ScrollBar::PAGE_DIVISOR * mb->get_factor());
  133. scroll_value_modified = true;
  134. } else if (h_scroll_enabled) {
  135. h_scroll->scroll(-h_scroll->get_page() / ScrollBar::PAGE_DIVISOR * mb->get_factor());
  136. scroll_value_modified = true;
  137. }
  138. }
  139. if (mb->get_button_index() == MouseButton::WHEEL_RIGHT) {
  140. if ((v_scroll_enabled && mb->is_shift_pressed()) || h_scroll_hidden) {
  141. v_scroll->scroll(v_scroll->get_page() / ScrollBar::PAGE_DIVISOR * mb->get_factor());
  142. scroll_value_modified = true;
  143. } else if (h_scroll_enabled) {
  144. h_scroll->scroll(h_scroll->get_page() / ScrollBar::PAGE_DIVISOR * mb->get_factor());
  145. scroll_value_modified = true;
  146. }
  147. }
  148. if (scroll_value_modified && (v_scroll->get_value() != prev_v_scroll || h_scroll->get_value() != prev_h_scroll)) {
  149. accept_event(); // Accept event if scroll changed.
  150. return;
  151. }
  152. }
  153. bool is_touchscreen_available = DisplayServer::get_singleton()->is_touchscreen_available();
  154. if (!is_touchscreen_available) {
  155. return;
  156. }
  157. if (mb->get_button_index() != MouseButton::LEFT) {
  158. return;
  159. }
  160. if (mb->is_pressed()) {
  161. if (drag_touching) {
  162. _cancel_drag();
  163. }
  164. drag_speed = Vector2();
  165. drag_accum = Vector2();
  166. last_drag_accum = Vector2();
  167. drag_from = Vector2(prev_h_scroll, prev_v_scroll);
  168. drag_touching = true;
  169. drag_touching_deaccel = false;
  170. beyond_deadzone = false;
  171. time_since_motion = 0;
  172. set_process_internal(true);
  173. time_since_motion = 0;
  174. } else {
  175. if (drag_touching) {
  176. if (drag_speed == Vector2()) {
  177. _cancel_drag();
  178. } else {
  179. drag_touching_deaccel = true;
  180. }
  181. }
  182. }
  183. return;
  184. }
  185. Ref<InputEventMouseMotion> mm = p_gui_input;
  186. if (mm.is_valid()) {
  187. if (drag_touching && !drag_touching_deaccel) {
  188. Vector2 motion = mm->get_relative();
  189. drag_accum -= motion;
  190. if (beyond_deadzone || (h_scroll_enabled && Math::abs(drag_accum.x) > deadzone) || (v_scroll_enabled && Math::abs(drag_accum.y) > deadzone)) {
  191. if (!beyond_deadzone) {
  192. propagate_notification(NOTIFICATION_SCROLL_BEGIN);
  193. emit_signal(SNAME("scroll_started"));
  194. beyond_deadzone = true;
  195. // Resetting drag_accum here ensures smooth scrolling after reaching deadzone.
  196. drag_accum = -motion;
  197. }
  198. Vector2 diff = drag_from + drag_accum;
  199. if (h_scroll_enabled) {
  200. h_scroll->scroll_to(diff.x);
  201. } else {
  202. drag_accum.x = 0;
  203. }
  204. if (v_scroll_enabled) {
  205. v_scroll->scroll_to(diff.y);
  206. } else {
  207. drag_accum.y = 0;
  208. }
  209. time_since_motion = 0;
  210. }
  211. }
  212. if (v_scroll->get_value() != prev_v_scroll || h_scroll->get_value() != prev_h_scroll) {
  213. accept_event(); // Accept event if scroll changed.
  214. }
  215. return;
  216. }
  217. Ref<InputEventPanGesture> pan_gesture = p_gui_input;
  218. if (pan_gesture.is_valid()) {
  219. if (h_scroll_enabled) {
  220. h_scroll->scroll(h_scroll->get_page() * pan_gesture->get_delta().x / ScrollBar::PAGE_DIVISOR);
  221. }
  222. if (v_scroll_enabled) {
  223. v_scroll->scroll(v_scroll->get_page() * pan_gesture->get_delta().y / ScrollBar::PAGE_DIVISOR);
  224. }
  225. if (v_scroll->get_value() != prev_v_scroll || h_scroll->get_value() != prev_h_scroll) {
  226. accept_event(); // Accept event if scroll changed.
  227. }
  228. return;
  229. }
  230. }
  231. void ScrollContainer::_update_scrollbar_position() {
  232. if (!_updating_scrollbars) {
  233. return;
  234. }
  235. float right_margin = theme_cache.panel_style->get_margin(SIDE_RIGHT);
  236. float left_margin = theme_cache.panel_style->get_margin(SIDE_LEFT);
  237. float top_margin = theme_cache.panel_style->get_margin(SIDE_TOP);
  238. float bottom_margin = theme_cache.panel_style->get_margin(SIDE_BOTTOM);
  239. if (draw_focus_border) {
  240. // Only update margins if the focus style's margins don't fit into the panel style's margins.
  241. float focus_margin = theme_cache.focus_style->get_margin(SIDE_RIGHT);
  242. if (focus_margin > right_margin) {
  243. right_margin += focus_margin;
  244. }
  245. focus_margin = theme_cache.focus_style->get_margin(SIDE_LEFT);
  246. if (focus_margin > left_margin) {
  247. left_margin += focus_margin;
  248. }
  249. focus_margin = theme_cache.focus_style->get_margin(SIDE_TOP);
  250. if (focus_margin > top_margin) {
  251. top_margin += focus_margin;
  252. }
  253. focus_margin = theme_cache.focus_style->get_margin(SIDE_BOTTOM);
  254. if (focus_margin > bottom_margin) {
  255. bottom_margin += focus_margin;
  256. }
  257. }
  258. Size2 hmin = h_scroll->is_visible() ? h_scroll->get_combined_minimum_size() : Size2();
  259. Size2 vmin = v_scroll->is_visible() ? v_scroll->get_combined_minimum_size() : Size2();
  260. int lmar = is_layout_rtl() ? right_margin : left_margin;
  261. int rmar = is_layout_rtl() ? left_margin : right_margin;
  262. h_scroll->set_anchor_and_offset(SIDE_LEFT, ANCHOR_BEGIN, lmar);
  263. h_scroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, -rmar - vmin.width);
  264. h_scroll->set_anchor_and_offset(SIDE_TOP, ANCHOR_END, -hmin.height - bottom_margin);
  265. h_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, -bottom_margin);
  266. v_scroll->set_anchor_and_offset(SIDE_LEFT, ANCHOR_END, -vmin.width - rmar);
  267. v_scroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, -rmar);
  268. v_scroll->set_anchor_and_offset(SIDE_TOP, ANCHOR_BEGIN, top_margin);
  269. v_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, -hmin.height - bottom_margin);
  270. _updating_scrollbars = false;
  271. }
  272. void ScrollContainer::_gui_focus_changed(Control *p_control) {
  273. if (follow_focus && is_ancestor_of(p_control)) {
  274. ensure_control_visible(p_control);
  275. }
  276. if (draw_focus_border) {
  277. const bool _should_draw_focus_border = has_focus() || child_has_focus();
  278. if (focus_border_is_drawn != _should_draw_focus_border) {
  279. queue_redraw();
  280. }
  281. }
  282. }
  283. void ScrollContainer::ensure_control_visible(Control *p_control) {
  284. ERR_FAIL_COND_MSG(!is_ancestor_of(p_control), "Must be an ancestor of the control.");
  285. // Just eliminate the rotation of this ScrollContainer.
  286. Transform2D other_in_this = get_global_transform().affine_inverse() * p_control->get_global_transform();
  287. Size2 size = get_size();
  288. Rect2 other_rect = other_in_this.xform(Rect2(Point2(), p_control->get_size()));
  289. float side_margin = v_scroll->is_visible() ? v_scroll->get_size().x : 0.0f;
  290. float bottom_margin = h_scroll->is_visible() ? h_scroll->get_size().y : 0.0f;
  291. Vector2 diff = Vector2(MAX(MIN(other_rect.position.x - (is_layout_rtl() ? side_margin : 0.0f), 0.0f), other_rect.position.x + other_rect.size.x - size.x + (!is_layout_rtl() ? side_margin : 0.0f)),
  292. MAX(MIN(other_rect.position.y, 0.0f), other_rect.position.y + other_rect.size.y - size.y + bottom_margin));
  293. set_h_scroll(get_h_scroll() + diff.x);
  294. set_v_scroll(get_v_scroll() + diff.y);
  295. }
  296. void ScrollContainer::_reposition_children() {
  297. update_scrollbars();
  298. Size2 size = get_size();
  299. Point2 ofs;
  300. Size2 panel_size = theme_cache.panel_style->get_minimum_size();
  301. Point2 panel_offset = theme_cache.panel_style->get_offset();
  302. size -= panel_size;
  303. ofs += panel_offset;
  304. if (draw_focus_border) {
  305. // Only update the size and offset if focus style's doesn't fit into the panel style's.
  306. Size2 focus_size = theme_cache.focus_style->get_minimum_size();
  307. if (focus_size > panel_size) {
  308. size -= focus_size - panel_size;
  309. }
  310. Point2 focus_offset = theme_cache.focus_style->get_offset();
  311. if (focus_offset > panel_offset) {
  312. ofs += focus_offset - panel_offset;
  313. }
  314. }
  315. bool rtl = is_layout_rtl();
  316. bool reserve_vscroll = _is_v_scroll_visible() || vertical_scroll_mode == SCROLL_MODE_RESERVE;
  317. if (_is_h_scroll_visible() || horizontal_scroll_mode == SCROLL_MODE_RESERVE) {
  318. size.y -= h_scroll->get_minimum_size().y;
  319. }
  320. if (reserve_vscroll) {
  321. size.x -= v_scroll->get_minimum_size().x;
  322. }
  323. for (int i = 0; i < get_child_count(); i++) {
  324. Control *c = as_sortable_control(get_child(i));
  325. if (!c) {
  326. continue;
  327. }
  328. if (c == h_scroll || c == v_scroll || c == focus_panel) {
  329. continue;
  330. }
  331. Size2 minsize = c->get_combined_minimum_size();
  332. Rect2 r = Rect2(-Size2(get_h_scroll(), get_v_scroll()), minsize);
  333. if (c->get_h_size_flags().has_flag(SIZE_EXPAND)) {
  334. r.size.width = MAX(size.width, minsize.width);
  335. }
  336. if (c->get_v_size_flags().has_flag(SIZE_EXPAND)) {
  337. r.size.height = MAX(size.height, minsize.height);
  338. }
  339. r.position += ofs;
  340. if (rtl && reserve_vscroll) {
  341. r.position.x += v_scroll->get_minimum_size().x;
  342. }
  343. r.position = r.position.floor();
  344. fit_child_in_rect(c, r);
  345. }
  346. if (draw_focus_border) {
  347. focus_panel->set_position(Vector2(0, 0));
  348. focus_panel->set_size(get_size());
  349. }
  350. queue_redraw();
  351. }
  352. void ScrollContainer::_accessibility_action_scroll_set(const Variant &p_data) {
  353. const Point2 &pos = p_data;
  354. h_scroll->set_value(pos.x);
  355. v_scroll->set_value(pos.y);
  356. }
  357. void ScrollContainer::_accessibility_action_scroll_up(const Variant &p_data) {
  358. v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page() / ScrollBar::PAGE_DIVISOR);
  359. }
  360. void ScrollContainer::_accessibility_action_scroll_down(const Variant &p_data) {
  361. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() / ScrollBar::PAGE_DIVISOR);
  362. }
  363. void ScrollContainer::_accessibility_action_scroll_left(const Variant &p_data) {
  364. h_scroll->set_value(h_scroll->get_value() - h_scroll->get_page() / ScrollBar::PAGE_DIVISOR);
  365. }
  366. void ScrollContainer::_accessibility_action_scroll_right(const Variant &p_data) {
  367. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() / ScrollBar::PAGE_DIVISOR);
  368. }
  369. void ScrollContainer::_notification(int p_what) {
  370. switch (p_what) {
  371. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  372. RID ae = get_accessibility_element();
  373. ERR_FAIL_COND(ae.is_null());
  374. DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_SCROLL_VIEW);
  375. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_DOWN, callable_mp(this, &ScrollContainer::_accessibility_action_scroll_down));
  376. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_LEFT, callable_mp(this, &ScrollContainer::_accessibility_action_scroll_left));
  377. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_RIGHT, callable_mp(this, &ScrollContainer::_accessibility_action_scroll_right));
  378. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_UP, callable_mp(this, &ScrollContainer::_accessibility_action_scroll_up));
  379. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SET_SCROLL_OFFSET, callable_mp(this, &ScrollContainer::_accessibility_action_scroll_set));
  380. } break;
  381. case NOTIFICATION_THEME_CHANGED:
  382. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  383. case NOTIFICATION_TRANSLATION_CHANGED: {
  384. _updating_scrollbars = true;
  385. callable_mp(this, is_ready() ? &ScrollContainer::_reposition_children : &ScrollContainer::_update_scrollbar_position).call_deferred();
  386. if (p_what == NOTIFICATION_THEME_CHANGED) {
  387. scroll_border = get_theme_constant(SNAME("scroll_border"), SNAME("Tree"));
  388. scroll_speed = get_theme_constant(SNAME("scroll_speed"), SNAME("Tree"));
  389. focus_panel->add_theme_style_override("panel", theme_cache.focus_style);
  390. }
  391. } break;
  392. case NOTIFICATION_READY: {
  393. Viewport *viewport = get_viewport();
  394. ERR_FAIL_NULL(viewport);
  395. viewport->connect("gui_focus_changed", callable_mp(this, &ScrollContainer::_gui_focus_changed));
  396. _reposition_children();
  397. } break;
  398. case NOTIFICATION_SORT_CHILDREN: {
  399. _reposition_children();
  400. } break;
  401. case NOTIFICATION_DRAW: {
  402. draw_style_box(theme_cache.panel_style, Rect2(Vector2(), get_size()));
  403. focus_border_is_drawn = draw_focus_border && (has_focus() || child_has_focus());
  404. focus_panel->set_visible(focus_border_is_drawn);
  405. } break;
  406. case NOTIFICATION_DRAG_BEGIN: {
  407. if (scroll_on_drag_hover && is_visible_in_tree()) {
  408. set_process_internal(true);
  409. }
  410. } break;
  411. case NOTIFICATION_DRAG_END: {
  412. set_process_internal(false);
  413. } break;
  414. case NOTIFICATION_INTERNAL_PROCESS: {
  415. if (scroll_on_drag_hover && get_viewport()->gui_is_dragging()) {
  416. Point2 mouse_position = get_viewport()->get_mouse_position() - get_global_position();
  417. Transform2D xform = get_transform();
  418. if (Rect2(Point2(), xform.get_scale() * get_size()).grow(scroll_border).has_point(mouse_position)) {
  419. Point2 point;
  420. if ((Math::abs(mouse_position.x) < Math::abs(mouse_position.x - get_size().width)) && (Math::abs(mouse_position.x) < scroll_border)) {
  421. point.x = mouse_position.x - scroll_border;
  422. } else if (Math::abs(mouse_position.x - get_size().width) < scroll_border) {
  423. point.x = mouse_position.x - (get_size().width - scroll_border);
  424. }
  425. if ((Math::abs(mouse_position.y) < Math::abs(mouse_position.y - get_size().height)) && (Math::abs(mouse_position.y) < scroll_border)) {
  426. point.y = mouse_position.y - scroll_border;
  427. } else if (Math::abs(mouse_position.y - get_size().height) < scroll_border) {
  428. point.y = mouse_position.y - (get_size().height - scroll_border);
  429. }
  430. point *= scroll_speed * get_process_delta_time();
  431. point += Point2(get_h_scroll(), get_v_scroll());
  432. h_scroll->set_value(point.x);
  433. v_scroll->set_value(point.y);
  434. }
  435. }
  436. if (drag_touching) {
  437. if (drag_touching_deaccel) {
  438. Vector2 pos = Vector2(h_scroll->get_value(), v_scroll->get_value());
  439. pos += drag_speed * get_process_delta_time();
  440. bool turnoff_h = false;
  441. bool turnoff_v = false;
  442. if (pos.x < 0) {
  443. pos.x = 0;
  444. turnoff_h = true;
  445. }
  446. if (pos.x > (h_scroll->get_max() - h_scroll->get_page())) {
  447. pos.x = h_scroll->get_max() - h_scroll->get_page();
  448. turnoff_h = true;
  449. }
  450. if (pos.y < 0) {
  451. pos.y = 0;
  452. turnoff_v = true;
  453. }
  454. if (pos.y > (v_scroll->get_max() - v_scroll->get_page())) {
  455. pos.y = v_scroll->get_max() - v_scroll->get_page();
  456. turnoff_v = true;
  457. }
  458. if (horizontal_scroll_mode != SCROLL_MODE_DISABLED) {
  459. h_scroll->scroll_to(pos.x);
  460. }
  461. if (vertical_scroll_mode != SCROLL_MODE_DISABLED) {
  462. v_scroll->scroll_to(pos.y);
  463. }
  464. float sgn_x = drag_speed.x < 0 ? -1 : 1;
  465. float val_x = Math::abs(drag_speed.x);
  466. val_x -= 1000 * get_process_delta_time();
  467. if (val_x < 0) {
  468. turnoff_h = true;
  469. }
  470. float sgn_y = drag_speed.y < 0 ? -1 : 1;
  471. float val_y = Math::abs(drag_speed.y);
  472. val_y -= 1000 * get_process_delta_time();
  473. if (val_y < 0) {
  474. turnoff_v = true;
  475. }
  476. drag_speed = Vector2(sgn_x * val_x, sgn_y * val_y);
  477. if (turnoff_h && turnoff_v) {
  478. _cancel_drag();
  479. }
  480. } else {
  481. if (time_since_motion == 0 || time_since_motion > 0.1) {
  482. Vector2 diff = drag_accum - last_drag_accum;
  483. last_drag_accum = drag_accum;
  484. drag_speed = diff / get_process_delta_time();
  485. }
  486. time_since_motion += get_process_delta_time();
  487. }
  488. }
  489. } break;
  490. }
  491. }
  492. void ScrollContainer::update_scrollbars() {
  493. Size2 size = get_size();
  494. Size2 panel_size = theme_cache.panel_style->get_minimum_size();
  495. size -= panel_size;
  496. if (draw_focus_border) {
  497. Size2 focus_size = theme_cache.focus_style->get_minimum_size();
  498. // Only update the size if the focus style's minimum size doesn't fit into the panel style's minimum size.
  499. if (focus_size > panel_size) {
  500. size -= focus_size - panel_size;
  501. }
  502. }
  503. Size2 hmin = h_scroll->get_combined_minimum_size();
  504. Size2 vmin = v_scroll->get_combined_minimum_size();
  505. 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));
  506. 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));
  507. h_scroll->set_max(largest_child_min_size.width);
  508. h_scroll->set_page(_is_v_scroll_visible() ? size.width - vmin.width : size.width);
  509. v_scroll->set_max(largest_child_min_size.height);
  510. v_scroll->set_page(_is_h_scroll_visible() ? size.height - hmin.height : size.height);
  511. // Avoid scrollbar overlapping.
  512. _updating_scrollbars = true;
  513. callable_mp(this, &ScrollContainer::_update_scrollbar_position).call_deferred();
  514. }
  515. void ScrollContainer::_scroll_moved(float) {
  516. queue_sort();
  517. }
  518. void ScrollContainer::set_h_scroll(int p_pos) {
  519. h_scroll->set_value(p_pos);
  520. _cancel_drag();
  521. }
  522. int ScrollContainer::get_h_scroll() const {
  523. return h_scroll->get_value();
  524. }
  525. void ScrollContainer::set_v_scroll(int p_pos) {
  526. v_scroll->set_value(p_pos);
  527. _cancel_drag();
  528. }
  529. int ScrollContainer::get_v_scroll() const {
  530. return v_scroll->get_value();
  531. }
  532. void ScrollContainer::set_horizontal_custom_step(float p_custom_step) {
  533. h_scroll->set_custom_step(p_custom_step);
  534. }
  535. float ScrollContainer::get_horizontal_custom_step() const {
  536. return h_scroll->get_custom_step();
  537. }
  538. void ScrollContainer::set_vertical_custom_step(float p_custom_step) {
  539. v_scroll->set_custom_step(p_custom_step);
  540. }
  541. float ScrollContainer::get_vertical_custom_step() const {
  542. return v_scroll->get_custom_step();
  543. }
  544. void ScrollContainer::set_horizontal_scroll_mode(ScrollMode p_mode) {
  545. if (horizontal_scroll_mode == p_mode) {
  546. return;
  547. }
  548. horizontal_scroll_mode = p_mode;
  549. update_minimum_size();
  550. queue_sort();
  551. }
  552. ScrollContainer::ScrollMode ScrollContainer::get_horizontal_scroll_mode() const {
  553. return horizontal_scroll_mode;
  554. }
  555. void ScrollContainer::set_vertical_scroll_mode(ScrollMode p_mode) {
  556. if (vertical_scroll_mode == p_mode) {
  557. return;
  558. }
  559. vertical_scroll_mode = p_mode;
  560. update_minimum_size();
  561. queue_sort();
  562. }
  563. ScrollContainer::ScrollMode ScrollContainer::get_vertical_scroll_mode() const {
  564. return vertical_scroll_mode;
  565. }
  566. int ScrollContainer::get_deadzone() const {
  567. return deadzone;
  568. }
  569. void ScrollContainer::set_deadzone(int p_deadzone) {
  570. deadzone = p_deadzone;
  571. }
  572. bool ScrollContainer::is_following_focus() const {
  573. return follow_focus;
  574. }
  575. void ScrollContainer::set_follow_focus(bool p_follow) {
  576. follow_focus = p_follow;
  577. }
  578. PackedStringArray ScrollContainer::get_configuration_warnings() const {
  579. PackedStringArray warnings = Container::get_configuration_warnings();
  580. int found = 0;
  581. for (int i = 0; i < get_child_count(); i++) {
  582. Control *c = as_sortable_control(get_child(i), SortableVisibilityMode::VISIBLE);
  583. if (!c) {
  584. continue;
  585. }
  586. if (c == h_scroll || c == v_scroll || c == focus_panel) {
  587. continue;
  588. }
  589. found++;
  590. }
  591. if (found != 1) {
  592. 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."));
  593. }
  594. return warnings;
  595. }
  596. void ScrollContainer::set_scroll_on_drag_hover(bool p_scroll) {
  597. scroll_on_drag_hover = p_scroll;
  598. }
  599. HScrollBar *ScrollContainer::get_h_scroll_bar() {
  600. return h_scroll;
  601. }
  602. VScrollBar *ScrollContainer::get_v_scroll_bar() {
  603. return v_scroll;
  604. }
  605. void ScrollContainer::_bind_methods() {
  606. ClassDB::bind_method(D_METHOD("set_h_scroll", "value"), &ScrollContainer::set_h_scroll);
  607. ClassDB::bind_method(D_METHOD("get_h_scroll"), &ScrollContainer::get_h_scroll);
  608. ClassDB::bind_method(D_METHOD("set_v_scroll", "value"), &ScrollContainer::set_v_scroll);
  609. ClassDB::bind_method(D_METHOD("get_v_scroll"), &ScrollContainer::get_v_scroll);
  610. ClassDB::bind_method(D_METHOD("set_horizontal_custom_step", "value"), &ScrollContainer::set_horizontal_custom_step);
  611. ClassDB::bind_method(D_METHOD("get_horizontal_custom_step"), &ScrollContainer::get_horizontal_custom_step);
  612. ClassDB::bind_method(D_METHOD("set_vertical_custom_step", "value"), &ScrollContainer::set_vertical_custom_step);
  613. ClassDB::bind_method(D_METHOD("get_vertical_custom_step"), &ScrollContainer::get_vertical_custom_step);
  614. ClassDB::bind_method(D_METHOD("set_horizontal_scroll_mode", "enable"), &ScrollContainer::set_horizontal_scroll_mode);
  615. ClassDB::bind_method(D_METHOD("get_horizontal_scroll_mode"), &ScrollContainer::get_horizontal_scroll_mode);
  616. ClassDB::bind_method(D_METHOD("set_vertical_scroll_mode", "enable"), &ScrollContainer::set_vertical_scroll_mode);
  617. ClassDB::bind_method(D_METHOD("get_vertical_scroll_mode"), &ScrollContainer::get_vertical_scroll_mode);
  618. ClassDB::bind_method(D_METHOD("set_deadzone", "deadzone"), &ScrollContainer::set_deadzone);
  619. ClassDB::bind_method(D_METHOD("get_deadzone"), &ScrollContainer::get_deadzone);
  620. ClassDB::bind_method(D_METHOD("set_follow_focus", "enabled"), &ScrollContainer::set_follow_focus);
  621. ClassDB::bind_method(D_METHOD("is_following_focus"), &ScrollContainer::is_following_focus);
  622. ClassDB::bind_method(D_METHOD("get_h_scroll_bar"), &ScrollContainer::get_h_scroll_bar);
  623. ClassDB::bind_method(D_METHOD("get_v_scroll_bar"), &ScrollContainer::get_v_scroll_bar);
  624. ClassDB::bind_method(D_METHOD("ensure_control_visible", "control"), &ScrollContainer::ensure_control_visible);
  625. ClassDB::bind_method(D_METHOD("set_draw_focus_border", "draw"), &ScrollContainer::set_draw_focus_border);
  626. ClassDB::bind_method(D_METHOD("get_draw_focus_border"), &ScrollContainer::get_draw_focus_border);
  627. ADD_SIGNAL(MethodInfo("scroll_started"));
  628. ADD_SIGNAL(MethodInfo("scroll_ended"));
  629. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "follow_focus"), "set_follow_focus", "is_following_focus");
  630. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_focus_border"), "set_draw_focus_border", "get_draw_focus_border");
  631. ADD_GROUP("Scroll", "scroll_");
  632. ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_horizontal", PROPERTY_HINT_NONE, "suffix:px"), "set_h_scroll", "get_h_scroll");
  633. ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_vertical", PROPERTY_HINT_NONE, "suffix:px"), "set_v_scroll", "get_v_scroll");
  634. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "scroll_horizontal_custom_step", PROPERTY_HINT_RANGE, "-1,4096,suffix:px"), "set_horizontal_custom_step", "get_horizontal_custom_step");
  635. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "scroll_vertical_custom_step", PROPERTY_HINT_RANGE, "-1,4096,suffix:px"), "set_vertical_custom_step", "get_vertical_custom_step");
  636. 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");
  637. 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");
  638. ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_deadzone"), "set_deadzone", "get_deadzone");
  639. BIND_ENUM_CONSTANT(SCROLL_MODE_DISABLED);
  640. BIND_ENUM_CONSTANT(SCROLL_MODE_AUTO);
  641. BIND_ENUM_CONSTANT(SCROLL_MODE_SHOW_ALWAYS);
  642. BIND_ENUM_CONSTANT(SCROLL_MODE_SHOW_NEVER);
  643. BIND_ENUM_CONSTANT(SCROLL_MODE_RESERVE);
  644. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ScrollContainer, panel_style, "panel");
  645. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ScrollContainer, focus_style, "focus");
  646. GLOBAL_DEF("gui/common/default_scroll_deadzone", 0);
  647. }
  648. void ScrollContainer::set_draw_focus_border(bool p_draw) {
  649. if (draw_focus_border == p_draw) {
  650. return;
  651. }
  652. draw_focus_border = p_draw;
  653. if (is_ready()) {
  654. _reposition_children();
  655. }
  656. }
  657. bool ScrollContainer::get_draw_focus_border() {
  658. return draw_focus_border;
  659. }
  660. bool ScrollContainer::child_has_focus() {
  661. const Control *focus_owner = get_viewport() ? get_viewport()->gui_get_focus_owner() : nullptr;
  662. return focus_owner && is_ancestor_of(focus_owner);
  663. }
  664. ScrollContainer::ScrollContainer() {
  665. h_scroll = memnew(HScrollBar);
  666. h_scroll->set_name("_h_scroll");
  667. add_child(h_scroll, false, INTERNAL_MODE_BACK);
  668. h_scroll->connect(SceneStringName(value_changed), callable_mp(this, &ScrollContainer::_scroll_moved));
  669. v_scroll = memnew(VScrollBar);
  670. v_scroll->set_name("_v_scroll");
  671. add_child(v_scroll, false, INTERNAL_MODE_BACK);
  672. v_scroll->connect(SceneStringName(value_changed), callable_mp(this, &ScrollContainer::_scroll_moved));
  673. // We need to use a PanelContainer for the focus style instead of just drawing it directly with RenderingService
  674. // due to a clippling issues. The Control that is being scrolled will be over the focus border because both will be
  675. // drawn on the same CanvasItem. If we decide to ignore clipping, the focus border will be drawn even over other
  676. // CanvasItems.
  677. focus_panel = memnew(PanelContainer);
  678. focus_panel->set_name("_focus");
  679. focus_panel->set_mouse_filter(MOUSE_FILTER_IGNORE);
  680. focus_panel->set_focus_mode(FOCUS_NONE);
  681. focus_panel->set_visible(draw_focus_border);
  682. add_child(focus_panel, false, INTERNAL_MODE_BACK);
  683. deadzone = GLOBAL_GET_CACHED(int, "gui/common/default_scroll_deadzone");
  684. set_clip_contents(true);
  685. }