From cf6737b1ac1532ec754cfd31f2f0a1ae3ddf1a99 Mon Sep 17 00:00:00 2001 From: SerpentSpirale Date: Mon, 6 Sep 2021 16:20:09 +0200 Subject: [PATCH] Detect swapped axis between triggers and RJoystick --- .../customcontrols/gamepad/Gamepad.java | 31 +++++++++++++------ 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/gamepad/Gamepad.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/gamepad/Gamepad.java index a79b3cc1b..2ff39b990 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/gamepad/Gamepad.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/gamepad/Gamepad.java @@ -56,17 +56,28 @@ public class Gamepad { private GamepadMap currentMap = gameMap; private boolean lastGrabbingState = true; - private final boolean hasDigitalTriggers; + private final boolean mModifierDigitalTriggers; + private boolean mModifierSwappedAxis = false; //Triggers and right stick axis are swapped. - private final Handler handler = new Handler(Looper.getMainLooper()); + private final Handler inputHandler = new Handler(Looper.getMainLooper()); private final Runnable switchStateRunnable; public Gamepad(BaseMainActivity gameActivity, InputDevice inputDevice){ //Toast.makeText(gameActivity.getApplicationContext(),"GAMEPAD CREATED", Toast.LENGTH_LONG).show(); + for(InputDevice.MotionRange range : inputDevice.getMotionRanges()){ + if(range.getAxis() == MotionEvent.AXIS_RX || range.getAxis() == MotionEvent.AXIS_RY){ + mModifierSwappedAxis = true; + break; + } + } leftJoystick = new GamepadJoystick(MotionEvent.AXIS_X, MotionEvent.AXIS_Y, inputDevice); - rightJoystick = new GamepadJoystick(MotionEvent.AXIS_Z, MotionEvent.AXIS_RZ, inputDevice); - hasDigitalTriggers = inputDevice.hasKeys(KeyEvent.KEYCODE_BUTTON_R2)[0]; + if(!mModifierSwappedAxis) + rightJoystick = new GamepadJoystick(MotionEvent.AXIS_Z, MotionEvent.AXIS_RZ, inputDevice); + else + rightJoystick = new GamepadJoystick(MotionEvent.AXIS_RX, MotionEvent.AXIS_RY, inputDevice); + + mModifierDigitalTriggers = inputDevice.hasKeys(KeyEvent.KEYCODE_BUTTON_R2)[0]; this.gameActivity = gameActivity; pointerView = this.gameActivity.findViewById(R.id.console_pointer); @@ -80,11 +91,11 @@ public class Gamepad { updateGrabbingState(); tick(); - handler.postDelayed(this, 16); + inputHandler.postDelayed(this, 16); } }; - handler.postDelayed(handlerRunnable, 16); + inputHandler.postDelayed(handlerRunnable, 16); //Initialize runnables to be used by the input system, avoiding generating one each time is better memory. switchStateRunnable = () -> { @@ -118,7 +129,7 @@ public class Gamepad { if(lastHorizontalValue != 0 || lastVerticalValue != 0){ GamepadJoystick currentJoystick = lastGrabbingState ? leftJoystick : rightJoystick; - acceleration = (mouseMagnitude - currentJoystick.getDeadzone())/(1 - currentJoystick.getDeadzone()); + double acceleration = (mouseMagnitude - currentJoystick.getDeadzone()) / (1 - currentJoystick.getDeadzone()); acceleration = Math.pow(acceleration, mouseMaxAcceleration); if(acceleration > 1) acceleration = 1; @@ -181,9 +192,9 @@ public class Gamepad { } private void updateAnalogTriggers(MotionEvent event){ - if(!hasDigitalTriggers){ - getCurrentMap().TRIGGER_LEFT.update((event.getAxisValue(MotionEvent.AXIS_LTRIGGER) > 0.5) || (event.getAxisValue(MotionEvent.AXIS_BRAKE) > 0.5)); - getCurrentMap().TRIGGER_RIGHT.update((event.getAxisValue(MotionEvent.AXIS_RTRIGGER) > 0.5) || (event.getAxisValue(MotionEvent.AXIS_GAS) > 0.5)); + if(!mModifierDigitalTriggers){ + getCurrentMap().TRIGGER_LEFT.update((event.getAxisValue(mModifierSwappedAxis ? MotionEvent.AXIS_Z : MotionEvent.AXIS_LTRIGGER) > 0.5) || (event.getAxisValue(MotionEvent.AXIS_BRAKE) > 0.5)); + getCurrentMap().TRIGGER_RIGHT.update((event.getAxisValue(mModifierSwappedAxis ? MotionEvent.AXIS_RZ : MotionEvent.AXIS_RTRIGGER) > 0.5) || (event.getAxisValue(MotionEvent.AXIS_GAS) > 0.5)); } }