From 84d3bfc506b3c8437b0b56aaf9ed80930cc6ea14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 4 Dec 2023 13:41:52 +0100 Subject: [PATCH] Add mouse wheel support for Android Fixes #18471 Tested on a Poco F4 phone with a generic Bluetooth mouse. --- Common/UI/ScrollView.cpp | 2 +- android/jni/app-android.cpp | 20 ++++++++++++++++++- .../src/org/ppsspp/ppsspp/NativeActivity.java | 2 +- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/Common/UI/ScrollView.cpp b/Common/UI/ScrollView.cpp index 94c712f0b4cd..6adbeca05c6d 100644 --- a/Common/UI/ScrollView.cpp +++ b/Common/UI/ScrollView.cpp @@ -119,7 +119,7 @@ bool ScrollView::Key(const KeyInput &input) { if (input.flags & KEY_DOWN) { if ((input.keyCode == NKCODE_EXT_MOUSEWHEEL_UP || input.keyCode == NKCODE_EXT_MOUSEWHEEL_DOWN) && (input.flags & KEY_HASWHEELDELTA)) { - scrollSpeed = (float)(short)(input.flags >> 16) * 1.25f; // Fudge factor + scrollSpeed = (float)(short)(input.flags >> 16) * 1.25f; // Fudge factor. TODO: Should be moved to the backends. } switch (input.keyCode) { diff --git a/android/jni/app-android.cpp b/android/jni/app-android.cpp index d4bfb28a253a..20d2f6fe7021 100644 --- a/android/jni/app-android.cpp +++ b/android/jni/app-android.cpp @@ -1235,7 +1235,25 @@ extern "C" jboolean Java_org_ppsspp_ppsspp_NativeApp_mouseWheelEvent( JNIEnv *env, jclass, jint stick, jfloat x, jfloat y) { if (!renderer_inited) return false; - // TODO: Support mousewheel for android + // TODO: Mousewheel should probably be an axis instead. + int wheelDelta = y * 30.0f; + if (wheelDelta > 500) wheelDelta = 500; + if (wheelDelta < -500) wheelDelta = -500; + + KeyInput key; + key.deviceId = DEVICE_ID_MOUSE; + if (wheelDelta < 0) { + key.keyCode = NKCODE_EXT_MOUSEWHEEL_DOWN; + wheelDelta = -wheelDelta; + } else { + key.keyCode = NKCODE_EXT_MOUSEWHEEL_UP; + } + // There's no separate keyup event for mousewheel events, + // so we release it with a slight delay. + key.flags = KEY_DOWN | KEY_HASWHEELDELTA | (wheelDelta << 16); + NativeKey(key); + key.flags = KEY_UP; + NativeKey(key); return true; } diff --git a/android/src/org/ppsspp/ppsspp/NativeActivity.java b/android/src/org/ppsspp/ppsspp/NativeActivity.java index cf93e4b287f7..dc826497cee4 100644 --- a/android/src/org/ppsspp/ppsspp/NativeActivity.java +++ b/android/src/org/ppsspp/ppsspp/NativeActivity.java @@ -1024,7 +1024,7 @@ public boolean onGenericMotionEvent(MotionEvent event) { // process the mouse hover movement... return true; case MotionEvent.ACTION_SCROLL: - NativeApp.mouseWheelEvent(event.getX(), event.getY()); + NativeApp.mouseWheelEvent(event.getAxisValue(MotionEvent.AXIS_HSCROLL), event.getAxisValue(MotionEvent.AXIS_VSCROLL)); return true; } }