jeu-de-combat/src/engine/input/GamepadInput.java
2021-06-02 15:23:44 +02:00

213 lines
6.7 KiB
Java

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 boolean buttonA_pressed ;
public boolean buttonB_pressed ;
public boolean buttonY_pressed ;
public boolean buttonX_pressed;
public boolean LB_pressed ;
public boolean RB_pressed ;
public boolean select_pressed;
public boolean start_pressed;
public boolean R_JoyClick_pressed;
public boolean L_JoyClick_pressed;
public boolean up_pressed ;
public boolean down_pressed;
public boolean left_pressed ;
public boolean right_pressed;
public boolean leftJoyRight;
public boolean leftJoyLeft;
public boolean leftJoyDown;
public boolean leftJoyUp;
public boolean rightJoyLeft;
public boolean rightJoyRight;
public boolean rightJoyDown ;
public boolean rightJoyUp;
public boolean LT_pressed ;
public boolean RT_pressed;
public GamepadInput (int gamepadNum){
this.gamepadNum = gamepadNum;
this.gamepadAxes = null;
this.gamepadButton = null;
this.buttonA_pressed = false;
this.buttonB_pressed = false;
this.buttonY_pressed = false;
this.buttonX_pressed = false;
this.LB_pressed = false;
this.RB_pressed = false;
this.select_pressed = false;
this.start_pressed = false;
this.R_JoyClick_pressed = false;
this.L_JoyClick_pressed = false;
this.up_pressed = false;
this.down_pressed = false;
this.left_pressed = false;
this.right_pressed = false;
this.leftJoyRight = false;
this.leftJoyLeft = false;
this.leftJoyDown = false;
this.leftJoyUp = false;
this.rightJoyLeft = false;
this.rightJoyRight = false;
this.rightJoyDown = false;
this.rightJoyUp = false;
this.LT_pressed = false;
this.RT_pressed = false;
}
public void inputRefresh() {
this.gamepadButton = glfwGetJoystickButtons(this.gamepadNum);
this.gamepadAxes = glfwGetJoystickAxes(this.gamepadNum);
assert gamepadAxes != null;
assert gamepadButton != null;
// A B X Y
buttonA_pressed = gamepadButton.get(buttonA) == 1;
buttonB_pressed = gamepadButton.get(buttonB) == 1;
buttonY_pressed = gamepadButton.get(buttonY) == 1;
buttonX_pressed = gamepadButton.get(buttonX) == 1;
// LB RB
LB_pressed = gamepadButton.get(LB) == 1;
RB_pressed = gamepadButton.get(RB) == 1;
//start select
start_pressed = gamepadButton.get(start) == 1;
select_pressed = gamepadButton.get(select) == 1;
//R & L JoystickClick
R_JoyClick_pressed = gamepadButton.get(R_JoystickClick) == 1;
L_JoyClick_pressed = gamepadButton.get(L_JoystickClick) == 1;
//up, down, left, right
up_pressed = gamepadButton.get(up) == 1;
down_pressed = gamepadButton.get(down) == 1;
left_pressed = gamepadButton.get(left) == 1;
right_pressed = gamepadButton.get(right) == 1;
//Left Joystick
leftJoyRight = gamepadAxes.get(leftJoyX_Axe) > 0.1;
leftJoyLeft = gamepadAxes.get(leftJoyX_Axe) < -0.1;
leftJoyDown = gamepadAxes.get(leftJoyY_Axe) > 0.1;
leftJoyUp = gamepadAxes.get(leftJoyY_Axe) < -0.1;
//Right Joystick
rightJoyRight = gamepadAxes.get(rightJoyX_Axe) > 0.1;
rightJoyLeft = gamepadAxes.get(rightJoyX_Axe) < -0.1;
rightJoyDown = gamepadAxes.get(rightJoyY_Axe) > 0.1;
rightJoyUp = gamepadAxes.get(rightJoyY_Axe) < -0.1;
//LT RT
LT_pressed = gamepadButton.get(LT) == 1;
RT_pressed = gamepadButton.get(RT) == 1;
}
public boolean checkPressed(int keyCode){
this.gamepadButton = glfwGetJoystickButtons(GLFW_JOYSTICK_1);
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.1) 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;
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;
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);
}
}