scroll_container.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  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. if (_is_h_scroll_visible() || horizontal_scroll_mode == SCROLL_MODE_RESERVE) {
  317. size.y -= h_scroll->get_minimum_size().y;
  318. }
  319. if (_is_v_scroll_visible() || vertical_scroll_mode == SCROLL_MODE_RESERVE) {
  320. size.x -= v_scroll->get_minimum_size().x;
  321. }
  322. for (int i = 0; i < get_child_count(); i++) {
  323. Control *c = as_sortable_control(get_child(i));
  324. if (!c) {
  325. continue;
  326. }
  327. if (c == h_scroll || c == v_scroll || c == focus_panel) {
  328. continue;
  329. }
  330. Size2 minsize = c->get_combined_minimum_size();
  331. Rect2 r = Rect2(-Size2(get_h_scroll(), get_v_scroll()), minsize);
  332. if (c->get_h_size_flags().has_flag(SIZE_EXPAND)) {
  333. r.size.width = MAX(size.width, minsize.width);
  334. }
  335. if (c->get_v_size_flags().has_flag(SIZE_EXPAND)) {
  336. r.size.height = MAX(size.height, minsize.height);
  337. }
  338. r.position += ofs;
  339. if (rtl && _is_v_scroll_visible()) {
  340. r.position.x += v_scroll->get_minimum_size().x;
  341. }
  342. r.position = r.position.floor();
  343. fit_child_in_rect(c, r);
  344. }
  345. if (draw_focus_border) {
  346. focus_panel->set_position(Vector2(0, 0));
  347. focus_panel->set_size(get_size());
  348. }
  349. queue_redraw();
  350. }
  351. void ScrollContainer::_accessibility_action_scroll_set(const Variant &p_data) {
  352. const Point2 &pos = p_data;
  353. h_scroll->set_value(pos.x);
  354. v_scroll->set_value(pos.y);
  355. }
  356. void ScrollContainer::_accessibility_action_scroll_up(const Variant &p_data) {
  357. v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page() / ScrollBar::PAGE_DIVISOR);
  358. }
  359. void ScrollContainer::_accessibility_action_scroll_down(const Variant &p_data) {
  360. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() / ScrollBar::PAGE_DIVISOR);
  361. }
  362. void ScrollContainer::_accessibility_action_scroll_left(const Variant &p_data) {
  363. h_scroll->set_value(h_scroll->get_value() - h_scroll->get_page() / ScrollBar::PAGE_DIVISOR);
  364. }
  365. void ScrollContainer::_accessibility_action_scroll_right(const Variant &p_data) {
  366. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() / ScrollBar::PAGE_DIVISOR);
  367. }
  368. void ScrollContainer::_notification(int p_what) {
  369. switch (p_what) {
  370. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  371. RID ae = get_accessibility_element();
  372. ERR_FAIL_COND(ae.is_null());
  373. DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_SCROLL_VIEW);
  374. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_DOWN, callable_mp(this, &ScrollContainer::_accessibility_action_scroll_down));
  375. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_LEFT, callable_mp(this, &ScrollContainer::_accessibility_action_scroll_left));
  376. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_RIGHT, callable_mp(this, &ScrollContainer::_accessibility_action_scroll_right));
  377. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_UP, callable_mp(this, &ScrollContainer::_accessibility_action_scroll_up));
  378. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SET_SCROLL_OFFSET, callable_mp(this, &ScrollContainer::_accessibility_action_scroll_set));
  379. } break;
  380. case NOTIFICATION_THEME_CHANGED:
  381. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  382. case NOTIFICATION_TRANSLATION_CHANGED: {
  383. _updating_scrollbars = true;
  384. callable_mp(this, is_ready() ? &ScrollContainer::_reposition_children : &ScrollContainer::_update_scrollbar_position).call_deferred();
  385. if (p_what == NOTIFICATION_THEME_CHANGED) {
  386. scroll_border = get_theme_constant(SNAME("scroll_border"), SNAME("Tree"));
  387. scroll_speed = get_theme_constant(SNAME("scroll_speed"), SNAME("Tree"));
  388. focus_panel->add_theme_style_override("panel", theme_cache.focus_style);
  389. }
  390. } break;
  391. case NOTIFICATION_READY: {
  392. Viewport *viewport = get_viewport();
  393. ERR_FAIL_NULL(viewport);
  394. viewport->connect("gui_focus_changed", callable_mp(this, &ScrollContainer::_gui_focus_changed));
  395. _reposition_children();
  396. } break;
  397. case NOTIFICATION_SORT_CHILDREN: {
  398. _reposition_children();
  399. } break;
  400. case NOTIFICATION_DRAW: {
  401. draw_style_box(theme_cache.panel_style, Rect2(Vector2(), get_size()));
  402. focus_border_is_drawn = draw_focus_border && (has_focus() || child_has_focus());
  403. focus_panel->set_visible(focus_border_is_drawn);
  404. } break;
  405. case NOTIFICATION_DRAG_BEGIN: {
  406. if (scroll_on_drag_hover && is_visible_in_tree()) {
  407. set_process_internal(true);
  408. }
  409. } break;
  410. case NOTIFICATION_DRAG_END: {
  411. set_process_internal(false);
  412. } break;
  413. case NOTIFICATION_INTERNAL_PROCESS: {
  414. if (scroll_on_drag_hover && get_viewport()->gui_is_dragging()) {
  415. Point2 mouse_position = get_viewport()->get_mouse_position() - get_global_position();
  416. Transform2D xform = get_transform();
  417. if (Rect2(Point2(), xform.get_scale() * get_size()).grow(scroll_border).has_point(mouse_position)) {
  418. Point2 point;
  419. if ((Math::abs(mouse_position.x) < Math::abs(mouse_position.x - get_size().width)) && (Math::abs(mouse_position.x) < scroll_border)) {
  420. point.x = mouse_position.x - scroll_border;
  421. } else if (Math::abs(mouse_position.x - get_size().width) < scroll_border) {
  422. point.x = mouse_position.x - (get_size().width - scroll_border);
  423. }
  424. if ((Math::abs(mouse_position.y) < Math::abs(mouse_position.y - get_size().height)) && (Math::abs(mouse_position.y) < scroll_border)) {
  425. point.y = mouse_position.y - scroll_border;
  426. } else if (Math::abs(mouse_position.y - get_size().height) < scroll_border) {
  427. point.y = mouse_position.y - (get_size().height - scroll_border);
  428. }
  429. point *= scroll_speed * get_process_delta_time();
  430. point += Point2(get_h_scroll(), get_v_scroll());
  431. h_scroll->set_value(point.x);
  432. v_scroll->set_value(point.y);
  433. }
  434. }
  435. if (drag_touching) {
  436. if (drag_touching_deaccel) {
  437. Vector2 pos = Vector2(h_scroll->get_value(), v_scroll->get_value());
  438. pos += drag_speed * get_process_delta_time();
  439. bool turnoff_h = false;
  440. bool turnoff_v = false;
  441. if (pos.x < 0) {
  442. pos.x = 0;
  443. turnoff_h = true;
  444. }
  445. if (pos.x > (h_scroll->get_max() - h_scroll->get_page())) {
  446. pos.x = h_scroll->get_max() - h_scroll->get_page();
  447. turnoff_h = true;
  448. }
  449. if (pos.y < 0) {
  450. pos.y = 0;
  451. turnoff_v = true;
  452. }
  453. if (pos.y > (v_scroll->get_max() - v_scroll->get_page())) {
  454. pos.y = v_scroll->get_max() - v_scroll->get_page();
  455. turnoff_v = true;
  456. }
  457. if (horizontal_scroll_mode != SCROLL_MODE_DISABLED) {
  458. h_scroll->scroll_to(pos.x);
  459. }
  460. if (vertical_scroll_mode != SCROLL_MODE_DISABLED) {
  461. v_scroll->scroll_to(pos.y);
  462. }
  463. float sgn_x = drag_speed.x < 0 ? -1 : 1;
  464. float val_x = Math::abs(drag_speed.x);
  465. val_x -= 1000 * get_process_delta_time();
  466. if (val_x < 0) {
  467. turnoff_h = true;
  468. }
  469. float sgn_y = drag_speed.y < 0 ? -1 : 1;
  470. float val_y = Math::abs(drag_speed.y);
  471. val_y -= 1000 * get_process_delta_time();
  472. if (val_y < 0) {
  473. turnoff_v = true;
  474. }
  475. drag_speed = Vector2(sgn_x * val_x, sgn_y * val_y);
  476. if (turnoff_h && turnoff_v) {
  477. _cancel_drag();
  478. }
  479. } else {
  480. if (time_since_motion == 0 || time_since_motion > 0.1) {
  481. Vector2 diff = drag_accum - last_drag_accum;
  482. last_drag_accum = drag_accum;
  483. drag_speed = diff / get_process_delta_time();
  484. }
  485. time_since_motion += get_process_delta_time();
  486. }
  487. }
  488. } break;
  489. }
  490. }
  491. void ScrollContainer::update_scrollbars() {
  492. Size2 size = get_size();
  493. Size2 panel_size = theme_cache.panel_style->get_minimum_size();
  494. size -= panel_size;
  495. if (draw_focus_border) {
  496. Size2 focus_size = theme_cache.focus_style->get_minimum_size();
  497. // Only update the size if the focus style's minimum size doesn't fit into the panel style's minimum size.
  498. if (focus_size > panel_size) {
  499. size -= focus_size - panel_size;
  500. }
  501. }
  502. Size2 hmin = h_scroll->get_combined_minimum_size();
  503. Size2 vmin = v_scroll->get_combined_minimum_size();
  504. 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));
  505. 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));
  506. h_scroll->set_max(largest_child_min_size.width);
  507. h_scroll->set_page(_is_v_scroll_visible() ? size.width - vmin.width : size.width);
  508. v_scroll->set_max(largest_child_min_size.height);
  509. v_scroll->set_page(_is_h_scroll_visible() ? size.height - hmin.height : size.height);
  510. // Avoid scrollbar overlapping.
  511. _updating_scrollbars = true;
  512. callable_mp(this, &ScrollContainer::_update_scrollbar_position).call_deferred();
  513. }
  514. void ScrollContainer::_scroll_moved(float) {
  515. queue_sort();
  516. }
  517. void ScrollContainer::set_h_scroll(int p_pos) {
  518. h_scroll->set_value(p_pos);
  519. _cancel_drag();
  520. }
  521. int ScrollContainer::get_h_scroll() const {
  522. return h_scroll->get_value();
  523. }
  524. void ScrollContainer::set_v_scroll(int p_pos) {
  525. v_scroll->set_value(p_pos);
  526. _cancel_drag();
  527. }
  528. int ScrollContainer::get_v_scroll() const {
  529. return v_scroll->get_value();
  530. }
  531. void ScrollContainer::set_horizontal_custom_step(float p_custom_step) {
  532. h_scroll->set_custom_step(p_custom_step);
  533. }
  534. float ScrollContainer::get_horizontal_custom_step() const {
  535. return h_scroll->get_custom_step();
  536. }
  537. void ScrollContainer::set_vertical_custom_step(float p_custom_step) {
  538. v_scroll->set_custom_step(p_custom_step);
  539. }
  540. float ScrollContainer::get_vertical_custom_step() const {
  541. return v_scroll->get_custom_step();
  542. }
  543. void ScrollContainer::set_horizontal_scroll_mode(ScrollMode p_mode) {
  544. if (horizontal_scroll_mode == p_mode) {
  545. return;
  546. }
  547. horizontal_scroll_mode = p_mode;
  548. update_minimum_size();
  549. queue_sort();
  550. }
  551. ScrollContainer::ScrollMode ScrollContainer::get_horizontal_scroll_mode() const {
  552. return horizontal_scroll_mode;
  553. }
  554. void ScrollContainer::set_vertical_scroll_mode(ScrollMode p_mode) {
  555. if (vertical_scroll_mode == p_mode) {
  556. return;
  557. }
  558. vertical_scroll_mode = p_mode;
  559. update_minimum_size();
  560. queue_sort();
  561. }
  562. ScrollContainer::ScrollMode ScrollContainer::get_vertical_scroll_mode() const {
  563. return vertical_scroll_mode;
  564. }
  565. int ScrollContainer::get_deadzone() const {
  566. return deadzone;
  567. }
  568. void ScrollContainer::set_deadzone(int p_deadzone) {
  569. deadzone = p_deadzone;
  570. }
  571. bool ScrollContainer::is_following_focus() const {
  572. return follow_focus;
  573. }
  574. void ScrollContainer::set_follow_focus(bool p_follow) {
  575. follow_focus = p_follow;
  576. }
  577. PackedStringArray ScrollContainer::get_configuration_warnings() const {
  578. PackedStringArray warnings = Container::get_configuration_warnings();
  579. int found = 0;
  580. for (int i = 0; i < get_child_count(); i++) {
  581. Control *c = as_sortable_control(get_child(i), SortableVisibilityMode::VISIBLE);
  582. if (!c) {
  583. continue;
  584. }
  585. if (c == h_scroll || c == v_scroll || c == focus_panel) {
  586. continue;
  587. }
  588. found++;
  589. }
  590. if (found != 1) {
  591. 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."));
  592. }
  593. return warnings;
  594. }
  595. void ScrollContainer::set_scroll_on_drag_hover(bool p_scroll) {
  596. scroll_on_drag_hover = p_scroll;
  597. }
  598. HScrollBar *ScrollContainer::get_h_scroll_bar() {
  599. return h_scroll;
  600. }
  601. VScrollBar *ScrollContainer::get_v_scroll_bar() {
  602. return v_scroll;
  603. }
  604. void ScrollContainer::_bind_methods() {
  605. ClassDB::bind_method(D_METHOD("set_h_scroll", "value"), &ScrollContainer::set_h_scroll);
  606. ClassDB::bind_method(D_METHOD("get_h_scroll"), &ScrollContainer::get_h_scroll);
  607. ClassDB::bind_method(D_METHOD("set_v_scroll", "value"), &ScrollContainer::set_v_scroll);
  608. ClassDB::bind_method(D_METHOD("get_v_scroll"), &ScrollContainer::get_v_scroll);
  609. ClassDB::bind_method(D_METHOD("set_horizontal_custom_step", "value"), &ScrollContainer::set_horizontal_custom_step);
  610. ClassDB::bind_method(D_METHOD("get_horizontal_custom_step"), &ScrollContainer::get_horizontal_custom_step);
  611. ClassDB::bind_method(D_METHOD("set_vertical_custom_step", "value"), &ScrollContainer::set_vertical_custom_step);
  612. ClassDB::bind_method(D_METHOD("get_vertical_custom_step"), &ScrollContainer::get_vertical_custom_step);
  613. ClassDB::bind_method(D_METHOD("set_horizontal_scroll_mode", "enable"), &ScrollContainer::set_horizontal_scroll_mode);
  614. ClassDB::bind_method(D_METHOD("get_horizontal_scroll_mode"), &ScrollContainer::get_horizontal_scroll_mode);
  615. ClassDB::bind_method(D_METHOD("set_vertical_scroll_mode", "enable"), &ScrollContainer::set_vertical_scroll_mode);
  616. ClassDB::bind_method(D_METHOD("get_vertical_scroll_mode"), &ScrollContainer::get_vertical_scroll_mode);
  617. ClassDB::bind_method(D_METHOD("set_deadzone", "deadzone"), &ScrollContainer::set_deadzone);
  618. ClassDB::bind_method(D_METHOD("get_deadzone"), &ScrollContainer::get_deadzone);
  619. ClassDB::bind_method(D_METHOD("set_follow_focus", "enabled"), &ScrollContainer::set_follow_focus);
  620. ClassDB::bind_method(D_METHOD("is_following_focus"), &ScrollContainer::is_following_focus);
  621. ClassDB::bind_method(D_METHOD("get_h_scroll_bar"), &ScrollContainer::get_h_scroll_bar);
  622. ClassDB::bind_method(D_METHOD("get_v_scroll_bar"), &ScrollContainer::get_v_scroll_bar);
  623. ClassDB::bind_method(D_METHOD("ensure_control_visible", "control"), &ScrollContainer::ensure_control_visible);
  624. ClassDB::bind_method(D_METHOD("set_draw_focus_border", "draw"), &ScrollContainer::set_draw_focus_border);
  625. ClassDB::bind_method(D_METHOD("get_draw_focus_border"), &ScrollContainer::get_draw_focus_border);
  626. ADD_SIGNAL(MethodInfo("scroll_started"));
  627. ADD_SIGNAL(MethodInfo("scroll_ended"));
  628. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "follow_focus"), "set_follow_focus", "is_following_focus");
  629. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_focus_border"), "set_draw_focus_border", "get_draw_focus_border");
  630. ADD_GROUP("Scroll", "scroll_");
  631. ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_horizontal", PROPERTY_HINT_NONE, "suffix:px"), "set_h_scroll", "get_h_scroll");
  632. ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_vertical", PROPERTY_HINT_NONE, "suffix:px"), "set_v_scroll", "get_v_scroll");
  633. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "scroll_horizontal_custom_step", PROPERTY_HINT_RANGE, "-1,4096,suffix:px"), "set_horizontal_custom_step", "get_horizontal_custom_step");
  634. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "scroll_vertical_custom_step", PROPERTY_HINT_RANGE, "-1,4096,suffix:px"), "set_vertical_custom_step", "get_vertical_custom_step");
  635. 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");
  636. 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");
  637. ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_deadzone"), "set_deadzone", "get_deadzone");
  638. BIND_ENUM_CONSTANT(SCROLL_MODE_DISABLED);
  639. BIND_ENUM_CONSTANT(SCROLL_MODE_AUTO);
  640. BIND_ENUM_CONSTANT(SCROLL_MODE_SHOW_ALWAYS);
  641. BIND_ENUM_CONSTANT(SCROLL_MODE_SHOW_NEVER);
  642. BIND_ENUM_CONSTANT(SCROLL_MODE_RESERVE);
  643. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ScrollContainer, panel_style, "panel");
  644. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ScrollContainer, focus_style, "focus");
  645. GLOBAL_DEF("gui/common/default_scroll_deadzone", 0);
  646. }
  647. void ScrollContainer::set_draw_focus_border(bool p_draw) {
  648. if (draw_focus_border == p_draw) {
  649. return;
  650. }
  651. draw_focus_border = p_draw;
  652. if (is_ready()) {
  653. _reposition_children();
  654. }
  655. }
  656. bool ScrollContainer::get_draw_focus_border() {
  657. return draw_focus_border;
  658. }
  659. bool ScrollContainer::child_has_focus() {
  660. const Control *focus_owner = get_viewport() ? get_viewport()->gui_get_focus_owner() : nullptr;
  661. return focus_owner && is_ancestor_of(focus_owner);
  662. }
  663. ScrollContainer::ScrollContainer() {
  664. h_scroll = memnew(HScrollBar);
  665. h_scroll->set_name("_h_scroll");
  666. add_child(h_scroll, false, INTERNAL_MODE_BACK);
  667. h_scroll->connect(SceneStringName(value_changed), callable_mp(this, &ScrollContainer::_scroll_moved));
  668. v_scroll = memnew(VScrollBar);
  669. v_scroll->set_name("_v_scroll");
  670. add_child(v_scroll, false, INTERNAL_MODE_BACK);
  671. v_scroll->connect(SceneStringName(value_changed), callable_mp(this, &ScrollContainer::_scroll_moved));
  672. // We need to use a PanelContainer for the focus style instead of just drawing it directly with RenderingService
  673. // due to a clippling issues. The Control that is being scrolled will be over the focus border because both will be
  674. // drawn on the same CanvasItem. If we decide to ignore clipping, the focus border will be drawn even over other
  675. // CanvasItems.
  676. focus_panel = memnew(PanelContainer);
  677. focus_panel->set_name("_focus");
  678. focus_panel->set_mouse_filter(MOUSE_FILTER_IGNORE);
  679. focus_panel->set_focus_mode(FOCUS_NONE);
  680. focus_panel->set_visible(draw_focus_border);
  681. add_child(focus_panel, false, INTERNAL_MODE_BACK);
  682. deadzone = GLOBAL_GET_CACHED(int, "gui/common/default_scroll_deadzone");
  683. set_clip_contents(true);
  684. }