| 1 | /**************************************************************************/ |
| 2 | /* scroll_bar.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_bar.h" |
| 32 | |
| 33 | #include "scene/main/window.h" |
| 34 | #include "scene/theme/theme_db.h" |
| 35 | |
| 36 | bool ScrollBar::focus_by_default = false; |
| 37 | |
| 38 | void ScrollBar::set_can_focus_by_default(bool p_can_focus) { |
| 39 | focus_by_default = p_can_focus; |
| 40 | } |
| 41 | |
| 42 | void ScrollBar::gui_input(const Ref<InputEvent> &p_event) { |
| 43 | ERR_FAIL_COND(p_event.is_null()); |
| 44 | |
| 45 | Ref<InputEventMouseMotion> m = p_event; |
| 46 | |
| 47 | Ref<InputEventMouseButton> b = p_event; |
| 48 | |
| 49 | Ref<InputEventPanGesture> pg = p_event; |
| 50 | |
| 51 | if (pg.is_valid()) { |
| 52 | accept_event(); |
| 53 | |
| 54 | if (orientation == HORIZONTAL) { |
| 55 | if (pg->get_delta().x != 0) { |
| 56 | if (pg->get_delta().x < 0) { |
| 57 | scroll(-MAX(Math::abs(pg->get_delta().x), get_step())); |
| 58 | } |
| 59 | if (pg->get_delta().x > 0) { |
| 60 | scroll(MAX(pg->get_delta().x, get_step())); |
| 61 | } |
| 62 | } else if (pg->get_delta().y != 0) { |
| 63 | if (pg->get_delta().y < 0) { |
| 64 | scroll(-MAX(Math::abs(pg->get_delta().y), get_step())); |
| 65 | } |
| 66 | if (pg->get_delta().y > 0) { |
| 67 | scroll(MAX(pg->get_delta().y, get_step())); |
| 68 | } |
| 69 | } |
| 70 | } else { |
| 71 | if (pg->get_delta().y < 0) { |
| 72 | scroll(-MAX(Math::abs(pg->get_delta().y), get_step())); |
| 73 | } |
| 74 | if (pg->get_delta().y > 0) { |
| 75 | scroll(MAX(pg->get_delta().y, get_step())); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | if (b.is_valid()) { |
| 81 | accept_event(); |
| 82 | |
| 83 | if (b->get_button_index() == MouseButton::WHEEL_DOWN && b->is_pressed()) { |
| 84 | double change = ((get_page() != 0.0) ? get_page() / PAGE_DIVISOR : (get_max() - get_min()) / 16.0) * b->get_factor(); |
| 85 | scroll(MAX(change, get_step())); |
| 86 | accept_event(); |
| 87 | } |
| 88 | |
| 89 | if (b->get_button_index() == MouseButton::WHEEL_UP && b->is_pressed()) { |
| 90 | double change = ((get_page() != 0.0) ? get_page() / PAGE_DIVISOR : (get_max() - get_min()) / 16.0) * b->get_factor(); |
| 91 | scroll(-MAX(change, get_step())); |
| 92 | accept_event(); |
| 93 | } |
| 94 | |
| 95 | if (b->get_button_index() != MouseButton::LEFT) { |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | if (b->is_pressed()) { |
| 100 | double ofs = orientation == VERTICAL ? b->get_position().y : b->get_position().x; |
| 101 | Ref<Texture2D> decr = theme_cache.decrement_icon; |
| 102 | Ref<Texture2D> incr = theme_cache.increment_icon; |
| 103 | |
| 104 | double decr_size = orientation == VERTICAL ? decr->get_height() : decr->get_width(); |
| 105 | double incr_size = orientation == VERTICAL ? incr->get_height() : incr->get_width(); |
| 106 | double grabber_ofs = get_grabber_offset(); |
| 107 | double grabber_size = get_grabber_size(); |
| 108 | double total = orientation == VERTICAL ? get_size().height : get_size().width; |
| 109 | |
| 110 | if (ofs < decr_size) { |
| 111 | decr_active = true; |
| 112 | scroll(-(custom_step >= 0 ? custom_step : get_step())); |
| 113 | queue_redraw(); |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | if (ofs > total - incr_size) { |
| 118 | incr_active = true; |
| 119 | scroll(custom_step >= 0 ? custom_step : get_step()); |
| 120 | queue_redraw(); |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | ofs -= decr_size + theme_cache.scroll_style->get_margin(orientation == VERTICAL ? SIDE_TOP : SIDE_LEFT); |
| 125 | |
| 126 | if (ofs < grabber_ofs) { |
| 127 | if (scrolling) { |
| 128 | target_scroll = CLAMP(target_scroll - get_page(), get_min(), get_max() - get_page()); |
| 129 | } else { |
| 130 | double change = get_page() != 0.0 ? get_page() : (get_max() - get_min()) / 16.0; |
| 131 | target_scroll = CLAMP(get_value() - change, get_min(), get_max() - get_page()); |
| 132 | } |
| 133 | |
| 134 | if (smooth_scroll_enabled) { |
| 135 | scrolling = true; |
| 136 | set_process_internal(true); |
| 137 | } else { |
| 138 | scroll_to(target_scroll); |
| 139 | } |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | ofs -= grabber_ofs; |
| 144 | |
| 145 | if (ofs < grabber_size) { |
| 146 | drag.active = true; |
| 147 | drag.pos_at_click = grabber_ofs + ofs; |
| 148 | drag.value_at_click = get_as_ratio(); |
| 149 | queue_redraw(); |
| 150 | } else { |
| 151 | if (scrolling) { |
| 152 | target_scroll = CLAMP(target_scroll + get_page(), get_min(), get_max() - get_page()); |
| 153 | } else { |
| 154 | double change = get_page() != 0.0 ? get_page() : (get_max() - get_min()) / 16.0; |
| 155 | target_scroll = CLAMP(get_value() + change, get_min(), get_max() - get_page()); |
| 156 | } |
| 157 | |
| 158 | if (smooth_scroll_enabled) { |
| 159 | scrolling = true; |
| 160 | set_process_internal(true); |
| 161 | } else { |
| 162 | scroll_to(target_scroll); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | } else { |
| 167 | incr_active = false; |
| 168 | decr_active = false; |
| 169 | drag.active = false; |
| 170 | queue_redraw(); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | if (m.is_valid()) { |
| 175 | accept_event(); |
| 176 | |
| 177 | if (drag.active) { |
| 178 | double ofs = orientation == VERTICAL ? m->get_position().y : m->get_position().x; |
| 179 | Ref<Texture2D> decr = theme_cache.decrement_icon; |
| 180 | |
| 181 | double decr_size = orientation == VERTICAL ? decr->get_height() : decr->get_width(); |
| 182 | ofs -= decr_size + theme_cache.scroll_style->get_margin(orientation == VERTICAL ? SIDE_TOP : SIDE_LEFT); |
| 183 | |
| 184 | double diff = (ofs - drag.pos_at_click) / get_area_size(); |
| 185 | |
| 186 | double prev_scroll = get_value(); |
| 187 | |
| 188 | set_as_ratio(drag.value_at_click + diff); |
| 189 | |
| 190 | if (!Math::is_equal_approx(prev_scroll, get_value())) { |
| 191 | emit_signal(SNAME("scrolling")); |
| 192 | } |
| 193 | } else { |
| 194 | double ofs = orientation == VERTICAL ? m->get_position().y : m->get_position().x; |
| 195 | Ref<Texture2D> decr = theme_cache.decrement_icon; |
| 196 | Ref<Texture2D> incr = theme_cache.increment_icon; |
| 197 | |
| 198 | double decr_size = orientation == VERTICAL ? decr->get_height() : decr->get_width(); |
| 199 | double incr_size = orientation == VERTICAL ? incr->get_height() : incr->get_width(); |
| 200 | double total = orientation == VERTICAL ? get_size().height : get_size().width; |
| 201 | |
| 202 | HighlightStatus new_hilite; |
| 203 | |
| 204 | if (ofs < decr_size) { |
| 205 | new_hilite = HIGHLIGHT_DECR; |
| 206 | |
| 207 | } else if (ofs > total - incr_size) { |
| 208 | new_hilite = HIGHLIGHT_INCR; |
| 209 | |
| 210 | } else { |
| 211 | new_hilite = HIGHLIGHT_RANGE; |
| 212 | } |
| 213 | |
| 214 | if (new_hilite != highlight) { |
| 215 | highlight = new_hilite; |
| 216 | queue_redraw(); |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | if (p_event->is_pressed()) { |
| 222 | if (p_event->is_action("ui_left", true)) { |
| 223 | if (orientation != HORIZONTAL) { |
| 224 | return; |
| 225 | } |
| 226 | scroll(-(custom_step >= 0 ? custom_step : get_step())); |
| 227 | |
| 228 | } else if (p_event->is_action("ui_right", true)) { |
| 229 | if (orientation != HORIZONTAL) { |
| 230 | return; |
| 231 | } |
| 232 | scroll(custom_step >= 0 ? custom_step : get_step()); |
| 233 | |
| 234 | } else if (p_event->is_action("ui_up", true)) { |
| 235 | if (orientation != VERTICAL) { |
| 236 | return; |
| 237 | } |
| 238 | |
| 239 | scroll(-(custom_step >= 0 ? custom_step : get_step())); |
| 240 | |
| 241 | } else if (p_event->is_action("ui_down", true)) { |
| 242 | if (orientation != VERTICAL) { |
| 243 | return; |
| 244 | } |
| 245 | scroll(custom_step >= 0 ? custom_step : get_step()); |
| 246 | |
| 247 | } else if (p_event->is_action("ui_home", true)) { |
| 248 | scroll_to(get_min()); |
| 249 | |
| 250 | } else if (p_event->is_action("ui_end", true)) { |
| 251 | scroll_to(get_max()); |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | void ScrollBar::_notification(int p_what) { |
| 257 | switch (p_what) { |
| 258 | case NOTIFICATION_ACCESSIBILITY_UPDATE: { |
| 259 | RID ae = get_accessibility_element(); |
| 260 | ERR_FAIL_COND(ae.is_null()); |
| 261 | |
| 262 | DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_SCROLL_BAR); |
| 263 | } break; |
| 264 | |
| 265 | case NOTIFICATION_DRAW: { |
| 266 | RID ci = get_canvas_item(); |
| 267 | |
| 268 | Ref<Texture2D> decr, incr; |
| 269 | |
| 270 | if (decr_active) { |
| 271 | decr = theme_cache.decrement_pressed_icon; |
| 272 | } else if (highlight == HIGHLIGHT_DECR) { |
| 273 | decr = theme_cache.decrement_hl_icon; |
| 274 | } else { |
| 275 | decr = theme_cache.decrement_icon; |
| 276 | } |
| 277 | |
| 278 | if (incr_active) { |
| 279 | incr = theme_cache.increment_pressed_icon; |
| 280 | } else if (highlight == HIGHLIGHT_INCR) { |
| 281 | incr = theme_cache.increment_hl_icon; |
| 282 | } else { |
| 283 | incr = theme_cache.increment_icon; |
| 284 | } |
| 285 | |
| 286 | Ref<StyleBox> grabber; |
| 287 | if (drag.active) { |
| 288 | grabber = theme_cache.grabber_pressed_style; |
| 289 | } else if (highlight == HIGHLIGHT_RANGE) { |
| 290 | grabber = theme_cache.grabber_hl_style; |
| 291 | } else { |
| 292 | grabber = theme_cache.grabber_style; |
| 293 | } |
| 294 | |
| 295 | Point2 ofs; |
| 296 | |
| 297 | decr->draw(ci, Point2()); |
| 298 | |
| 299 | if (orientation == HORIZONTAL) { |
| 300 | ofs.x += decr->get_width(); |
| 301 | } else { |
| 302 | ofs.y += decr->get_height(); |
| 303 | } |
| 304 | |
| 305 | Size2 area = get_size(); |
| 306 | |
| 307 | if (orientation == HORIZONTAL) { |
| 308 | area.width -= incr->get_width() + decr->get_width(); |
| 309 | } else { |
| 310 | area.height -= incr->get_height() + decr->get_height(); |
| 311 | } |
| 312 | |
| 313 | if (has_focus(true)) { |
| 314 | theme_cache.scroll_focus_style->draw(ci, Rect2(ofs, area)); |
| 315 | } else { |
| 316 | theme_cache.scroll_style->draw(ci, Rect2(ofs, area)); |
| 317 | } |
| 318 | |
| 319 | if (orientation == HORIZONTAL) { |
| 320 | ofs.width += area.width; |
| 321 | } else { |
| 322 | ofs.height += area.height; |
| 323 | } |
| 324 | |
| 325 | incr->draw(ci, ofs); |
| 326 | Rect2 grabber_rect; |
| 327 | |
| 328 | if (orientation == HORIZONTAL) { |
| 329 | int padding_top = MAX(theme_cache.padding_top, 0); |
| 330 | int padding_bottom = MAX(theme_cache.padding_bottom, 0); |
| 331 | grabber_rect.size.width = get_grabber_size(); |
| 332 | grabber_rect.size.height = get_size().height - padding_top - padding_bottom; |
| 333 | grabber_rect.position.y = padding_top; |
| 334 | grabber_rect.position.x = get_grabber_offset() + decr->get_width() + theme_cache.scroll_style->get_margin(SIDE_LEFT); |
| 335 | } else { |
| 336 | int padding_left = MAX(theme_cache.padding_left, 0); |
| 337 | int padding_right = MAX(theme_cache.padding_right, 0); |
| 338 | grabber_rect.size.width = get_size().width - padding_left - padding_right; |
| 339 | grabber_rect.size.height = get_grabber_size(); |
| 340 | grabber_rect.position.y = get_grabber_offset() + decr->get_height() + theme_cache.scroll_style->get_margin(SIDE_TOP); |
| 341 | grabber_rect.position.x = padding_left; |
| 342 | } |
| 343 | |
| 344 | grabber->draw(ci, grabber_rect); |
| 345 | } break; |
| 346 | |
| 347 | case NOTIFICATION_ENTER_TREE: { |
| 348 | if (has_node(drag_node_path)) { |
| 349 | Node *n = get_node(drag_node_path); |
| 350 | drag_node = Object::cast_to<Control>(n); |
| 351 | } |
| 352 | |
| 353 | if (drag_node) { |
| 354 | drag_node->connect(SceneStringName(gui_input), callable_mp(this, &ScrollBar::_drag_node_input)); |
| 355 | drag_node->connect(SceneStringName(tree_exiting), callable_mp(this, &ScrollBar::_drag_node_exit), CONNECT_ONE_SHOT); |
| 356 | } |
| 357 | } break; |
| 358 | |
| 359 | case NOTIFICATION_EXIT_TREE: { |
| 360 | if (drag_node) { |
| 361 | drag_node->disconnect(SceneStringName(gui_input), callable_mp(this, &ScrollBar::_drag_node_input)); |
| 362 | drag_node->disconnect(SceneStringName(tree_exiting), callable_mp(this, &ScrollBar::_drag_node_exit)); |
| 363 | } |
| 364 | |
| 365 | drag_node = nullptr; |
| 366 | } break; |
| 367 | |
| 368 | case NOTIFICATION_INTERNAL_PROCESS: { |
| 369 | if (scrolling) { |
| 370 | if (get_value() != target_scroll) { |
| 371 | double target = target_scroll - get_value(); |
| 372 | double dist = Math::abs(target); |
| 373 | double vel = ((target / dist) * 500) * get_process_delta_time(); |
| 374 | |
| 375 | if (Math::abs(vel) >= dist) { |
| 376 | scroll_to(target_scroll); |
| 377 | scrolling = false; |
| 378 | set_process_internal(false); |
| 379 | } else { |
| 380 | scroll(vel); |
| 381 | } |
| 382 | } else { |
| 383 | scrolling = false; |
| 384 | set_process_internal(false); |
| 385 | } |
| 386 | |
| 387 | } else if (drag_node_touching) { |
| 388 | if (drag_node_touching_deaccel) { |
| 389 | Vector2 pos = Vector2(orientation == HORIZONTAL ? get_value() : 0, orientation == VERTICAL ? get_value() : 0); |
| 390 | pos += drag_node_speed * get_process_delta_time(); |
| 391 | |
| 392 | bool turnoff = false; |
| 393 | |
| 394 | if (orientation == HORIZONTAL) { |
| 395 | if (pos.x < 0) { |
| 396 | pos.x = 0; |
| 397 | turnoff = true; |
| 398 | } |
| 399 | |
| 400 | if (pos.x > (get_max() - get_page())) { |
| 401 | pos.x = get_max() - get_page(); |
| 402 | turnoff = true; |
| 403 | } |
| 404 | |
| 405 | scroll_to(pos.x); |
| 406 | |
| 407 | float sgn_x = drag_node_speed.x < 0 ? -1 : 1; |
| 408 | float val_x = Math::abs(drag_node_speed.x); |
| 409 | val_x -= 1000 * get_process_delta_time(); |
| 410 | |
| 411 | if (val_x < 0) { |
| 412 | turnoff = true; |
| 413 | } |
| 414 | |
| 415 | drag_node_speed.x = sgn_x * val_x; |
| 416 | |
| 417 | } else { |
| 418 | if (pos.y < 0) { |
| 419 | pos.y = 0; |
| 420 | turnoff = true; |
| 421 | } |
| 422 | |
| 423 | if (pos.y > (get_max() - get_page())) { |
| 424 | pos.y = get_max() - get_page(); |
| 425 | turnoff = true; |
| 426 | } |
| 427 | |
| 428 | scroll_to(pos.y); |
| 429 | |
| 430 | float sgn_y = drag_node_speed.y < 0 ? -1 : 1; |
| 431 | float val_y = Math::abs(drag_node_speed.y); |
| 432 | val_y -= 1000 * get_process_delta_time(); |
| 433 | |
| 434 | if (val_y < 0) { |
| 435 | turnoff = true; |
| 436 | } |
| 437 | drag_node_speed.y = sgn_y * val_y; |
| 438 | } |
| 439 | |
| 440 | if (turnoff) { |
| 441 | set_process_internal(false); |
| 442 | drag_node_touching = false; |
| 443 | drag_node_touching_deaccel = false; |
| 444 | } |
| 445 | |
| 446 | } else { |
| 447 | if (time_since_motion == 0 || time_since_motion > 0.1) { |
| 448 | Vector2 diff = drag_node_accum - last_drag_node_accum; |
| 449 | last_drag_node_accum = drag_node_accum; |
| 450 | drag_node_speed = diff / get_process_delta_time(); |
| 451 | } |
| 452 | |
| 453 | time_since_motion += get_process_delta_time(); |
| 454 | } |
| 455 | } |
| 456 | } break; |
| 457 | |
| 458 | case NOTIFICATION_VISIBILITY_CHANGED: { |
| 459 | if (!is_visible()) { |
| 460 | incr_active = false; |
| 461 | decr_active = false; |
| 462 | drag.active = false; |
| 463 | } |
| 464 | } break; |
| 465 | |
| 466 | case NOTIFICATION_MOUSE_EXIT: { |
| 467 | highlight = HIGHLIGHT_NONE; |
| 468 | queue_redraw(); |
| 469 | } break; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | double ScrollBar::get_grabber_min_size() const { |
| 474 | Ref<StyleBox> grabber = theme_cache.grabber_style; |
| 475 | Size2 gminsize = grabber->get_minimum_size(); |
| 476 | return (orientation == VERTICAL) ? gminsize.height : gminsize.width; |
| 477 | } |
| 478 | |
| 479 | double ScrollBar::get_grabber_size() const { |
| 480 | float range = get_max() - get_min(); |
| 481 | if (range <= 0) { |
| 482 | return 0; |
| 483 | } |
| 484 | |
| 485 | float page = (get_page() > 0) ? get_page() : 0; |
| 486 | double area_size = get_area_size(); |
| 487 | double grabber_size = page / range * area_size; |
| 488 | return grabber_size + get_grabber_min_size(); |
| 489 | } |
| 490 | |
| 491 | double ScrollBar::get_area_size() const { |
| 492 | switch (orientation) { |
| 493 | case VERTICAL: { |
| 494 | double area = get_size().height; |
| 495 | area -= theme_cache.scroll_style->get_minimum_size().height; |
| 496 | area -= theme_cache.increment_icon->get_height(); |
| 497 | area -= theme_cache.decrement_icon->get_height(); |
| 498 | area -= get_grabber_min_size(); |
| 499 | return area; |
| 500 | } break; |
| 501 | case HORIZONTAL: { |
| 502 | double area = get_size().width; |
| 503 | area -= theme_cache.scroll_style->get_minimum_size().width; |
| 504 | area -= theme_cache.increment_icon->get_width(); |
| 505 | area -= theme_cache.decrement_icon->get_width(); |
| 506 | area -= get_grabber_min_size(); |
| 507 | return area; |
| 508 | } break; |
| 509 | default: { |
| 510 | return 0.0; |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | double ScrollBar::get_grabber_offset() const { |
| 516 | return get_area_size() * get_as_ratio(); |
| 517 | } |
| 518 | |
| 519 | Size2 ScrollBar::get_minimum_size() const { |
| 520 | Ref<Texture2D> incr = theme_cache.increment_icon; |
| 521 | Ref<Texture2D> decr = theme_cache.decrement_icon; |
| 522 | Ref<StyleBox> bg = theme_cache.scroll_style; |
| 523 | Size2 minsize; |
| 524 | |
| 525 | if (orientation == VERTICAL) { |
| 526 | int padding_left = MAX(theme_cache.padding_left, 0); |
| 527 | int padding_right = MAX(theme_cache.padding_right, 0); |
| 528 | minsize.width = MAX(incr->get_size().width, bg->get_minimum_size().width); |
| 529 | minsize.width += padding_left + padding_right; |
| 530 | minsize.height += incr->get_size().height; |
| 531 | minsize.height += decr->get_size().height; |
| 532 | minsize.height += bg->get_minimum_size().height; |
| 533 | minsize.height += get_grabber_min_size(); |
| 534 | } |
| 535 | |
| 536 | if (orientation == HORIZONTAL) { |
| 537 | int padding_top = MAX(theme_cache.padding_top, 0); |
| 538 | int padding_bottom = MAX(theme_cache.padding_bottom, 0); |
| 539 | minsize.height = MAX(incr->get_size().height, bg->get_minimum_size().height); |
| 540 | minsize.height += padding_top + padding_bottom; |
| 541 | minsize.width += incr->get_size().width; |
| 542 | minsize.width += decr->get_size().width; |
| 543 | minsize.width += bg->get_minimum_size().width; |
| 544 | minsize.width += get_grabber_min_size(); |
| 545 | } |
| 546 | |
| 547 | return minsize; |
| 548 | } |
| 549 | |
| 550 | void ScrollBar::scroll(double p_amount) { |
| 551 | scroll_to(get_value() + p_amount); |
| 552 | } |
| 553 | |
| 554 | void ScrollBar::scroll_to(double p_position) { |
| 555 | double prev_scroll = get_value(); |
| 556 | set_value(p_position); |
| 557 | if (!Math::is_equal_approx(prev_scroll, get_value())) { |
| 558 | emit_signal(SNAME("scrolling")); |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | void ScrollBar::set_custom_step(float p_custom_step) { |
| 563 | custom_step = p_custom_step; |
| 564 | } |
| 565 | |
| 566 | float ScrollBar::get_custom_step() const { |
| 567 | return custom_step; |
| 568 | } |
| 569 | |
| 570 | void ScrollBar::_drag_node_exit() { |
| 571 | if (drag_node) { |
| 572 | drag_node->disconnect(SceneStringName(gui_input), callable_mp(this, &ScrollBar::_drag_node_input)); |
| 573 | } |
| 574 | drag_node = nullptr; |
| 575 | } |
| 576 | |
| 577 | void ScrollBar::_drag_node_input(const Ref<InputEvent> &p_input) { |
| 578 | if (!drag_node_enabled) { |
| 579 | return; |
| 580 | } |
| 581 | |
| 582 | Ref<InputEventMouseButton> mb = p_input; |
| 583 | |
| 584 | if (mb.is_valid()) { |
| 585 | if (mb->get_button_index() != MouseButton::LEFT) { |
| 586 | return; |
| 587 | } |
| 588 | |
| 589 | if (mb->is_pressed()) { |
| 590 | drag_node_speed = Vector2(); |
| 591 | drag_node_accum = Vector2(); |
| 592 | last_drag_node_accum = Vector2(); |
| 593 | drag_node_from = Vector2(orientation == HORIZONTAL ? get_value() : 0, orientation == VERTICAL ? get_value() : 0); |
| 594 | drag_node_touching = DisplayServer::get_singleton()->is_touchscreen_available(); |
| 595 | drag_node_touching_deaccel = false; |
| 596 | time_since_motion = 0; |
| 597 | |
| 598 | if (drag_node_touching) { |
| 599 | set_process_internal(true); |
| 600 | time_since_motion = 0; |
| 601 | } |
| 602 | |
| 603 | } else { |
| 604 | if (drag_node_touching) { |
| 605 | if (drag_node_speed == Vector2()) { |
| 606 | drag_node_touching_deaccel = false; |
| 607 | drag_node_touching = false; |
| 608 | set_process_internal(false); |
| 609 | } else { |
| 610 | drag_node_touching_deaccel = true; |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | Ref<InputEventMouseMotion> mm = p_input; |
| 617 | |
| 618 | if (mm.is_valid()) { |
| 619 | if (drag_node_touching && !drag_node_touching_deaccel) { |
| 620 | Vector2 motion = mm->get_relative(); |
| 621 | |
| 622 | drag_node_accum -= motion; |
| 623 | Vector2 diff = drag_node_from + drag_node_accum; |
| 624 | |
| 625 | if (orientation == HORIZONTAL) { |
| 626 | scroll_to(diff.x); |
| 627 | } |
| 628 | |
| 629 | if (orientation == VERTICAL) { |
| 630 | scroll_to(diff.y); |
| 631 | } |
| 632 | |
| 633 | time_since_motion = 0; |
| 634 | } |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | void ScrollBar::set_drag_node(const NodePath &p_path) { |
| 639 | if (is_inside_tree()) { |
| 640 | if (drag_node) { |
| 641 | drag_node->disconnect(SceneStringName(gui_input), callable_mp(this, &ScrollBar::_drag_node_input)); |
| 642 | drag_node->disconnect(SceneStringName(tree_exiting), callable_mp(this, &ScrollBar::_drag_node_exit)); |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | drag_node = nullptr; |
| 647 | drag_node_path = p_path; |
| 648 | |
| 649 | if (is_inside_tree()) { |
| 650 | if (has_node(p_path)) { |
| 651 | Node *n = get_node(p_path); |
| 652 | drag_node = Object::cast_to<Control>(n); |
| 653 | } |
| 654 | |
| 655 | if (drag_node) { |
| 656 | drag_node->connect(SceneStringName(gui_input), callable_mp(this, &ScrollBar::_drag_node_input)); |
| 657 | drag_node->connect(SceneStringName(tree_exiting), callable_mp(this, &ScrollBar::_drag_node_exit), CONNECT_ONE_SHOT); |
| 658 | } |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | NodePath ScrollBar::get_drag_node() const { |
| 663 | return drag_node_path; |
| 664 | } |
| 665 | |
| 666 | void ScrollBar::set_drag_node_enabled(bool p_enable) { |
| 667 | drag_node_enabled = p_enable; |
| 668 | } |
| 669 | |
| 670 | void ScrollBar::set_smooth_scroll_enabled(bool p_enable) { |
| 671 | smooth_scroll_enabled = p_enable; |
| 672 | } |
| 673 | |
| 674 | bool ScrollBar::is_smooth_scroll_enabled() const { |
| 675 | return smooth_scroll_enabled; |
| 676 | } |
| 677 | |
| 678 | void ScrollBar::_bind_methods() { |
| 679 | ClassDB::bind_method(D_METHOD("set_custom_step", "step"), &ScrollBar::set_custom_step); |
| 680 | ClassDB::bind_method(D_METHOD("get_custom_step"), &ScrollBar::get_custom_step); |
| 681 | |
| 682 | ADD_SIGNAL(MethodInfo("scrolling")); |
| 683 | |
| 684 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "custom_step", PROPERTY_HINT_RANGE, "-1,4096,suffix:px"), "set_custom_step", "get_custom_step"); |
| 685 | |
| 686 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ScrollBar, scroll_style, "scroll"); |
| 687 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ScrollBar, scroll_focus_style, "scroll_focus"); |
| 688 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ScrollBar, grabber_style, "grabber"); |
| 689 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ScrollBar, grabber_hl_style, "grabber_highlight"); |
| 690 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ScrollBar, grabber_pressed_style, "grabber_pressed"); |
| 691 | |
| 692 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, ScrollBar, increment_icon, "increment"); |
| 693 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, ScrollBar, increment_hl_icon, "increment_highlight"); |
| 694 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, ScrollBar, increment_pressed_icon, "increment_pressed"); |
| 695 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, ScrollBar, decrement_icon, "decrement"); |
| 696 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, ScrollBar, decrement_hl_icon, "decrement_highlight"); |
| 697 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, ScrollBar, decrement_pressed_icon, "decrement_pressed"); |
| 698 | } |
| 699 | |
| 700 | ScrollBar::ScrollBar(Orientation p_orientation) { |
| 701 | orientation = p_orientation; |
| 702 | |
| 703 | if (focus_by_default) { |
| 704 | set_focus_mode(FOCUS_ALL); |
| 705 | } else { |
| 706 | set_focus_mode(FOCUS_ACCESSIBILITY); |
| 707 | } |
| 708 | set_step(0); |
| 709 | } |
| 710 | |
| 711 | ScrollBar::~ScrollBar() { |
| 712 | } |
| 713 | |
| 714 | void VScrollBar::_bind_methods() { |
| 715 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, VScrollBar, padding_left); |
| 716 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, VScrollBar, padding_right); |
| 717 | } |
| 718 | |
| 719 | void HScrollBar::_bind_methods() { |
| 720 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, HScrollBar, padding_top); |
| 721 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, HScrollBar, padding_bottom); |
| 722 | } |