Reference the joystick that changed state instead of the ID.

Use separate connection methods (onConnected/onDisconnected).
accellbaker
James Khan 6 years ago
parent 2f6185b5cf
commit b9b4a2d75b
  1. 20
      jme3-core/src/main/java/com/jme3/input/InputManager.java
  2. 16
      jme3-core/src/main/java/com/jme3/input/JoystickConnectionListener.java
  3. 12
      jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/GlfwJoystickInput.java
  4. 14
      jme3-lwjgl3/src/main/java/com/jme3/system/lwjgl/LwjglContext.java

@ -992,14 +992,24 @@ public class InputManager implements RawInputListener {
}
/**
* Called when a joystick has been added or removed.
* Called when a joystick has been connected.
* This should only be called internally.
* @param joystickId the ID of the joystick.
* @param connected the connection state of the joystick.
* @param joystick the joystick that has been connected.
*/
public void fireJoystickConnectionEvent(int joystickId, boolean connected) {
public void fireJoystickConnectedEvent(Joystick joystick) {
for (JoystickConnectionListener listener : joystickConnectionListeners) {
listener.connectionChanged(joystickId, connected);
listener.onConnected(joystick);
}
}
/**
* Called when a joystick has been disconnected.
* This should only be called internally.
* @param joystick the joystick that has been disconnected.
*/
public void fireJoystickDisconnectedEvent(Joystick joystick) {
for (JoystickConnectionListener listener : joystickConnectionListeners) {
listener.onDisconnected(joystick);
}
}

@ -1,7 +1,21 @@
package com.jme3.input;
/**
* Listens for the state of a joystick connection.
* @author jayfella
*/
public interface JoystickConnectionListener {
void connectionChanged(int joystickId, boolean connected);
/**
* Occurs when a new joystick has been detected.
* @param joystick the joystick that has been detected.
*/
void onConnected(Joystick joystick);
/**
* Occurs when an existing joystick has been disconnected.
* @param joystick the joystick that has been disconnected.
*/
void onDisconnected(Joystick joystick);
}

@ -57,8 +57,6 @@ public class GlfwJoystickInput implements JoyInput {
private final Map<JoystickButton, Boolean> joyButtonPressed = new HashMap<>();
// private InputManager inputManager;
private boolean initialized = false;
@Override
@ -68,8 +66,14 @@ public class GlfwJoystickInput implements JoyInput {
}
}
public void fireJoystickConnectionEvent(int jid, boolean connected) {
((InputManager)listener).fireJoystickConnectionEvent(jid, connected);
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() {

@ -239,9 +239,17 @@ public abstract class LwjglContext implements JmeContext {
@Override
public void invoke(int jid, int event) {
// fire the event after joysticks were reloaded.
joyInput.reloadJoysticks();
joyInput.fireJoystickConnectionEvent(jid, event == GLFW.GLFW_CONNECTED);
// Invoke the disconnected event before we reload the joysticks or we lose the reference to it.
// Invoke the connected event after we reload the joysticks to obtain the reference to it.
if ( event == GLFW.GLFW_CONNECTED ) {
joyInput.reloadJoysticks();
joyInput.fireJoystickConnectedEvent(jid);
}
else {
joyInput.fireJoystickDisconnectedEvent(jid);
joyInput.reloadJoysticks();
}
}
});

Loading…
Cancel
Save