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