diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/BaseMainActivity.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/BaseMainActivity.java index 1f697fa2b..b4ab4ea48 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/BaseMainActivity.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/BaseMainActivity.java @@ -38,6 +38,8 @@ public class BaseMainActivity extends LoggableActivity { LWJGLGLFWKeycode.GLFW_KEY_4, LWJGLGLFWKeycode.GLFW_KEY_5, LWJGLGLFWKeycode.GLFW_KEY_6, LWJGLGLFWKeycode.GLFW_KEY_7, LWJGLGLFWKeycode.GLFW_KEY_8, LWJGLGLFWKeycode.GLFW_KEY_9}; + private Gamepad gamepad; + private boolean rightOverride = false; public float scaleFactor = 1; private int fingerStillThreshold = 8; @@ -654,12 +656,16 @@ public class BaseMainActivity extends LoggableActivity { } - private final Gamepad gamepad = new Gamepad(this); + @Override public boolean dispatchGenericMotionEvent(MotionEvent ev) { int mouseCursorIndex = -1; if(Gamepad.isGamepadEvent(ev)){ + if(gamepad == null){ + gamepad = new Gamepad(this, Tools.grabFirstGamepad()); + } + gamepad.update(ev); return true; } @@ -712,6 +718,10 @@ public class BaseMainActivity extends LoggableActivity { System.out.println(event); if(Gamepad.isGamepadEvent(event)){ + if(gamepad == null){ + gamepad = new Gamepad(this, Tools.grabFirstGamepad()); + } + gamepad.update(event); return true; } 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 c073eebcc..0393e5490 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 @@ -7,6 +7,7 @@ import android.view.MotionEvent; import net.kdt.pojavlaunch.BaseMainActivity; import net.kdt.pojavlaunch.LWJGLGLFWKeycode; +import net.kdt.pojavlaunch.Tools; import org.lwjgl.glfw.CallbackBridge; @@ -19,7 +20,6 @@ import static net.kdt.pojavlaunch.customcontrols.gamepad.GamepadJoystick.DIRECTI import static net.kdt.pojavlaunch.customcontrols.gamepad.GamepadJoystick.DIRECTION_SOUTH_EAST; import static net.kdt.pojavlaunch.customcontrols.gamepad.GamepadJoystick.DIRECTION_SOUTH_WEST; import static net.kdt.pojavlaunch.customcontrols.gamepad.GamepadJoystick.DIRECTION_WEST; -import static net.kdt.pojavlaunch.customcontrols.gamepad.GamepadJoystick.JOYSTICK_DEADZONE; public class Gamepad { @@ -28,10 +28,10 @@ public class Gamepad { private GamepadDpad gamepadDpad = new GamepadDpad(); - private final GamepadJoystick leftJoystick = new GamepadJoystick(MotionEvent.AXIS_X, MotionEvent.AXIS_Y); + private final GamepadJoystick leftJoystick; private int currentJoystickDirection = DIRECTION_NONE; - private final GamepadJoystick rightJoystick = new GamepadJoystick(MotionEvent.AXIS_Z, MotionEvent.AXIS_RZ); + private final GamepadJoystick rightJoystick; private float lastHorizontalValue = 0.0f; private float lastVerticalValue = 0.0f; @@ -51,7 +51,11 @@ public class Gamepad { private Thread mouseThread; - public Gamepad(BaseMainActivity gameActivity){ + public Gamepad(BaseMainActivity gameActivity, InputDevice inputDevice){ + leftJoystick = new GamepadJoystick(MotionEvent.AXIS_X, MotionEvent.AXIS_Y, inputDevice); + rightJoystick = new GamepadJoystick(MotionEvent.AXIS_Z, MotionEvent.AXIS_RZ, inputDevice); + + this.gameActivity = gameActivity; createMapping(); @@ -64,7 +68,6 @@ public class Gamepad { @Override public void run() { - while (!isInterrupted()) { long now = System.nanoTime(); delta += (now - lastTime) / ns; @@ -89,8 +92,9 @@ public class Gamepad { private void tick(){ if(lastHorizontalValue != 0 || lastVerticalValue != 0){ + GamepadJoystick currentJoystick = isGrabbing ? leftJoystick : rightJoystick; - acceleration = (mouseMagnitude - JOYSTICK_DEADZONE)/(1 - JOYSTICK_DEADZONE); + acceleration = (mouseMagnitude - currentJoystick.getDeadzone())/(1 - currentJoystick.getDeadzone()); acceleration = Math.pow(acceleration, mouseMaxAcceleration); if(acceleration > 1){ @@ -220,8 +224,7 @@ public class Gamepad { } private void updateMouseJoystick(MotionEvent event){ - GamepadJoystick currentJoystick = CallbackBridge.isGrabbing() ? rightJoystick : leftJoystick; - + GamepadJoystick currentJoystick = isGrabbing ? rightJoystick : leftJoystick; lastHorizontalValue = currentJoystick.getHorizontalAxis(event); lastVerticalValue = currentJoystick.getVerticalAxis(event); @@ -230,7 +233,7 @@ public class Gamepad { } private void updateDirectionalJoystick(MotionEvent event){ - GamepadJoystick currentJoystick = CallbackBridge.isGrabbing() ? leftJoystick : rightJoystick; + GamepadJoystick currentJoystick = isGrabbing ? leftJoystick : rightJoystick; int lastJoystickDirection = currentJoystickDirection; currentJoystickDirection = currentJoystick.getHeightDirection(event); 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 0ff2de668..b83f06470 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 @@ -18,14 +18,17 @@ public class GamepadJoystick { public static final int DIRECTION_WEST = 2; public static final int DIRECTION_NORTH_WEST = 1; - public static final float JOYSTICK_DEADZONE = 0.20f; + private float deadzone; private final int verticalAxis; private final int horizontalAxis; - public GamepadJoystick(int horizontalAxis, int verticalAxis){ + public GamepadJoystick(int horizontalAxis, int verticalAxis, InputDevice device){ this.verticalAxis = verticalAxis; this.horizontalAxis = horizontalAxis; + + deadzone = Math.max(device.getMotionRange(verticalAxis).getFlat(), + device.getMotionRange(horizontalAxis).getFlat() ); } public double getAngleRadian(MotionEvent event){ @@ -63,18 +66,17 @@ public class GamepadJoystick { } private float applyDeadzone(MotionEvent event, int axis){ - //TODO: tweakable deadzone ? /* 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 < JOYSTICK_DEADZONE){ + if (magnitude < deadzone){ return 0; - }else{ - //if( Math.abs(event.getAxisValue(axis)) < 0.035) return 0; - return (float) ((event.getAxisValue(axis) / magnitude) * ((magnitude - JOYSTICK_DEADZONE) / (1 - JOYSTICK_DEADZONE))); } + + return (float) ((event.getAxisValue(axis) / magnitude) * ((magnitude - deadzone) / (1 - deadzone))); } public static boolean isJoystickEvent(MotionEvent event){ @@ -84,10 +86,12 @@ public class GamepadJoystick { public int getHeightDirection(MotionEvent event){ - if(getMagnitude(event) <= JOYSTICK_DEADZONE ) return DIRECTION_NONE; + if(getMagnitude(event) <= deadzone) return DIRECTION_NONE; return ((int) ((getAngle(event)+22.5)/45)) % 8; } - + public float getDeadzone() { + return deadzone; + } }