Merge pull request #1087 from jayfella/master
Add support for listening to joystick connection/disconnection.
This commit is contained in:
commit
33ade6b874
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,21 @@
|
||||
package com.jme3.input;
|
||||
|
||||
/**
|
||||
* Listens for the state of a joystick connection.
|
||||
* @author jayfella
|
||||
*/
|
||||
public interface JoystickConnectionListener {
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
}
|
@ -49,7 +49,7 @@ import static org.lwjgl.glfw.GLFW.*;
|
||||
*/
|
||||
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;
|
||||
|
||||
@ -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
|
||||
public Joystick[] loadJoysticks(final InputManager inputManager) {
|
||||
|
||||
for (int i = 0; i < GLFW_JOYSTICK_LAST; i++) {
|
||||
if (glfwJoystickPresent(i)) {
|
||||
final String name = glfwGetJoystickName(i);
|
||||
@ -126,23 +146,33 @@ public class GlfwJoystickInput implements JoyInput {
|
||||
@Override
|
||||
public void update() {
|
||||
for (final Map.Entry<Integer, GlfwJoystick> entry : joysticks.entrySet()) {
|
||||
|
||||
// Axes
|
||||
final FloatBuffer axisValues = glfwGetJoystickAxes(entry.getKey());
|
||||
|
||||
for (final JoystickAxis axis : entry.getValue().getAxes()) {
|
||||
final float value = axisValues.get(axis.getAxisId());
|
||||
listener.onJoyAxisEvent(new JoyAxisEvent(axis, value));
|
||||
// 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()) {
|
||||
final float value = axisValues.get(axis.getAxisId());
|
||||
listener.onJoyAxisEvent(new JoyAxisEvent(axis, value));
|
||||
}
|
||||
}
|
||||
|
||||
// Buttons
|
||||
final ByteBuffer byteBuffer = glfwGetJoystickButtons(entry.getKey());
|
||||
|
||||
for (final JoystickButton button : entry.getValue().getButtons()) {
|
||||
final boolean pressed = byteBuffer.get(button.getButtonId()) == GLFW_PRESS;
|
||||
if (byteBuffer != null) {
|
||||
for (final JoystickButton button : entry.getValue().getButtons()) {
|
||||
final boolean pressed = byteBuffer.get(button.getButtonId()) == GLFW_PRESS;
|
||||
|
||||
if (joyButtonPressed.get(button) != pressed) {
|
||||
joyButtonPressed.put(button, pressed);
|
||||
listener.onJoyButtonEvent(new JoyButtonEvent(button, pressed));
|
||||
if (joyButtonPressed.get(button) != pressed) {
|
||||
joyButtonPressed.put(button, pressed);
|
||||
listener.onJoyButtonEvent(new JoyButtonEvent(button, pressed));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -37,6 +37,7 @@ import static java.util.stream.Collectors.toSet;
|
||||
import static org.lwjgl.opencl.CL10.CL_CONTEXT_PLATFORM;
|
||||
import static org.lwjgl.opengl.GL.createCapabilities;
|
||||
import static org.lwjgl.opengl.GL11.glGetInteger;
|
||||
|
||||
import com.jme3.input.lwjgl.GlfwJoystickInput;
|
||||
import com.jme3.input.lwjgl.GlfwKeyInput;
|
||||
import com.jme3.input.lwjgl.GlfwMouseInput;
|
||||
@ -65,6 +66,7 @@ import com.jme3.util.LWJGLBufferAllocator.ConcurrentLWJGLBufferAllocator;
|
||||
import org.lwjgl.PointerBuffer;
|
||||
import org.lwjgl.Version;
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
import org.lwjgl.glfw.GLFWJoystickCallback;
|
||||
import org.lwjgl.opencl.APPLEGLSharing;
|
||||
import org.lwjgl.opencl.CL10;
|
||||
import org.lwjgl.opencl.KHRGLSharing;
|
||||
@ -233,6 +235,24 @@ public abstract class LwjglContext implements JmeContext {
|
||||
joyInput.initialize();
|
||||
}
|
||||
|
||||
GLFW.glfwSetJoystickCallback(new GLFWJoystickCallback() {
|
||||
@Override
|
||||
public void invoke(int jid, int event) {
|
||||
|
||||
// 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();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
renderable.set(true);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user