New deadzone system + auto-grab controller

This commit is contained in:
SerpentSpirale 2021-05-21 14:57:40 +02:00
parent 90aac77ade
commit 4edb0ebe34
3 changed files with 36 additions and 19 deletions

View File

@ -38,6 +38,8 @@ public class BaseMainActivity extends LoggableActivity {
LWJGLGLFWKeycode.GLFW_KEY_4, LWJGLGLFWKeycode.GLFW_KEY_5, LWJGLGLFWKeycode.GLFW_KEY_6, LWJGLGLFWKeycode.GLFW_KEY_4, LWJGLGLFWKeycode.GLFW_KEY_5, LWJGLGLFWKeycode.GLFW_KEY_6,
LWJGLGLFWKeycode.GLFW_KEY_7, LWJGLGLFWKeycode.GLFW_KEY_8, LWJGLGLFWKeycode.GLFW_KEY_9}; LWJGLGLFWKeycode.GLFW_KEY_7, LWJGLGLFWKeycode.GLFW_KEY_8, LWJGLGLFWKeycode.GLFW_KEY_9};
private Gamepad gamepad;
private boolean rightOverride = false; private boolean rightOverride = false;
public float scaleFactor = 1; public float scaleFactor = 1;
private int fingerStillThreshold = 8; private int fingerStillThreshold = 8;
@ -654,12 +656,16 @@ public class BaseMainActivity extends LoggableActivity {
} }
private final Gamepad gamepad = new Gamepad(this);
@Override @Override
public boolean dispatchGenericMotionEvent(MotionEvent ev) { public boolean dispatchGenericMotionEvent(MotionEvent ev) {
int mouseCursorIndex = -1; int mouseCursorIndex = -1;
if(Gamepad.isGamepadEvent(ev)){ if(Gamepad.isGamepadEvent(ev)){
if(gamepad == null){
gamepad = new Gamepad(this, Tools.grabFirstGamepad());
}
gamepad.update(ev); gamepad.update(ev);
return true; return true;
} }
@ -712,6 +718,10 @@ public class BaseMainActivity extends LoggableActivity {
System.out.println(event); System.out.println(event);
if(Gamepad.isGamepadEvent(event)){ if(Gamepad.isGamepadEvent(event)){
if(gamepad == null){
gamepad = new Gamepad(this, Tools.grabFirstGamepad());
}
gamepad.update(event); gamepad.update(event);
return true; return true;
} }

View File

@ -7,6 +7,7 @@ import android.view.MotionEvent;
import net.kdt.pojavlaunch.BaseMainActivity; import net.kdt.pojavlaunch.BaseMainActivity;
import net.kdt.pojavlaunch.LWJGLGLFWKeycode; import net.kdt.pojavlaunch.LWJGLGLFWKeycode;
import net.kdt.pojavlaunch.Tools;
import org.lwjgl.glfw.CallbackBridge; 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_EAST;
import static net.kdt.pojavlaunch.customcontrols.gamepad.GamepadJoystick.DIRECTION_SOUTH_WEST; 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.DIRECTION_WEST;
import static net.kdt.pojavlaunch.customcontrols.gamepad.GamepadJoystick.JOYSTICK_DEADZONE;
public class Gamepad { public class Gamepad {
@ -28,10 +28,10 @@ public class Gamepad {
private GamepadDpad gamepadDpad = new GamepadDpad(); 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 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 lastHorizontalValue = 0.0f;
private float lastVerticalValue = 0.0f; private float lastVerticalValue = 0.0f;
@ -51,7 +51,11 @@ public class Gamepad {
private Thread mouseThread; 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; this.gameActivity = gameActivity;
createMapping(); createMapping();
@ -64,7 +68,6 @@ public class Gamepad {
@Override @Override
public void run() { public void run() {
while (!isInterrupted()) { while (!isInterrupted()) {
long now = System.nanoTime(); long now = System.nanoTime();
delta += (now - lastTime) / ns; delta += (now - lastTime) / ns;
@ -89,8 +92,9 @@ public class Gamepad {
private void tick(){ private void tick(){
if(lastHorizontalValue != 0 || lastVerticalValue != 0){ 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); acceleration = Math.pow(acceleration, mouseMaxAcceleration);
if(acceleration > 1){ if(acceleration > 1){
@ -220,8 +224,7 @@ public class Gamepad {
} }
private void updateMouseJoystick(MotionEvent event){ private void updateMouseJoystick(MotionEvent event){
GamepadJoystick currentJoystick = CallbackBridge.isGrabbing() ? rightJoystick : leftJoystick; GamepadJoystick currentJoystick = isGrabbing ? rightJoystick : leftJoystick;
lastHorizontalValue = currentJoystick.getHorizontalAxis(event); lastHorizontalValue = currentJoystick.getHorizontalAxis(event);
lastVerticalValue = currentJoystick.getVerticalAxis(event); lastVerticalValue = currentJoystick.getVerticalAxis(event);
@ -230,7 +233,7 @@ public class Gamepad {
} }
private void updateDirectionalJoystick(MotionEvent event){ private void updateDirectionalJoystick(MotionEvent event){
GamepadJoystick currentJoystick = CallbackBridge.isGrabbing() ? leftJoystick : rightJoystick; GamepadJoystick currentJoystick = isGrabbing ? leftJoystick : rightJoystick;
int lastJoystickDirection = currentJoystickDirection; int lastJoystickDirection = currentJoystickDirection;
currentJoystickDirection = currentJoystick.getHeightDirection(event); currentJoystickDirection = currentJoystick.getHeightDirection(event);

View File

@ -18,14 +18,17 @@ public class GamepadJoystick {
public static final int DIRECTION_WEST = 2; public static final int DIRECTION_WEST = 2;
public static final int DIRECTION_NORTH_WEST = 1; 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 verticalAxis;
private final int horizontalAxis; private final int horizontalAxis;
public GamepadJoystick(int horizontalAxis, int verticalAxis){ public GamepadJoystick(int horizontalAxis, int verticalAxis, InputDevice device){
this.verticalAxis = verticalAxis; this.verticalAxis = verticalAxis;
this.horizontalAxis = horizontalAxis; this.horizontalAxis = horizontalAxis;
deadzone = Math.max(device.getMotionRange(verticalAxis).getFlat(),
device.getMotionRange(horizontalAxis).getFlat() );
} }
public double getAngleRadian(MotionEvent event){ public double getAngleRadian(MotionEvent event){
@ -63,18 +66,17 @@ public class GamepadJoystick {
} }
private float applyDeadzone(MotionEvent event, int axis){ private float applyDeadzone(MotionEvent event, int axis){
//TODO: tweakable deadzone ?
/* /*
This piece of code also modifies the value This piece of code also modifies the value
to make it seem like there was no deadzone in the first place to make it seem like there was no deadzone in the first place
*/ */
double magnitude = getMagnitude(event); double magnitude = getMagnitude(event);
if (magnitude < JOYSTICK_DEADZONE){ if (magnitude < deadzone){
return 0; 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){ public static boolean isJoystickEvent(MotionEvent event){
@ -84,10 +86,12 @@ public class GamepadJoystick {
public int getHeightDirection(MotionEvent event){ 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; return ((int) ((getAngle(event)+22.5)/45)) % 8;
} }
public float getDeadzone() {
return deadzone;
}
} }