Implemented scrolling factor for smooth trackpad scrolling

Working platforms platform: OSX, Windows.
Support for almost all ui elements, including project list.
This commit is contained in:
toger5 2017-02-22 00:45:31 +02:00 committed by Rémi Verschelde
parent c8aea60324
commit 304a1f5b5a
15 changed files with 118 additions and 71 deletions

View File

@ -188,6 +188,7 @@ struct InputEventMouse {
struct InputEventMouseButton : public InputEventMouse {
double factor;
int button_index;
bool pressed; //otherwise released
bool doubleclick; //last even less than doubleclick time
@ -272,7 +273,10 @@ struct InputEvent {
InputEvent xform_by(const Transform2D &p_xform) const;
bool operator==(const InputEvent &p_event) const;
operator String() const;
InputEvent() { zeromem(this, sizeof(InputEvent)); }
InputEvent() {
zeromem(this, sizeof(InputEvent));
mouse_button.factor = 1;
}
};
#endif

View File

@ -1871,21 +1871,35 @@ void AnimationKeyEditor::_track_editor_gui_input(const InputEvent &p_input) {
if (mb.button_index == BUTTON_WHEEL_UP && mb.pressed) {
if (mb.mod.command) {
zoom->set_value(zoom->get_value() + zoom->get_step());
} else {
v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page() / 8);
v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page() * mb.factor / 8);
}
}
if (mb.button_index == BUTTON_WHEEL_DOWN && mb.pressed) {
if (mb.mod.command) {
zoom->set_value(zoom->get_value() - zoom->get_step());
} else {
v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() / 8);
v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * mb.factor / 8);
}
}
if (mb.button_index == BUTTON_WHEEL_RIGHT && mb.pressed) {
h_scroll->set_value(h_scroll->get_value() - h_scroll->get_page() * mb.factor / 8);
}
if (mb.button_index == BUTTON_WHEEL_LEFT && mb.pressed) {
v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * mb.factor / 8);
}
if (mb.button_index == BUTTON_RIGHT && mb.pressed) {
Point2 mpos = Point2(mb.x, mb.y) - ofs;

View File

@ -1049,7 +1049,7 @@ void CanvasItemEditor::_viewport_gui_input(const InputEvent &p_event) {
return;
float prev_zoom = zoom;
zoom = zoom * 0.95;
zoom = zoom * (1 - (0.05 * b.factor));
{
Point2 ofs(b.x, b.y);
ofs = ofs / prev_zoom - ofs / zoom;
@ -1067,7 +1067,7 @@ void CanvasItemEditor::_viewport_gui_input(const InputEvent &p_event) {
return;
float prev_zoom = zoom;
zoom = zoom * (1.0 / 0.95);
zoom = zoom * ((0.95 + (0.05 * b.factor)) / 0.95);
{
Point2 ofs(b.x, b.y);
ofs = ofs / prev_zoom - ofs / zoom;

View File

@ -522,10 +522,10 @@ void Polygon2DEditor::_uv_input(const InputEvent &p_input) {
} else if (mb.button_index == BUTTON_WHEEL_UP && mb.pressed) {
uv_zoom->set_value(uv_zoom->get_value() / 0.9);
uv_zoom->set_value(uv_zoom->get_value() / (1 - (0.1 * mb.factor)));
} else if (mb.button_index == BUTTON_WHEEL_DOWN && mb.pressed) {
uv_zoom->set_value(uv_zoom->get_value() * 0.9);
uv_zoom->set_value(uv_zoom->get_value() * (1 - (0.1 * mb.factor)));
}
} else if (p_input.type == InputEvent::MOUSE_MOTION) {

View File

@ -519,12 +519,12 @@ bool GridMapEditor::forward_spatial_input_event(Camera *p_camera, const InputEve
if (p_event.mouse_button.button_index == BUTTON_WHEEL_UP && (p_event.mouse_button.mod.command || p_event.mouse_button.mod.shift)) {
if (p_event.mouse_button.pressed)
floor->set_value(floor->get_value() + 1);
floor->set_value(floor->get_value() + p_event.mouse_button.factor);
return true; //eaten
} else if (p_event.mouse_button.button_index == BUTTON_WHEEL_DOWN && (p_event.mouse_button.mod.command || p_event.mouse_button.mod.shift)) {
if (p_event.mouse_button.pressed)
floor->set_value(floor->get_value() - 1);
floor->set_value(floor->get_value() - p_event.mouse_button.factor);
return true;
}

View File

@ -788,6 +788,22 @@ static int translateKey(unsigned int key) {
*/
}
inline void sendScrollEvent(int button, double factor) {
InputEvent ev;
ev.type = InputEvent::MOUSE_BUTTON;
ev.mouse_button.button_index = button;
ev.mouse_button.factor = factor;
ev.mouse_button.pressed = true;
ev.mouse_button.x = mouse_x;
ev.mouse_button.y = mouse_y;
ev.mouse_button.global_x = mouse_x;
ev.mouse_button.global_y = mouse_y;
ev.mouse_button.button_mask = button_mask;
OS_OSX::singleton->push_input(ev);
ev.mouse_button.pressed = false;
OS_OSX::singleton->push_input(ev);
}
- (void)scrollWheel:(NSEvent *)event {
double deltaX, deltaY;
@ -797,48 +813,21 @@ static int translateKey(unsigned int key) {
deltaY = [event scrollingDeltaY];
if ([event hasPreciseScrollingDeltas]) {
deltaX *= 0.1;
deltaY *= 0.1;
deltaX *= 0.03;
deltaY *= 0.03;
}
} else {
} else
#endif // MAC_OS_X_VERSION_MAX_ALLOWED
{
deltaX = [event deltaX];
deltaY = [event deltaY];
}
#else
deltaX = [event deltaX];
deltaY = [event deltaY];
#endif /*MAC_OS_X_VERSION_MAX_ALLOWED*/
if (fabs(deltaY)) {
InputEvent ev;
ev.type = InputEvent::MOUSE_BUTTON;
ev.mouse_button.button_index = deltaY > 0 ? BUTTON_WHEEL_UP : BUTTON_WHEEL_DOWN;
ev.mouse_button.pressed = true;
ev.mouse_button.x = mouse_x;
ev.mouse_button.y = mouse_y;
ev.mouse_button.global_x = mouse_x;
ev.mouse_button.global_y = mouse_y;
ev.mouse_button.button_mask = button_mask;
OS_OSX::singleton->push_input(ev);
ev.mouse_button.pressed = false;
OS_OSX::singleton->push_input(ev);
}
if (fabs(deltaX)) {
InputEvent ev;
ev.type = InputEvent::MOUSE_BUTTON;
ev.mouse_button.button_index = deltaX < 0 ? BUTTON_WHEEL_RIGHT : BUTTON_WHEEL_LEFT;
ev.mouse_button.pressed = true;
ev.mouse_button.x = mouse_x;
ev.mouse_button.y = mouse_y;
ev.mouse_button.global_x = mouse_x;
ev.mouse_button.global_y = mouse_y;
ev.mouse_button.button_mask = button_mask;
OS_OSX::singleton->push_input(ev);
ev.mouse_button.pressed = false;
OS_OSX::singleton->push_input(ev);
sendScrollEvent(0 > deltaX ? BUTTON_WHEEL_RIGHT : BUTTON_WHEEL_LEFT, fabs(deltaX * 0.3));
}
if (fabs(deltaY)) {
sendScrollEvent(0 < deltaY ? BUTTON_WHEEL_UP : BUTTON_WHEEL_DOWN, fabs(deltaY * 0.3));
}
}
@ -1630,7 +1619,6 @@ void OS_OSX::process_events() {
void OS_OSX::push_input(const InputEvent &p_event) {
InputEvent ev = p_event;
//print_line("EV: "+String(ev));
input->parse_input_event(ev);
}

View File

@ -509,10 +509,13 @@ LRESULT OS_Windows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
if (!motion)
return 0;
if (motion < 0)
if (motion < 0) {
mb.button_index = BUTTON_WHEEL_LEFT;
else
mb.factor = fabs((double)motion / (double)WHEEL_DELTA);
} else {
mb.button_index = BUTTON_WHEEL_RIGHT;
mb.factor = fabs((double)motion / (double)WHEEL_DELTA);
}
} break;
/*
case WM_XBUTTONDOWN: {

View File

@ -962,6 +962,18 @@ void GraphEdit::_gui_input(const InputEvent &p_ev) {
//too difficult to get right
//set_zoom(zoom/ZOOM_SCALE);
}
if (b.button_index == BUTTON_WHEEL_UP) {
h_scroll->set_value(h_scroll->get_value() - h_scroll->get_page() * b.factor / 8);
}
if (b.button_index == BUTTON_WHEEL_DOWN) {
h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * b.factor / 8);
}
if (b.button_index == BUTTON_WHEEL_RIGHT) {
v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * b.factor / 8);
}
if (b.button_index == BUTTON_WHEEL_LEFT) {
v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page() * b.factor / 8);
}
}
if (p_ev.type == InputEvent::KEY && p_ev.key.scancode == KEY_D && p_ev.key.pressed && p_ev.key.mod.command) {

View File

@ -533,10 +533,14 @@ void ItemList::_gui_input(const InputEvent &p_event) {
}
if (p_event.type == InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index == BUTTON_WHEEL_UP && p_event.mouse_button.pressed) {
scroll_bar->set_value(scroll_bar->get_value() - scroll_bar->get_page() * p_event.mouse_button.factor / 8);
scroll_bar->set_value(scroll_bar->get_value() - scroll_bar->get_page() / 8);
}
if (p_event.type == InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index == BUTTON_WHEEL_DOWN && p_event.mouse_button.pressed) {
scroll_bar->set_value(scroll_bar->get_value() + scroll_bar->get_page() * p_event.mouse_button.factor / 8);
scroll_bar->set_value(scroll_bar->get_value() + scroll_bar->get_page() / 8);
}

View File

@ -273,7 +273,7 @@ void PopupMenu::_gui_input(const InputEvent &p_event) {
Point2 pos = get_position();
int s = (vseparation + font->get_height()) * 3;
pos.y -= s;
pos.y -= (s * b.factor);
set_position(pos);
//update hover
@ -293,7 +293,7 @@ void PopupMenu::_gui_input(const InputEvent &p_event) {
Point2 pos = get_position();
int s = (vseparation + font->get_height()) * 3;
pos.y += s;
pos.y += (s * b.factor);
set_position(pos);
//update hover

View File

@ -780,12 +780,14 @@ void RichTextLabel::_gui_input(InputEvent p_event) {
if (b.button_index == BUTTON_WHEEL_UP) {
if (scroll_active)
vscroll->set_value(vscroll->get_value() - vscroll->get_page() / 8);
vscroll->set_value(vscroll->get_value() - vscroll->get_page() * b.factor * 0.5 / 8);
}
if (b.button_index == BUTTON_WHEEL_DOWN) {
if (scroll_active)
vscroll->set_value(vscroll->get_value() + vscroll->get_page() / 8);
vscroll->set_value(vscroll->get_value() + vscroll->get_page() * b.factor * 0.5 / 8);
}
} break;
case InputEvent::KEY: {

View File

@ -85,20 +85,32 @@ void ScrollContainer::_gui_input(const InputEvent &p_gui_input) {
const InputEventMouseButton &mb = p_gui_input.mouse_button;
if (mb.button_index == BUTTON_WHEEL_UP && mb.pressed) {
if (h_scroll->is_visible_in_tree() && !v_scroll->is_visible_in_tree()) {
// only horizontal is enabled, scroll horizontally
h_scroll->set_value(h_scroll->get_value() - h_scroll->get_page() / 8);
if (h_scroll->is_visible() && !v_scroll->is_visible()) {
h_scroll->set_value(h_scroll->get_value() - h_scroll->get_page() / 8 * mb.factor);
} else if (v_scroll->is_visible_in_tree()) {
v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page() / 8);
v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page() / 8 * mb.factor);
}
}
if (mb.button_index == BUTTON_WHEEL_DOWN && mb.pressed) {
if (h_scroll->is_visible_in_tree() && !v_scroll->is_visible_in_tree()) {
// only horizontal is enabled, scroll horizontally
h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() / 8);
} else if (v_scroll->is_visible_in_tree()) {
v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() / 8);
if (h_scroll->is_visible() && !v_scroll->is_visible()) {
h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() / 8 * mb.factor);
} else if (v_scroll->is_visible()) {
v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() / 8 * mb.factor);
}
}
if (mb.button_index == BUTTON_WHEEL_LEFT && mb.pressed) {
if (h_scroll->is_visible_in_tree()) {
h_scroll->set_value(h_scroll->get_value() - h_scroll->get_page() * mb.factor / 8);
}
}
if (mb.button_index == BUTTON_WHEEL_RIGHT && mb.pressed) {
if (h_scroll->is_visible_in_tree()) {
h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * mb.factor / 8);
}
}

View File

@ -115,13 +115,15 @@ void SpinBox::_gui_input(const InputEvent &p_event) {
} break;
case BUTTON_WHEEL_UP: {
if (line_edit->has_focus()) {
set_value(get_value() + get_step());
set_value(get_value() + get_step() * mb.factor);
accept_event();
}
} break;
case BUTTON_WHEEL_DOWN: {
if (line_edit->has_focus()) {
set_value(get_value() - get_step());
set_value(get_value() - get_step() * mb.factor);
accept_event();
}
} break;

View File

@ -334,7 +334,10 @@ void TextEdit::_update_scrollbars() {
v_scroll->show();
v_scroll->set_max(total_rows);
v_scroll->set_page(visible_rows);
if (fabs(v_scroll->get_value() - (double)cursor.line_ofs) >= 1) {
v_scroll->set_value(cursor.line_ofs);
}
} else {
cursor.line_ofs = 0;
@ -346,7 +349,9 @@ void TextEdit::_update_scrollbars() {
h_scroll->show();
h_scroll->set_max(total_width);
h_scroll->set_page(visible_width);
if (fabs(h_scroll->get_value() - (double)cursor.x_ofs) >= 1) {
h_scroll->set_value(cursor.x_ofs);
}
} else {
@ -1480,17 +1485,18 @@ void TextEdit::_gui_input(const InputEvent &p_gui_input) {
}
if (mb.pressed) {
if (mb.button_index == BUTTON_WHEEL_UP && !mb.mod.command) {
v_scroll->set_value(v_scroll->get_value() - 3);
v_scroll->set_value(v_scroll->get_value() - (3 * mb.factor));
}
if (mb.button_index == BUTTON_WHEEL_DOWN && !mb.mod.command) {
v_scroll->set_value(v_scroll->get_value() + 3);
v_scroll->set_value(v_scroll->get_value() + (3 * mb.factor));
}
if (mb.button_index == BUTTON_WHEEL_LEFT) {
h_scroll->set_value(h_scroll->get_value() - 3);
h_scroll->set_value(h_scroll->get_value() - (100 * mb.factor));
}
if (mb.button_index == BUTTON_WHEEL_RIGHT) {
h_scroll->set_value(h_scroll->get_value() + 3);
h_scroll->set_value(h_scroll->get_value() + (100 * mb.factor));
}
if (mb.button_index == BUTTON_LEFT) {

View File

@ -2341,11 +2341,11 @@ void Tree::_gui_input(InputEvent p_event) {
} break;
case BUTTON_WHEEL_UP: {
v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page() / 8);
v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page() * b.factor / 8);
} break;
case BUTTON_WHEEL_DOWN: {
v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() / 8);
v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * b.factor / 8);
} break;
}