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