2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* scroll_bar.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2021-01-01 19:13:46 +00:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "scroll_bar.h"
|
2017-08-27 19:07:15 +00:00
|
|
|
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/os/keyboard.h"
|
|
|
|
#include "core/os/os.h"
|
2020-11-07 22:33:38 +00:00
|
|
|
#include "core/string/print_string.h"
|
2020-03-04 01:51:12 +00:00
|
|
|
#include "scene/main/window.h"
|
2017-08-27 19:07:15 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool ScrollBar::focus_by_default = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
void ScrollBar::set_can_focus_by_default(bool p_can_focus) {
|
2017-03-05 15:44:50 +00:00
|
|
|
focus_by_default = p_can_focus;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
void ScrollBar::_gui_input(Ref<InputEvent> p_event) {
|
2021-04-05 06:52:21 +00:00
|
|
|
ERR_FAIL_COND(p_event.is_null());
|
|
|
|
|
2017-08-22 19:02:08 +00:00
|
|
|
Ref<InputEventMouseMotion> m = p_event;
|
|
|
|
if (!m.is_valid() || drag.active) {
|
2021-07-17 21:22:52 +00:00
|
|
|
emit_signal(SNAME("scrolling"));
|
2017-08-22 19:02:08 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
Ref<InputEventMouseButton> b = p_event;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
if (b.is_valid()) {
|
|
|
|
accept_event();
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2021-01-08 03:37:37 +00:00
|
|
|
if (b->get_button_index() == MOUSE_BUTTON_WHEEL_DOWN && b->is_pressed()) {
|
2017-05-20 15:38:03 +00:00
|
|
|
set_value(get_value() + get_page() / 4.0);
|
|
|
|
accept_event();
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2021-01-08 03:37:37 +00:00
|
|
|
if (b->get_button_index() == MOUSE_BUTTON_WHEEL_UP && b->is_pressed()) {
|
2017-05-20 15:38:03 +00:00
|
|
|
set_value(get_value() - get_page() / 4.0);
|
|
|
|
accept_event();
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-01-08 03:37:37 +00:00
|
|
|
if (b->get_button_index() != MOUSE_BUTTON_LEFT) {
|
2017-05-20 15:38:03 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
if (b->is_pressed()) {
|
2017-06-03 08:54:24 +00:00
|
|
|
double ofs = orientation == VERTICAL ? b->get_position().y : b->get_position().x;
|
2021-07-17 21:22:52 +00:00
|
|
|
Ref<Texture2D> decr = get_theme_icon(SNAME("decrement"));
|
|
|
|
Ref<Texture2D> incr = get_theme_icon(SNAME("increment"));
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
double decr_size = orientation == VERTICAL ? decr->get_height() : decr->get_width();
|
|
|
|
double incr_size = orientation == VERTICAL ? incr->get_height() : incr->get_width();
|
|
|
|
double grabber_ofs = get_grabber_offset();
|
|
|
|
double grabber_size = get_grabber_size();
|
|
|
|
double total = orientation == VERTICAL ? get_size().height : get_size().width;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
if (ofs < decr_size) {
|
|
|
|
set_value(get_value() - (custom_step >= 0 ? custom_step : get_step()));
|
|
|
|
return;
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
if (ofs > total - incr_size) {
|
|
|
|
set_value(get_value() + (custom_step >= 0 ? custom_step : get_step()));
|
|
|
|
return;
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
ofs -= decr_size;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
if (ofs < grabber_ofs) {
|
2017-08-19 14:23:45 +00:00
|
|
|
if (scrolling) {
|
2017-09-17 20:31:21 +00:00
|
|
|
target_scroll = CLAMP(target_scroll - get_page(), get_min(), get_max() - get_page());
|
2017-08-19 14:23:45 +00:00
|
|
|
} else {
|
2017-09-17 20:31:21 +00:00
|
|
|
target_scroll = CLAMP(get_value() - get_page(), get_min(), get_max() - get_page());
|
2017-08-19 14:23:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (smooth_scroll_enabled) {
|
|
|
|
scrolling = true;
|
2018-04-11 07:28:14 +00:00
|
|
|
set_physics_process_internal(true);
|
2017-08-19 14:23:45 +00:00
|
|
|
} else {
|
|
|
|
set_value(target_scroll);
|
|
|
|
}
|
2017-05-20 15:38:03 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
ofs -= grabber_ofs;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
if (ofs < grabber_size) {
|
|
|
|
drag.active = true;
|
|
|
|
drag.pos_at_click = grabber_ofs + ofs;
|
|
|
|
drag.value_at_click = get_as_ratio();
|
|
|
|
update();
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2017-08-19 14:23:45 +00:00
|
|
|
if (scrolling) {
|
2017-09-17 20:31:21 +00:00
|
|
|
target_scroll = CLAMP(target_scroll + get_page(), get_min(), get_max() - get_page());
|
2017-08-19 14:23:45 +00:00
|
|
|
} else {
|
2017-09-17 20:31:21 +00:00
|
|
|
target_scroll = CLAMP(get_value() + get_page(), get_min(), get_max() - get_page());
|
2017-08-19 14:23:45 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-08-19 14:23:45 +00:00
|
|
|
if (smooth_scroll_enabled) {
|
|
|
|
scrolling = true;
|
2018-04-11 07:28:14 +00:00
|
|
|
set_physics_process_internal(true);
|
2017-08-19 14:23:45 +00:00
|
|
|
} else {
|
|
|
|
set_value(target_scroll);
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
} else {
|
|
|
|
drag.active = false;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
if (m.is_valid()) {
|
|
|
|
accept_event();
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
if (drag.active) {
|
2017-06-03 08:54:24 +00:00
|
|
|
double ofs = orientation == VERTICAL ? m->get_position().y : m->get_position().x;
|
2021-07-17 21:22:52 +00:00
|
|
|
Ref<Texture2D> decr = get_theme_icon(SNAME("decrement"));
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
double decr_size = orientation == VERTICAL ? decr->get_height() : decr->get_width();
|
|
|
|
ofs -= decr_size;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
double diff = (ofs - drag.pos_at_click) / get_area_size();
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
set_as_ratio(drag.value_at_click + diff);
|
|
|
|
} else {
|
2017-06-03 08:54:24 +00:00
|
|
|
double ofs = orientation == VERTICAL ? m->get_position().y : m->get_position().x;
|
2021-07-17 21:22:52 +00:00
|
|
|
Ref<Texture2D> decr = get_theme_icon(SNAME("decrement"));
|
|
|
|
Ref<Texture2D> incr = get_theme_icon(SNAME("increment"));
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
double decr_size = orientation == VERTICAL ? decr->get_height() : decr->get_width();
|
|
|
|
double incr_size = orientation == VERTICAL ? incr->get_height() : incr->get_width();
|
|
|
|
double total = orientation == VERTICAL ? get_size().height : get_size().width;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
HighlightStatus new_hilite;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
if (ofs < decr_size) {
|
|
|
|
new_hilite = HIGHLIGHT_DECR;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
} else if (ofs > total - incr_size) {
|
|
|
|
new_hilite = HIGHLIGHT_INCR;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
} else {
|
|
|
|
new_hilite = HIGHLIGHT_RANGE;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
if (new_hilite != highlight) {
|
|
|
|
highlight = new_hilite;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2018-02-23 11:10:23 +00:00
|
|
|
if (p_event->is_pressed()) {
|
|
|
|
if (p_event->is_action("ui_left")) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (orientation != HORIZONTAL) {
|
2018-02-23 11:10:23 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-02-23 11:10:23 +00:00
|
|
|
set_value(get_value() - (custom_step >= 0 ? custom_step : get_step()));
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2018-02-23 11:10:23 +00:00
|
|
|
} else if (p_event->is_action("ui_right")) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (orientation != HORIZONTAL) {
|
2018-02-23 11:10:23 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-02-23 11:10:23 +00:00
|
|
|
set_value(get_value() + (custom_step >= 0 ? custom_step : get_step()));
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2018-02-23 11:10:23 +00:00
|
|
|
} else if (p_event->is_action("ui_up")) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (orientation != VERTICAL) {
|
2018-02-23 11:10:23 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2018-02-23 11:10:23 +00:00
|
|
|
set_value(get_value() - (custom_step >= 0 ? custom_step : get_step()));
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2018-02-23 11:10:23 +00:00
|
|
|
} else if (p_event->is_action("ui_down")) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (orientation != VERTICAL) {
|
2018-02-23 11:10:23 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-02-23 11:10:23 +00:00
|
|
|
set_value(get_value() + (custom_step >= 0 ? custom_step : get_step()));
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2018-02-23 11:17:15 +00:00
|
|
|
} else if (p_event->is_action("ui_home")) {
|
|
|
|
set_value(get_min());
|
2017-05-20 15:38:03 +00:00
|
|
|
|
2018-02-23 11:17:15 +00:00
|
|
|
} else if (p_event->is_action("ui_end")) {
|
|
|
|
set_value(get_max());
|
2016-03-08 23:00:52 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollBar::_notification(int p_what) {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_what == NOTIFICATION_DRAW) {
|
2014-02-10 01:10:30 +00:00
|
|
|
RID ci = get_canvas_item();
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2021-07-17 21:22:52 +00:00
|
|
|
Ref<Texture2D> decr = highlight == HIGHLIGHT_DECR ? get_theme_icon(SNAME("decrement_highlight")) : get_theme_icon(SNAME("decrement"));
|
|
|
|
Ref<Texture2D> incr = highlight == HIGHLIGHT_INCR ? get_theme_icon(SNAME("increment_highlight")) : get_theme_icon(SNAME("increment"));
|
|
|
|
Ref<StyleBox> bg = has_focus() ? get_theme_stylebox(SNAME("scroll_focus")) : get_theme_stylebox(SNAME("scroll"));
|
2017-08-12 18:59:55 +00:00
|
|
|
|
|
|
|
Ref<StyleBox> grabber;
|
2020-05-14 14:41:43 +00:00
|
|
|
if (drag.active) {
|
2021-07-17 21:22:52 +00:00
|
|
|
grabber = get_theme_stylebox(SNAME("grabber_pressed"));
|
2020-05-14 14:41:43 +00:00
|
|
|
} else if (highlight == HIGHLIGHT_RANGE) {
|
2021-07-17 21:22:52 +00:00
|
|
|
grabber = get_theme_stylebox(SNAME("grabber_highlight"));
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2021-07-17 21:22:52 +00:00
|
|
|
grabber = get_theme_stylebox(SNAME("grabber"));
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
Point2 ofs;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2018-07-14 13:02:08 +00:00
|
|
|
decr->draw(ci, Point2());
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (orientation == HORIZONTAL) {
|
2017-03-05 15:44:50 +00:00
|
|
|
ofs.x += decr->get_width();
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
ofs.y += decr->get_height();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Size2 area = get_size();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (orientation == HORIZONTAL) {
|
2017-03-05 15:44:50 +00:00
|
|
|
area.width -= incr->get_width() + decr->get_width();
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
area.height -= incr->get_height() + decr->get_height();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bg->draw(ci, Rect2(ofs, area));
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (orientation == HORIZONTAL) {
|
2017-03-05 15:44:50 +00:00
|
|
|
ofs.width += area.width;
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
ofs.height += area.height;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-07-14 13:02:08 +00:00
|
|
|
incr->draw(ci, ofs);
|
2014-02-10 01:10:30 +00:00
|
|
|
Rect2 grabber_rect;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (orientation == HORIZONTAL) {
|
|
|
|
grabber_rect.size.width = get_grabber_size();
|
|
|
|
grabber_rect.size.height = get_size().height;
|
2017-06-03 22:25:13 +00:00
|
|
|
grabber_rect.position.y = 0;
|
2020-12-22 16:24:29 +00:00
|
|
|
grabber_rect.position.x = get_grabber_offset() + decr->get_width() + bg->get_margin(SIDE_LEFT);
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
grabber_rect.size.width = get_size().width;
|
|
|
|
grabber_rect.size.height = get_grabber_size();
|
2020-12-22 16:24:29 +00:00
|
|
|
grabber_rect.position.y = get_grabber_offset() + decr->get_height() + bg->get_margin(SIDE_TOP);
|
2017-06-03 22:25:13 +00:00
|
|
|
grabber_rect.position.x = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
grabber->draw(ci, grabber_rect);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_what == NOTIFICATION_ENTER_TREE) {
|
2018-09-14 22:55:22 +00:00
|
|
|
if (has_node(drag_node_path)) {
|
|
|
|
Node *n = get_node(drag_node_path);
|
|
|
|
drag_node = Object::cast_to<Control>(n);
|
2014-06-19 05:23:03 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 22:55:22 +00:00
|
|
|
if (drag_node) {
|
2020-02-21 17:28:45 +00:00
|
|
|
drag_node->connect("gui_input", callable_mp(this, &ScrollBar::_drag_node_input));
|
|
|
|
drag_node->connect("tree_exiting", callable_mp(this, &ScrollBar::_drag_node_exit), varray(), CONNECT_ONESHOT);
|
2014-06-19 05:23:03 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_what == NOTIFICATION_EXIT_TREE) {
|
2018-09-14 22:55:22 +00:00
|
|
|
if (drag_node) {
|
2020-02-21 17:28:45 +00:00
|
|
|
drag_node->disconnect("gui_input", callable_mp(this, &ScrollBar::_drag_node_input));
|
|
|
|
drag_node->disconnect("tree_exiting", callable_mp(this, &ScrollBar::_drag_node_exit));
|
2014-06-19 05:23:03 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
drag_node = nullptr;
|
2014-06-19 05:23:03 +00:00
|
|
|
}
|
|
|
|
|
2018-04-11 07:28:14 +00:00
|
|
|
if (p_what == NOTIFICATION_INTERNAL_PHYSICS_PROCESS) {
|
2017-08-19 14:23:45 +00:00
|
|
|
if (scrolling) {
|
|
|
|
if (get_value() != target_scroll) {
|
|
|
|
double target = target_scroll - get_value();
|
|
|
|
double dist = sqrt(target * target);
|
2017-09-30 14:19:07 +00:00
|
|
|
double vel = ((target / dist) * 500) * get_physics_process_delta_time();
|
2017-08-19 14:23:45 +00:00
|
|
|
|
2017-09-24 15:19:52 +00:00
|
|
|
if (Math::abs(vel) >= dist) {
|
2017-08-19 14:23:45 +00:00
|
|
|
set_value(target_scroll);
|
2018-11-24 14:01:34 +00:00
|
|
|
scrolling = false;
|
|
|
|
set_physics_process_internal(false);
|
2017-08-19 14:23:45 +00:00
|
|
|
} else {
|
|
|
|
set_value(get_value() + vel);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
scrolling = false;
|
2018-04-11 07:28:14 +00:00
|
|
|
set_physics_process_internal(false);
|
2017-08-19 14:23:45 +00:00
|
|
|
}
|
2014-06-19 05:23:03 +00:00
|
|
|
|
2018-09-14 22:55:22 +00:00
|
|
|
} else if (drag_node_touching) {
|
|
|
|
if (drag_node_touching_deaccel) {
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector2 pos = Vector2(orientation == HORIZONTAL ? get_value() : 0, orientation == VERTICAL ? get_value() : 0);
|
2018-09-14 22:55:22 +00:00
|
|
|
pos += drag_node_speed * get_physics_process_delta_time();
|
2014-06-19 05:23:03 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool turnoff = false;
|
2014-06-19 05:23:03 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (orientation == HORIZONTAL) {
|
|
|
|
if (pos.x < 0) {
|
|
|
|
pos.x = 0;
|
|
|
|
turnoff = true;
|
2014-06-19 05:23:03 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (pos.x > (get_max() - get_page())) {
|
|
|
|
pos.x = get_max() - get_page();
|
|
|
|
turnoff = true;
|
2014-06-19 05:23:03 +00:00
|
|
|
}
|
|
|
|
|
2017-01-04 04:16:14 +00:00
|
|
|
set_value(pos.x);
|
2014-06-19 05:23:03 +00:00
|
|
|
|
2018-09-14 22:55:22 +00:00
|
|
|
float sgn_x = drag_node_speed.x < 0 ? -1 : 1;
|
|
|
|
float val_x = Math::abs(drag_node_speed.x);
|
2017-09-30 14:19:07 +00:00
|
|
|
val_x -= 1000 * get_physics_process_delta_time();
|
2014-06-19 05:23:03 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (val_x < 0) {
|
|
|
|
turnoff = true;
|
2014-06-19 05:23:03 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 22:55:22 +00:00
|
|
|
drag_node_speed.x = sgn_x * val_x;
|
2014-06-19 05:23:03 +00:00
|
|
|
|
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (pos.y < 0) {
|
|
|
|
pos.y = 0;
|
|
|
|
turnoff = true;
|
2014-06-19 05:23:03 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (pos.y > (get_max() - get_page())) {
|
|
|
|
pos.y = get_max() - get_page();
|
|
|
|
turnoff = true;
|
2014-06-19 05:23:03 +00:00
|
|
|
}
|
|
|
|
|
2017-01-04 04:16:14 +00:00
|
|
|
set_value(pos.y);
|
2014-06-19 05:23:03 +00:00
|
|
|
|
2018-09-14 22:55:22 +00:00
|
|
|
float sgn_y = drag_node_speed.y < 0 ? -1 : 1;
|
|
|
|
float val_y = Math::abs(drag_node_speed.y);
|
2017-09-30 14:19:07 +00:00
|
|
|
val_y -= 1000 * get_physics_process_delta_time();
|
2014-06-19 05:23:03 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (val_y < 0) {
|
|
|
|
turnoff = true;
|
2014-06-19 05:23:03 +00:00
|
|
|
}
|
2018-09-14 22:55:22 +00:00
|
|
|
drag_node_speed.y = sgn_y * val_y;
|
2014-06-19 05:23:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (turnoff) {
|
2018-04-11 07:28:14 +00:00
|
|
|
set_physics_process_internal(false);
|
2018-09-14 22:55:22 +00:00
|
|
|
drag_node_touching = false;
|
|
|
|
drag_node_touching_deaccel = false;
|
2014-06-19 05:23:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (time_since_motion == 0 || time_since_motion > 0.1) {
|
2018-09-14 22:55:22 +00:00
|
|
|
Vector2 diff = drag_node_accum - last_drag_node_accum;
|
|
|
|
last_drag_node_accum = drag_node_accum;
|
|
|
|
drag_node_speed = diff / get_physics_process_delta_time();
|
2014-06-19 05:23:03 +00:00
|
|
|
}
|
|
|
|
|
2017-09-30 14:19:07 +00:00
|
|
|
time_since_motion += get_physics_process_delta_time();
|
2014-06-19 05:23:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_what == NOTIFICATION_MOUSE_EXIT) {
|
2017-05-02 20:13:12 +00:00
|
|
|
highlight = HIGHLIGHT_NONE;
|
2014-02-10 01:10:30 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
double ScrollBar::get_grabber_min_size() const {
|
2021-07-17 21:22:52 +00:00
|
|
|
Ref<StyleBox> grabber = get_theme_stylebox(SNAME("grabber"));
|
2017-03-05 15:44:50 +00:00
|
|
|
Size2 gminsize = grabber->get_minimum_size() + grabber->get_center_size();
|
|
|
|
return (orientation == VERTICAL) ? gminsize.height : gminsize.width;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double ScrollBar::get_grabber_size() const {
|
2017-03-05 15:44:50 +00:00
|
|
|
float range = get_max() - get_min();
|
2020-05-14 14:41:43 +00:00
|
|
|
if (range <= 0) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return 0;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
float page = (get_page() > 0) ? get_page() : 0;
|
2017-01-14 11:26:56 +00:00
|
|
|
/*
|
|
|
|
if (grabber_range < get_step())
|
|
|
|
grabber_range=get_step();
|
|
|
|
*/
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
double area_size = get_area_size();
|
2014-02-10 01:10:30 +00:00
|
|
|
double grabber_size = page / range * area_size;
|
2017-03-05 15:44:50 +00:00
|
|
|
return grabber_size + get_grabber_min_size();
|
2016-03-08 23:00:52 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
double ScrollBar::get_area_size() const {
|
2019-06-20 14:59:48 +00:00
|
|
|
switch (orientation) {
|
|
|
|
case VERTICAL: {
|
|
|
|
double area = get_size().height;
|
2021-07-17 21:22:52 +00:00
|
|
|
area -= get_theme_stylebox(SNAME("scroll"))->get_minimum_size().height;
|
|
|
|
area -= get_theme_icon(SNAME("increment"))->get_height();
|
|
|
|
area -= get_theme_icon(SNAME("decrement"))->get_height();
|
2019-06-20 14:59:48 +00:00
|
|
|
area -= get_grabber_min_size();
|
|
|
|
return area;
|
|
|
|
} break;
|
|
|
|
case HORIZONTAL: {
|
|
|
|
double area = get_size().width;
|
2021-07-17 21:22:52 +00:00
|
|
|
area -= get_theme_stylebox(SNAME("scroll"))->get_minimum_size().width;
|
|
|
|
area -= get_theme_icon(SNAME("increment"))->get_width();
|
|
|
|
area -= get_theme_icon(SNAME("decrement"))->get_width();
|
2019-06-20 14:59:48 +00:00
|
|
|
area -= get_grabber_min_size();
|
|
|
|
return area;
|
|
|
|
} break;
|
|
|
|
default: {
|
|
|
|
return 0.0;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
double ScrollBar::get_area_offset() const {
|
2021-02-09 17:24:36 +00:00
|
|
|
double ofs = 0.0;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (orientation == VERTICAL) {
|
2021-07-17 21:22:52 +00:00
|
|
|
ofs += get_theme_stylebox(SNAME("hscroll"))->get_margin(SIDE_TOP);
|
|
|
|
ofs += get_theme_icon(SNAME("decrement"))->get_height();
|
2016-03-08 23:00:52 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (orientation == HORIZONTAL) {
|
2021-07-17 21:22:52 +00:00
|
|
|
ofs += get_theme_stylebox(SNAME("hscroll"))->get_margin(SIDE_LEFT);
|
|
|
|
ofs += get_theme_icon(SNAME("decrement"))->get_width();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
|
|
|
return ofs;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double ScrollBar::get_grabber_offset() const {
|
2017-01-13 17:08:30 +00:00
|
|
|
return (get_area_size()) * get_as_ratio();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Size2 ScrollBar::get_minimum_size() const {
|
2021-07-17 21:22:52 +00:00
|
|
|
Ref<Texture2D> incr = get_theme_icon(SNAME("increment"));
|
|
|
|
Ref<Texture2D> decr = get_theme_icon(SNAME("decrement"));
|
|
|
|
Ref<StyleBox> bg = get_theme_stylebox(SNAME("scroll"));
|
2014-02-10 01:10:30 +00:00
|
|
|
Size2 minsize;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (orientation == VERTICAL) {
|
|
|
|
minsize.width = MAX(incr->get_size().width, (bg->get_minimum_size() + bg->get_center_size()).width);
|
|
|
|
minsize.height += incr->get_size().height;
|
|
|
|
minsize.height += decr->get_size().height;
|
|
|
|
minsize.height += bg->get_minimum_size().height;
|
|
|
|
minsize.height += get_grabber_min_size();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (orientation == HORIZONTAL) {
|
|
|
|
minsize.height = MAX(incr->get_size().height, (bg->get_center_size() + bg->get_minimum_size()).height);
|
|
|
|
minsize.width += incr->get_size().width;
|
|
|
|
minsize.width += decr->get_size().width;
|
|
|
|
minsize.width += bg->get_minimum_size().width;
|
|
|
|
minsize.width += get_grabber_min_size();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return minsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollBar::set_custom_step(float p_custom_step) {
|
2017-03-05 15:44:50 +00:00
|
|
|
custom_step = p_custom_step;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float ScrollBar::get_custom_step() const {
|
|
|
|
return custom_step;
|
|
|
|
}
|
|
|
|
|
2018-09-14 22:55:22 +00:00
|
|
|
void ScrollBar::_drag_node_exit() {
|
|
|
|
if (drag_node) {
|
2020-02-21 17:28:45 +00:00
|
|
|
drag_node->disconnect("gui_input", callable_mp(this, &ScrollBar::_drag_node_input));
|
2014-06-19 05:23:03 +00:00
|
|
|
}
|
2020-04-01 23:20:12 +00:00
|
|
|
drag_node = nullptr;
|
2014-06-17 14:58:35 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 22:55:22 +00:00
|
|
|
void ScrollBar::_drag_node_input(const Ref<InputEvent> &p_input) {
|
2020-02-19 19:12:07 +00:00
|
|
|
if (!drag_node_enabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
Ref<InputEventMouseButton> mb = p_input;
|
2014-06-19 05:23:03 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
if (mb.is_valid()) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (mb->get_button_index() != 1) {
|
2017-05-20 15:38:03 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-06-19 05:23:03 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
if (mb->is_pressed()) {
|
2018-09-14 22:55:22 +00:00
|
|
|
drag_node_speed = Vector2();
|
|
|
|
drag_node_accum = Vector2();
|
|
|
|
last_drag_node_accum = Vector2();
|
|
|
|
drag_node_from = Vector2(orientation == HORIZONTAL ? get_value() : 0, orientation == VERTICAL ? get_value() : 0);
|
2020-07-12 23:15:20 +00:00
|
|
|
drag_node_touching = DisplayServer::get_singleton()->screen_is_touchscreen(DisplayServer::get_singleton()->window_get_current_screen(get_viewport()->get_window_id()));
|
2018-09-14 22:55:22 +00:00
|
|
|
drag_node_touching_deaccel = false;
|
|
|
|
time_since_motion = 0;
|
2014-06-19 05:23:03 +00:00
|
|
|
|
2018-09-14 22:55:22 +00:00
|
|
|
if (drag_node_touching) {
|
|
|
|
set_physics_process_internal(true);
|
2017-05-20 15:38:03 +00:00
|
|
|
time_since_motion = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2018-09-14 22:55:22 +00:00
|
|
|
if (drag_node_touching) {
|
|
|
|
if (drag_node_speed == Vector2()) {
|
|
|
|
drag_node_touching_deaccel = false;
|
|
|
|
drag_node_touching = false;
|
2018-04-11 07:28:14 +00:00
|
|
|
set_physics_process_internal(false);
|
2017-05-20 15:38:03 +00:00
|
|
|
} else {
|
2018-09-14 22:55:22 +00:00
|
|
|
drag_node_touching_deaccel = true;
|
2014-06-19 05:23:03 +00:00
|
|
|
}
|
2017-05-20 15:38:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-06-19 05:23:03 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
Ref<InputEventMouseMotion> mm = p_input;
|
2014-06-19 05:23:03 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
if (mm.is_valid()) {
|
2018-09-14 22:55:22 +00:00
|
|
|
if (drag_node_touching && !drag_node_touching_deaccel) {
|
2017-05-20 15:38:03 +00:00
|
|
|
Vector2 motion = Vector2(mm->get_relative().x, mm->get_relative().y);
|
2014-06-19 05:23:03 +00:00
|
|
|
|
2018-09-14 22:55:22 +00:00
|
|
|
drag_node_accum -= motion;
|
|
|
|
Vector2 diff = drag_node_from + drag_node_accum;
|
2017-05-20 15:38:03 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (orientation == HORIZONTAL) {
|
2017-05-20 15:38:03 +00:00
|
|
|
set_value(diff.x);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-09-14 22:55:22 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (orientation == VERTICAL) {
|
2017-05-20 15:38:03 +00:00
|
|
|
set_value(diff.y);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-09-14 22:55:22 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
time_since_motion = 0;
|
|
|
|
}
|
2014-06-19 05:23:03 +00:00
|
|
|
}
|
2014-06-17 14:58:35 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 22:55:22 +00:00
|
|
|
void ScrollBar::set_drag_node(const NodePath &p_path) {
|
2014-11-06 00:20:42 +00:00
|
|
|
if (is_inside_tree()) {
|
2018-09-14 22:55:22 +00:00
|
|
|
if (drag_node) {
|
2020-02-21 17:28:45 +00:00
|
|
|
drag_node->disconnect("gui_input", callable_mp(this, &ScrollBar::_drag_node_input));
|
|
|
|
drag_node->disconnect("tree_exiting", callable_mp(this, &ScrollBar::_drag_node_exit));
|
2014-06-19 05:23:03 +00:00
|
|
|
}
|
2014-06-17 14:58:35 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
drag_node = nullptr;
|
2018-09-14 22:55:22 +00:00
|
|
|
drag_node_path = p_path;
|
2014-06-17 14:58:35 +00:00
|
|
|
|
2014-11-06 00:20:42 +00:00
|
|
|
if (is_inside_tree()) {
|
2014-06-19 05:23:03 +00:00
|
|
|
if (has_node(p_path)) {
|
|
|
|
Node *n = get_node(p_path);
|
2018-09-14 22:55:22 +00:00
|
|
|
drag_node = Object::cast_to<Control>(n);
|
2014-06-19 05:23:03 +00:00
|
|
|
}
|
2014-06-17 14:58:35 +00:00
|
|
|
|
2018-09-14 22:55:22 +00:00
|
|
|
if (drag_node) {
|
2020-02-21 17:28:45 +00:00
|
|
|
drag_node->connect("gui_input", callable_mp(this, &ScrollBar::_drag_node_input));
|
|
|
|
drag_node->connect("tree_exiting", callable_mp(this, &ScrollBar::_drag_node_exit), varray(), CONNECT_ONESHOT);
|
2014-06-19 05:23:03 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-17 14:58:35 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 22:55:22 +00:00
|
|
|
NodePath ScrollBar::get_drag_node() const {
|
|
|
|
return drag_node_path;
|
2014-06-17 14:58:35 +00:00
|
|
|
}
|
|
|
|
|
2020-02-19 19:12:07 +00:00
|
|
|
void ScrollBar::set_drag_node_enabled(bool p_enable) {
|
|
|
|
drag_node_enabled = p_enable;
|
|
|
|
}
|
|
|
|
|
2017-08-19 14:23:45 +00:00
|
|
|
void ScrollBar::set_smooth_scroll_enabled(bool p_enable) {
|
|
|
|
smooth_scroll_enabled = p_enable;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScrollBar::is_smooth_scroll_enabled() const {
|
|
|
|
return smooth_scroll_enabled;
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void ScrollBar::_bind_methods() {
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_gui_input"), &ScrollBar::_gui_input);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_custom_step", "step"), &ScrollBar::set_custom_step);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_custom_step"), &ScrollBar::get_custom_step);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-08-22 19:02:08 +00:00
|
|
|
ADD_SIGNAL(MethodInfo("scrolling"));
|
|
|
|
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 18:20:53 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "custom_step", PROPERTY_HINT_RANGE, "-1,4096"), "set_custom_step", "get_custom_step");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ScrollBar::ScrollBar(Orientation p_orientation) {
|
|
|
|
orientation = p_orientation;
|
2017-08-19 14:23:45 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (focus_by_default) {
|
2017-03-05 15:44:50 +00:00
|
|
|
set_focus_mode(FOCUS_ALL);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-11-27 17:20:11 +00:00
|
|
|
set_step(0);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ScrollBar::~ScrollBar() {
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|