|
|
@ -49,7 +49,7 @@ import static org.lwjgl.glfw.GLFW.*; |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class GlfwJoystickInput implements JoyInput { |
|
|
|
public class GlfwJoystickInput implements JoyInput { |
|
|
|
|
|
|
|
|
|
|
|
private static final Logger LOGGER = Logger.getLogger(InputManager.class.getName()); |
|
|
|
private static final Logger LOGGER = Logger.getLogger(GlfwJoystickInput.class.getName()); |
|
|
|
|
|
|
|
|
|
|
|
private RawInputListener listener; |
|
|
|
private RawInputListener listener; |
|
|
|
|
|
|
|
|
|
|
@ -66,8 +66,28 @@ public class GlfwJoystickInput implements JoyInput { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void fireJoystickConnectedEvent(int jid) { |
|
|
|
|
|
|
|
Joystick joystick = joysticks.get(jid); |
|
|
|
|
|
|
|
((InputManager)listener).fireJoystickConnectedEvent(joystick); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void fireJoystickDisconnectedEvent(int jid) { |
|
|
|
|
|
|
|
Joystick joystick = joysticks.get(jid); |
|
|
|
|
|
|
|
((InputManager)listener).fireJoystickDisconnectedEvent(joystick); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void reloadJoysticks() { |
|
|
|
|
|
|
|
joysticks.clear(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
InputManager inputManager = (InputManager) listener; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Joystick[] joysticks = loadJoysticks(inputManager); |
|
|
|
|
|
|
|
inputManager.setJoysticks(joysticks); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public Joystick[] loadJoysticks(final InputManager inputManager) { |
|
|
|
public Joystick[] loadJoysticks(final InputManager inputManager) { |
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < GLFW_JOYSTICK_LAST; i++) { |
|
|
|
for (int i = 0; i < GLFW_JOYSTICK_LAST; i++) { |
|
|
|
if (glfwJoystickPresent(i)) { |
|
|
|
if (glfwJoystickPresent(i)) { |
|
|
|
final String name = glfwGetJoystickName(i); |
|
|
|
final String name = glfwGetJoystickName(i); |
|
|
@ -126,17 +146,26 @@ public class GlfwJoystickInput implements JoyInput { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void update() { |
|
|
|
public void update() { |
|
|
|
for (final Map.Entry<Integer, GlfwJoystick> entry : joysticks.entrySet()) { |
|
|
|
for (final Map.Entry<Integer, GlfwJoystick> entry : joysticks.entrySet()) { |
|
|
|
|
|
|
|
|
|
|
|
// Axes
|
|
|
|
// Axes
|
|
|
|
final FloatBuffer axisValues = glfwGetJoystickAxes(entry.getKey()); |
|
|
|
final FloatBuffer axisValues = glfwGetJoystickAxes(entry.getKey()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if a joystick is added or removed, the callback reloads the joysticks.
|
|
|
|
|
|
|
|
// when the callback is called and reloads the joystick, this iterator may already have started iterating.
|
|
|
|
|
|
|
|
// To avoid a NullPointerException we null-check the axisValues and bytebuffer objects.
|
|
|
|
|
|
|
|
// If the joystick it's iterating over no-longer exists it will return null.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (axisValues != null) { |
|
|
|
for (final JoystickAxis axis : entry.getValue().getAxes()) { |
|
|
|
for (final JoystickAxis axis : entry.getValue().getAxes()) { |
|
|
|
final float value = axisValues.get(axis.getAxisId()); |
|
|
|
final float value = axisValues.get(axis.getAxisId()); |
|
|
|
listener.onJoyAxisEvent(new JoyAxisEvent(axis, value)); |
|
|
|
listener.onJoyAxisEvent(new JoyAxisEvent(axis, value)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Buttons
|
|
|
|
// Buttons
|
|
|
|
final ByteBuffer byteBuffer = glfwGetJoystickButtons(entry.getKey()); |
|
|
|
final ByteBuffer byteBuffer = glfwGetJoystickButtons(entry.getKey()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (byteBuffer != null) { |
|
|
|
for (final JoystickButton button : entry.getValue().getButtons()) { |
|
|
|
for (final JoystickButton button : entry.getValue().getButtons()) { |
|
|
|
final boolean pressed = byteBuffer.get(button.getButtonId()) == GLFW_PRESS; |
|
|
|
final boolean pressed = byteBuffer.get(button.getButtonId()) == GLFW_PRESS; |
|
|
|
|
|
|
|
|
|
|
@ -147,6 +176,7 @@ public class GlfwJoystickInput implements JoyInput { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void destroy() { |
|
|
|
public void destroy() { |
|
|
|