From d4997c6fb9e94f189075239e1ab58669f1c9af3b Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Fri, 5 Feb 2021 00:39:42 +1000 Subject: [PATCH] Android: Manually scale axis values instead of using motion range --- .../src/cpp/android_controller_interface.cpp | 10 +------- .../duckstation/EmulationSurfaceView.java | 23 +++++++++++++++---- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/android/app/src/cpp/android_controller_interface.cpp b/android/app/src/cpp/android_controller_interface.cpp index e9d5241c9..6b97d0cbc 100644 --- a/android/app/src/cpp/android_controller_interface.cpp +++ b/android/app/src/cpp/android_controller_interface.cpp @@ -112,15 +112,7 @@ bool AndroidControllerInterface::HandleAxisEvent(u32 index, u32 axis, float valu const AxisCallback& cb = m_controllers[index].axis_mapping[static_cast(axis)][AxisSide::Full]; if (cb) { - // Extend triggers from a 0 - 1 range to a -1 - 1 range for consistency with other inputs - if (axis == 4 && axis == 5) - { - cb((value * 2.0f) - 1.0f); - } - else - { - cb(value); - } + cb(value); return true; } diff --git a/android/app/src/main/java/com/github/stenzek/duckstation/EmulationSurfaceView.java b/android/app/src/main/java/com/github/stenzek/duckstation/EmulationSurfaceView.java index 0cdcb41c8..261ad4dac 100644 --- a/android/app/src/main/java/com/github/stenzek/duckstation/EmulationSurfaceView.java +++ b/android/app/src/main/java/com/github/stenzek/duckstation/EmulationSurfaceView.java @@ -208,6 +208,10 @@ public class EmulationSurfaceView extends SurfaceView { return !isExternalKeyCode(keyCode); } + private float clamp(float value, float min, float max) { + return (value < min) ? min : ((value > max) ? max : value); + } + @Override public boolean onGenericMotionEvent(MotionEvent event) { final int source = event.getSource(); @@ -222,12 +226,21 @@ public class EmulationSurfaceView extends SurfaceView { final float axisValue = event.getAxisValue(mapping.deviceAxisOrButton); float emuValue; - if (mapping.deviceMotionRange != null) { - final float transformedValue = (axisValue - mapping.deviceMotionRange.getMin()) / mapping.deviceMotionRange.getRange(); - emuValue = (transformedValue * 2.0f) - 1.0f; - } else { - emuValue = axisValue; + switch (mapping.deviceAxisOrButton) { + case MotionEvent.AXIS_BRAKE: + case MotionEvent.AXIS_GAS: + case MotionEvent.AXIS_LTRIGGER: + case MotionEvent.AXIS_RTRIGGER: + // Scale 0..1 -> -1..1. + emuValue = (clamp(axisValue, 0.0f, 1.0f) * 2.0f) - 1.0f; + break; + + default: + // Everything else should already by -1..1 as per Android documentation. + emuValue = clamp(axisValue, -1.0f, 1.0f); + break; } + Log.d("EmulationSurfaceView", String.format("axis %d value %f emuvalue %f", mapping.deviceAxisOrButton, axisValue, emuValue)); if (mapping.axisMapping >= 0) {