package engine.input; import engine.math.Vector3f; import java.nio.ByteBuffer; import java.nio.FloatBuffer; import static engine.input.InputConst.*; import static org.lwjgl.glfw.GLFW.*; /* Buttons 0 : Croix / A 1: rond /B 2: carré / X 3: triangle / Y 4: L1 / LB 5: R1 / RB 6:select 7:start 8:L3 9:R3 10: haut 11: droite 12: bas 13: gauche */ /* Axes 0 : left X axe ( right : 1 left -1) 1: left Y axe ( down : 1 , Up -1) 2: right X axe ( right : 1 left -1) 3: right Y axe ( down : 1 , Up -1) 4:L2 / LT : 1 active, -1 unactive 5: R2 /RT : 1 active, -1 unactive */ public class GamepadInput { private final int gamepadNum; private ByteBuffer gamepadButton; private FloatBuffer gamepadAxes; public GamepadInput (int gamepadNum){ this.gamepadNum = gamepadNum; this.gamepadAxes = null; this.gamepadButton = null; } public void inputRefresh() { this.gamepadButton = glfwGetJoystickButtons(this.gamepadNum); this.gamepadAxes = glfwGetJoystickAxes(this.gamepadNum); assert gamepadAxes != null; assert gamepadButton != null; } public boolean checkPressed(int keyCode){ this.gamepadButton = glfwGetJoystickButtons(this.gamepadNum); assert gamepadButton != null; return gamepadButton.get(keyCode) == 1; } /** * Envoie la position du stick pointé par axisId sous forme de direction général (9 directions) * @param axisId l'identifiant du joystick (AXE X) * @return la direction du stick valeur possible : RIGHT, UPPER_RIGHT, UP, UPPER_LEFT, LEFT, LOWER_LEFT, DOWN, LOWER_RIGHT, CENTER; */ public int getAxisDiscreet(int axisId){ float x = gamepadAxes.get(axisId); float y = gamepadAxes.get(axisId + 1); float magnitude = (float) Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)); float angle = (float) Math.toDegrees(2 * Math.atan(y /(x + magnitude))); if (magnitude < 0.3) return CENTER; if (angle < 22.5 && angle > -22.5) return RIGHT; else if (angle > -67.5 && angle < -22.5) return UPPER_RIGHT; else if (angle > -112.5 && angle < -67.5) return UP_STICK; else if (angle > -157.5 && angle < -112.5) return UPPER_LEFT; else if (angle < -157.5 || angle > 157.5) return LEFT; else if (angle < 157.5 && angle > 112.5) return LOWER_LEFT; else if (angle < 112.5 && angle > 67.5) return DOWN_STICK; else if (angle < 67.5 && angle > 22.5) return LOWER_RIGHT; return -1; // TEST } /** * Envoie la position du stick pointé par axisID (AXE X) sous forme de Vecteur * @param axisId l'identifiant du joystick (AXE X) * @return un Vecteur représentant la position actuel du joystick */ public Vector3f getAxisContinuous(int axisId){ float x = gamepadAxes.get(axisId); float y = gamepadAxes.get(axisId + 1); if (x < 0.1 && x > -0.1) x = 0; if (y < 0.1 && y > -0.1) y = 0; System.out.println("x: " + x + " y: " + y); return new Vector3f(x,y); } public String getGamepadName(){ return glfwGetGamepadName(this.gamepadNum); } }