diff --git a/app/src/main/java/com/droiddeck/launcher/input/PadBridge.java b/app/src/main/java/com/droiddeck/launcher/input/PadBridge.java index c7dd27a271..71744bf866 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/PadBridge.java +++ b/app/src/main/java/com/droiddeck/launcher/input/PadBridge.java @@ -56,6 +56,7 @@ public final class PadBridge { private final FakeInputWriter[] playerWriters = new FakeInputWriter[SLOTS]; private final PadState[] playerStates = new PadState[SLOTS]; private final float[][] rawSticks = new float[SLOTS][4]; + private final boolean[][] keyTriggers = new boolean[SLOTS][2]; private final int[] retainedSticks = new int[SLOTS]; private final PadState effectiveState = new PadState(); private final PadState homeGuideReport = new PadState(); @@ -407,11 +408,11 @@ public final class PadBridge { break; case KeyEvent.KEYCODE_BUTTON_L2: pad.leftTrigger = keyTrigger(pad.leftTrigger, pressed, event.getRepeatCount(), - analogTrigger(event.getDevice(), MotionEvent.AXIS_LTRIGGER, MotionEvent.AXIS_BRAKE)); + analogTrigger(event.getDevice(), MotionEvent.AXIS_LTRIGGER, MotionEvent.AXIS_BRAKE), keyTriggers[slot], 0); break; case KeyEvent.KEYCODE_BUTTON_R2: pad.rightTrigger = keyTrigger(pad.rightTrigger, pressed, event.getRepeatCount(), - analogTrigger(event.getDevice(), MotionEvent.AXIS_RTRIGGER, MotionEvent.AXIS_GAS)); + analogTrigger(event.getDevice(), MotionEvent.AXIS_RTRIGGER, MotionEvent.AXIS_GAS), keyTriggers[slot], 1); break; case KeyEvent.KEYCODE_DPAD_UP: pad.up = pressed; break; case KeyEvent.KEYCODE_DPAD_RIGHT: pad.right = pressed; break; @@ -471,6 +472,7 @@ public final class PadBridge { stateFor(slot).clear(); retainedSticks[slot] = 0; Arrays.fill(rawSticks[slot], 0f); + Arrays.fill(keyTriggers[slot], false); } private void mutateState(java.util.function.Consumer mutation) { @@ -525,11 +527,11 @@ public final class PadBridge { float lt = 0f; float rt = 0f; if (wired || hasAxis(device, source, MotionEvent.AXIS_LTRIGGER, MotionEvent.AXIS_BRAKE)) { - lt = leftTrigger(event, wired); + lt = axisTrigger(leftTrigger(event, wired), keyTriggers[slot], 0); pad.leftTrigger = lt; } if (wired || hasAxis(device, source, MotionEvent.AXIS_RTRIGGER, MotionEvent.AXIS_GAS)) { - rt = rightTrigger(event, wired); + rt = axisTrigger(rightTrigger(event, wired), keyTriggers[slot], 1); pad.rightTrigger = rt; } if (hasAxis(device, source, MotionEvent.AXIS_HAT_X)) { @@ -553,9 +555,27 @@ public final class PadBridge { return true; } - static float keyTrigger(float current, boolean pressed, int repeatCount, boolean analog) { - if (analog || repeatCount > 0) return current; - return pressed ? 1f : 0f; + static float keyTrigger(float current, boolean pressed, int repeatCount, boolean analog, boolean[] held, int side) { + if (repeatCount > 0) return current; + if (!analog) { + held[side] = pressed; + return pressed ? 1f : 0f; + } + if (pressed) { + if (current != 0f) return current; + held[side] = true; + return 1f; + } + if (!held[side]) return current; + held[side] = false; + return 0f; + } + + static float axisTrigger(float value, boolean[] held, int side) { + if (!held[side]) return value; + if (value == 0f) return 1f; + held[side] = false; + return value; } static boolean analogTrigger(InputDevice device, int... axes) { diff --git a/app/src/test/java/com/droiddeck/launcher/input/PadPartialAxesTest.kt b/app/src/test/java/com/droiddeck/launcher/input/PadPartialAxesTest.kt index 2a2bade1c4..ae46c9d5e3 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/PadPartialAxesTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/PadPartialAxesTest.kt @@ -126,11 +126,11 @@ class PadPartialAxesTest { @Test fun wiredXboxTriggerKeysDoNotOverrideTheirAxes() { val device = device(MotionEvent.AXIS_RX, MotionEvent.AXIS_RY, MotionEvent.AXIS_Z, MotionEvent.AXIS_RZ, wired = true) - motion(device, MotionEvent.AXIS_Z to 0f, MotionEvent.AXIS_RZ to -1f) + motion(device, MotionEvent.AXIS_Z to 0f, MotionEvent.AXIS_RZ to -0.6f) key(device, KeyEvent.KEYCODE_BUTTON_L2) key(device, KeyEvent.KEYCODE_BUTTON_R2) assertEquals(0.5f, pad().leftTrigger) - assertEquals(0f, pad().rightTrigger) + assertEquals(0.2f, pad().rightTrigger, 0.0001f) } @Test fun digitalTriggerKeysStillPressAndRelease() { @@ -148,11 +148,46 @@ class PadPartialAxesTest { } @Test fun keyTriggerDecision() { - assertEquals(1f, PadBridge.keyTrigger(0f, true, 0, false)) - assertEquals(0f, PadBridge.keyTrigger(1f, false, 0, false)) - assertEquals(0.25f, PadBridge.keyTrigger(0.25f, true, 1, false)) - assertEquals(0.25f, PadBridge.keyTrigger(0.25f, true, 0, true)) - assertEquals(0.25f, PadBridge.keyTrigger(0.25f, false, 0, true)) + val held = BooleanArray(2) + assertEquals(1f, PadBridge.keyTrigger(0f, true, 0, false, held, 0)) + assertEquals(0f, PadBridge.keyTrigger(1f, false, 0, false, held, 0)) + assertEquals(0.25f, PadBridge.keyTrigger(0.25f, true, 1, false, held, 0)) + assertEquals(0.25f, PadBridge.keyTrigger(0.25f, true, 0, true, held, 0)) + assertEquals(0.25f, PadBridge.keyTrigger(0.25f, false, 0, true, held, 0)) + assertEquals(1f, PadBridge.keyTrigger(0f, true, 0, true, held, 1)) + assertEquals(1f, PadBridge.axisTrigger(0f, held, 1)) + assertEquals(0f, PadBridge.keyTrigger(1f, false, 0, true, held, 1)) + assertEquals(0f, PadBridge.axisTrigger(0f, held, 1)) + } + + @Test fun silentTriggerAxesLetTheirKeysPress() { + val device = device(MotionEvent.AXIS_LTRIGGER, MotionEvent.AXIS_RTRIGGER) + motion(device) + key(device, KeyEvent.KEYCODE_BUTTON_L2) + key(device, KeyEvent.KEYCODE_BUTTON_R2) + assertEquals(1f, pad().leftTrigger) + assertEquals(1f, pad().rightTrigger) + motion(device, MotionEvent.AXIS_X to 0.8f) + key(device, KeyEvent.KEYCODE_BUTTON_L2, repeat = 1) + assertEquals(1f, pad().leftTrigger) + assertEquals(1f, pad().rightTrigger) + key(device, KeyEvent.KEYCODE_BUTTON_L2, KeyEvent.ACTION_UP) + assertEquals(0f, pad().leftTrigger) + assertEquals(1f, pad().rightTrigger) + key(device, KeyEvent.KEYCODE_BUTTON_R2, KeyEvent.ACTION_UP) + assertEquals(0f, pad().rightTrigger) + } + + @Test fun aLiveAxisTakesOverFromItsKey() { + val device = device(MotionEvent.AXIS_LTRIGGER) + key(device, KeyEvent.KEYCODE_BUTTON_L2) + assertEquals(1f, pad().leftTrigger) + motion(device, MotionEvent.AXIS_LTRIGGER to 0.4f) + assertEquals(0.4f, pad().leftTrigger) + motion(device, MotionEvent.AXIS_LTRIGGER to 0f) + assertEquals(0f, pad().leftTrigger) + key(device, KeyEvent.KEYCODE_BUTTON_L2, KeyEvent.ACTION_UP) + assertEquals(0f, pad().leftTrigger) } private fun pad(): PadState = requireNotNull(JoyConPadTest.RecordingWriter.pads[0])