From bdf4f38ac49a90a3bfd73074a40fe85f94675e4c Mon Sep 17 00:00:00 2001 From: Fredia Huya-Kouadio Date: Fri, 28 Feb 2025 13:54:30 -0800 Subject: [PATCH] Fix Android mouse capture issues - Allow mouse capture to be enabled in `_ready` - Update the input handler logic to avoid dropping mouse captured motion events (cherry picked from commit bea6472ea479f5c96cad825e71cf3a1d6a69ccdc) --- platform/android/android_input_handler.cpp | 2 +- .../org/godotengine/godot/input/GodotInputHandler.java | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/platform/android/android_input_handler.cpp b/platform/android/android_input_handler.cpp index 9a5af7a349d..859f4c3c1b3 100644 --- a/platform/android/android_input_handler.cpp +++ b/platform/android/android_input_handler.cpp @@ -337,7 +337,7 @@ void AndroidInputHandler::process_mouse_event(int p_event_action, int p_event_an } break; case AMOTION_EVENT_ACTION_MOVE: { - if (!mouse_event_info.valid) { + if (!p_source_mouse_relative && !mouse_event_info.valid) { return; } diff --git a/platform/android/java/lib/src/org/godotengine/godot/input/GodotInputHandler.java b/platform/android/java/lib/src/org/godotengine/godot/input/GodotInputHandler.java index 6f036c15a32..d9d1cedc6a0 100644 --- a/platform/android/java/lib/src/org/godotengine/godot/input/GodotInputHandler.java +++ b/platform/android/java/lib/src/org/godotengine/godot/input/GodotInputHandler.java @@ -59,6 +59,7 @@ import androidx.annotation.NonNull; import java.util.Collections; import java.util.HashSet; import java.util.Set; +import java.util.concurrent.atomic.AtomicInteger; /** * Handles input related events for the {@link GodotRenderView} view. @@ -83,7 +84,7 @@ public class GodotInputHandler implements InputManager.InputDeviceListener, Sens /** * Used to decide whether mouse capture can be enabled. */ - private int lastSeenToolType = MotionEvent.TOOL_TYPE_UNKNOWN; + private AtomicInteger lastSeenToolType = new AtomicInteger(MotionEvent.TOOL_TYPE_UNKNOWN); private int rotaryInputAxis = ROTARY_INPUT_VERTICAL_AXIS; @@ -149,7 +150,8 @@ public class GodotInputHandler implements InputManager.InputDeviceListener, Sens } public boolean canCapturePointer() { - return lastSeenToolType == MotionEvent.TOOL_TYPE_MOUSE; + return lastSeenToolType.get() == MotionEvent.TOOL_TYPE_MOUSE || + lastSeenToolType.get() == MotionEvent.TOOL_TYPE_UNKNOWN; } public void onPointerCaptureChange(boolean hasCapture) { @@ -210,7 +212,7 @@ public class GodotInputHandler implements InputManager.InputDeviceListener, Sens } public boolean onTouchEvent(final MotionEvent event) { - lastSeenToolType = getEventToolType(event); + lastSeenToolType.set(getEventToolType(event)); this.scaleGestureDetector.onTouchEvent(event); if (this.gestureDetector.onTouchEvent(event)) { @@ -236,7 +238,7 @@ public class GodotInputHandler implements InputManager.InputDeviceListener, Sens } public boolean onGenericMotionEvent(MotionEvent event) { - lastSeenToolType = getEventToolType(event); + lastSeenToolType.set(getEventToolType(event)); if (event.isFromSource(InputDevice.SOURCE_JOYSTICK) && event.getActionMasked() == MotionEvent.ACTION_MOVE) { // Check if the device exists