scroll_container.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  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 || c == h_scroll || c == v_scroll || c == focus_panel || c == scroll_hint_top_left || c == scroll_hint_bottom_right) {
  43. continue;
  44. }
  45. Size2 child_min_size = c->get_combined_minimum_size();
  46. largest_child_min_size = largest_child_min_size.max(child_min_size);
  47. }
  48. Size2 min_size;
  49. const Size2 size = get_size();
  50. if (horizontal_scroll_mode == SCROLL_MODE_DISABLED) {
  51. min_size.x = largest_child_min_size.x;
  52. 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);
  53. if (v_scroll_show && v_scroll->get_parent() == this) {
  54. min_size.x += v_scroll->get_minimum_size().x + theme_cache.scrollbar_h_separation;
  55. }
  56. }
  57. if (vertical_scroll_mode == SCROLL_MODE_DISABLED) {
  58. min_size.y = largest_child_min_size.y;
  59. 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);
  60. if (h_scroll_show && h_scroll->get_parent() == this) {
  61. min_size.y += h_scroll->get_minimum_size().y + theme_cache.scrollbar_v_separation;
  62. }
  63. }
  64. Rect2 margins = _get_margins();
  65. min_size += margins.position + margins.size;
  66. return min_size;
  67. }
  68. void ScrollContainer::_cancel_drag() {
  69. set_process_internal(false);
  70. drag_touching_deaccel = false;
  71. drag_touching = false;
  72. drag_speed = Vector2();
  73. drag_accum = Vector2();
  74. last_drag_accum = Vector2();
  75. drag_from = Vector2();
  76. if (beyond_deadzone) {
  77. emit_signal(SNAME("scroll_ended"));
  78. propagate_notification(NOTIFICATION_SCROLL_END);
  79. beyond_deadzone = false;
  80. }
  81. }
  82. bool ScrollContainer::_is_h_scroll_visible() const {
  83. // Scrolls may have been moved out for reasons.
  84. return h_scroll->is_visible() && h_scroll->get_parent() == this;
  85. }
  86. bool ScrollContainer::_is_v_scroll_visible() const {
  87. return v_scroll->is_visible() && v_scroll->get_parent() == this;
  88. }
  89. Rect2 ScrollContainer::_get_margins() const {
  90. float right_margin = theme_cache.panel_style->get_margin(SIDE_RIGHT);
  91. float left_margin = theme_cache.panel_style->get_margin(SIDE_LEFT);
  92. float top_margin = theme_cache.panel_style->get_margin(SIDE_TOP);
  93. float bottom_margin = theme_cache.panel_style->get_margin(SIDE_BOTTOM);
  94. if (draw_focus_border) {
  95. // Only update margins if the focus style's margins don't fit into the panel style's margins.
  96. float focus_margin = theme_cache.focus_style->get_margin(SIDE_RIGHT);
  97. if (focus_margin > right_margin) {
  98. right_margin = focus_margin;
  99. }
  100. focus_margin = theme_cache.focus_style->get_margin(SIDE_LEFT);
  101. if (focus_margin > left_margin) {
  102. left_margin = focus_margin;
  103. }
  104. focus_margin = theme_cache.focus_style->get_margin(SIDE_TOP);
  105. if (focus_margin > top_margin) {
  106. top_margin = focus_margin;
  107. }
  108. focus_margin = theme_cache.focus_style->get_margin(SIDE_BOTTOM);
  109. if (focus_margin > bottom_margin) {
  110. bottom_margin = focus_margin;
  111. }
  112. }
  113. return Rect2(left_margin, top_margin, right_margin, bottom_margin);
  114. }
  115. void ScrollContainer::gui_input(const Ref<InputEvent> &p_gui_input) {
  116. ERR_FAIL_COND(p_gui_input.is_null());
  117. double prev_v_scroll = v_scroll->get_value();
  118. double prev_h_scroll = h_scroll->get_value();
  119. bool h_scroll_enabled = horizontal_scroll_mode != SCROLL_MODE_DISABLED;
  120. bool v_scroll_enabled = vertical_scroll_mode != SCROLL_MODE_DISABLED;
  121. Ref<InputEventMouseButton> mb = p_gui_input;
  122. if (mb.is_valid()) {
  123. if (mb->is_pressed()) {
  124. bool scroll_value_modified = false;
  125. bool v_scroll_hidden = !v_scroll->is_visible() && vertical_scroll_mode != SCROLL_MODE_SHOW_NEVER;
  126. if (mb->get_button_index() == MouseButton::WHEEL_UP) {
  127. // By default, the vertical orientation takes precedence. This is an exception.
  128. if ((h_scroll_enabled && mb->is_shift_pressed()) || v_scroll_hidden) {
  129. h_scroll->scroll(-h_scroll->get_page() / ScrollBar::PAGE_DIVISOR * mb->get_factor());
  130. scroll_value_modified = true;
  131. } else if (v_scroll_enabled) {
  132. v_scroll->scroll(-v_scroll->get_page() / ScrollBar::PAGE_DIVISOR * mb->get_factor());
  133. scroll_value_modified = true;
  134. }
  135. }
  136. if (mb->get_button_index() == MouseButton::WHEEL_DOWN) {
  137. if ((h_scroll_enabled && mb->is_shift_pressed()) || v_scroll_hidden) {
  138. h_scroll->scroll(h_scroll->get_page() / ScrollBar::PAGE_DIVISOR * mb->get_factor());
  139. scroll_value_modified = true;
  140. } else if (v_scroll_enabled) {
  141. v_scroll->scroll(v_scroll->get_page() / ScrollBar::PAGE_DIVISOR * mb->get_factor());
  142. scroll_value_modified = true;
  143. }
  144. }
  145. bool h_scroll_hidden = !h_scroll->is_visible() && horizontal_scroll_mode != SCROLL_MODE_SHOW_NEVER;
  146. if (mb->get_button_index() == MouseButton::WHEEL_LEFT) {
  147. // By default, the horizontal orientation takes precedence. This is an exception.
  148. if ((v_scroll_enabled && mb->is_shift_pressed()) || h_scroll_hidden) {
  149. v_scroll->scroll(-v_scroll->get_page() / ScrollBar::PAGE_DIVISOR * mb->get_factor());
  150. scroll_value_modified = true;
  151. } else if (h_scroll_enabled) {
  152. h_scroll->scroll(-h_scroll->get_page() / ScrollBar::PAGE_DIVISOR * mb->get_factor());
  153. scroll_value_modified = true;
  154. }
  155. }
  156. if (mb->get_button_index() == MouseButton::WHEEL_RIGHT) {
  157. if ((v_scroll_enabled && mb->is_shift_pressed()) || h_scroll_hidden) {
  158. v_scroll->scroll(v_scroll->get_page() / ScrollBar::PAGE_DIVISOR * mb->get_factor());
  159. scroll_value_modified = true;
  160. } else if (h_scroll_enabled) {
  161. h_scroll->scroll(h_scroll->get_page() / ScrollBar::PAGE_DIVISOR * mb->get_factor());
  162. scroll_value_modified = true;
  163. }
  164. }
  165. if (scroll_value_modified && (v_scroll->get_value() != prev_v_scroll || h_scroll->get_value() != prev_h_scroll)) {
  166. accept_event(); // Accept event if scroll changed.
  167. return;
  168. }
  169. }
  170. bool is_touchscreen_available = DisplayServer::get_singleton()->is_touchscreen_available();
  171. if (!is_touchscreen_available) {
  172. return;
  173. }
  174. if (mb->get_button_index() != MouseButton::LEFT) {
  175. return;
  176. }
  177. if (mb->is_pressed()) {
  178. if (drag_touching) {
  179. _cancel_drag();
  180. }
  181. drag_speed = Vector2();
  182. drag_accum = Vector2();
  183. last_drag_accum = Vector2();
  184. drag_from = Vector2(prev_h_scroll, prev_v_scroll);
  185. drag_touching = true;
  186. drag_touching_deaccel = false;
  187. beyond_deadzone = false;
  188. time_since_motion = 0;
  189. set_process_internal(true);
  190. time_since_motion = 0;
  191. } else {
  192. if (drag_touching) {
  193. if (drag_speed == Vector2()) {
  194. _cancel_drag();
  195. } else {
  196. drag_touching_deaccel = true;
  197. }
  198. }
  199. }
  200. return;
  201. }
  202. Ref<InputEventMouseMotion> mm = p_gui_input;
  203. if (mm.is_valid()) {
  204. if (drag_touching && !drag_touching_deaccel) {
  205. Vector2 motion = mm->get_relative();
  206. drag_accum -= motion;
  207. if (beyond_deadzone || (h_scroll_enabled && Math::abs(drag_accum.x) > deadzone) || (v_scroll_enabled && Math::abs(drag_accum.y) > deadzone)) {
  208. if (!beyond_deadzone) {
  209. propagate_notification(NOTIFICATION_SCROLL_BEGIN);
  210. emit_signal(SNAME("scroll_started"));
  211. beyond_deadzone = true;
  212. // Resetting drag_accum here ensures smooth scrolling after reaching deadzone.
  213. drag_accum = -motion;
  214. }
  215. Vector2 diff = drag_from + drag_accum;
  216. if (h_scroll_enabled) {
  217. h_scroll->scroll_to(diff.x);
  218. } else {
  219. drag_accum.x = 0;
  220. }
  221. if (v_scroll_enabled) {
  222. v_scroll->scroll_to(diff.y);
  223. } else {
  224. drag_accum.y = 0;
  225. }
  226. time_since_motion = 0;
  227. }
  228. }
  229. if (v_scroll->get_value() != prev_v_scroll || h_scroll->get_value() != prev_h_scroll) {
  230. accept_event(); // Accept event if scroll changed.
  231. }
  232. return;
  233. }
  234. Ref<InputEventPanGesture> pan_gesture = p_gui_input;
  235. if (pan_gesture.is_valid()) {
  236. if (h_scroll_enabled) {
  237. h_scroll->scroll(h_scroll->get_page() * pan_gesture->get_delta().x / ScrollBar::PAGE_DIVISOR);
  238. }
  239. if (v_scroll_enabled) {
  240. v_scroll->scroll(v_scroll->get_page() * pan_gesture->get_delta().y / ScrollBar::PAGE_DIVISOR);
  241. }
  242. if (v_scroll->get_value() != prev_v_scroll || h_scroll->get_value() != prev_h_scroll) {
  243. accept_event(); // Accept event if scroll changed.
  244. }
  245. return;
  246. }
  247. }
  248. void ScrollContainer::_update_scrollbar_position() {
  249. if (!_updating_scrollbars) {
  250. return;
  251. }
  252. Rect2 margins = _get_margins();
  253. Size2 hmin = h_scroll->is_visible() ? h_scroll->get_combined_minimum_size() : Size2();
  254. Size2 vmin = v_scroll->is_visible() ? v_scroll->get_combined_minimum_size() : Size2();
  255. int lmar = is_layout_rtl() ? margins.size.x : margins.position.x;
  256. int rmar = is_layout_rtl() ? margins.position.x : margins.size.x;
  257. h_scroll->set_anchor_and_offset(SIDE_LEFT, ANCHOR_BEGIN, lmar);
  258. h_scroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, -rmar - vmin.width);
  259. h_scroll->set_anchor_and_offset(SIDE_TOP, ANCHOR_END, -hmin.height - margins.size.y);
  260. h_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, -margins.size.y);
  261. v_scroll->set_anchor_and_offset(SIDE_LEFT, ANCHOR_END, -vmin.width - rmar);
  262. v_scroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, -rmar);
  263. v_scroll->set_anchor_and_offset(SIDE_TOP, ANCHOR_BEGIN, margins.position.y);
  264. v_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, -hmin.height - margins.size.y);
  265. _updating_scrollbars = false;
  266. }
  267. void ScrollContainer::_gui_focus_changed(Control *p_control) {
  268. if (follow_focus && is_ancestor_of(p_control)) {
  269. ensure_control_visible(p_control);
  270. }
  271. if (draw_focus_border) {
  272. const bool _should_draw_focus_border = has_focus(true) || child_has_focus();
  273. if (focus_border_is_drawn != _should_draw_focus_border) {
  274. queue_redraw();
  275. }
  276. }
  277. }
  278. void ScrollContainer::ensure_control_visible(Control *p_control) {
  279. ERR_FAIL_COND_MSG(!is_ancestor_of(p_control), "Must be an ancestor of the control.");
  280. // Just eliminate the rotation of this ScrollContainer.
  281. Transform2D other_in_this = get_global_transform().affine_inverse() * p_control->get_global_transform();
  282. Size2 size = get_size();
  283. Rect2 other_rect = other_in_this.xform(Rect2(Point2(), p_control->get_size()));
  284. float side_margin = v_scroll->is_visible() ? v_scroll->get_size().x : 0.0f;
  285. float bottom_margin = h_scroll->is_visible() ? h_scroll->get_size().y : 0.0f;
  286. 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)),
  287. MAX(MIN(other_rect.position.y, 0.0f), other_rect.position.y + other_rect.size.y - size.y + bottom_margin));
  288. set_h_scroll(get_h_scroll() + diff.x);
  289. set_v_scroll(get_v_scroll() + diff.y);
  290. }
  291. void ScrollContainer::_reposition_children() {
  292. _update_scrollbars();
  293. _update_scroll_hints();
  294. Rect2 margins = _get_margins();
  295. Size2 size = get_size();
  296. size -= margins.position + margins.size;
  297. Point2 ofs = margins.position;
  298. bool rtl = is_layout_rtl();
  299. bool reserve_vscroll = _is_v_scroll_visible() || vertical_scroll_mode == SCROLL_MODE_RESERVE;
  300. if (_is_h_scroll_visible() || horizontal_scroll_mode == SCROLL_MODE_RESERVE) {
  301. size.y -= h_scroll->get_minimum_size().y + theme_cache.scrollbar_v_separation;
  302. }
  303. if (reserve_vscroll) {
  304. int width = v_scroll->get_minimum_size().x + theme_cache.scrollbar_h_separation;
  305. size.x -= width;
  306. if (rtl) {
  307. ofs.x += width;
  308. }
  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. r.position = r.position.floor();
  325. fit_child_in_rect(c, r);
  326. }
  327. if (draw_focus_border) {
  328. focus_panel->set_position(Vector2(0, 0));
  329. focus_panel->set_size(get_size());
  330. }
  331. queue_redraw();
  332. }
  333. void ScrollContainer::_accessibility_action_scroll_set(const Variant &p_data) {
  334. const Point2 &pos = p_data;
  335. h_scroll->set_value(pos.x);
  336. v_scroll->set_value(pos.y);
  337. }
  338. void ScrollContainer::_accessibility_action_scroll_up(const Variant &p_data) {
  339. if ((DisplayServer::AccessibilityScrollUnit)p_data == DisplayServer::SCROLL_UNIT_ITEM) {
  340. v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page() / ScrollBar::PAGE_DIVISOR);
  341. } else {
  342. v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page());
  343. }
  344. }
  345. void ScrollContainer::_accessibility_action_scroll_down(const Variant &p_data) {
  346. if ((DisplayServer::AccessibilityScrollUnit)p_data == DisplayServer::SCROLL_UNIT_ITEM) {
  347. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() / ScrollBar::PAGE_DIVISOR);
  348. } else {
  349. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page());
  350. }
  351. }
  352. void ScrollContainer::_accessibility_action_scroll_left(const Variant &p_data) {
  353. if ((DisplayServer::AccessibilityScrollUnit)p_data == DisplayServer::SCROLL_UNIT_ITEM) {
  354. h_scroll->set_value(h_scroll->get_value() - h_scroll->get_page() / ScrollBar::PAGE_DIVISOR);
  355. } else {
  356. h_scroll->set_value(h_scroll->get_value() - h_scroll->get_page());
  357. }
  358. }
  359. void ScrollContainer::_accessibility_action_scroll_right(const Variant &p_data) {
  360. if ((DisplayServer::AccessibilityScrollUnit)p_data == DisplayServer::SCROLL_UNIT_ITEM) {
  361. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() / ScrollBar::PAGE_DIVISOR);
  362. } else {
  363. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page());
  364. }
  365. }
  366. void ScrollContainer::_notification(int p_what) {
  367. switch (p_what) {
  368. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  369. RID ae = get_accessibility_element();
  370. ERR_FAIL_COND(ae.is_null());
  371. DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_SCROLL_VIEW);
  372. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_DOWN, callable_mp(this, &ScrollContainer::_accessibility_action_scroll_down));
  373. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_LEFT, callable_mp(this, &ScrollContainer::_accessibility_action_scroll_left));
  374. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_RIGHT, callable_mp(this, &ScrollContainer::_accessibility_action_scroll_right));
  375. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_UP, callable_mp(this, &ScrollContainer::_accessibility_action_scroll_up));
  376. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SET_SCROLL_OFFSET, callable_mp(this, &ScrollContainer::_accessibility_action_scroll_set));
  377. } break;
  378. case NOTIFICATION_THEME_CHANGED: {
  379. _update_scroll_hints();
  380. [[fallthrough]];
  381. }
  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(true) || 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. Rect2 margins = _get_margins();
  494. Size2 size = get_size();
  495. size -= margins.position + margins.size;
  496. Size2 hmin = h_scroll->get_combined_minimum_size();
  497. Size2 vmin = v_scroll->get_combined_minimum_size();
  498. 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));
  499. 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));
  500. h_scroll->set_max(largest_child_min_size.width);
  501. h_scroll->set_page(_is_v_scroll_visible() ? size.width - vmin.width : size.width);
  502. v_scroll->set_max(largest_child_min_size.height);
  503. v_scroll->set_page(_is_h_scroll_visible() ? size.height - hmin.height : size.height);
  504. // Avoid scrollbar overlapping.
  505. _updating_scrollbars = true;
  506. callable_mp(this, &ScrollContainer::_update_scrollbar_position).call_deferred();
  507. }
  508. void ScrollContainer::_update_scroll_hints() {
  509. Size2 size = get_size();
  510. Rect2 margins = _get_margins();
  511. Size2 scroll_size = size - margins.position - margins.size;
  512. float v_scroll_value = v_scroll->get_value();
  513. bool v_scroll_below_max = v_scroll_value < (largest_child_min_size.height - scroll_size.height - 1);
  514. bool show_vertical_hints = v_scroll_value > 1 || v_scroll_below_max;
  515. float h_scroll_value = h_scroll->get_value();
  516. bool h_scroll_below_max = h_scroll_value < (largest_child_min_size.width - scroll_size.width - 1);
  517. bool show_horizontal_hints = h_scroll_value > 1 || h_scroll_below_max;
  518. bool rtl = is_layout_rtl();
  519. if (show_vertical_hints) {
  520. scroll_hint_top_left->set_texture(theme_cache.scroll_hint_vertical);
  521. scroll_hint_top_left->set_modulate(theme_cache.scroll_hint_vertical_color);
  522. 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);
  523. scroll_hint_top_left->set_anchor_and_offset(SIDE_LEFT, ANCHOR_BEGIN, rtl ? -size.x : 0);
  524. scroll_hint_top_left->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, rtl ? 0 : size.x);
  525. scroll_hint_top_left->set_anchor_and_offset(SIDE_TOP, ANCHOR_BEGIN, 0);
  526. scroll_hint_top_left->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_BEGIN, theme_cache.scroll_hint_vertical->get_height());
  527. scroll_hint_bottom_right->set_flip_h(false);
  528. scroll_hint_bottom_right->set_flip_v(true);
  529. scroll_hint_bottom_right->set_texture(theme_cache.scroll_hint_vertical);
  530. scroll_hint_bottom_right->set_modulate(theme_cache.scroll_hint_vertical_color);
  531. 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);
  532. scroll_hint_bottom_right->set_anchor_and_offset(SIDE_LEFT, ANCHOR_BEGIN, rtl ? -size.x : 0);
  533. scroll_hint_bottom_right->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, rtl ? 0 : size.x);
  534. scroll_hint_bottom_right->set_anchor_and_offset(SIDE_TOP, ANCHOR_END, -theme_cache.scroll_hint_vertical->get_height());
  535. scroll_hint_bottom_right->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0);
  536. } else {
  537. scroll_hint_top_left->set_texture(theme_cache.scroll_hint_horizontal);
  538. scroll_hint_top_left->set_modulate(theme_cache.scroll_hint_horizontal_color);
  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_modulate(theme_cache.scroll_hint_horizontal_color);
  548. 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);
  549. scroll_hint_bottom_right->set_anchor_and_offset(SIDE_LEFT, ANCHOR_END, rtl ? -size.x : -theme_cache.scroll_hint_horizontal->get_width());
  550. scroll_hint_bottom_right->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, rtl ? (-size.x + theme_cache.scroll_hint_horizontal->get_width()) : 0);
  551. scroll_hint_bottom_right->set_anchor_and_offset(SIDE_TOP, ANCHOR_BEGIN, 0);
  552. scroll_hint_bottom_right->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0);
  553. }
  554. }
  555. void ScrollContainer::_scroll_moved(float) {
  556. queue_sort();
  557. }
  558. void ScrollContainer::set_h_scroll(int p_pos) {
  559. h_scroll->set_value(p_pos);
  560. _cancel_drag();
  561. }
  562. int ScrollContainer::get_h_scroll() const {
  563. return h_scroll->get_value();
  564. }
  565. void ScrollContainer::set_v_scroll(int p_pos) {
  566. v_scroll->set_value(p_pos);
  567. _cancel_drag();
  568. }
  569. int ScrollContainer::get_v_scroll() const {
  570. return v_scroll->get_value();
  571. }
  572. void ScrollContainer::set_horizontal_custom_step(float p_custom_step) {
  573. h_scroll->set_custom_step(p_custom_step);
  574. }
  575. float ScrollContainer::get_horizontal_custom_step() const {
  576. return h_scroll->get_custom_step();
  577. }
  578. void ScrollContainer::set_vertical_custom_step(float p_custom_step) {
  579. v_scroll->set_custom_step(p_custom_step);
  580. }
  581. float ScrollContainer::get_vertical_custom_step() const {
  582. return v_scroll->get_custom_step();
  583. }
  584. void ScrollContainer::set_horizontal_scroll_mode(ScrollMode p_mode) {
  585. if (horizontal_scroll_mode == p_mode) {
  586. return;
  587. }
  588. horizontal_scroll_mode = p_mode;
  589. update_minimum_size();
  590. queue_sort();
  591. }
  592. ScrollContainer::ScrollMode ScrollContainer::get_horizontal_scroll_mode() const {
  593. return horizontal_scroll_mode;
  594. }
  595. void ScrollContainer::set_vertical_scroll_mode(ScrollMode p_mode) {
  596. if (vertical_scroll_mode == p_mode) {
  597. return;
  598. }
  599. vertical_scroll_mode = p_mode;
  600. update_minimum_size();
  601. queue_sort();
  602. }
  603. ScrollContainer::ScrollMode ScrollContainer::get_vertical_scroll_mode() const {
  604. return vertical_scroll_mode;
  605. }
  606. int ScrollContainer::get_deadzone() const {
  607. return deadzone;
  608. }
  609. void ScrollContainer::set_deadzone(int p_deadzone) {
  610. deadzone = p_deadzone;
  611. }
  612. void ScrollContainer::set_scroll_hint_mode(ScrollHintMode p_mode) {
  613. if (scroll_hint_mode == p_mode) {
  614. return;
  615. }
  616. scroll_hint_mode = p_mode;
  617. _update_scroll_hints();
  618. }
  619. ScrollContainer::ScrollHintMode ScrollContainer::get_scroll_hint_mode() const {
  620. return scroll_hint_mode;
  621. }
  622. void ScrollContainer::set_tile_scroll_hint(bool p_enable) {
  623. if (tile_scroll_hint == p_enable) {
  624. return;
  625. }
  626. scroll_hint_top_left->set_stretch_mode(p_enable ? TextureRect::STRETCH_TILE : TextureRect::STRETCH_SCALE);
  627. scroll_hint_bottom_right->set_stretch_mode(p_enable ? TextureRect::STRETCH_TILE : TextureRect::STRETCH_SCALE);
  628. tile_scroll_hint = p_enable;
  629. }
  630. bool ScrollContainer::is_scroll_hint_tiled() {
  631. return tile_scroll_hint;
  632. }
  633. bool ScrollContainer::is_following_focus() const {
  634. return follow_focus;
  635. }
  636. void ScrollContainer::set_follow_focus(bool p_follow) {
  637. follow_focus = p_follow;
  638. }
  639. PackedStringArray ScrollContainer::get_configuration_warnings() const {
  640. PackedStringArray warnings = Container::get_configuration_warnings();
  641. int found = 0;
  642. for (int i = 0; i < get_child_count(); i++) {
  643. Control *c = as_sortable_control(get_child(i), SortableVisibilityMode::VISIBLE);
  644. if (!c || c == h_scroll || c == v_scroll || c == focus_panel || c == scroll_hint_top_left || c == scroll_hint_bottom_right) {
  645. continue;
  646. }
  647. found++;
  648. }
  649. if (found != 1) {
  650. 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."));
  651. }
  652. return warnings;
  653. }
  654. void ScrollContainer::set_scroll_on_drag_hover(bool p_scroll) {
  655. scroll_on_drag_hover = p_scroll;
  656. }
  657. HScrollBar *ScrollContainer::get_h_scroll_bar() {
  658. return h_scroll;
  659. }
  660. VScrollBar *ScrollContainer::get_v_scroll_bar() {
  661. return v_scroll;
  662. }
  663. void ScrollContainer::_bind_methods() {
  664. ClassDB::bind_method(D_METHOD("set_h_scroll", "value"), &ScrollContainer::set_h_scroll);
  665. ClassDB::bind_method(D_METHOD("get_h_scroll"), &ScrollContainer::get_h_scroll);
  666. ClassDB::bind_method(D_METHOD("set_v_scroll", "value"), &ScrollContainer::set_v_scroll);
  667. ClassDB::bind_method(D_METHOD("get_v_scroll"), &ScrollContainer::get_v_scroll);
  668. ClassDB::bind_method(D_METHOD("set_horizontal_custom_step", "value"), &ScrollContainer::set_horizontal_custom_step);
  669. ClassDB::bind_method(D_METHOD("get_horizontal_custom_step"), &ScrollContainer::get_horizontal_custom_step);
  670. ClassDB::bind_method(D_METHOD("set_vertical_custom_step", "value"), &ScrollContainer::set_vertical_custom_step);
  671. ClassDB::bind_method(D_METHOD("get_vertical_custom_step"), &ScrollContainer::get_vertical_custom_step);
  672. ClassDB::bind_method(D_METHOD("set_horizontal_scroll_mode", "enable"), &ScrollContainer::set_horizontal_scroll_mode);
  673. ClassDB::bind_method(D_METHOD("get_horizontal_scroll_mode"), &ScrollContainer::get_horizontal_scroll_mode);
  674. ClassDB::bind_method(D_METHOD("set_vertical_scroll_mode", "enable"), &ScrollContainer::set_vertical_scroll_mode);
  675. ClassDB::bind_method(D_METHOD("get_vertical_scroll_mode"), &ScrollContainer::get_vertical_scroll_mode);
  676. ClassDB::bind_method(D_METHOD("set_deadzone", "deadzone"), &ScrollContainer::set_deadzone);
  677. ClassDB::bind_method(D_METHOD("get_deadzone"), &ScrollContainer::get_deadzone);
  678. ClassDB::bind_method(D_METHOD("set_scroll_hint_mode", "scroll_hint_mode"), &ScrollContainer::set_scroll_hint_mode);
  679. ClassDB::bind_method(D_METHOD("get_scroll_hint_mode"), &ScrollContainer::get_scroll_hint_mode);
  680. ClassDB::bind_method(D_METHOD("set_tile_scroll_hint", "tile_scroll_hint"), &ScrollContainer::set_tile_scroll_hint);
  681. ClassDB::bind_method(D_METHOD("is_scroll_hint_tiled"), &ScrollContainer::is_scroll_hint_tiled);
  682. ClassDB::bind_method(D_METHOD("set_follow_focus", "enabled"), &ScrollContainer::set_follow_focus);
  683. ClassDB::bind_method(D_METHOD("is_following_focus"), &ScrollContainer::is_following_focus);
  684. ClassDB::bind_method(D_METHOD("get_h_scroll_bar"), &ScrollContainer::get_h_scroll_bar);
  685. ClassDB::bind_method(D_METHOD("get_v_scroll_bar"), &ScrollContainer::get_v_scroll_bar);
  686. ClassDB::bind_method(D_METHOD("ensure_control_visible", "control"), &ScrollContainer::ensure_control_visible);
  687. ClassDB::bind_method(D_METHOD("set_draw_focus_border", "draw"), &ScrollContainer::set_draw_focus_border);
  688. ClassDB::bind_method(D_METHOD("get_draw_focus_border"), &ScrollContainer::get_draw_focus_border);
  689. ADD_SIGNAL(MethodInfo("scroll_started"));
  690. ADD_SIGNAL(MethodInfo("scroll_ended"));
  691. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "follow_focus"), "set_follow_focus", "is_following_focus");
  692. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_focus_border"), "set_draw_focus_border", "get_draw_focus_border");
  693. ADD_GROUP("Scrollbar", "");
  694. ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_horizontal", PROPERTY_HINT_NONE, "suffix:px"), "set_h_scroll", "get_h_scroll");
  695. ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_vertical", PROPERTY_HINT_NONE, "suffix:px"), "set_v_scroll", "get_v_scroll");
  696. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "scroll_horizontal_custom_step", PROPERTY_HINT_RANGE, "-1,4096,suffix:px"), "set_horizontal_custom_step", "get_horizontal_custom_step");
  697. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "scroll_vertical_custom_step", PROPERTY_HINT_RANGE, "-1,4096,suffix:px"), "set_vertical_custom_step", "get_vertical_custom_step");
  698. 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");
  699. 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");
  700. ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_deadzone"), "set_deadzone", "get_deadzone");
  701. ADD_GROUP("Scroll Hint", "");
  702. 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");
  703. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "tile_scroll_hint"), "set_tile_scroll_hint", "is_scroll_hint_tiled");
  704. BIND_ENUM_CONSTANT(SCROLL_MODE_DISABLED);
  705. BIND_ENUM_CONSTANT(SCROLL_MODE_AUTO);
  706. BIND_ENUM_CONSTANT(SCROLL_MODE_SHOW_ALWAYS);
  707. BIND_ENUM_CONSTANT(SCROLL_MODE_SHOW_NEVER);
  708. BIND_ENUM_CONSTANT(SCROLL_MODE_RESERVE);
  709. BIND_ENUM_CONSTANT(SCROLL_HINT_MODE_DISABLED);
  710. BIND_ENUM_CONSTANT(SCROLL_HINT_MODE_ALL);
  711. BIND_ENUM_CONSTANT(SCROLL_HINT_MODE_TOP_AND_LEFT);
  712. BIND_ENUM_CONSTANT(SCROLL_HINT_MODE_BOTTOM_AND_RIGHT);
  713. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, ScrollContainer, scrollbar_h_separation);
  714. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, ScrollContainer, scrollbar_v_separation);
  715. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ScrollContainer, panel_style, "panel");
  716. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ScrollContainer, focus_style, "focus");
  717. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ScrollContainer, scroll_hint_vertical);
  718. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ScrollContainer, scroll_hint_horizontal);
  719. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, ScrollContainer, scroll_hint_vertical_color);
  720. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, ScrollContainer, scroll_hint_horizontal_color);
  721. GLOBAL_DEF("gui/common/default_scroll_deadzone", 0);
  722. }
  723. void ScrollContainer::set_draw_focus_border(bool p_draw) {
  724. if (draw_focus_border == p_draw) {
  725. return;
  726. }
  727. draw_focus_border = p_draw;
  728. if (is_ready()) {
  729. _reposition_children();
  730. }
  731. }
  732. bool ScrollContainer::get_draw_focus_border() {
  733. return draw_focus_border;
  734. }
  735. bool ScrollContainer::child_has_focus() {
  736. const Control *focus_owner = get_viewport() ? get_viewport()->gui_get_focus_owner() : nullptr;
  737. return focus_owner && focus_owner->has_focus(true) && is_ancestor_of(focus_owner);
  738. }
  739. ScrollContainer::ScrollContainer() {
  740. scroll_hint_top_left = memnew(TextureRect);
  741. scroll_hint_top_left->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
  742. scroll_hint_top_left->set_mouse_filter(MOUSE_FILTER_IGNORE);
  743. scroll_hint_top_left->hide();
  744. add_child(scroll_hint_top_left, false, INTERNAL_MODE_BACK);
  745. scroll_hint_bottom_right = memnew(TextureRect);
  746. scroll_hint_bottom_right->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
  747. scroll_hint_bottom_right->set_mouse_filter(MOUSE_FILTER_IGNORE);
  748. scroll_hint_bottom_right->hide();
  749. add_child(scroll_hint_bottom_right, false, INTERNAL_MODE_BACK);
  750. h_scroll = memnew(HScrollBar);
  751. h_scroll->set_name("_h_scroll");
  752. add_child(h_scroll, false, INTERNAL_MODE_BACK);
  753. h_scroll->connect(SceneStringName(value_changed), callable_mp(this, &ScrollContainer::_scroll_moved));
  754. v_scroll = memnew(VScrollBar);
  755. v_scroll->set_name("_v_scroll");
  756. add_child(v_scroll, false, INTERNAL_MODE_BACK);
  757. v_scroll->connect(SceneStringName(value_changed), callable_mp(this, &ScrollContainer::_scroll_moved));
  758. // We need to use a PanelContainer for the focus style instead of just drawing it directly with RenderingService
  759. // due to a clippling issues. The Control that is being scrolled will be over the focus border because both will be
  760. // drawn on the same CanvasItem. If we decide to ignore clipping, the focus border will be drawn even over other
  761. // CanvasItems.
  762. focus_panel = memnew(PanelContainer);
  763. focus_panel->set_name("_focus");
  764. focus_panel->set_mouse_filter(MOUSE_FILTER_IGNORE);
  765. focus_panel->set_focus_mode(FOCUS_NONE);
  766. focus_panel->set_visible(draw_focus_border);
  767. add_child(focus_panel, false, INTERNAL_MODE_BACK);
  768. deadzone = GLOBAL_GET_CACHED(int, "gui/common/default_scroll_deadzone");
  769. set_clip_contents(true);
  770. }