From: Brian M. <ma...@us...> - 2002-11-17 18:14:15
|
Update of /cvsroot/java-game-lib/LWJGL/src/java/org/lwjgl/input In directory usw-pr-cvs1:/tmp/cvs-serv7681 Modified Files: Joystick.java Log Message: first stab at the joystick implementation Index: Joystick.java CVS Browser: http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/java-game-lib/LWJGL/src/java/org/lwjgl/input/Joystick.java =================================================================== RCS file: /cvsroot/java-game-lib/LWJGL/src/java/org/lwjgl/input/Joystick.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -d -r1.7 -r1.8 --- Joystick.java 28 Aug 2002 21:58:14 -0000 1.7 +++ Joystick.java 17 Nov 2002 18:14:13 -0000 1.8 @@ -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 <ci...@us...> + * + * 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 <br...@ma...> * @version $Revision$ */ public class Joystick { @@ -59,38 +59,47 @@ } /** 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; - - /** 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; + /** 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; - /** 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() { } /** @@ -102,159 +111,71 @@ } /** - * 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. * * @param button The index of the button you wish to test (0..getNumButtons()) * @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() - */ - 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 + * Native method to poll the joystick */ - public static int enableBuffer() { - assert created : "The joystick has not been created."; - return nEnableBuffer(); - } - + private native void nPoll(); + /** - * 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 - */ - private static native int nRead(int readBufferAddress); - - /** - * Reads the joystick buffer. + * @return true if the joystick was created */ - 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 native boolean nCreate(); + + /** + * Native method the destroy the joystick */ - private static native int nEnableBuffer(); - + private native void nDestroy(); + /** - * 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 + * Register fields with the native library */ - 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(); +} \ No newline at end of file |