Detect swapped axis between triggers and RJoystick

This commit is contained in:
SerpentSpirale 2021-09-06 16:20:09 +02:00 committed by SerpentSpirale
parent 4c71e66f48
commit cf6737b1ac

View File

@ -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));
}
}