diff options
author | Pedro J. Estébanez <pedrojrulez@gmail.com> | 2021-08-13 00:31:16 +0200 |
---|---|---|
committer | Pedro J. Estébanez <pedrojrulez@gmail.com> | 2021-08-13 11:19:19 +0200 |
commit | 7c864d41c9aa8c6b1c585175fc997af8070f6b30 (patch) | |
tree | 03ee430a5d2787190969291619bfa7af1063fd52 | |
parent | 39efccf3b8298e30aa67a726ffd8752b3dff4c66 (diff) |
Improve input event accumulation
- API has been simplified: all events now go through `parse_input_event()`. Whether they are accumulated or not depends on the `use_accumulated_input` flag.
- Event accumulation is now thread-safe (it was not needed so far, but it prepares the ground for the following changes).
- Touch drag events now support accumulation.
-rw-r--r-- | core/input/input.cpp | 16 | ||||
-rw-r--r-- | core/input/input.h | 1 | ||||
-rw-r--r-- | core/input/input_event.cpp | 16 | ||||
-rw-r--r-- | core/input/input_event.h | 2 | ||||
-rw-r--r-- | platform/android/display_server_android.cpp | 28 | ||||
-rw-r--r-- | platform/linuxbsd/display_server_x11.cpp | 20 | ||||
-rw-r--r-- | platform/osx/display_server_osx.mm | 16 | ||||
-rw-r--r-- | platform/windows/display_server_windows.cpp | 20 |
8 files changed, 67 insertions, 52 deletions
diff --git a/core/input/input.cpp b/core/input/input.cpp index 2da50b7dff..ffe2659126 100644 --- a/core/input/input.cpp +++ b/core/input/input.cpp @@ -460,10 +460,6 @@ Vector3 Input::get_gyroscope() const { return gyroscope; } -void Input::parse_input_event(const Ref<InputEvent> &p_event) { - _parse_input_event_impl(p_event, false); -} - void Input::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_emulated) { // Notes on mouse-touch emulation: // - Emulated mouse events are parsed, that is, re-routed to this method, so they make the same effects @@ -472,8 +468,6 @@ void Input::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_em // - Emulated touch events are handed right to the main loop (i.e., the SceneTree) because they don't // require additional handling by this class. - _THREAD_SAFE_METHOD_ - Ref<InputEventKey> k = p_event; if (k.is_valid() && !k->is_echo() && k->get_keycode() != 0) { if (k->is_pressed()) { @@ -838,11 +832,13 @@ void Input::set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, co set_custom_mouse_cursor_func(p_cursor, p_shape, p_hotspot); } -void Input::accumulate_input_event(const Ref<InputEvent> &p_event) { +void Input::parse_input_event(const Ref<InputEvent> &p_event) { + _THREAD_SAFE_METHOD_ + ERR_FAIL_COND(p_event.is_null()); if (!use_accumulated_input) { - parse_input_event(p_event); + _parse_input_event_impl(p_event, false); return; } if (!accumulated_events.is_empty() && accumulated_events.back()->get()->accumulate(p_event)) { @@ -853,8 +849,10 @@ void Input::accumulate_input_event(const Ref<InputEvent> &p_event) { } void Input::flush_accumulated_events() { + _THREAD_SAFE_METHOD_ + while (accumulated_events.front()) { - parse_input_event(accumulated_events.front()->get()); + _parse_input_event_impl(accumulated_events.front()->get(), false); accumulated_events.pop_front(); } } diff --git a/core/input/input.h b/core/input/input.h index ee991aa725..d08e6437a5 100644 --- a/core/input/input.h +++ b/core/input/input.h @@ -323,7 +323,6 @@ public: String get_joy_guid(int p_device) const; void set_fallback_mapping(String p_guid); - void accumulate_input_event(const Ref<InputEvent> &p_event); void flush_accumulated_events(); void set_use_accumulated_input(bool p_enable); diff --git a/core/input/input_event.cpp b/core/input/input_event.cpp index 6e5c1a58ae..16bb92d94b 100644 --- a/core/input/input_event.cpp +++ b/core/input/input_event.cpp @@ -1210,6 +1210,22 @@ String InputEventScreenDrag::to_string() { return vformat("InputEventScreenDrag: index=%d, position=(%s), relative=(%s), speed=(%s)", index, String(get_position()), String(get_relative()), String(get_speed())); } +bool InputEventScreenDrag::accumulate(const Ref<InputEvent> &p_event) { + Ref<InputEventScreenDrag> drag = p_event; + if (drag.is_null()) + return false; + + if (get_index() != drag->get_index()) { + return false; + } + + set_position(drag->get_position()); + set_speed(drag->get_speed()); + relative += drag->get_relative(); + + return true; +} + void InputEventScreenDrag::_bind_methods() { ClassDB::bind_method(D_METHOD("set_index", "index"), &InputEventScreenDrag::set_index); ClassDB::bind_method(D_METHOD("get_index"), &InputEventScreenDrag::get_index); diff --git a/core/input/input_event.h b/core/input/input_event.h index 57b6091123..517d63eb40 100644 --- a/core/input/input_event.h +++ b/core/input/input_event.h @@ -410,6 +410,8 @@ public: virtual String as_text() const override; virtual String to_string() override; + virtual bool accumulate(const Ref<InputEvent> &p_event) override; + InputEventScreenDrag() {} }; diff --git a/platform/android/display_server_android.cpp b/platform/android/display_server_android.cpp index d200d024c5..c864a9b68d 100644 --- a/platform/android/display_server_android.cpp +++ b/platform/android/display_server_android.cpp @@ -557,7 +557,7 @@ void DisplayServerAndroid::process_key_event(int p_keycode, int p_scancode, int OS_Android::get_singleton()->main_loop_request_go_back(); } - Input::get_singleton()->accumulate_input_event(ev); + Input::get_singleton()->parse_input_event(ev); } void DisplayServerAndroid::process_touch(int p_event, int p_pointer, const Vector<DisplayServerAndroid::TouchPos> &p_points) { @@ -571,7 +571,7 @@ void DisplayServerAndroid::process_touch(int p_event, int p_pointer, const Vecto ev->set_index(touch[i].id); ev->set_pressed(false); ev->set_position(touch[i].pos); - Input::get_singleton()->accumulate_input_event(ev); + Input::get_singleton()->parse_input_event(ev); } } @@ -588,7 +588,7 @@ void DisplayServerAndroid::process_touch(int p_event, int p_pointer, const Vecto ev->set_index(touch[i].id); ev->set_pressed(true); ev->set_position(touch[i].pos); - Input::get_singleton()->accumulate_input_event(ev); + Input::get_singleton()->parse_input_event(ev); } } break; @@ -614,7 +614,7 @@ void DisplayServerAndroid::process_touch(int p_event, int p_pointer, const Vecto ev->set_index(touch[i].id); ev->set_position(p_points[idx].pos); ev->set_relative(p_points[idx].pos - touch[i].pos); - Input::get_singleton()->accumulate_input_event(ev); + Input::get_singleton()->parse_input_event(ev); touch.write[i].pos = p_points[idx].pos; } @@ -629,7 +629,7 @@ void DisplayServerAndroid::process_touch(int p_event, int p_pointer, const Vecto ev->set_index(touch[i].id); ev->set_pressed(false); ev->set_position(touch[i].pos); - Input::get_singleton()->accumulate_input_event(ev); + Input::get_singleton()->parse_input_event(ev); } touch.clear(); } @@ -646,7 +646,7 @@ void DisplayServerAndroid::process_touch(int p_event, int p_pointer, const Vecto ev->set_index(tp.id); ev->set_pressed(true); ev->set_position(tp.pos); - Input::get_singleton()->accumulate_input_event(ev); + Input::get_singleton()->parse_input_event(ev); break; } @@ -660,7 +660,7 @@ void DisplayServerAndroid::process_touch(int p_event, int p_pointer, const Vecto ev->set_index(touch[i].id); ev->set_pressed(false); ev->set_position(touch[i].pos); - Input::get_singleton()->accumulate_input_event(ev); + Input::get_singleton()->parse_input_event(ev); touch.remove(i); break; @@ -682,7 +682,7 @@ void DisplayServerAndroid::process_hover(int p_type, Point2 p_pos) { ev->set_position(p_pos); ev->set_global_position(p_pos); ev->set_relative(p_pos - hover_prev_pos); - Input::get_singleton()->accumulate_input_event(ev); + Input::get_singleton()->parse_input_event(ev); hover_prev_pos = p_pos; } break; } @@ -710,7 +710,7 @@ void DisplayServerAndroid::process_mouse_event(int input_device, int event_actio ev->set_button_index(_button_index_from_mask(changed_button_mask)); ev->set_button_mask(event_buttons_mask); - Input::get_singleton()->accumulate_input_event(ev); + Input::get_singleton()->parse_input_event(ev); } break; case AMOTION_EVENT_ACTION_MOVE: { @@ -728,7 +728,7 @@ void DisplayServerAndroid::process_mouse_event(int input_device, int event_actio ev->set_relative(event_pos); } ev->set_button_mask(event_buttons_mask); - Input::get_singleton()->accumulate_input_event(ev); + Input::get_singleton()->parse_input_event(ev); } break; case AMOTION_EVENT_ACTION_SCROLL: { Ref<InputEventMouseButton> ev; @@ -763,11 +763,11 @@ void DisplayServerAndroid::_wheel_button_click(MouseButton event_buttons_mask, c evd->set_button_index(wheel_button); evd->set_button_mask(MouseButton(event_buttons_mask ^ (1 << (wheel_button - 1)))); evd->set_factor(factor); - Input::get_singleton()->accumulate_input_event(evd); + Input::get_singleton()->parse_input_event(evd); Ref<InputEventMouseButton> evdd = evd->duplicate(); evdd->set_pressed(false); evdd->set_button_mask(event_buttons_mask); - Input::get_singleton()->accumulate_input_event(evdd); + Input::get_singleton()->parse_input_event(evdd); } void DisplayServerAndroid::process_double_tap(int event_android_button_mask, Point2 p_pos) { @@ -781,7 +781,7 @@ void DisplayServerAndroid::process_double_tap(int event_android_button_mask, Poi ev->set_button_index(_button_index_from_mask(event_button_mask)); ev->set_button_mask(event_button_mask); ev->set_double_click(true); - Input::get_singleton()->accumulate_input_event(ev); + Input::get_singleton()->parse_input_event(ev); } MouseButton DisplayServerAndroid::_button_index_from_mask(MouseButton button_mask) { @@ -807,7 +807,7 @@ void DisplayServerAndroid::process_scroll(Point2 p_pos) { _set_key_modifier_state(ev); ev->set_position(p_pos); ev->set_delta(p_pos - scroll_prev_pos); - Input::get_singleton()->accumulate_input_event(ev); + Input::get_singleton()->parse_input_event(ev); scroll_prev_pos = p_pos; } diff --git a/platform/linuxbsd/display_server_x11.cpp b/platform/linuxbsd/display_server_x11.cpp index 5b26b23806..9a368860e7 100644 --- a/platform/linuxbsd/display_server_x11.cpp +++ b/platform/linuxbsd/display_server_x11.cpp @@ -2247,7 +2247,7 @@ void DisplayServerX11::_handle_key_event(WindowID p_window, XKeyEvent *p_event, k->set_shift_pressed(true); } - Input::get_singleton()->accumulate_input_event(k); + Input::get_singleton()->parse_input_event(k); } memfree(utf8string); return; @@ -2396,7 +2396,7 @@ void DisplayServerX11::_handle_key_event(WindowID p_window, XKeyEvent *p_event, } } - Input::get_singleton()->accumulate_input_event(k); + Input::get_singleton()->parse_input_event(k); } Atom DisplayServerX11::_process_selection_request_target(Atom p_target, Window p_requestor, Atom p_property) const { @@ -2883,13 +2883,13 @@ void DisplayServerX11::process_events() { // in a spurious mouse motion event being sent to Godot; remember it to be able to filter it out xi.mouse_pos_to_filter = pos; } - Input::get_singleton()->accumulate_input_event(st); + Input::get_singleton()->parse_input_event(st); } else { if (!xi.state.has(index)) { // Defensive break; } xi.state.erase(index); - Input::get_singleton()->accumulate_input_event(st); + Input::get_singleton()->parse_input_event(st); } } break; @@ -2906,7 +2906,7 @@ void DisplayServerX11::process_events() { sd->set_index(index); sd->set_position(pos); sd->set_relative(pos - curr_pos_elem->value()); - Input::get_singleton()->accumulate_input_event(sd); + Input::get_singleton()->parse_input_event(sd); curr_pos_elem->value() = pos; } @@ -3058,7 +3058,7 @@ void DisplayServerX11::process_events() { st->set_index(E->key()); st->set_window_id(window_id); st->set_position(E->get()); - Input::get_singleton()->accumulate_input_event(st); + Input::get_singleton()->parse_input_event(st); } xi.state.clear(); #endif @@ -3156,7 +3156,7 @@ void DisplayServerX11::process_events() { mb->set_window_id(window_id_other); mb->set_position(Vector2(x, y)); mb->set_global_position(mb->get_position()); - Input::get_singleton()->accumulate_input_event(mb); + Input::get_singleton()->parse_input_event(mb); } break; } @@ -3164,7 +3164,7 @@ void DisplayServerX11::process_events() { } } - Input::get_singleton()->accumulate_input_event(mb); + Input::get_singleton()->parse_input_event(mb); } break; case MotionNotify: { @@ -3280,7 +3280,7 @@ void DisplayServerX11::process_events() { // this is so that the relative motion doesn't get messed up // after we regain focus. if (focused) { - Input::get_singleton()->accumulate_input_event(mm); + Input::get_singleton()->parse_input_event(mm); } else { // Propagate the event to the focused window, // because it's received only on the topmost window. @@ -3300,7 +3300,7 @@ void DisplayServerX11::process_events() { mm->set_position(pos_focused); mm->set_global_position(pos_focused); mm->set_speed(Input::get_singleton()->get_last_mouse_speed()); - Input::get_singleton()->accumulate_input_event(mm); + Input::get_singleton()->parse_input_event(mm); break; } diff --git a/platform/osx/display_server_osx.mm b/platform/osx/display_server_osx.mm index cc8400980a..47998044bd 100644 --- a/platform/osx/display_server_osx.mm +++ b/platform/osx/display_server_osx.mm @@ -690,7 +690,7 @@ static void _mouseDownEvent(DisplayServer::WindowID window_id, NSEvent *event, M mb->set_double_click([event clickCount] == 2); } - Input::get_singleton()->accumulate_input_event(mb); + Input::get_singleton()->parse_input_event(mb); } - (void)mouseDown:(NSEvent *)event { @@ -799,7 +799,7 @@ static void _mouseDownEvent(DisplayServer::WindowID window_id, NSEvent *event, M _get_key_modifier_state([event modifierFlags], mm); Input::get_singleton()->set_mouse_position(wd.mouse_pos); - Input::get_singleton()->accumulate_input_event(mm); + Input::get_singleton()->parse_input_event(mm); } - (void)rightMouseDown:(NSEvent *)event { @@ -875,7 +875,7 @@ static void _mouseDownEvent(DisplayServer::WindowID window_id, NSEvent *event, M ev->set_position(_get_mouse_pos(wd, [event locationInWindow])); ev->set_factor([event magnification] + 1.0); - Input::get_singleton()->accumulate_input_event(ev); + Input::get_singleton()->parse_input_event(ev); } - (void)viewDidChangeBackingProperties { @@ -1357,7 +1357,7 @@ inline void sendScrollEvent(DisplayServer::WindowID window_id, MouseButton butto DS_OSX->last_button_state |= (MouseButton)mask; sc->set_button_mask(DS_OSX->last_button_state); - Input::get_singleton()->accumulate_input_event(sc); + Input::get_singleton()->parse_input_event(sc); sc.instantiate(); sc->set_window_id(window_id); @@ -1369,7 +1369,7 @@ inline void sendScrollEvent(DisplayServer::WindowID window_id, MouseButton butto DS_OSX->last_button_state &= (MouseButton)~mask; sc->set_button_mask(DS_OSX->last_button_state); - Input::get_singleton()->accumulate_input_event(sc); + Input::get_singleton()->parse_input_event(sc); } inline void sendPanEvent(DisplayServer::WindowID window_id, double dx, double dy, int modifierFlags) { @@ -1384,7 +1384,7 @@ inline void sendPanEvent(DisplayServer::WindowID window_id, double dx, double dy pg->set_position(wd.mouse_pos); pg->set_delta(Vector2(-dx, -dy)); - Input::get_singleton()->accumulate_input_event(pg); + Input::get_singleton()->parse_input_event(pg); } - (void)scrollWheel:(NSEvent *)event { @@ -3198,7 +3198,7 @@ String DisplayServerOSX::keyboard_get_layout_name(int p_index) const { void DisplayServerOSX::_push_input(const Ref<InputEvent> &p_event) { Ref<InputEvent> ev = p_event; - Input::get_singleton()->accumulate_input_event(ev); + Input::get_singleton()->parse_input_event(ev); } void DisplayServerOSX::_release_pressed_events() { @@ -3253,7 +3253,7 @@ void DisplayServerOSX::_send_event(NSEvent *p_event) { k->set_physical_keycode(KEY_PERIOD); k->set_echo([p_event isARepeat]); - Input::get_singleton()->accumulate_input_event(k); + Input::get_singleton()->parse_input_event(k); } } } diff --git a/platform/windows/display_server_windows.cpp b/platform/windows/display_server_windows.cpp index 42a517879b..b49f7e3321 100644 --- a/platform/windows/display_server_windows.cpp +++ b/platform/windows/display_server_windows.cpp @@ -1738,7 +1738,7 @@ void DisplayServerWindows::_touch_event(WindowID p_window, bool p_pressed, float event->set_pressed(p_pressed); event->set_position(Vector2(p_x, p_y)); - Input::get_singleton()->accumulate_input_event(event); + Input::get_singleton()->parse_input_event(event); } void DisplayServerWindows::_drag_event(WindowID p_window, float p_x, float p_y, int idx) { @@ -1757,7 +1757,7 @@ void DisplayServerWindows::_drag_event(WindowID p_window, float p_x, float p_y, event->set_position(Vector2(p_x, p_y)); event->set_relative(Vector2(p_x, p_y) - curr->get()); - Input::get_singleton()->accumulate_input_event(event); + Input::get_singleton()->parse_input_event(event); curr->get() = Vector2(p_x, p_y); } @@ -2022,7 +2022,7 @@ LRESULT DisplayServerWindows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARA } if (windows[window_id].window_has_focus && mm->get_relative() != Vector2()) - Input::get_singleton()->accumulate_input_event(mm); + Input::get_singleton()->parse_input_event(mm); } delete[] lpb; } break; @@ -2111,7 +2111,7 @@ LRESULT DisplayServerWindows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARA old_x = mm->get_position().x; old_y = mm->get_position().y; if (windows[window_id].window_has_focus) - Input::get_singleton()->accumulate_input_event(mm); + Input::get_singleton()->parse_input_event(mm); } return 0; } @@ -2258,7 +2258,7 @@ LRESULT DisplayServerWindows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARA old_x = mm->get_position().x; old_y = mm->get_position().y; if (windows[window_id].window_has_focus) { - Input::get_singleton()->accumulate_input_event(mm); + Input::get_singleton()->parse_input_event(mm); } return 0; //Pointer event handled return 0 to avoid duplicate WM_MOUSEMOVE event @@ -2364,7 +2364,7 @@ LRESULT DisplayServerWindows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARA old_x = mm->get_position().x; old_y = mm->get_position().y; if (windows[window_id].window_has_focus) - Input::get_singleton()->accumulate_input_event(mm); + Input::get_singleton()->parse_input_event(mm); } break; case WM_LBUTTONDOWN: @@ -2533,7 +2533,7 @@ LRESULT DisplayServerWindows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARA mb->set_global_position(mb->get_position()); - Input::get_singleton()->accumulate_input_event(mb); + Input::get_singleton()->parse_input_event(mb); if (mb->is_pressed() && mb->get_button_index() > 3 && mb->get_button_index() < 8) { //send release for mouse wheel Ref<InputEventMouseButton> mbd = mb->duplicate(); @@ -2541,7 +2541,7 @@ LRESULT DisplayServerWindows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARA last_button_state &= (MouseButton) ~(1 << (mbd->get_button_index() - 1)); mbd->set_button_mask(last_button_state); mbd->set_pressed(false); - Input::get_singleton()->accumulate_input_event(mbd); + Input::get_singleton()->parse_input_event(mbd); } } break; @@ -2866,7 +2866,7 @@ void DisplayServerWindows::_process_key_events() { if (k->get_unicode() < 32) k->set_unicode(0); - Input::get_singleton()->accumulate_input_event(k); + Input::get_singleton()->parse_input_event(k); } //do nothing @@ -2924,7 +2924,7 @@ void DisplayServerWindows::_process_key_events() { k->set_echo((ke.uMsg == WM_KEYDOWN && (ke.lParam & (1 << 30)))); - Input::get_singleton()->accumulate_input_event(k); + Input::get_singleton()->parse_input_event(k); } break; } |