diff options
author | Fredia Huya-Kouadio <fhuyakou@gmail.com> | 2022-08-14 23:07:43 -0700 |
---|---|---|
committer | Fredia Huya-Kouadio <fhuyakou@gmail.com> | 2022-09-07 13:30:10 -0700 |
commit | 1b3511ad49ae6e8dd3a1ce6ce89ba50c823a585b (patch) | |
tree | 6d4b662492e5b4deabce4ef83049eabb17153194 /platform/android/java/lib | |
parent | 337e4d185af0ff0ea2dba60abecd4af50b9d10f8 (diff) |
Cleanup the Android input logic implementation
Diffstat (limited to 'platform/android/java/lib')
6 files changed, 521 insertions, 215 deletions
diff --git a/platform/android/java/lib/src/org/godotengine/godot/GodotGLRenderView.java b/platform/android/java/lib/src/org/godotengine/godot/GodotGLRenderView.java index 08da1b1832..513021f1d1 100644 --- a/platform/android/java/lib/src/org/godotengine/godot/GodotGLRenderView.java +++ b/platform/android/java/lib/src/org/godotengine/godot/GodotGLRenderView.java @@ -31,7 +31,6 @@ package org.godotengine.godot; import org.godotengine.godot.gl.GLSurfaceView; import org.godotengine.godot.gl.GodotRenderer; -import org.godotengine.godot.input.GodotGestureHandler; import org.godotengine.godot.input.GodotInputHandler; import org.godotengine.godot.utils.GLUtils; import org.godotengine.godot.xr.XRMode; @@ -46,7 +45,6 @@ import android.annotation.SuppressLint; import android.content.Context; import android.graphics.PixelFormat; import android.os.Build; -import android.view.GestureDetector; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.PointerIcon; @@ -75,7 +73,6 @@ import androidx.annotation.Keep; public class GodotGLRenderView extends GLSurfaceView implements GodotRenderView { private final Godot godot; private final GodotInputHandler inputHandler; - private final GestureDetector detector; private final GodotRenderer godotRenderer; private PointerIcon pointerIcon; @@ -85,7 +82,6 @@ public class GodotGLRenderView extends GLSurfaceView implements GodotRenderView this.godot = godot; this.inputHandler = new GodotInputHandler(this); - this.detector = new GestureDetector(context, new GodotGestureHandler(this)); this.godotRenderer = new GodotRenderer(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { pointerIcon = PointerIcon.getSystemIcon(getContext(), PointerIcon.TYPE_DEFAULT); @@ -132,7 +128,6 @@ public class GodotGLRenderView extends GLSurfaceView implements GodotRenderView @Override public boolean onTouchEvent(MotionEvent event) { super.onTouchEvent(event); - this.detector.onTouchEvent(event); return inputHandler.onTouchEvent(event); } @@ -156,6 +151,24 @@ public class GodotGLRenderView extends GLSurfaceView implements GodotRenderView return inputHandler.onGenericMotionEvent(event); } + @Override + public void onPointerCaptureChange(boolean hasCapture) { + super.onPointerCaptureChange(hasCapture); + inputHandler.onPointerCaptureChange(hasCapture); + } + + @Override + public void requestPointerCapture() { + super.requestPointerCapture(); + inputHandler.onPointerCaptureChange(true); + } + + @Override + public void releasePointerCapture() { + super.releasePointerCapture(); + inputHandler.onPointerCaptureChange(false); + } + /** * called from JNI to change pointer icon */ diff --git a/platform/android/java/lib/src/org/godotengine/godot/GodotLib.java b/platform/android/java/lib/src/org/godotengine/godot/GodotLib.java index f855fc6cf6..d4fa12ed15 100644 --- a/platform/android/java/lib/src/org/godotengine/godot/GodotLib.java +++ b/platform/android/java/lib/src/org/godotengine/godot/GodotLib.java @@ -92,7 +92,7 @@ public class GodotLib { public static native void newcontext(Surface p_surface); /** - * Forward {@link Activity#onBackPressed()} event from the main thread to the GL thread. + * Forward {@link Activity#onBackPressed()} event. */ public static native void back(); @@ -108,63 +108,60 @@ public class GodotLib { public static native void ttsCallback(int event, int id, int pos); /** - * Forward touch events from the main thread to the GL thread. + * Forward touch events. */ - public static native void touch(int inputDevice, int event, int pointer, int pointerCount, float[] positions); - public static native void touch(int inputDevice, int event, int pointer, int pointerCount, float[] positions, int buttonsMask); - public static native void touch(int inputDevice, int event, int pointer, int pointerCount, float[] positions, int buttonsMask, float verticalFactor, float horizontalFactor); + public static native void dispatchTouchEvent(int event, int pointer, int pointerCount, float[] positions); /** - * Forward hover events from the main thread to the GL thread. + * Dispatch mouse events */ - public static native void hover(int type, float x, float y); + public static native void dispatchMouseEvent(int event, int buttonMask, float x, float y, float deltaX, float deltaY, boolean doubleClick, boolean sourceMouseRelative); - /** - * Forward double_tap events from the main thread to the GL thread. - */ - public static native void doubleTap(int buttonMask, int x, int y); + public static native void magnify(float x, float y, float factor); + + public static native void pan(float x, float y, float deltaX, float deltaY); /** - * Forward accelerometer sensor events from the main thread to the GL thread. + * Forward accelerometer sensor events. * @see android.hardware.SensorEventListener#onSensorChanged(SensorEvent) */ public static native void accelerometer(float x, float y, float z); /** - * Forward gravity sensor events from the main thread to the GL thread. + * Forward gravity sensor events. * @see android.hardware.SensorEventListener#onSensorChanged(SensorEvent) */ public static native void gravity(float x, float y, float z); /** - * Forward magnetometer sensor events from the main thread to the GL thread. + * Forward magnetometer sensor events. * @see android.hardware.SensorEventListener#onSensorChanged(SensorEvent) */ public static native void magnetometer(float x, float y, float z); /** - * Forward gyroscope sensor events from the main thread to the GL thread. + * Forward gyroscope sensor events. * @see android.hardware.SensorEventListener#onSensorChanged(SensorEvent) */ public static native void gyroscope(float x, float y, float z); /** - * Forward regular key events from the main thread to the GL thread. + * Forward regular key events. */ public static native void key(int p_keycode, int p_physical_keycode, int p_unicode, boolean p_pressed); /** - * Forward game device's key events from the main thread to the GL thread. + * Forward game device's key events. */ public static native void joybutton(int p_device, int p_but, boolean p_pressed); /** - * Forward joystick devices axis motion events from the main thread to the GL thread. + * Forward joystick devices axis motion events. */ public static native void joyaxis(int p_device, int p_axis, float p_value); /** - * Forward joystick devices hat motion events from the main thread to the GL thread. + * Forward joystick devices hat motion events. */ public static native void joyhat(int p_device, int p_hat_x, int p_hat_y); diff --git a/platform/android/java/lib/src/org/godotengine/godot/GodotVulkanRenderView.java b/platform/android/java/lib/src/org/godotengine/godot/GodotVulkanRenderView.java index c386a2d2eb..fa6c3280b9 100644 --- a/platform/android/java/lib/src/org/godotengine/godot/GodotVulkanRenderView.java +++ b/platform/android/java/lib/src/org/godotengine/godot/GodotVulkanRenderView.java @@ -30,7 +30,6 @@ package org.godotengine.godot; -import org.godotengine.godot.input.GodotGestureHandler; import org.godotengine.godot.input.GodotInputHandler; import org.godotengine.godot.vulkan.VkRenderer; import org.godotengine.godot.vulkan.VkSurfaceView; @@ -38,7 +37,6 @@ import org.godotengine.godot.vulkan.VkSurfaceView; import android.annotation.SuppressLint; import android.content.Context; import android.os.Build; -import android.view.GestureDetector; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.PointerIcon; @@ -49,7 +47,6 @@ import androidx.annotation.Keep; public class GodotVulkanRenderView extends VkSurfaceView implements GodotRenderView { private final Godot godot; private final GodotInputHandler mInputHandler; - private final GestureDetector mGestureDetector; private final VkRenderer mRenderer; private PointerIcon pointerIcon; @@ -58,7 +55,6 @@ public class GodotVulkanRenderView extends VkSurfaceView implements GodotRenderV this.godot = godot; mInputHandler = new GodotInputHandler(this); - mGestureDetector = new GestureDetector(context, new GodotGestureHandler(this)); mRenderer = new VkRenderer(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { pointerIcon = PointerIcon.getSystemIcon(getContext(), PointerIcon.TYPE_DEFAULT); @@ -106,7 +102,6 @@ public class GodotVulkanRenderView extends VkSurfaceView implements GodotRenderV @Override public boolean onTouchEvent(MotionEvent event) { super.onTouchEvent(event); - mGestureDetector.onTouchEvent(event); return mInputHandler.onTouchEvent(event); } @@ -130,6 +125,24 @@ public class GodotVulkanRenderView extends VkSurfaceView implements GodotRenderV return mInputHandler.onGenericMotionEvent(event); } + @Override + public void requestPointerCapture() { + super.requestPointerCapture(); + mInputHandler.onPointerCaptureChange(true); + } + + @Override + public void releasePointerCapture() { + super.releasePointerCapture(); + mInputHandler.onPointerCaptureChange(false); + } + + @Override + public void onPointerCaptureChange(boolean hasCapture) { + super.onPointerCaptureChange(hasCapture); + mInputHandler.onPointerCaptureChange(hasCapture); + } + /** * called from JNI to change pointer icon */ diff --git a/platform/android/java/lib/src/org/godotengine/godot/input/GodotGestureHandler.java b/platform/android/java/lib/src/org/godotengine/godot/input/GodotGestureHandler.java deleted file mode 100644 index 778efa914a..0000000000 --- a/platform/android/java/lib/src/org/godotengine/godot/input/GodotGestureHandler.java +++ /dev/null @@ -1,87 +0,0 @@ -/*************************************************************************/ -/* GodotGestureHandler.java */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -package org.godotengine.godot.input; - -import org.godotengine.godot.GodotLib; -import org.godotengine.godot.GodotRenderView; - -import android.view.GestureDetector; -import android.view.MotionEvent; - -/** - * Handles gesture input related events for the {@link GodotRenderView} view. - * https://developer.android.com/reference/android/view/GestureDetector.SimpleOnGestureListener - */ -public class GodotGestureHandler extends GestureDetector.SimpleOnGestureListener { - private final GodotRenderView mRenderView; - - public GodotGestureHandler(GodotRenderView godotView) { - mRenderView = godotView; - } - - private void queueEvent(Runnable task) { - mRenderView.queueOnRenderThread(task); - } - - @Override - public boolean onDown(MotionEvent event) { - super.onDown(event); - //Log.i("GodotGesture", "onDown"); - return true; - } - - @Override - public boolean onSingleTapConfirmed(MotionEvent event) { - super.onSingleTapConfirmed(event); - return true; - } - - @Override - public void onLongPress(MotionEvent event) { - //Log.i("GodotGesture", "onLongPress"); - } - - @Override - public boolean onDoubleTap(MotionEvent event) { - //Log.i("GodotGesture", "onDoubleTap"); - final int x = Math.round(event.getX()); - final int y = Math.round(event.getY()); - final int buttonMask = event.getButtonState(); - GodotLib.doubleTap(buttonMask, x, y); - return true; - } - - @Override - public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) { - //Log.i("GodotGesture", "onFling"); - return true; - } -} diff --git a/platform/android/java/lib/src/org/godotengine/godot/input/GodotGestureHandler.kt b/platform/android/java/lib/src/org/godotengine/godot/input/GodotGestureHandler.kt new file mode 100644 index 0000000000..9715c31fc1 --- /dev/null +++ b/platform/android/java/lib/src/org/godotengine/godot/input/GodotGestureHandler.kt @@ -0,0 +1,289 @@ +/*************************************************************************/ +/* GodotGestureHandler.kt */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +package org.godotengine.godot.input + +import android.os.Build +import android.view.GestureDetector.SimpleOnGestureListener +import android.view.InputDevice +import android.view.MotionEvent +import android.view.ScaleGestureDetector +import android.view.ScaleGestureDetector.OnScaleGestureListener +import org.godotengine.godot.GodotLib + +/** + * Handles regular and scale gesture input related events for the [GodotView] view. + * + * @See https://developer.android.com/reference/android/view/GestureDetector.SimpleOnGestureListener + * @See https://developer.android.com/reference/android/view/ScaleGestureDetector.OnScaleGestureListener + */ +internal class GodotGestureHandler : SimpleOnGestureListener(), OnScaleGestureListener { + + companion object { + private val TAG = GodotGestureHandler::class.java.simpleName + } + + /** + * Enable pan and scale gestures + */ + var panningAndScalingEnabled = false + + private var doubleTapInProgress = false + private var dragInProgress = false + private var scaleInProgress = false + private var contextClickInProgress = false + private var pointerCaptureInProgress = false + + override fun onDown(event: MotionEvent): Boolean { + // Don't send / register a down event while we're in the middle of a double-tap + if (!doubleTapInProgress) { + // Send the down event + GodotInputHandler.handleMotionEvent(event) + } + return true + } + + override fun onSingleTapUp(event: MotionEvent): Boolean { + GodotInputHandler.handleMotionEvent(event) + return true + } + + override fun onLongPress(event: MotionEvent) { + contextClickRouter(event) + } + + private fun contextClickRouter(event: MotionEvent) { + if (scaleInProgress) { + return + } + + // Cancel the previous down event + GodotInputHandler.handleMotionEvent( + event.source, + MotionEvent.ACTION_CANCEL, + event.buttonState, + event.x, + event.y + ) + + // Turn a context click into a single tap right mouse button click. + GodotInputHandler.handleMouseEvent( + MotionEvent.ACTION_DOWN, + MotionEvent.BUTTON_SECONDARY, + event.x, + event.y + ) + contextClickInProgress = true + } + + fun onPointerCaptureChange(hasCapture: Boolean) { + if (pointerCaptureInProgress == hasCapture) { + return + } + + if (!hasCapture) { + // Dispatch a mouse relative ACTION_UP event to signal the end of the capture + GodotInputHandler.handleMouseEvent( + MotionEvent.ACTION_UP, + 0, + 0f, + 0f, + 0f, + 0f, + false, + true + ) + } + pointerCaptureInProgress = hasCapture + } + + fun onMotionEvent(event: MotionEvent): Boolean { + return when (event.actionMasked) { + MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL, MotionEvent.ACTION_BUTTON_RELEASE -> { + onActionUp(event) + } + MotionEvent.ACTION_MOVE -> { + onActionMove(event) + } + else -> false + } + } + + private fun onActionUp(event: MotionEvent): Boolean { + val sourceMouseRelative = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + event.isFromSource(InputDevice.SOURCE_MOUSE_RELATIVE) + } else { + false + } + when { + pointerCaptureInProgress -> { + return if (event.actionMasked == MotionEvent.ACTION_CANCEL) { + // Don't dispatch the ACTION_CANCEL while a capture is in progress + true + } else { + GodotInputHandler.handleMouseEvent( + MotionEvent.ACTION_UP, + event.buttonState, + event.x, + event.y, + 0f, + 0f, + false, + sourceMouseRelative + ) + pointerCaptureInProgress = false + true + } + } + dragInProgress -> { + GodotInputHandler.handleMotionEvent(event) + dragInProgress = false + return true + } + contextClickInProgress -> { + GodotInputHandler.handleMouseEvent( + event.actionMasked, + 0, + event.x, + event.y, + 0f, + 0f, + false, + sourceMouseRelative + ) + contextClickInProgress = false + return true + } + else -> return false + } + } + + private fun onActionMove(event: MotionEvent): Boolean { + if (contextClickInProgress) { + val sourceMouseRelative = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + event.isFromSource(InputDevice.SOURCE_MOUSE_RELATIVE) + } else { + false + } + GodotInputHandler.handleMouseEvent( + event.actionMasked, + MotionEvent.BUTTON_SECONDARY, + event.x, + event.y, + 0f, + 0f, + false, + sourceMouseRelative + ) + return true + } + return false + } + + override fun onDoubleTapEvent(event: MotionEvent): Boolean { + if (event.actionMasked == MotionEvent.ACTION_UP) { + doubleTapInProgress = false + } + return true + } + + override fun onDoubleTap(event: MotionEvent): Boolean { + doubleTapInProgress = true + val x = event.x + val y = event.y + val buttonMask = + if (GodotInputHandler.isMouseEvent(event)) { + event.buttonState + } else { + MotionEvent.BUTTON_PRIMARY + } + GodotInputHandler.handleMouseEvent(MotionEvent.ACTION_DOWN, buttonMask, x, y, true) + GodotInputHandler.handleMouseEvent(MotionEvent.ACTION_UP, 0, x, y, false) + + return true + } + + override fun onScroll( + originEvent: MotionEvent, + terminusEvent: MotionEvent, + distanceX: Float, + distanceY: Float + ): Boolean { + if (scaleInProgress) { + if (dragInProgress) { + // Cancel the drag + GodotInputHandler.handleMotionEvent( + originEvent.source, + MotionEvent.ACTION_CANCEL, + originEvent.buttonState, + originEvent.x, + originEvent.y + ) + dragInProgress = false + } + return true + } + + dragInProgress = true + + val x = terminusEvent.x + val y = terminusEvent.y + if (terminusEvent.pointerCount >= 2 && panningAndScalingEnabled) { + GodotLib.pan(x, y, distanceX / 5f, distanceY / 5f) + } else { + GodotInputHandler.handleMotionEvent(terminusEvent) + } + return true + } + + override fun onScale(detector: ScaleGestureDetector?): Boolean { + if (detector == null || !panningAndScalingEnabled) { + return false + } + GodotLib.magnify( + detector.focusX, + detector.focusY, + detector.scaleFactor + ) + return true + } + + override fun onScaleBegin(detector: ScaleGestureDetector?): Boolean { + if (detector == null || !panningAndScalingEnabled) { + return false + } + scaleInProgress = true + return true + } + + override fun onScaleEnd(detector: ScaleGestureDetector?) { + scaleInProgress = false + } +} 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 da15b2490c..03cb8034fa 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 @@ -41,13 +41,13 @@ import android.os.Build; import android.util.Log; import android.util.SparseArray; import android.util.SparseIntArray; +import android.view.GestureDetector; import android.view.InputDevice; -import android.view.InputDevice.MotionRange; import android.view.KeyEvent; import android.view.MotionEvent; +import android.view.ScaleGestureDetector; import java.util.Collections; -import java.util.Comparator; import java.util.HashSet; import java.util.Set; @@ -55,21 +55,49 @@ import java.util.Set; * Handles input related events for the {@link GodotRenderView} view. */ public class GodotInputHandler implements InputManager.InputDeviceListener { - private final GodotRenderView mRenderView; - private final InputManager mInputManager; - - private final String tag = this.getClass().getSimpleName(); + private static final String TAG = GodotInputHandler.class.getSimpleName(); private final SparseIntArray mJoystickIds = new SparseIntArray(4); private final SparseArray<Joystick> mJoysticksDevices = new SparseArray<>(4); + private final GodotRenderView mRenderView; + private final InputManager mInputManager; + private final GestureDetector gestureDetector; + private final ScaleGestureDetector scaleGestureDetector; + private final GodotGestureHandler godotGestureHandler; + public GodotInputHandler(GodotRenderView godotView) { + final Context context = godotView.getView().getContext(); mRenderView = godotView; - mInputManager = (InputManager)mRenderView.getView().getContext().getSystemService(Context.INPUT_SERVICE); + mInputManager = (InputManager)context.getSystemService(Context.INPUT_SERVICE); mInputManager.registerInputDeviceListener(this, null); + + this.godotGestureHandler = new GodotGestureHandler(); + this.gestureDetector = new GestureDetector(context, godotGestureHandler); + this.gestureDetector.setIsLongpressEnabled(false); + this.scaleGestureDetector = new ScaleGestureDetector(context, godotGestureHandler); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + this.scaleGestureDetector.setStylusScaleEnabled(true); + } } - private boolean isKeyEvent_GameDevice(int source) { + /** + * Enable long press events. This is false by default. + */ + public void enableLongPress(boolean enable) { + this.gestureDetector.setIsLongpressEnabled(enable); + } + + /** + * Enable multi-fingers pan & scale gestures. This is false by default. + * + * Note: This may interfere with multi-touch handling / support. + */ + public void enablePanningAndScalingGestures(boolean enable) { + this.godotGestureHandler.setPanningAndScalingEnabled(enable); + } + + private boolean isKeyEventGameDevice(int source) { // Note that keyboards are often (SOURCE_KEYBOARD | SOURCE_DPAD) if (source == (InputDevice.SOURCE_KEYBOARD | InputDevice.SOURCE_DPAD)) return false; @@ -77,6 +105,10 @@ public class GodotInputHandler implements InputManager.InputDeviceListener { return (source & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK || (source & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD || (source & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD; } + public void onPointerCaptureChange(boolean hasCapture) { + godotGestureHandler.onPointerCaptureChange(hasCapture); + } + public boolean onKeyUp(final int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { return true; @@ -87,7 +119,7 @@ public class GodotInputHandler implements InputManager.InputDeviceListener { } int source = event.getSource(); - if (isKeyEvent_GameDevice(source)) { + if (isKeyEventGameDevice(source)) { // Check if the device exists final int deviceId = event.getDeviceId(); if (mJoystickIds.indexOfKey(deviceId) >= 0) { @@ -121,11 +153,10 @@ public class GodotInputHandler implements InputManager.InputDeviceListener { } int source = event.getSource(); - //Log.e(TAG, String.format("Key down! source %d, device %d, joystick %d, %d, %d", event.getDeviceId(), source, (source & InputDevice.SOURCE_JOYSTICK), (source & InputDevice.SOURCE_DPAD), (source & InputDevice.SOURCE_GAMEPAD))); final int deviceId = event.getDeviceId(); // Check if source is a game device and that the device is a registered gamepad - if (isKeyEvent_GameDevice(source)) { + if (isKeyEventGameDevice(source)) { if (event.getRepeatCount() > 0) // ignore key echo return true; @@ -145,47 +176,41 @@ public class GodotInputHandler implements InputManager.InputDeviceListener { } public boolean onTouchEvent(final MotionEvent event) { - // Mouse drag (mouse pressed and move) doesn't fire onGenericMotionEvent so this is needed - if (event.isFromSource(InputDevice.SOURCE_MOUSE)) { - if (event.getAction() != MotionEvent.ACTION_MOVE) { - // we return true because every time a mouse event is fired, the event is already handled - // in onGenericMotionEvent, so by touch event we can say that the event is also handled - return true; - } - return handleMouseEvent(event); + this.scaleGestureDetector.onTouchEvent(event); + if (this.gestureDetector.onTouchEvent(event)) { + // The gesture detector has handled the event. + return true; } - final int evcount = event.getPointerCount(); - if (evcount == 0) + if (godotGestureHandler.onMotionEvent(event)) { + // The gesture handler has handled the event. return true; + } - if (mRenderView != null) { - final float[] arr = new float[event.getPointerCount() * 3]; // pointerId1, x1, y1, pointerId2, etc... + // Drag events are handled by the [GodotGestureHandler] + if (event.getActionMasked() == MotionEvent.ACTION_MOVE) { + return true; + } - for (int i = 0; i < event.getPointerCount(); i++) { - arr[i * 3 + 0] = event.getPointerId(i); - arr[i * 3 + 1] = event.getX(i); - arr[i * 3 + 2] = event.getY(i); - } - final int action = event.getActionMasked(); - final int pointer_idx = event.getPointerId(event.getActionIndex()); - - switch (action) { - case MotionEvent.ACTION_DOWN: - case MotionEvent.ACTION_CANCEL: - case MotionEvent.ACTION_UP: - case MotionEvent.ACTION_MOVE: - case MotionEvent.ACTION_POINTER_UP: - case MotionEvent.ACTION_POINTER_DOWN: { - GodotLib.touch(event.getSource(), action, pointer_idx, evcount, arr); - } break; - } + if (isMouseEvent(event)) { + return handleMouseEvent(event); } - return true; + + return handleTouchEvent(event); } public boolean onGenericMotionEvent(MotionEvent event) { - if (event.isFromSource(InputDevice.SOURCE_JOYSTICK) && event.getAction() == MotionEvent.ACTION_MOVE) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && gestureDetector.onGenericMotionEvent(event)) { + // The gesture detector has handled the event. + return true; + } + + if (godotGestureHandler.onMotionEvent(event)) { + // The gesture handler has handled the event. + return true; + } + + if (event.isFromSource(InputDevice.SOURCE_JOYSTICK) && event.getActionMasked() == MotionEvent.ACTION_MOVE) { // Check if the device exists final int deviceId = event.getDeviceId(); if (mJoystickIds.indexOfKey(deviceId) >= 0) { @@ -198,15 +223,14 @@ public class GodotInputHandler implements InputManager.InputDeviceListener { for (int i = 0; i < joystick.axes.size(); i++) { final int axis = joystick.axes.get(i); final float value = event.getAxisValue(axis); - /** - * As all axes are polled for each event, only fire an axis event if the value has actually changed. - * Prevents flooding Godot with repeated events. + /* + As all axes are polled for each event, only fire an axis event if the value has actually changed. + Prevents flooding Godot with repeated events. */ if (joystick.axesValues.indexOfKey(axis) < 0 || (float)joystick.axesValues.get(axis) != value) { // save value to prevent repeats joystick.axesValues.put(axis, value); - final int godotAxisIdx = i; - GodotLib.joyaxis(godotJoyId, godotAxisIdx, value); + GodotLib.joyaxis(godotJoyId, i, value); } } @@ -221,17 +245,8 @@ public class GodotInputHandler implements InputManager.InputDeviceListener { } return true; } - } else if (event.isFromSource(InputDevice.SOURCE_STYLUS)) { - final float x = event.getX(); - final float y = event.getY(); - final int type = event.getAction(); - GodotLib.hover(type, x, y); - return true; - - } else if (event.isFromSource(InputDevice.SOURCE_MOUSE) || event.isFromSource(InputDevice.SOURCE_MOUSE_RELATIVE)) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - return handleMouseEvent(event); - } + } else if (isMouseEvent(event)) { + return handleMouseEvent(event); } return false; @@ -243,7 +258,7 @@ public class GodotInputHandler implements InputManager.InputDeviceListener { for (int deviceId : deviceIds) { InputDevice device = mInputManager.getInputDevice(deviceId); if (DEBUG) { - Log.v("GodotInputHandler", String.format("init() deviceId:%d, Name:%s\n", deviceId, device.getName())); + Log.v(TAG, String.format("init() deviceId:%d, Name:%s\n", deviceId, device.getName())); } onInputDeviceAdded(deviceId); } @@ -288,13 +303,12 @@ public class GodotInputHandler implements InputManager.InputDeviceListener { joystick.name = device.getName(); //Helps with creating new joypad mappings. - Log.i(tag, "=== New Input Device: " + joystick.name); + Log.i(TAG, "=== New Input Device: " + joystick.name); Set<Integer> already = new HashSet<>(); for (InputDevice.MotionRange range : device.getMotionRanges()) { boolean isJoystick = range.isFromSource(InputDevice.SOURCE_JOYSTICK); boolean isGamepad = range.isFromSource(InputDevice.SOURCE_GAMEPAD); - //Log.i(tag, "axis: "+range.getAxis()+ ", isJoystick: "+isJoystick+", isGamepad: "+isGamepad); if (!isJoystick && !isGamepad) { continue; } @@ -306,14 +320,14 @@ public class GodotInputHandler implements InputManager.InputDeviceListener { already.add(axis); joystick.axes.add(axis); } else { - Log.w(tag, " - DUPLICATE AXIS VALUE IN LIST: " + axis); + Log.w(TAG, " - DUPLICATE AXIS VALUE IN LIST: " + axis); } } } Collections.sort(joystick.axes); for (int idx = 0; idx < joystick.axes.size(); idx++) { //Helps with creating new joypad mappings. - Log.i(tag, " - Mapping Android axis " + joystick.axes.get(idx) + " to Godot axis " + idx); + Log.i(TAG, " - Mapping Android axis " + joystick.axes.get(idx) + " to Godot axis " + idx); } mJoysticksDevices.put(deviceId, joystick); @@ -338,13 +352,6 @@ public class GodotInputHandler implements InputManager.InputDeviceListener { onInputDeviceAdded(deviceId); } - private static class RangeComparator implements Comparator<MotionRange> { - @Override - public int compare(MotionRange arg0, MotionRange arg1) { - return arg0.getAxis() - arg1.getAxis(); - } - } - public static int getGodotButton(int keyCode) { int button; switch (keyCode) { @@ -410,39 +417,113 @@ public class GodotInputHandler implements InputManager.InputDeviceListener { return button; } - private boolean handleMouseEvent(final MotionEvent event) { - switch (event.getActionMasked()) { + static boolean isMouseEvent(MotionEvent event) { + return isMouseEvent(event.getSource()); + } + + private static boolean isMouseEvent(int eventSource) { + boolean mouseSource = ((eventSource & InputDevice.SOURCE_MOUSE) == InputDevice.SOURCE_MOUSE) || ((eventSource & InputDevice.SOURCE_STYLUS) == InputDevice.SOURCE_STYLUS); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + mouseSource = mouseSource || ((eventSource & InputDevice.SOURCE_MOUSE_RELATIVE) == InputDevice.SOURCE_MOUSE_RELATIVE); + } + return mouseSource; + } + + static boolean handleMotionEvent(final MotionEvent event) { + if (isMouseEvent(event)) { + return handleMouseEvent(event); + } + + return handleTouchEvent(event); + } + + static boolean handleMotionEvent(int eventSource, int eventAction, int buttonsMask, float x, float y) { + return handleMotionEvent(eventSource, eventAction, buttonsMask, x, y, 0, 0); + } + + static boolean handleMotionEvent(int eventSource, int eventAction, int buttonsMask, float x, float y, float deltaX, float deltaY) { + if (isMouseEvent(eventSource)) { + return handleMouseEvent(eventAction, buttonsMask, x, y, deltaX, deltaY, false, false); + } + + return handleTouchEvent(eventAction, x, y); + } + + static boolean handleMouseEvent(final MotionEvent event) { + final int eventAction = event.getActionMasked(); + final float x = event.getX(); + final float y = event.getY(); + final int buttonsMask = event.getButtonState(); + + final float verticalFactor = event.getAxisValue(MotionEvent.AXIS_VSCROLL); + final float horizontalFactor = event.getAxisValue(MotionEvent.AXIS_HSCROLL); + boolean sourceMouseRelative = false; + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { + sourceMouseRelative = event.isFromSource(InputDevice.SOURCE_MOUSE_RELATIVE); + } + return handleMouseEvent(eventAction, buttonsMask, x, y, horizontalFactor, verticalFactor, false, sourceMouseRelative); + } + + static boolean handleMouseEvent(int eventAction, int buttonsMask, float x, float y) { + return handleMouseEvent(eventAction, buttonsMask, x, y, 0, 0, false, false); + } + + static boolean handleMouseEvent(int eventAction, int buttonsMask, float x, float y, boolean doubleClick) { + return handleMouseEvent(eventAction, buttonsMask, x, y, 0, 0, doubleClick, false); + } + + static boolean handleMouseEvent(int eventAction, int buttonsMask, float x, float y, float deltaX, float deltaY, boolean doubleClick, boolean sourceMouseRelative) { + switch (eventAction) { + case MotionEvent.ACTION_CANCEL: + case MotionEvent.ACTION_UP: + // Zero-up the button state + buttonsMask = 0; + // FALL THROUGH + case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_HOVER_ENTER: + case MotionEvent.ACTION_HOVER_EXIT: case MotionEvent.ACTION_HOVER_MOVE: - case MotionEvent.ACTION_HOVER_EXIT: { - final float x = event.getX(); - final float y = event.getY(); - final int type = event.getAction(); - GodotLib.hover(type, x, y); - return true; - } - case MotionEvent.ACTION_BUTTON_PRESS: - case MotionEvent.ACTION_BUTTON_RELEASE: - case MotionEvent.ACTION_MOVE: { - final float x = event.getX(); - final float y = event.getY(); - final int buttonsMask = event.getButtonState(); - final int action = event.getAction(); - GodotLib.touch(event.getSource(), action, 0, 1, new float[] { 0, x, y }, buttonsMask); - return true; - } + case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_SCROLL: { - final float x = event.getX(); - final float y = event.getY(); - final int buttonsMask = event.getButtonState(); - final int action = event.getAction(); - final float verticalFactor = event.getAxisValue(MotionEvent.AXIS_VSCROLL); - final float horizontalFactor = event.getAxisValue(MotionEvent.AXIS_HSCROLL); - GodotLib.touch(event.getSource(), action, 0, 1, new float[] { 0, x, y }, buttonsMask, verticalFactor, horizontalFactor); + GodotLib.dispatchMouseEvent(eventAction, buttonsMask, x, y, deltaX, deltaY, doubleClick, sourceMouseRelative); + return true; } + } + return false; + } + + static boolean handleTouchEvent(final MotionEvent event) { + final int pointerCount = event.getPointerCount(); + if (pointerCount == 0) { + return true; + } + + final float[] positions = new float[pointerCount * 3]; // pointerId1, x1, y1, pointerId2, etc... + + for (int i = 0; i < pointerCount; i++) { + positions[i * 3 + 0] = event.getPointerId(i); + positions[i * 3 + 1] = event.getX(i); + positions[i * 3 + 2] = event.getY(i); + } + final int action = event.getActionMasked(); + final int actionPointerId = event.getPointerId(event.getActionIndex()); + + return handleTouchEvent(action, actionPointerId, pointerCount, positions); + } + + static boolean handleTouchEvent(int eventAction, float x, float y) { + return handleTouchEvent(eventAction, 0, 1, new float[] { 0, x, y }); + } + + static boolean handleTouchEvent(int eventAction, int actionPointerId, int pointerCount, float[] positions) { + switch (eventAction) { case MotionEvent.ACTION_DOWN: - case MotionEvent.ACTION_UP: { - // we can safely ignore these cases because they are always come beside ACTION_BUTTON_PRESS and ACTION_BUTTON_RELEASE + case MotionEvent.ACTION_CANCEL: + case MotionEvent.ACTION_UP: + case MotionEvent.ACTION_MOVE: + case MotionEvent.ACTION_POINTER_UP: + case MotionEvent.ACTION_POINTER_DOWN: { + GodotLib.dispatchTouchEvent(eventAction, actionPointerId, pointerCount, positions); return true; } } |