Bladeren bron

Reference the joystick that changed state instead of the ID.
Use separate connection methods (onConnected/onDisconnected).

James Khan 6 jaren geleden
bovenliggende
commit
b9b4a2d75b

+ 15 - 5
jme3-core/src/main/java/com/jme3/input/InputManager.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.
      * 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) {
         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);
         }
         }
     }
     }
 
 

+ 15 - 1
jme3-core/src/main/java/com/jme3/input/JoystickConnectionListener.java

@@ -1,7 +1,21 @@
 package com.jme3.input;
 package com.jme3.input;
 
 
+/**
+ * Listens for the state of a joystick connection.
+ * @author jayfella
+ */
 public interface JoystickConnectionListener {
 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);
 
 
 }
 }

+ 8 - 4
jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/GlfwJoystickInput.java

@@ -57,8 +57,6 @@ public class GlfwJoystickInput implements JoyInput {
 
 
     private final Map<JoystickButton, Boolean> joyButtonPressed = new HashMap<>();
     private final Map<JoystickButton, Boolean> joyButtonPressed = new HashMap<>();
 
 
-    // private InputManager inputManager;
-
     private boolean initialized = false;
     private boolean initialized = false;
 
 
     @Override
     @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() {
     public void reloadJoysticks() {

+ 11 - 3
jme3-lwjgl3/src/main/java/com/jme3/system/lwjgl/LwjglContext.java

@@ -239,9 +239,17 @@ public abstract class LwjglContext implements JmeContext {
             @Override
             @Override
             public void invoke(int jid, int event) {
             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();
+                }
             }
             }
         });
         });