Gamepad.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package me.hammerle.snuviengine.api;
  2. import java.nio.ByteBuffer;
  3. import java.nio.FloatBuffer;
  4. import static org.lwjgl.glfw.GLFW.*;
  5. public final class Gamepad {
  6. public static final class GamepadButton {
  7. private final String name;
  8. private final int mapping;
  9. private boolean isDown = false;
  10. private int time = 0;
  11. private boolean isReleased = false;
  12. protected GamepadButton(String name, int mapping) {
  13. this.name = name;
  14. this.mapping = mapping;
  15. }
  16. public String getName() {
  17. return name;
  18. }
  19. protected int getMapping() {
  20. return mapping;
  21. }
  22. protected void tick(boolean isDown) {
  23. if(isReleased) {
  24. isReleased = false;
  25. time = 0;
  26. }
  27. if(this.isDown && !isDown) {
  28. isReleased = true;
  29. time++;
  30. }
  31. this.isDown = isDown;
  32. if(isDown) {
  33. time++;
  34. }
  35. }
  36. public boolean isDown() {
  37. return isDown;
  38. }
  39. public boolean isReleased() {
  40. return isReleased;
  41. }
  42. public int getTime() {
  43. return time;
  44. }
  45. public void setTime(int time) {
  46. this.time = time;
  47. }
  48. }
  49. public final GamepadButton a = new GamepadButton("A", 1);
  50. public final GamepadButton b = new GamepadButton("B", 2);
  51. public final GamepadButton x = new GamepadButton("X", 0);
  52. public final GamepadButton y = new GamepadButton("Y", 3);
  53. public final GamepadButton l = new GamepadButton("L", 4);
  54. public final GamepadButton r = new GamepadButton("R", 5);
  55. public final GamepadButton start = new GamepadButton("Start", 9);
  56. public final GamepadButton select = new GamepadButton("Select", 8);
  57. public final GamepadButton left = new GamepadButton("Left", 0);
  58. public final GamepadButton right = new GamepadButton("Right", 0);
  59. public final GamepadButton up = new GamepadButton("Up", 1);
  60. public final GamepadButton down = new GamepadButton("Down", 1);
  61. private final GamepadButton[] buttons = new GamepadButton[]{
  62. a, b, x, y, l, r, start, select
  63. };
  64. public final GamepadButton[] bindings = new GamepadButton[]{
  65. a, b, x, y, l, r, start, select, left, right, up, down
  66. };
  67. public final int joystickId;
  68. protected Gamepad(int joystickId) {
  69. this.joystickId = joystickId;
  70. }
  71. protected void tick() {
  72. if(!glfwJoystickPresent(joystickId)) {
  73. return;
  74. }
  75. updateButtons();
  76. updateAxes();
  77. }
  78. private void updateButtons() {
  79. ByteBuffer buttonBuffer = glfwGetJoystickButtons(joystickId);
  80. if(buttonBuffer == null) {
  81. return;
  82. }
  83. for(GamepadButton binding : buttons) {
  84. binding.tick(buttonBuffer.get(binding.getMapping()) != 0);
  85. }
  86. }
  87. private void updateAxes() {
  88. FloatBuffer axesBuffer = glfwGetJoystickAxes(joystickId);
  89. if(axesBuffer == null) {
  90. return;
  91. }
  92. left.tick(axesBuffer.get(left.getMapping()) < -0.5f);
  93. right.tick(axesBuffer.get(right.getMapping()) > 0.5f);
  94. up.tick(axesBuffer.get(up.getMapping()) < -0.5f);
  95. down.tick(axesBuffer.get(down.getMapping()) > 0.5f);
  96. }
  97. }