Okay, after finding out that jME doesn't have support for Joysticks I took it upon myself to write support. Now, I originally planned to go with the same style as Mouse and Keyboard handling in jME, but I went ahead and went (with what I think) is a much better way to go about it and stuck with a similar approach as is done in Swing with the event model. Please see the code below:
import java.util.*;
import org.lwjgl.*;
import org.lwjgl.input.*;
/**
* @author Matthew D. Hicks
*/
public class JoystickManager extends Thread {
private static JoystickManager instance;
private ArrayList listeners;
private JoystickManager() throws LWJGLException {
Controllers.create();
listeners = new ArrayList();
}
public void run() {
Controllers.clearEvents();
JoystickButtonEvent buttonEvent;
JoystickAxisEvent axisEvent;
JoystickListener listener;
while (true) {
while (Controllers.next()) {
Controller source = Controllers.getEventSource();
if (Controllers.isEventButton()) {
buttonEvent = new JoystickButtonEvent(source.getName(), Controllers.getEventControlIndex(), source.isButtonPressed(Controllers.getEventControlIndex()));
for (int i = 0; i < listeners.size(); i++) {
listener = (JoystickListener)listeners.get(i);
listener.onButton(buttonEvent);
}
} else if (Controllers.isEventAxis()) {
axisEvent = new JoystickAxisEvent(source.getName(), Controllers.getEventControlIndex(), source.getAxisName(Controllers.getEventControlIndex()), source.getAxisValue(Controllers.getEventControlIndex()));
for (int i = 0; i < listeners.size(); i++) {
listener = (JoystickListener)listeners.get(i);
listener.onAxis(axisEvent);
}
}
}
try {
Thread.sleep(5);
} catch(InterruptedException exc) {
}
}
}
public static final void initialize() {
if (instance == null) {
try {
instance = new JoystickManager();
instance.start();
} catch(LWJGLException exc) {
throw new RuntimeException("Unable to create Controllers", exc);
}
}
}
public static final void addListener(JoystickListener listener) {
initialize();
instance.listeners.add(listener);
}
public static final void removeListener(JoystickListener listener) {
initialize();
instance.listeners.remove(listener);
}
public static final void removeListeners() {
initialize();
instance.listeners.clear();
}
public static final void setDeadZone(String controller, int axis, float value) {
initialize();
Controller c = getController(controller);
if (c != null) {
c.setDeadZone(axis, value);
}
}
public static final String[] getControllers() {
initialize();
String[] controllers = new String[Controllers.getControllerCount()];
for (int i = 0; i < controllers.length; i++) {
controllers[i] = Controllers.getController(i).getName();
}
return controllers;
}
private static final Controller getController(String controller) {
int count = Controllers.getControllerCount();
for (int i = 0; i < count; i++) {
if (Controllers.getController(i).getName().equals(controller)) {
return Controllers.getController(i);
}
}
return null;
}
}
/**
* @author Matthew D. Hicks
*/
public interface JoystickListener {
public void onButton(JoystickButtonEvent evt);
public void onAxis(JoystickAxisEvent evt);
}
/**
* @author Matthew D. Hicks
*/
public class JoystickButtonEvent {
private String controller;
private int button;
private boolean pressed;
public JoystickButtonEvent(String controller, int button, boolean pressed) {
this.controller = controller;
this.button = button;
this.pressed = pressed;
}
public String getController() {
return controller;
}
public int getButton() {
return button;
}
public boolean isPressed() {
return pressed;
}
public boolean isReleased() {
return !pressed;
}
}
/**
* @author Matthew D. Hicks
*/
public class JoystickAxisEvent {
private String controller;
private int axis;
private String axisName;
private float axisValue;
public JoystickAxisEvent(String controller, int axis, String axisName, float axisValue) {
this.controller = controller;
this.axis = axis;
this.axisName = axisName;
this.axisValue = axisValue;
}
public String getController() {
return controller;
}
public int getAxis() {
return axis;
}
public String getAxisName() {
return axisName;
}
public float getAxisValue() {
return axisValue;
}
}
Finally, here's a simple example of how to use it:
JoystickManager.initialize();
JoystickManager.addListener(new JoystickListener() {
public void onButton(JoystickButtonEvent evt) {
System.out.println("Button Event: " + evt.getButton() + ", " + evt.isPressed());
}
public void onAxis(JoystickAxisEvent evt) {
System.out.println("Axis Event: " + evt.getAxis() + ", " + evt.getAxisName() + ", " + evt.getAxisValue());
}
});
This does require more dependencies to LWJGL via JInput and some other JARs that come with LWJGL but if we're developing on it anyway, we might as well go all the way. When JOGL support gets finished we can create an implementation for that as well.
Hope you like it. :)
darkfrog