scroll_container.cpp 37 KB

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