first stab at the joystick implementation

This commit is contained in:
Brian Matzon 2002-11-17 18:14:13 +00:00
parent a9c614c2b2
commit 84e7d0fe98
1 changed files with 71 additions and 150 deletions

View File

@ -32,24 +32,24 @@
package org.lwjgl.input; package org.lwjgl.input;
import java.nio.ByteBuffer;
import org.lwjgl.Display;
import org.lwjgl.Sys; import org.lwjgl.Sys;
/** /**
* $Id$ * $Id$
* * <br>
* A raw Joystick interface. This can be used to poll the current state of the * A raw Joystick interface. This can be used to poll the current state of the
* joystick buttons, and determine the joystick position. The joystick position * joystick buttons, and determine the joystick position. The joystick position
* is returned as floats in the range -1.0f to 1.0f. * is returned as ints in the range -1000 to 1000.
* *
* No buffering is available. * No buffering is available.
* *
* Up to 8 buttons are available. A scrolly wheel or paddle, if present, is the z * Currently n (native limits, currently 128) buttons, the x, y, z axis is supported along with a POV (or HAT), where the z axis
* value. This will be in the range of 0.0f to 1.0f. * represents a throttle. In the future the joystick may support more buttons and
* axises and other features. but this is a platform issue.
* *
* @author cix_foo <cix_foo@users.sourceforge.net> * The joystick implementation currently only supports the first attached joystick.
*
* @author Brian Matzon <brian@matzon.dk>
* @version $Revision$ * @version $Revision$
*/ */
public class Joystick { public class Joystick {
@ -59,38 +59,47 @@ public class Joystick {
} }
/** Has the joystick been created? */ /** Has the joystick been created? */
private static boolean created; private boolean created;
/** The joystick buttons status from the last poll */ /** The joystick buttons status */
private static final boolean[] button = new boolean[8]; private boolean[] buttons;
/** X position, range -1.0f to 1.0f */ /** X position, range -1000 to 1000 */
public static float x; public int x = -1;
/** Y position, range -1.0f to 1.0f */ /** Y position, range -1000 to 1000 */
public static float y; public int y = -1;
/** Z position, range 0.0f to 1.0f */ /** Z position, range -1000 to 1000 */
public static float z; public int z = -1;
/** /** Position of Point of View from -1 to 27000 (360 degrees) */
* The joystick events from the last read: a sequence of Events public int pov;
*/
private static ByteBuffer readBuffer;
/** Address of the read buffer */ /** Constant specifying centered POV */
private static int readBufferAddress; public static final int POV_CENTER = -1;
/** The size in bytes of a single joystick event */ /** Constant specifying nortward POV */
private static final int JOYSTICK_EVENT_SIZE = 20; public static final int POV_NORTH = 0;
/** The stride in bytes of a single joystick event */ /** Constant specifying southward POV */
private static final int JOYSTICK_EVENT_STRIDE = 32; public static final int POV_SOUTH = 18000;
/** Constant specifying eastward POV */
public static final int POV_EAST = 27000;
/** Constant specifying westward POV */
public static final int POV_WEST = 9000;
/* Joystick capabilities */
public int buttonCount = -1;
public boolean hasZAxis = false;
public boolean hasPOV = false;
/** /**
* Joystick cannot be constructed. * Joystick cannot be constructed.
*/ */
private Joystick() { public Joystick() {
} }
/** /**
@ -101,83 +110,41 @@ public class Joystick {
initIDs(); initIDs();
} }
/**
* Register fields with the native library
*/
private static native void initIDs();
/** /**
* "Create" the joystick. The display must first have been created. * "Create" the joystick. The display must first have been created.
* @throws Exception if the joystick could not be created for any reason * @throws Exception if the joystick could not be created for any reason
*/ */
public static void create() throws Exception { public void create() throws Exception {
if (created) if (created) {
return; return;
if (!Display.isCreated()) }
throw new Exception("The display has not yet been created.");
if (!nCreate()) if (!nCreate()) {
throw new Exception("The joystick could not be created."); throw new Exception("The joystick could not be created.");
}
created = true; created = true;
} }
/**
* Native method to create the joystick
*
* @return true if the joystick was created
*/
private static native boolean nCreate();
/** /**
* "Destroy" the joystick * "Destroy" the joystick
*/ */
public static void destroy() { public void destroy() {
if (!created) if (!created) {
return; return;
}
created = false; created = false;
nDestroy(); nDestroy();
} }
/**
* Native method the destroy the joystick
*/
private static native void nDestroy();
/** /**
* Polls the joystick. * Polls the joystick.
*/ */
public static void poll() { public void poll() {
assert created : "The joystick has not been created."; assert created : "The joystick has not been created.";
nPoll(); nPoll();
} }
/**
* Native method to poll the joystick
*/
private static native void nPoll();
/**
* Queries the number of buttons the joystick has
* @return the number of buttons the joystick has
*/
public static int getNumButtons() {
assert created : "The joystick has not been created.";
return nGetNumButtons();
}
/**
* Native implementation of getNumButtons()
*/
private static native int nGetNumButtons();
/**
* Queries whether the joystick has a Z value
* @return true if the joystick has a Z value
*/
public static boolean hasZValue() {
assert created : "The joystick has not been created.";
return nHasZValue();
}
/** /**
* See if a particular mouse button is down. * See if a particular mouse button is down.
* *
@ -185,76 +152,30 @@ public class Joystick {
* @return true if the specified button is down * @return true if the specified button is down
* @see #getNumButtons() * @see #getNumButtons()
*/ */
public static boolean isButtonDown(int button) { public boolean isButtonDown(int button) {
assert created : "The joystick has not been created."; assert created : "The joystick has not been created.";
return Joystick.button[button]; return buttons[button];
}
/**
* Native implementation of hasZValue()
*/
private static native boolean nHasZValue();
/**
* Enable joystick buffering. Must be called after the joystick is created.
* @return the size of the joystick buffer in events, or 0 if no buffering
* can be enabled for any reason
*/
public static int enableBuffer() {
assert created : "The joystick has not been created.";
return nEnableBuffer();
} }
/** /**
* Native method to read the joystick buffer * Native method to poll the joystick
*/
private native void nPoll();
/**
* Native method to create the joystick
* *
* @param readBufferAddress the address of the joystick buffer * @return true if the joystick was created
* @return the number of joystick events read
*/ */
private static native int nRead(int readBufferAddress); private native boolean nCreate();
/** /**
* Reads the joystick buffer. * Native method the destroy the joystick
*/ */
public static void read() { private native void nDestroy();
assert created : "The joystick has not been created.";
assert readBuffer != null : "Joystick buffering has not been enabled.";
readBuffer.clear();
readBuffer.limit(nRead(readBufferAddress) << 1);
}
/** /**
* Native method to enable the buffer * Register fields with the native library
* @return the size of the buffer allocated, in events (1 event is 2 bytes),
* or 0 if no buffer can be allocated
*/ */
private static native int nEnableBuffer(); private static native void initIDs();
/**
* Gets the next joystick event. This returns its results as if a poll() had
* been called.
*
* @return true if a joystick event was read, false otherwise
*/
public static boolean next() {
assert created : "The joystick has not been created.";
assert readBuffer != null : "Joystick buffering has not been enabled.";
if (readBuffer.hasRemaining()) {
x = readBuffer.getFloat();
y = readBuffer.getFloat();
z = readBuffer.getFloat();
for (int i = 0; i < button.length; i ++)
button[i] = readBuffer.get() != (byte)0;
readBuffer.position(readBuffer.position() + (JOYSTICK_EVENT_STRIDE - JOYSTICK_EVENT_SIZE));
return true;
} else
return false;
}
} }