diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/gamepad/GamepadJoystick.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/gamepad/GamepadJoystick.java index 8c3cf9738..6529b9ccb 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/gamepad/GamepadJoystick.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/gamepad/GamepadJoystick.java @@ -3,20 +3,22 @@ package net.kdt.pojavlaunch.customcontrols.gamepad; import android.view.InputDevice; import android.view.MotionEvent; + import com.google.android.material.math.MathUtils; public class GamepadJoystick { //Directions public static final int DIRECTION_NONE = -1; //GamepadJoystick at the center - public static final int DIRECTION_NORTH = 0; - public static final int DIRECTION_NORTH_EAST = 7; - public static final int DIRECTION_EAST = 6; - public static final int DIRECTION_SOUTH_EAST = 5; - public static final int DIRECTION_SOUTH = 4; - public static final int DIRECTION_SOUTH_WEST = 3; - public static final int DIRECTION_WEST = 2; - public static final int DIRECTION_NORTH_WEST = 1; + + public static final int DIRECTION_EAST = 0; + public static final int DIRECTION_NORTH_EAST = 1; + public static final int DIRECTION_NORTH = 2; + public static final int DIRECTION_NORTH_WEST = 3; + public static final int DIRECTION_WEST = 4; + public static final int DIRECTION_SOUTH_WEST = 5; + public static final int DIRECTION_SOUTH = 6; + public static final int DIRECTION_SOUTH_EAST = 7; private float deadzone; @@ -27,10 +29,9 @@ public class GamepadJoystick { this.verticalAxis = verticalAxis; this.horizontalAxis = horizontalAxis; - /* - Some controllers aren't recognized as such by android, so we fallback to a default value of 0.2 - And some others don't report their MotionRange. This was the case with the xbox one series S controller. - */ + //Some controllers aren't recognized as such by android, so we fallback to a default value of 0.2 + //And some others don't report their MotionRange. This was the case with the xbox one series S controller. + try { deadzone = Math.max(device.getMotionRange(verticalAxis).getFlat(), device.getMotionRange(horizontalAxis).getFlat()) * 1.9f; } catch (NullPointerException e){ deadzone = 0.2f; } @@ -38,21 +39,17 @@ public class GamepadJoystick { } public double getAngleRadian(MotionEvent event){ - float x = getHorizontalAxis(event); - float y = getVerticalAxis(event); - if(x == y && x == 0) - return 0.00; //atan2 don't like when x and y == 0 - - return -Math.atan2(y, x); + //From -PI to PI + return -Math.atan2(getVerticalAxis(event), getHorizontalAxis(event)); } - public double getAngle(MotionEvent event){ - float x = getHorizontalAxis(event); - float y = getVerticalAxis(event); - if(x == y && x == 0) - return 0.00; //atan2 don't like when x and y == 0 - return 180+(Math.atan2(x, y)*57); + public double getAngleDegree(MotionEvent event){ + //From 0 to 360 degrees + double result = Math.toDegrees(getAngleRadian(event)); + if(result < 0) result += 360; + + return result; } public double getMagnitude(MotionEvent event){ @@ -71,17 +68,13 @@ public class GamepadJoystick { } private float applyDeadzone(MotionEvent event, int axis){ - /* - This piece of code also modifies the value - to make it seem like there was no deadzone in the first place - */ + //This piece of code also modifies the value + //to make it seem like there was no deadzone in the first place double magnitude = getMagnitude(event); - if (magnitude < deadzone){ - return 0; - } + if (magnitude < deadzone) return 0; - return (float) ((event.getAxisValue(axis) / magnitude) * ((magnitude - deadzone) / (1 - deadzone))); + return (float) ( (event.getAxisValue(axis) / magnitude) * ((magnitude - deadzone) / (1 - deadzone)) ); } public static boolean isJoystickEvent(MotionEvent event){ @@ -92,7 +85,7 @@ public class GamepadJoystick { public int getHeightDirection(MotionEvent event){ if(getMagnitude(event) <= deadzone) return DIRECTION_NONE; - return ((int) ((getAngle(event)+22.5)/45)) % 8; + return ((int) ((getAngleDegree(event)+22.5)/45)) % 8; }