Skip to content

Commit b9b4a2d

Browse files
committed
Reference the joystick that changed state instead of the ID.
Use separate connection methods (onConnected/onDisconnected).
1 parent 2f6185b commit b9b4a2d

File tree

4 files changed

+49
-13
lines changed

4 files changed

+49
-13
lines changed

jme3-core/src/main/java/com/jme3/input/InputManager.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -992,14 +992,24 @@ public void clearJoystickConnectionListeners() {
992992
}
993993

994994
/**
995-
* Called when a joystick has been added or removed.
995+
* Called when a joystick has been connected.
996996
* This should only be called internally.
997-
* @param joystickId the ID of the joystick.
998-
* @param connected the connection state of the joystick.
997+
* @param joystick the joystick that has been connected.
999998
*/
1000-
public void fireJoystickConnectionEvent(int joystickId, boolean connected) {
999+
public void fireJoystickConnectedEvent(Joystick joystick) {
10011000
for (JoystickConnectionListener listener : joystickConnectionListeners) {
1002-
listener.connectionChanged(joystickId, connected);
1001+
listener.onConnected(joystick);
1002+
}
1003+
}
1004+
1005+
/**
1006+
* Called when a joystick has been disconnected.
1007+
* This should only be called internally.
1008+
* @param joystick the joystick that has been disconnected.
1009+
*/
1010+
public void fireJoystickDisconnectedEvent(Joystick joystick) {
1011+
for (JoystickConnectionListener listener : joystickConnectionListeners) {
1012+
listener.onDisconnected(joystick);
10031013
}
10041014
}
10051015

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
package com.jme3.input;
22

3+
/**
4+
* Listens for the state of a joystick connection.
5+
* @author jayfella
6+
*/
37
public interface JoystickConnectionListener {
48

5-
void connectionChanged(int joystickId, boolean connected);
9+
/**
10+
* Occurs when a new joystick has been detected.
11+
* @param joystick the joystick that has been detected.
12+
*/
13+
void onConnected(Joystick joystick);
14+
15+
/**
16+
* Occurs when an existing joystick has been disconnected.
17+
* @param joystick the joystick that has been disconnected.
18+
*/
19+
void onDisconnected(Joystick joystick);
620

721
}

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ public class GlfwJoystickInput implements JoyInput {
5757

5858
private final Map<JoystickButton, Boolean> joyButtonPressed = new HashMap<>();
5959

60-
// private InputManager inputManager;
61-
6260
private boolean initialized = false;
6361

6462
@Override
@@ -68,8 +66,14 @@ public void setJoyRumble(final int joyId, final float amount) {
6866
}
6967
}
7068

71-
public void fireJoystickConnectionEvent(int jid, boolean connected) {
72-
((InputManager)listener).fireJoystickConnectionEvent(jid, connected);
69+
public void fireJoystickConnectedEvent(int jid) {
70+
Joystick joystick = joysticks.get(jid);
71+
((InputManager)listener).fireJoystickConnectedEvent(joystick);
72+
}
73+
74+
public void fireJoystickDisconnectedEvent(int jid) {
75+
Joystick joystick = joysticks.get(jid);
76+
((InputManager)listener).fireJoystickDisconnectedEvent(joystick);
7377
}
7478

7579
public void reloadJoysticks() {

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,17 @@ protected void initContextFirstTime() {
239239
@Override
240240
public void invoke(int jid, int event) {
241241

242-
// fire the event after joysticks were reloaded.
243-
joyInput.reloadJoysticks();
244-
joyInput.fireJoystickConnectionEvent(jid, event == GLFW.GLFW_CONNECTED);
242+
// Invoke the disconnected event before we reload the joysticks or we lose the reference to it.
243+
// Invoke the connected event after we reload the joysticks to obtain the reference to it.
244+
245+
if ( event == GLFW.GLFW_CONNECTED ) {
246+
joyInput.reloadJoysticks();
247+
joyInput.fireJoystickConnectedEvent(jid);
248+
}
249+
else {
250+
joyInput.fireJoystickDisconnectedEvent(jid);
251+
joyInput.reloadJoysticks();
252+
}
245253
}
246254
});
247255

0 commit comments

Comments
 (0)