From b7545631de04a8cb5c9522373310469243fb2bd0 Mon Sep 17 00:00:00 2001 From: Marcel Admiraal Date: Thu, 13 Jan 2022 09:17:10 +0000 Subject: [PATCH] Use mouse event relative motion to calculate mouse speed --- main/input_default.cpp | 15 ++++++++------- platform/javascript/os_javascript.cpp | 1 - platform/osx/os_osx.mm | 1 - platform/windows/os_windows.cpp | 4 ---- platform/x11/os_x11.cpp | 1 - 5 files changed, 8 insertions(+), 14 deletions(-) diff --git a/main/input_default.cpp b/main/input_default.cpp index 7ca580f8edeb..529c2cdfbf88 100644 --- a/main/input_default.cpp +++ b/main/input_default.cpp @@ -355,18 +355,20 @@ void InputDefault::_parse_input_event_impl(const Ref &p_event, bool Ref mm = p_event; if (mm.is_valid()) { - Point2 pos = mm->get_global_position(); - if (mouse_pos != pos) { - set_mouse_position(pos); + Point2 position = mm->get_global_position(); + if (mouse_pos != position) { + set_mouse_position(position); } + Vector2 relative = mm->get_relative(); + mouse_speed_track.update(relative); if (main_loop && emulate_touch_from_mouse && !p_is_emulated && mm->get_button_mask() & 1) { Ref drag_event; drag_event.instance(); - drag_event->set_position(mm->get_position()); - drag_event->set_relative(mm->get_relative()); - drag_event->set_speed(mm->get_speed()); + drag_event->set_position(position); + drag_event->set_relative(relative); + drag_event->set_speed(get_last_mouse_speed()); main_loop->input_event(drag_event); } @@ -552,7 +554,6 @@ void InputDefault::set_main_loop(MainLoop *p_main_loop) { } void InputDefault::set_mouse_position(const Point2 &p_posf) { - mouse_speed_track.update(p_posf - mouse_pos); mouse_pos = p_posf; } diff --git a/platform/javascript/os_javascript.cpp b/platform/javascript/os_javascript.cpp index 665c4243cfac..c6cbf862f78a 100644 --- a/platform/javascript/os_javascript.cpp +++ b/platform/javascript/os_javascript.cpp @@ -316,7 +316,6 @@ void OS_JavaScript::mouse_move_callback(double p_x, double p_y, double p_rel_x, ev->set_global_position(ev->get_position()); ev->set_relative(Vector2(p_rel_x, p_rel_y)); - os->input->set_mouse_position(ev->get_position()); ev->set_speed(os->input->get_last_mouse_speed()); os->input->parse_input_event(ev); diff --git a/platform/osx/os_osx.mm b/platform/osx/os_osx.mm index e53797f8f4e2..72839ce14dec 100644 --- a/platform/osx/os_osx.mm +++ b/platform/osx/os_osx.mm @@ -806,7 +806,6 @@ - (void)mouseMoved:(NSEvent *)event { mm->set_relative(relativeMotion); get_key_modifier_state([event modifierFlags], mm); - OS_OSX::singleton->input->set_mouse_position(Point2(mouse_x, mouse_y)); OS_OSX::singleton->push_input(mm); } diff --git a/platform/windows/os_windows.cpp b/platform/windows/os_windows.cpp index 85081ebcb535..50fe280ab263 100644 --- a/platform/windows/os_windows.cpp +++ b/platform/windows/os_windows.cpp @@ -401,7 +401,6 @@ LRESULT OS_Windows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) mm->set_position(c); mm->set_global_position(c); - input->set_mouse_position(c); mm->set_speed(Vector2(0, 0)); if (raw->data.mouse.usFlags == MOUSE_MOVE_RELATIVE) { @@ -508,7 +507,6 @@ LRESULT OS_Windows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) SetCursorPos(pos.x, pos.y); } - input->set_mouse_position(mm->get_position()); mm->set_speed(input->get_last_mouse_speed()); if (old_invalid) { @@ -652,7 +650,6 @@ LRESULT OS_Windows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) SetCursorPos(pos.x, pos.y); } - input->set_mouse_position(mm->get_position()); mm->set_speed(input->get_last_mouse_speed()); if (old_invalid) { @@ -754,7 +751,6 @@ LRESULT OS_Windows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) SetCursorPos(pos.x, pos.y); } - input->set_mouse_position(mm->get_position()); mm->set_speed(input->get_last_mouse_speed()); if (old_invalid) { diff --git a/platform/x11/os_x11.cpp b/platform/x11/os_x11.cpp index 4e7711e48d91..72204638e5ac 100644 --- a/platform/x11/os_x11.cpp +++ b/platform/x11/os_x11.cpp @@ -2828,7 +2828,6 @@ void OS_X11::process_xevents() { mm->set_button_mask(get_mouse_button_state()); mm->set_position(posi); mm->set_global_position(posi); - input->set_mouse_position(posi); mm->set_speed(input->get_last_mouse_speed()); mm->set_relative(rel);