From b9b4a2d75b84a9519095a5bba341b316629b76d8 Mon Sep 17 00:00:00 2001 From: James Khan Date: Mon, 13 May 2019 09:59:49 +0100 Subject: [PATCH] Reference the joystick that changed state instead of the ID. Use separate connection methods (onConnected/onDisconnected). --- .../java/com/jme3/input/InputManager.java | 20 ++++++++++++++----- .../input/JoystickConnectionListener.java | 16 ++++++++++++++- .../jme3/input/lwjgl/GlfwJoystickInput.java | 12 +++++++---- .../com/jme3/system/lwjgl/LwjglContext.java | 14 ++++++++++--- 4 files changed, 49 insertions(+), 13 deletions(-) diff --git a/jme3-core/src/main/java/com/jme3/input/InputManager.java b/jme3-core/src/main/java/com/jme3/input/InputManager.java index 1455860f5..eb15f1c8b 100644 --- a/jme3-core/src/main/java/com/jme3/input/InputManager.java +++ b/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. - * @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); } } diff --git a/jme3-core/src/main/java/com/jme3/input/JoystickConnectionListener.java b/jme3-core/src/main/java/com/jme3/input/JoystickConnectionListener.java index 93c43d2c1..17457f5a4 100644 --- a/jme3-core/src/main/java/com/jme3/input/JoystickConnectionListener.java +++ b/jme3-core/src/main/java/com/jme3/input/JoystickConnectionListener.java @@ -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); } diff --git a/jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/GlfwJoystickInput.java b/jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/GlfwJoystickInput.java index da5f47eee..6f3f6a379 100644 --- a/jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/GlfwJoystickInput.java +++ b/jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/GlfwJoystickInput.java @@ -57,8 +57,6 @@ public class GlfwJoystickInput implements JoyInput { private final Map 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() { diff --git a/jme3-lwjgl3/src/main/java/com/jme3/system/lwjgl/LwjglContext.java b/jme3-lwjgl3/src/main/java/com/jme3/system/lwjgl/LwjglContext.java index 27925f477..a34ce795f 100644 --- a/jme3-lwjgl3/src/main/java/com/jme3/system/lwjgl/LwjglContext.java +++ b/jme3-lwjgl3/src/main/java/com/jme3/system/lwjgl/LwjglContext.java @@ -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(); + } } });