From 7be9c26e208743df1ebb264a5b4f3671b1cfca64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20J=2E=20Est=C3=A9banez?= Date: Tue, 3 Aug 2021 18:59:20 +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/os/input.h | 4 +++- main/input_default.cpp | 33 +++++++++++++++++++++------------ main/input_default.h | 7 +++++-- main/main.cpp | 5 +++++ platform/osx/os_osx.mm | 2 +- platform/windows/os_windows.cpp | 2 +- platform/x11/os_x11.cpp | 2 +- 7 files changed, 37 insertions(+), 18 deletions(-) diff --git a/core/os/input.h b/core/os/input.h index f3d1e322b07..738d0481fbc 100644 --- a/core/os/input.h +++ b/core/os/input.h @@ -136,7 +136,9 @@ public: virtual int get_joy_axis_index_from_string(String p_axis) = 0; virtual void parse_input_event(const Ref &p_event) = 0; - virtual void flush_accumulated_events() = 0; + virtual void flush_buffered_events() = 0; + virtual bool is_using_input_buffering() = 0; + virtual void set_use_input_buffering(bool p_enable) = 0; virtual void set_use_accumulated_input(bool p_enable) = 0; Input(); diff --git a/main/input_default.cpp b/main/input_default.cpp index e40a001d108..2fb17942c65 100644 --- a/main/input_default.cpp +++ b/main/input_default.cpp @@ -696,31 +696,39 @@ void InputDefault::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.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.empty() && accumulated_events.back()->get()->accumulate(p_event)) { - return; //event was accumulated, exit - } - - accumulated_events.push_back(p_event); } -void InputDefault::flush_accumulated_events() { +void InputDefault::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 InputDefault::is_using_input_buffering() { + return use_input_buffering; +} + +void InputDefault::set_use_input_buffering(bool p_enable) { + use_input_buffering = p_enable; +} + void InputDefault::set_use_accumulated_input(bool p_enable) { use_accumulated_input = p_enable; } void InputDefault::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(); @@ -734,6 +742,7 @@ void InputDefault::release_pressed_events() { } InputDefault::InputDefault() { + use_input_buffering = false; use_accumulated_input = false; mouse_button_mask = 0; emulate_touch_from_mouse = false; diff --git a/main/input_default.h b/main/input_default.h index 7402444c734..ed4144f31ae 100644 --- a/main/input_default.h +++ b/main/input_default.h @@ -205,7 +205,8 @@ private: void _parse_input_event_impl(const Ref &p_event, bool p_is_emulated); - List> accumulated_events; + List> buffered_events; + bool use_input_buffering; bool use_accumulated_input; protected: @@ -302,7 +303,9 @@ public: String get_joy_guid_remapped(int p_device) const; void set_fallback_mapping(String p_guid); - virtual void flush_accumulated_events(); + virtual void flush_buffered_events(); + virtual bool is_using_input_buffering(); + virtual void set_use_input_buffering(bool p_enable); virtual void set_use_accumulated_input(bool p_enable); virtual void release_pressed_events(); diff --git a/main/main.cpp b/main/main.cpp index 05c2a5ddf0d..5f6605f201c 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -2217,6 +2217,11 @@ bool Main::iteration() { iterating--; + // Needed for OSs using input buffering regardless accumulation (like Android) + if (InputDefault::get_singleton()->is_using_input_buffering()) { + InputDefault::get_singleton()->flush_buffered_events(); + } + if (fixed_fps != -1) { return exit; } diff --git a/platform/osx/os_osx.mm b/platform/osx/os_osx.mm index 4355a835db1..15b0b246c90 100644 --- a/platform/osx/os_osx.mm +++ b/platform/osx/os_osx.mm @@ -3159,7 +3159,7 @@ void OS_OSX::process_events() { [autoreleasePool drain]; autoreleasePool = [[NSAutoreleasePool alloc] init]; - input->flush_accumulated_events(); + input->flush_buffered_events(); } void OS_OSX::process_key_events() { diff --git a/platform/windows/os_windows.cpp b/platform/windows/os_windows.cpp index 32807d0a5d2..3a70273f9fa 100644 --- a/platform/windows/os_windows.cpp +++ b/platform/windows/os_windows.cpp @@ -2529,7 +2529,7 @@ void OS_Windows::process_events() { if (!drop_events) { process_key_events(); - input->flush_accumulated_events(); + input->flush_buffered_events(); } } diff --git a/platform/x11/os_x11.cpp b/platform/x11/os_x11.cpp index 3d64d5674a5..f60ed01c68f 100644 --- a/platform/x11/os_x11.cpp +++ b/platform/x11/os_x11.cpp @@ -2904,7 +2904,7 @@ void OS_X11::process_xevents() { */ } - input->flush_accumulated_events(); + input->flush_buffered_events(); } MainLoop *OS_X11::get_main_loop() const {