From dc187324beded208b8aef77d2e5e0cc1650f4d78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20J=2E=20Est=C3=A9banez?= Date: Fri, 13 Aug 2021 00:38:41 +0200 Subject: [PATCH] Add input buffering framework Input buffering is implicitly used by event accumulation, but this commit makes it more generic so it can be enabled for other uses. For desktop OSs it's currently not feasible given main and UI threads are the same). --- core/input/input.cpp | 32 +++++++++++++-------- core/input/input.h | 7 +++-- main/main.cpp | 5 ++++ platform/android/display_server_android.cpp | 2 +- platform/linuxbsd/display_server_x11.cpp | 2 +- platform/osx/display_server_osx.mm | 2 +- platform/windows/display_server_windows.cpp | 2 +- 7 files changed, 34 insertions(+), 18 deletions(-) diff --git a/core/input/input.cpp b/core/input/input.cpp index ffe2659126..8ba8b892ac 100644 --- a/core/input/input.cpp +++ b/core/input/input.cpp @@ -837,32 +837,40 @@ void Input::parse_input_event(const Ref &p_event) { ERR_FAIL_COND(p_event.is_null()); - if (!use_accumulated_input) { + if (use_accumulated_input) { + if (buffered_events.is_empty() || !buffered_events.back()->get()->accumulate(p_event)) { + buffered_events.push_back(p_event); + } + } else if (use_input_buffering) { + buffered_events.push_back(p_event); + } else { _parse_input_event_impl(p_event, false); - return; } - if (!accumulated_events.is_empty() && accumulated_events.back()->get()->accumulate(p_event)) { - return; //event was accumulated, exit - } - - accumulated_events.push_back(p_event); } -void Input::flush_accumulated_events() { +void Input::flush_buffered_events() { _THREAD_SAFE_METHOD_ - while (accumulated_events.front()) { - _parse_input_event_impl(accumulated_events.front()->get(), false); - accumulated_events.pop_front(); + while (buffered_events.front()) { + _parse_input_event_impl(buffered_events.front()->get(), false); + buffered_events.pop_front(); } } +bool Input::is_using_input_buffering() { + return use_input_buffering; +} + +void Input::set_use_input_buffering(bool p_enable) { + use_input_buffering = p_enable; +} + void Input::set_use_accumulated_input(bool p_enable) { use_accumulated_input = p_enable; } void Input::release_pressed_events() { - flush_accumulated_events(); // this is needed to release actions strengths + flush_buffered_events(); // this is needed to release actions strengths keys_pressed.clear(); joy_buttons_pressed.clear(); diff --git a/core/input/input.h b/core/input/input.h index d08e6437a5..6819fc8eb0 100644 --- a/core/input/input.h +++ b/core/input/input.h @@ -111,6 +111,7 @@ private: bool emulate_touch_from_mouse = false; bool emulate_mouse_from_touch = false; + bool use_input_buffering = false; bool use_accumulated_input = false; int mouse_from_touch_index = -1; @@ -213,7 +214,7 @@ private: void _parse_input_event_impl(const Ref &p_event, bool p_is_emulated); - List> accumulated_events; + List> buffered_events; friend class DisplayServer; @@ -323,7 +324,9 @@ public: String get_joy_guid(int p_device) const; void set_fallback_mapping(String p_guid); - void flush_accumulated_events(); + void flush_buffered_events(); + bool is_using_input_buffering(); + void set_use_input_buffering(bool p_enable); void set_use_accumulated_input(bool p_enable); void release_pressed_events(); diff --git a/main/main.cpp b/main/main.cpp index 7351dafa22..8cfcacbfe8 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -2587,6 +2587,11 @@ bool Main::iteration() { iterating--; + // Needed for OSs using input buffering regardless accumulation (like Android) + if (Input::get_singleton()->is_using_input_buffering()) { + Input::get_singleton()->flush_buffered_events(); + } + if (fixed_fps != -1) { return exit; } diff --git a/platform/android/display_server_android.cpp b/platform/android/display_server_android.cpp index c864a9b68d..7be4235876 100644 --- a/platform/android/display_server_android.cpp +++ b/platform/android/display_server_android.cpp @@ -335,7 +335,7 @@ bool DisplayServerAndroid::can_any_window_draw() const { } void DisplayServerAndroid::process_events() { - Input::get_singleton()->flush_accumulated_events(); + Input::get_singleton()->flush_buffered_events(); } Vector DisplayServerAndroid::get_rendering_drivers_func() { diff --git a/platform/linuxbsd/display_server_x11.cpp b/platform/linuxbsd/display_server_x11.cpp index 9a368860e7..a39941339a 100644 --- a/platform/linuxbsd/display_server_x11.cpp +++ b/platform/linuxbsd/display_server_x11.cpp @@ -3433,7 +3433,7 @@ void DisplayServerX11::process_events() { */ } - Input::get_singleton()->flush_accumulated_events(); + Input::get_singleton()->flush_buffered_events(); } void DisplayServerX11::release_rendering_thread() { diff --git a/platform/osx/display_server_osx.mm b/platform/osx/display_server_osx.mm index 47998044bd..43b7d7c1e0 100644 --- a/platform/osx/display_server_osx.mm +++ b/platform/osx/display_server_osx.mm @@ -3331,7 +3331,7 @@ void DisplayServerOSX::process_events() { if (!drop_events) { _process_key_events(); - Input::get_singleton()->flush_accumulated_events(); + Input::get_singleton()->flush_buffered_events(); } for (Map::Element *E = windows.front(); E; E = E->next()) { diff --git a/platform/windows/display_server_windows.cpp b/platform/windows/display_server_windows.cpp index b49f7e3321..b6489e7a95 100644 --- a/platform/windows/display_server_windows.cpp +++ b/platform/windows/display_server_windows.cpp @@ -1527,7 +1527,7 @@ void DisplayServerWindows::process_events() { if (!drop_events) { _process_key_events(); - Input::get_singleton()->flush_accumulated_events(); + Input::get_singleton()->flush_buffered_events(); } }