123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- package me.hammerle.snuviengine.api;
- import java.nio.ByteBuffer;
- import java.nio.FloatBuffer;
- import static org.lwjgl.glfw.GLFW.*;
- public final class Gamepad {
- public static final class Button {
- private final String name;
- private final int mapping;
- private boolean isDown = false;
- private int time = 0;
- private boolean isReleased = false;
- protected Button(String name, int mapping) {
- this.name = name;
- this.mapping = mapping;
- }
- public String getName() {
- return name;
- }
- protected int getMapping() {
- return mapping;
- }
- protected void tick(boolean isDown) {
- if(isReleased) {
- isReleased = false;
- time = 0;
- }
- if(this.isDown && !isDown) {
- isReleased = true;
- time++;
- }
- this.isDown = isDown;
- if(isDown) {
- time++;
- }
- }
- public boolean isDown() {
- return isDown;
- }
- public boolean isReleased() {
- return isReleased;
- }
- public int getTime() {
- return time;
- }
- public void setTime(int time) {
- this.time = time;
- }
- }
- public final Button a = new Button("A", 1);
- public final Button b = new Button("B", 2);
- public final Button x = new Button("X", 0);
- public final Button y = new Button("Y", 3);
- public final Button l = new Button("L", 4);
- public final Button r = new Button("R", 5);
- public final Button start = new Button("Start", 9);
- public final Button select = new Button("Select", 8);
- public final Button left = new Button("Left", 0);
- public final Button right = new Button("Right", 0);
- public final Button up = new Button("Up", 1);
- public final Button down = new Button("Down", 1);
- private final Button[] buttons = new Button[]{
- a, b, x, y, l, r, start, select
- };
- public final Button[] bindings = new Button[]{
- a, b, x, y, l, r, start, select, left, right, up, down
- };
- public final int joystickId;
- protected Gamepad(int joystickId) {
- this.joystickId = joystickId;
- }
- protected void tick() {
- if(!glfwJoystickPresent(joystickId)) {
- return;
- }
- updateButtons();
- updateAxes();
- }
- private void updateButtons() {
- ByteBuffer buttonBuffer = glfwGetJoystickButtons(joystickId);
- if(buttonBuffer == null) {
- return;
- }
- for(Button binding : buttons) {
- binding.tick(buttonBuffer.get(binding.getMapping()) != 0);
- }
- }
- private void updateAxes() {
- FloatBuffer axesBuffer = glfwGetJoystickAxes(joystickId);
- if(axesBuffer == null) {
- return;
- }
- left.tick(axesBuffer.get(left.getMapping()) < -0.5f);
- right.tick(axesBuffer.get(right.getMapping()) > 0.5f);
- up.tick(axesBuffer.get(up.getMapping()) < -0.5f);
- down.tick(axesBuffer.get(down.getMapping()) > 0.5f);
- }
- }
|