Initial controller input checkin. Source for the wrapper round JInput including the test tool.

This commit is contained in:
Kevin Glass 2005-06-29 20:11:15 +00:00
parent a14316a498
commit 0ae6c420ef
5 changed files with 1237 additions and 0 deletions

View File

@ -0,0 +1,242 @@
package org.lwjgl.input;
/**
* A game controller of some sort that will provide input. The controller
* presents buttons and axes. Buttons are either pressed or not pressed. Axis
* provide analogue values.
*
* @author Kevin Glass
*/
public interface Controller {
/**
* Get the name assigned to this controller.
*
* @return The name assigned to this controller
*/
public String getName();
/**
* Get the index of this controller in the collection
*
* @return The index of this controller in the collection
*/
public int getIndex();
/**
* Retrieve the number of buttons available on this controller
*
* @return The number of butotns available on this controller
*/
public int getButtonCount();
/**
* Get the name of the specified button. Be warned, often this is
* as exciting as "Button X"
*
* @param index The index of the button whose name should be retrieved
* @return The name of the button requested
*/
public String getButtonName(int index);
/**
* Check if a button is currently pressed
*
* @param index The button to check
* @return True if the button is currently pressed
*/
public boolean isButtonPressed(int index);
/**
* Poll the controller for new data. This will also update
* events
*/
public void poll();
/**
* Get the X-Axis value of the POV on this controller
*
* @return The X-Axis value of the POV on this controller
*/
public float getPovX();
/**
* Get the Y-Axis value of the POV on this controller
*
* @return The Y-Axis value of the POV on this controller
*/
public float getPovY();
/**
* Get the dead zone for a specified axis
*
* @param index The index of the axis for which to retrieve the dead zone
* @return The dead zone for the specified axis
*/
public float getDeadZone(int index);
/**
* Set the dead zone for the specified axis
*
* @param index The index of hte axis for which to set the dead zone
* @param zone The dead zone to use for the specified axis
*/
public void setDeadZone(int index,float zone);
/**
* Retrieve the number of axes available on this controller.
*
* @return The number of axes available on this controller.
*/
public int getAxisCount();
/**
* Get the name that's given to the specified axis
*
* @param index The index of the axis whose name should be retrieved
* @return The name of the specified axis.
*/
public String getAxisName(int index);
/**
* Retrieve the value thats currently available on a specified axis. The
* value will always be between 1.0 and -1.0 and will calibrate as values
* are passed read. It may be useful to get the player to wiggle the joystick
* from side to side to get the calibration right.
*
* @param index The index of axis to be read
* @return The value from the specified axis.
*/
public float getAxisValue(int index);
/**
* Get the value from the X axis if there is one. If no X axis is
* defined a zero value will be returned.
*
* @return The value from the X axis
*/
public float getXAxisValue();
/**
* Get the dead zone for the X axis.
*
* @return The dead zone for the X axis
*/
public float getXAxisDeadZone();
/**
* Set the dead zone for the X axis
*
* @param zone The dead zone to use for the X axis
*/
public void setXAxisDeadZone(float zone);
/**
* Get the value from the Y axis if there is one. If no Y axis is
* defined a zero value will be returned.
*
* @return The value from the Y axis
*/
public float getYAxisValue();
/**
* Get the dead zone for the Y axis.
*
* @return The dead zone for the Y axis
*/
public float getYAxisDeadZone();
/**
* Set the dead zone for the Y axis
*
* @param zone The dead zone to use for the Y axis
*/
public void setYAxisDeadZone(float zone);
/**
* Get the value from the Z axis if there is one. If no Z axis is
* defined a zero value will be returned.
*
* @return The value from the Z axis
*/
public float getZAxisValue();
/**
* Get the dead zone for the Z axis.
*
* @return The dead zone for the Z axis
*/
public float getZAxisDeadZone();
/**
* Set the dead zone for the Z axis
*
* @param zone The dead zone to use for the Z axis
*/
public void setZAxisDeadZone(float zone);
/**
* Get the value from the RX axis if there is one. If no RX axis is
* defined a zero value will be returned.
*
* @return The value from the RX axis
*/
public float getRXAxisValue();
/**
* Get the dead zone for the RX axis.
*
* @return The dead zone for the RX axis
*/
public float getRXAxisDeadZone();
/**
* Set the dead zone for the RX axis
*
* @param zone The dead zone to use for the RX axis
*/
public void setRXAxisDeadZone(float zone);
/**
* Get the value from the RY axis if there is one. If no RY axis is
* defined a zero value will be returned.
*
* @return The value from the RY axis
*/
public float getRYAxisValue();
/**
* Get the dead zone for the RY axis.
*
* @return The dead zone for the RY axis
*/
public float getRYAxisDeadZone();
/**
* Set the dead zone for the RY axis
*
* @param zone The dead zone to use for the RY axis
*/
public void setRYAxisDeadZone(float zone);
/**
* Get the value from the RZ axis if there is one. If no RZ axis is
* defined a zero value will be returned.
*
* @return The value from the RZ axis
*/
public float getRZAxisValue();
/**
* Get the dead zone for the RZ axis.
*
* @return The dead zone for the RZ axis
*/
public float getRZAxisDeadZone();
/**
* Set the dead zone for the RZ axis
*
* @param zone The dead zone to use for the RZ axis
*/
public void setRZAxisDeadZone(float zone);
}

View File

@ -0,0 +1,124 @@
package org.lwjgl.input;
/**
* An event occuring on a controller.
*
* @author Kevin Glass
*/
public class ControllerEvent {
/** Indicates the event was caused by a button */
public static final int BUTTON = 1;
/** Indicates the event was caused by a axis */
public static final int AXIS = 2;
/** Indicates the event was caused by a pov X */
public static final int POVX = 3;
/** Indicates the event was caused by a pov Y */
public static final int POVY = 4;
/** The controller generating the event */
private Controller source;
/** The index of the input (axis or button) that generated the event */
private int index;
/** Type of control that generated the event */
private int type;
/** True if this event was caused by the x axis */
private boolean xaxis;
/** True if this event was caused by the y axis */
private boolean yaxis;
/**
* Create a new event
*
* @param source The source of the event
* @param type The type of control generating this event
* @param index The index of the input that generated the event
* @param xaxis True if this event was caused by the x-axis
* @param yaxis True if this event was caused by the y-axis
*/
public ControllerEvent(Controller source,int type,int index,boolean xaxis,boolean yaxis) {
this.source = source;
this.type = type;
this.index = index;
this.xaxis = xaxis;
this.yaxis = yaxis;
}
/**
* Get the controller that generated this event
*
* @return The controller that generated this event
*/
public Controller getSource() {
return source;
}
/**
* Get the index of the control generating this event
*
* @return The index of the control generating this event
*/
public int getControlIndex() {
return index;
}
/**
* Check if this event was generated by a button
*
* @return True if this event was generated by a button
*/
public boolean isButton() {
return type == BUTTON;
}
/**
* Check if this event was generated by a axis
*
* @return True if this event was generated by a axis
*/
public boolean isAxis() {
return type == AXIS;
}
/**
* Check if this event was generated by a pov
*
* @return True if this event was generated by a pov
*/
public boolean isPovY() {
return type == POVY;
}
/**
*
* Check if this event was generated by a pov
*
* @return True if this event was generated by a pov
*/
public boolean isPovX() {
return type == POVX;
}
/**
* Check if this event was caused by the X axis
*
* @return True if this event was caused by the X axis
*/
public boolean isXAxis() {
return xaxis;
}
/**
* Check if this event was caused by the Y axis
*
* @return True if this event was caused by the Y axis
*/
public boolean isYAxis() {
return yaxis;
}
/*
* @see java.lang.Object#toString()
*/
public String toString() {
return "["+source+" type="+type+" xaxis="+xaxis+" yaxis="+yaxis+"]";
}
}

View File

@ -0,0 +1,230 @@
package org.lwjgl.input;
import java.util.ArrayList;
import net.java.games.input.ControllerEnvironment;
import org.lwjgl.LWJGLException;
/**
* The collection of controllers currently connected.
*
* @author Kevin Glass
*/
public class Controllers {
/** The name of the plugin used on windows */
private static final String WINDOWS_PLUGIN = "net.java.games.input.DirectInputEnvironmentPlugin";
/** The name of the plugin used on linux */
private static final String LINUX_PLUGIN = "net.java.games.input.LinuxEnvironmentPlugin";
/** The name of the plugin used on macos */
private static final String MACOS_PLUGIN = "net.java.games.input.OSXEnvironmentPlugin";
/** The controllers available */
private static ArrayList controllers = new ArrayList();
/** The number of controllers */
private static int controllerCount;
/** The current list of events */
private static ArrayList events = new ArrayList();
/** The current event */
private static ControllerEvent event;
/**
* Initialise the controllers collection
*
* @throws LWJGLException Indicates a failure to initialise the controller library.
*/
public static void create() throws LWJGLException {
try {
String plugins = System.getProperty("jinput.plugins");
if (plugins == null) {
if (System.getProperty("os.name").startsWith("Win")) {
System.setProperty("jinput.plugins",WINDOWS_PLUGIN);
}
if (System.getProperty("os.name").startsWith("Lin")) {
System.setProperty("jinput.plugins",LINUX_PLUGIN);
}
if (System.getProperty("os.name").startsWith("Mac")) {
System.setProperty("jinput.plugins",MACOS_PLUGIN);
}
}
ControllerEnvironment env = ControllerEnvironment.getDefaultEnvironment();
net.java.games.input.Controller[] found = env.getControllers();
ArrayList lollers = new ArrayList();
for (int i=0;i<found.length;i++) {
net.java.games.input.Controller c = found[i];
if ((!c.getType().equals(net.java.games.input.Controller.Type.KEYBOARD)) &&
(!c.getType().equals(net.java.games.input.Controller.Type.MOUSE))) {
lollers.add(c);
}
}
int length = lollers.size();
for (int i=0;i<length;i++) {
net.java.games.input.Controller c = (net.java.games.input.Controller) lollers.get(i);
createController(c);
}
} catch (Throwable e) {
throw new LWJGLException("Failed to initialise controllers",e);
}
}
/**
* Utility to create a controller based on its potential sub-controllers
*
* @param c The controller to add
*/
private static void createController(net.java.games.input.Controller c) {
net.java.games.input.Controller[] sub = c.getControllers();
if (sub.length == 0) {
JInputController controller = new JInputController(controllerCount,c);
controllers.add(controller);
controllerCount++;
} else {
for (int i=0;i<sub.length;i++) {
createController(sub[i]);
}
}
}
/**
* Get a controller from the collection
*
* @param index The index of the controller to retrieve
* @return The controller requested
*/
public static Controller getController(int index) {
return (Controller) controllers.get(index);
}
/**
* Retrieve a count of the number of controllers
*
* @return The number of controllers available
*/
public static int getControllerCount() {
return controllers.size();
}
/**
* Poll the controllers available. This will both update their state
* and generate events that must be cleared.
*/
public static void poll() {
for (int i=0;i<controllers.size();i++) {
getController(i).poll();
}
}
/**
* Clear any events stored for the controllers in this set
*/
public static void clearEvents() {
events.clear();
}
/**
* Move to the next event that has been stored.
*
* @return True if there is still an event to process
*/
public static boolean next() {
if (events.size() == 0) {
event = null;
return false;
}
event = (ControllerEvent) events.remove(0);
return event != null;
}
/**
* Get the source of the current event
*
* @return The source of the current event
*/
public static Controller getEventSource() {
return event.getSource();
}
/**
* Get the index of the control that caused the current event
*
* @return The index of the control that cause the current event
*/
public static int getEventControlIndex() {
return event.getControlIndex();
}
/**
* Check if the current event was caused by a button
*
* @return True if the current event was caused by a button
*/
public static boolean isEventButton() {
return event.isButton();
}
/**
* Check if the current event was caused by a axis
*
* @return True if the current event was caused by a axis
*/
public static boolean isEventAxis() {
return event.isAxis();
}
/**
* Check if the current event was caused by movement on the x-axis
*
* @return True if the current event was cause by movement on the x-axis
*/
public static boolean isEventXAxis() {
return event.isXAxis();
}
/**
* Check if the current event was caused by movement on the y-axis
*
* @return True if the current event was caused by movement on the y-axis
*/
public static boolean isEventYAxis() {
return event.isYAxis();
}
/**
* Check if the current event was cause by the POV x-axis
*
* @return True if the current event was caused by the POV x-axis
*/
public static boolean isEventPovX() {
return event.isPovX();
}
/**
* Check if the current event was cause by the POV x-axis
*
* @return True if the current event was caused by the POV x-axis
*/
public static boolean isEventPovY() {
return event.isPovY();
}
/**
* Add an event to the stack of events that have been caused
*
* @param event The event to add to the list
*/
static void addEvent(ControllerEvent event) {
if (event != null) {
events.add(event);
}
}
}

View File

@ -0,0 +1,487 @@
package org.lwjgl.input;
import java.util.ArrayList;
import net.java.games.input.Component;
/**
* A wrapper round a JInput controller that attempts to make the interface
* more useable.
*
* @author Kevin Glass
*/
public class JInputController implements Controller {
/** The JInput controller this class is wrapping */
private net.java.games.input.Controller target;
/** The index that has been assigned to this controller */
private int index;
/** The Buttons that have been detected on the JInput controller */
private ArrayList buttons = new ArrayList();
/** The Axes that have been detected on the JInput controller */
private ArrayList axes = new ArrayList();
/** The POVs that have been detected on the JInput controller */
private ArrayList pov = new ArrayList();
/** The state of the buttons last check */
private boolean[] buttonState;
/** The values that were read from the pov last check */
private float[] povValues;
/** The values that were read from the axes last check */
private float[] axesValue;
/** The maximum values read for each axis */
private float[] axesMax;
/** The dead zones for each axis */
private float[] deadZones;
/** The index of the X axis or -1 if no X axis is defined */
private int xaxis = -1;
/** The index of the Y axis or -1 if no Y axis is defined */
private int yaxis = -1;
/** The index of the X axis or -1 if no Z axis is defined */
private int zaxis = -1;
/** The index of the RX axis or -1 if no RX axis is defined */
private int rxaxis = -1;
/** The index of the RY axis or -1 if no RY axis is defined */
private int ryaxis = -1;
/** The index of the RZ axis or -1 if no RZ axis is defined */
private int rzaxis = -1;
/**
* Create a new controller that wraps round a JInput controller and hopefully
* makes it easier to use.
*
* @param index The index this controller has been assigned to
* @param target The target JInput controller this class is wrapping
*/
public JInputController(int index,net.java.games.input.Controller target) {
this.target = target;
this.index = index;
Component[] sourceAxes = target.getComponents();
for (int i=0;i<sourceAxes.length;i++) {
if (sourceAxes[i].getIdentifier() instanceof Component.Identifier.Button) {
buttons.add(sourceAxes[i]);
} else if (sourceAxes[i].getIdentifier().equals(Component.Identifier.Axis.POV)) {
pov.add(sourceAxes[i]);
} else {
axes.add(sourceAxes[i]);
}
}
buttonState = new boolean[buttons.size()];
povValues = new float[pov.size()];
axesValue = new float[axes.size()];
int buttonsCount = 0;
int axesCount = 0;
// initialise the state
for (int i=0;i<sourceAxes.length;i++) {
if (sourceAxes[i].getIdentifier() instanceof Component.Identifier.Button) {
buttonState[buttonsCount] = sourceAxes[i].getPollData() != 0;
buttonsCount++;
} else if (sourceAxes[i].getIdentifier().equals(Component.Identifier.Axis.POV)) {
// no account for POV yet
// pov.add(sourceAxes[i]);
} else {
axesValue[axesCount] = sourceAxes[i].getPollData();
if (sourceAxes[i].getIdentifier().equals(Component.Identifier.Axis.X)) {
xaxis = axesCount;
}
if (sourceAxes[i].getIdentifier().equals(Component.Identifier.Axis.Y)) {
yaxis = axesCount;
}
if (sourceAxes[i].getIdentifier().equals(Component.Identifier.Axis.Z)) {
zaxis = axesCount;
}
if (sourceAxes[i].getIdentifier().equals(Component.Identifier.Axis.RX)) {
rxaxis = axesCount;
}
if (sourceAxes[i].getIdentifier().equals(Component.Identifier.Axis.RY)) {
ryaxis = axesCount;
}
if (sourceAxes[i].getIdentifier().equals(Component.Identifier.Axis.RZ)) {
rzaxis = axesCount;
}
axesCount++;
}
}
axesMax = new float[axes.size()];
deadZones = new float[axes.size()];
for (int i=0;i<axesMax.length;i++) {
axesMax[i] = 1.0f;
deadZones[i] = 0.05f;
}
}
/*
* @see org.lwjgl.input.Controller#getName()
*/
public String getName() {
String name = target.getName();
return name.substring(0,name.length()/2);
}
/*
* @see org.lwjgl.input.Controller#getIndex()
*/
public int getIndex() {
return index;
}
/*
* @see org.lwjgl.input.Controller#getButtonCount()
*/
public int getButtonCount() {
return buttons.size();
}
/*
* @see org.lwjgl.input.Controller#getButtonName(int)
*/
public String getButtonName(int index) {
Component button = (Component) buttons.get(index);
return button.getName();
}
/*
* @see org.lwjgl.input.Controller#isButtonPressed(int)
*/
public boolean isButtonPressed(int index) {
return buttonState[index];
}
/*
* @see org.lwjgl.input.Controller#poll()
*/
public void poll() {
target.poll();
// read buttons to update events
for (int i=0;i<getButtonCount();i++) {
Component button = (Component) buttons.get(i);
float data = button.getPollData();
if (buttonState[i] != (data != 0)) {
// fire button pressed event
Controllers.addEvent(new ControllerEvent(this,ControllerEvent.BUTTON,i,false,false));
}
buttonState[i] = data != 0;
}
// read povs to update events
for (int i=0;i<pov.size();i++) {
Component p = (Component) pov.get(i);
float data = p.getPollData();
if (povValues[i] != data) {
float prevX = getPovX();
float prevY = getPovY();
povValues[i] = data;
if (prevX != getPovX()) {
Controllers.addEvent(new ControllerEvent(this,ControllerEvent.POVX,0,false,false));
}
if (prevY != getPovY()) {
Controllers.addEvent(new ControllerEvent(this,ControllerEvent.POVY,0,false,false));
}
}
povValues[i] = data;
}
// read axis to update events
for (int i=0;i<getAxisCount();i++) {
Component axis = (Component) axes.get(i);
float value = axis.getPollData();
// fixed dead zone since most axis don't report it :(
if (Math.abs(value) < deadZones[i]) {
value = 0;
}
if (Math.abs(value) < axis.getDeadZone()) {
value = 0;
}
if (Math.abs(value) > axesMax[i]) {
axesMax[i] = Math.abs(value);
}
// normalize the value based on maximum value read in the past
value /= axesMax[i];
if (axesValue[i] != value) {
// fire event
Controllers.addEvent(new ControllerEvent(this,ControllerEvent.AXIS,i,i == xaxis,i == yaxis));
}
axesValue[i] = value;
}
}
/*
* @see org.lwjgl.input.Controller#getAxisCount()
*/
public int getAxisCount() {
return axes.size();
}
/*
* @see org.lwjgl.input.Controller#getAxisName(int)
*/
public String getAxisName(int index) {
Component axis = (Component) axes.get(index);
return axis.getName();
}
/*
* @see org.lwjgl.input.Controller#getAxisValue(int)
*/
public float getAxisValue(int index) {
return axesValue[index];
}
/*
* @see org.lwjgl.input.Controller#getXAxisValue()
*/
public float getXAxisValue() {
if (xaxis == -1) {
return 0;
}
return getAxisValue(xaxis);
}
/*
* @see org.lwjgl.input.Controller#getYAxisValue()
*/
public float getYAxisValue() {
if (yaxis == -1) {
return 0;
}
return getAxisValue(yaxis);
}
/*
* @see org.lwjgl.input.Controller#getXAxisDeadZone()
*/
public float getXAxisDeadZone() {
if (xaxis == -1) {
return 0;
}
return getDeadZone(xaxis);
}
/*
* @see org.lwjgl.input.Controller#getYAxisDeadZone()
*/
public float getYAxisDeadZone() {
if (yaxis == -1) {
return 0;
}
return getDeadZone(yaxis);
}
/*
* @see org.lwjgl.input.Controller#setXAxisDeadZone(float)
*/
public void setXAxisDeadZone(float zone) {
setDeadZone(xaxis,zone);
}
/*
* @see org.lwjgl.input.Controller#setYAxisDeadZone(float)
*/
public void setYAxisDeadZone(float zone) {
setDeadZone(yaxis,zone);
}
/*
* @see org.lwjgl.input.Controller#getDeadZone(int)
*/
public float getDeadZone(int index) {
return deadZones[index];
}
/*
* @see org.lwjgl.input.Controller#setDeadZone(int, float)
*/
public void setDeadZone(int index, float zone) {
deadZones[index] = zone;
}
/*
* @see org.lwjgl.input.Controller#getZAxisValue()
*/
public float getZAxisValue() {
if (zaxis == -1) {
return 0;
}
return getAxisValue(zaxis);
}
/*
* @see org.lwjgl.input.Controller#getZAxisDeadZone()
*/
public float getZAxisDeadZone() {
if (zaxis == -1) {
return 0;
}
return getDeadZone(zaxis);
}
/*
* @see org.lwjgl.input.Controller#setZAxisDeadZone(float)
*/
public void setZAxisDeadZone(float zone) {
setDeadZone(zaxis,zone);
}
/*
* @see org.lwjgl.input.Controller#getRXAxisValue()
*/
public float getRXAxisValue() {
if (rxaxis == -1) {
return 0;
}
return getAxisValue(rxaxis);
}
/*
* @see org.lwjgl.input.Controller#getRXAxisDeadZone()
*/
public float getRXAxisDeadZone() {
if (rxaxis == -1) {
return 0;
}
return getDeadZone(rxaxis);
}
/*
* @see org.lwjgl.input.Controller#setRXAxisDeadZone(float)
*/
public void setRXAxisDeadZone(float zone) {
setDeadZone(rxaxis,zone);
}
/*
* @see org.lwjgl.input.Controller#getRYAxisValue()
*/
public float getRYAxisValue() {
if (ryaxis == -1) {
return 0;
}
return getAxisValue(ryaxis);
}
/*
* @see org.lwjgl.input.Controller#getRYAxisDeadZone()
*/
public float getRYAxisDeadZone() {
if (ryaxis == -1) {
return 0;
}
return getDeadZone(ryaxis);
}
/*
* @see org.lwjgl.input.Controller#setRYAxisDeadZone(float)
*/
public void setRYAxisDeadZone(float zone) {
setDeadZone(ryaxis,zone);
}
/*
* @see org.lwjgl.input.Controller#getRZAxisValue()
*/
public float getRZAxisValue() {
if (rzaxis == -1) {
return 0;
}
return getAxisValue(rzaxis);
}
/*
* @see org.lwjgl.input.Controller#getRZAxisDeadZone()
*/
public float getRZAxisDeadZone() {
if (rzaxis == -1) {
return 0;
}
return getDeadZone(rzaxis);
}
/*
* @see org.lwjgl.input.Controller#setRZAxisDeadZone(float)
*/
public void setRZAxisDeadZone(float zone) {
setDeadZone(rzaxis,zone);
}
/*
* @see org.lwjgl.input.Controller#getPovX()
*/
public float getPovX() {
if (pov.size() == 0) {
return 0;
}
float value = povValues[0];
if ((value == Component.POV.DOWN_LEFT) ||
(value == Component.POV.UP_LEFT) ||
(value == Component.POV.LEFT)) {
return -1;
}
if ((value == Component.POV.DOWN_RIGHT) ||
(value == Component.POV.UP_RIGHT) ||
(value == Component.POV.RIGHT)) {
return 1;
}
return 0;
}
/*
* @see org.lwjgl.input.Controller#getPovY()
*/
public float getPovY() {
if (pov.size() == 0) {
return 0;
}
float value = povValues[0];
if ((value == Component.POV.DOWN_LEFT) ||
(value == Component.POV.DOWN_RIGHT) ||
(value == Component.POV.DOWN)) {
return 1;
}
if ((value == Component.POV.UP_LEFT) ||
(value == Component.POV.UP_RIGHT) ||
(value == Component.POV.UP)) {
return -1;
}
return 0;
}
}

View File

@ -0,0 +1,154 @@
package org.lwjgl.test.input;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import org.lwjgl.input.Controller;
import org.lwjgl.input.Controllers;
/**
* Oops. Forgot to document this one.
*
* @author Kevin Glass
*/
public class TestControllers extends JPanel {
public static int total = 0;
private JTextField[] values;
private JTextField[] names;
private Controller controller;
private int buttonCount;
private int itemCount;
public TestControllers(int index) {
controller = Controllers.getController(index);
setLayout(null);
buttonCount = controller.getButtonCount();
itemCount = controller.getButtonCount() + controller.getAxisCount() + 2;
values = new JTextField[itemCount];
names = new JTextField[itemCount];
for (int i=0;i<controller.getButtonCount();i++) {
names[i] = new JTextField();
names[i].setEditable(false);
names[i].setBounds(0,i*30,100,30);
names[i].setText(controller.getButtonName(i));
add(names[i]);
values[i] = new JTextField();
values[i].setEditable(false);
values[i].setBounds(100,i*30,100,30);
add(values[i]);
}
for (int i=buttonCount;i<buttonCount+controller.getAxisCount();i++) {
names[i] = new JTextField();
names[i].setEditable(false);
names[i].setBounds(0,i*30,100,30);
names[i].setText(controller.getAxisName(i-buttonCount));
add(names[i]);
values[i] = new JTextField();
values[i].setEditable(false);
values[i].setBounds(100,i*30,100,30);
add(values[i]);
}
int i = itemCount - 2;
names[i] = new JTextField();
names[i].setEditable(false);
names[i].setBounds(0,i*30,100,30);
names[i].setText("POV X");
add(names[i]);
values[i] = new JTextField();
values[i].setEditable(false);
values[i].setBounds(100,i*30,100,30);
add(values[i]);
i = itemCount - 1;
names[i] = new JTextField();
names[i].setEditable(false);
names[i].setBounds(0,i*30,100,30);
names[i].setText("POV Y");
add(names[i]);
values[i] = new JTextField();
values[i].setEditable(false);
values[i].setBounds(100,i*30,100,30);
add(values[i]);
total++;
setPreferredSize(new Dimension(200,30*itemCount));
JFrame frame = new JFrame(controller.getName());
frame.setContentPane(new JScrollPane(this));
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
total--;
if (total == 0) {
System.exit(0);
}
}
});
frame.setSize(230,400);
frame.setLocation(index*30,index*30);
frame.setVisible(true);
Thread t = new Thread() {
public void run() {
while (true) {
try { Thread.sleep(100); } catch (Exception e) {};
pollAndUpdate();
}
}
};
t.start();
}
public void pollAndUpdate() {
Controllers.poll();
for (int i=0;i<controller.getButtonCount();i++) {
values[i].setText(""+controller.isButtonPressed(i));
}
for (int i=buttonCount;i<buttonCount+controller.getAxisCount();i++) {
values[i].setText(""+controller.getAxisValue(i-buttonCount));
}
values[itemCount-2].setText(""+controller.getPovX());
values[itemCount-1].setText(""+controller.getPovY());
while (Controllers.next()) {
System.out.println("Event Fired: ");
System.out.println("\t"+Controllers.getEventSource()+":"+Controllers.getEventControlIndex()+":"+Controllers.isEventButton());
System.out.println("\t"+Controllers.isEventXAxis()+":"+Controllers.isEventYAxis());
}
}
public static void main(String[] argv) {
try {
Controllers.create();
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
int count = Controllers.getControllerCount();
System.out.println(count+" Controllers Found");
for (int i=0;i<count;i++) {
Controller controller = Controllers.getController(i);
System.out.println(controller.getName());
}
if (count == 0) {
System.exit(0);
}
for (int i=0;i<count;i++) {
new TestControllers(i);
}
}
}