From 0ae6c420ef138cb291c3ef2b3802cda911657ecb Mon Sep 17 00:00:00 2001 From: Kevin Glass Date: Wed, 29 Jun 2005 20:11:15 +0000 Subject: [PATCH] Initial controller input checkin. Source for the wrapper round JInput including the test tool. --- src/java/org/lwjgl/input/Controller.java | 242 +++++++++ src/java/org/lwjgl/input/ControllerEvent.java | 124 +++++ src/java/org/lwjgl/input/Controllers.java | 230 +++++++++ .../org/lwjgl/input/JInputController.java | 487 ++++++++++++++++++ .../org/lwjgl/test/input/TestControllers.java | 154 ++++++ 5 files changed, 1237 insertions(+) create mode 100644 src/java/org/lwjgl/input/Controller.java create mode 100644 src/java/org/lwjgl/input/ControllerEvent.java create mode 100644 src/java/org/lwjgl/input/Controllers.java create mode 100644 src/java/org/lwjgl/input/JInputController.java create mode 100644 src/java/org/lwjgl/test/input/TestControllers.java diff --git a/src/java/org/lwjgl/input/Controller.java b/src/java/org/lwjgl/input/Controller.java new file mode 100644 index 00000000..c1ac7cbe --- /dev/null +++ b/src/java/org/lwjgl/input/Controller.java @@ -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); +} diff --git a/src/java/org/lwjgl/input/ControllerEvent.java b/src/java/org/lwjgl/input/ControllerEvent.java new file mode 100644 index 00000000..2826e172 --- /dev/null +++ b/src/java/org/lwjgl/input/ControllerEvent.java @@ -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+"]"; + } +} diff --git a/src/java/org/lwjgl/input/Controllers.java b/src/java/org/lwjgl/input/Controllers.java new file mode 100644 index 00000000..727658d6 --- /dev/null +++ b/src/java/org/lwjgl/input/Controllers.java @@ -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 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; + } + + +} diff --git a/src/java/org/lwjgl/test/input/TestControllers.java b/src/java/org/lwjgl/test/input/TestControllers.java new file mode 100644 index 00000000..f3649f31 --- /dev/null +++ b/src/java/org/lwjgl/test/input/TestControllers.java @@ -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