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;
import java.nio.ByteBuffer;
import org.lwjgl.Display;
import org.lwjgl.Sys;
/**
* $Id$
*
* <br>
* 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
* 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.
*
* Up to 8 buttons are available. A scrolly wheel or paddle, if present, is the z
* value. This will be in the range of 0.0f to 1.0f.
*
* @author cix_foo <cix_foo@users.sourceforge.net>
*
* Currently n (native limits, currently 128) buttons, the x, y, z axis is supported along with a POV (or HAT), where the z axis
* represents a throttle. In the future the joystick may support more buttons and
* axises and other features. but this is a platform issue.
*
* The joystick implementation currently only supports the first attached joystick.
*
* @author Brian Matzon <brian@matzon.dk>
* @version $Revision$
*/
public class Joystick {
@ -59,38 +59,47 @@ public class Joystick {
}
/** Has the joystick been created? */
private static boolean created;
private boolean created;
/** The joystick buttons status from the last poll */
private static final boolean[] button = new boolean[8];
/** The joystick buttons status */
private boolean[] buttons;
/** X position, range -1.0f to 1.0f */
public static float x;
/** X position, range -1000 to 1000 */
public int x = -1;
/** Y position, range -1.0f to 1.0f */
public static float y;
/** Y position, range -1000 to 1000 */
public int y = -1;
/** Z position, range 0.0f to 1.0f */
public static float z;
/** Z position, range -1000 to 1000 */
public int z = -1;
/** Position of Point of View from -1 to 27000 (360 degrees) */
public int pov;
/** Constant specifying centered POV */
public static final int POV_CENTER = -1;
/**
* The joystick events from the last read: a sequence of Events
*/
private static ByteBuffer readBuffer;
/** Constant specifying nortward POV */
public static final int POV_NORTH = 0;
/** Constant specifying southward POV */
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;
/** Address of the read buffer */
private static int readBufferAddress;
/** The size in bytes of a single joystick event */
private static final int JOYSTICK_EVENT_SIZE = 20;
/** The stride in bytes of a single joystick event */
private static final int JOYSTICK_EVENT_STRIDE = 32;
/**
* Joystick cannot be constructed.
*/
private Joystick() {
public Joystick() {
}
/**
@ -101,83 +110,41 @@ public class Joystick {
initIDs();
}
/**
* Register fields with the native library
*/
private static native void initIDs();
/**
* "Create" the joystick. The display must first have been created.
* @throws Exception if the joystick could not be created for any reason
*/
public static void create() throws Exception {
if (created)
public void create() throws Exception {
if (created) {
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.");
}
created = true;
}
/**
* Native method to create the joystick
*
* @return true if the joystick was created
*/
private static native boolean nCreate();
/**
* "Destroy" the joystick
*/
public static void destroy() {
if (!created)
public void destroy() {
if (!created) {
return;
}
created = false;
nDestroy();
}
/**
* Native method the destroy the joystick
*/
private static native void nDestroy();
/**
* Polls the joystick.
*/
public static void poll() {
public void poll() {
assert created : "The joystick has not been created.";
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.
*
@ -185,76 +152,30 @@ public class Joystick {
* @return true if the specified button is down
* @see #getNumButtons()
*/
public static boolean isButtonDown(int button) {
public boolean isButtonDown(int button) {
assert created : "The joystick has not been created.";
return Joystick.button[button];
return buttons[button];
}
/**
* Native implementation of hasZValue()
* Native method to poll the joystick
*/
private static native boolean nHasZValue();
private native void nPoll();
/**
* 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 create the joystick
*
* @param readBufferAddress the address of the joystick buffer
* @return the number of joystick events read
* @return true if the joystick was created
*/
private static native int nRead(int readBufferAddress);
private native boolean nCreate();
/**
* Native method the destroy the joystick
*/
private native void nDestroy();
/**
* Reads the joystick buffer.
* Register fields with the native library
*/
public static void read() {
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
* @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();
/**
* 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;
}
}
private static native void initIDs();
}