Joystick replaced by Controller

GamePad no modelled as a controller
This commit is contained in:
Brian Matzon 2003-01-03 18:47:42 +00:00
parent 5bd5caa365
commit 988f115218
9 changed files with 310 additions and 783 deletions

View file

@ -37,32 +37,32 @@ 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 ints in the range -1000 to 1000.
* A raw Controller interface. This can be used to poll the current state of a
* controllers buttons, and axis positions. The axis positions
* are returned as ints in the range -1000 to 1000.
*
* No buffering is available.
*
* Currently n (native limits, currently 128 - might change) buttons, the x, y,
* z axis is supported along with a POV (or HAT) and a slider, where the z axis
* represents a throttle. In the future the joystick may support more buttons
* represents a throttle. In the future the controller 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.
* The Controller implementation currently only supports the first attached device.
*
* @author Brian Matzon <brian@matzon.dk>
* @version $Revision$
*/
public class Joystick {
public class Controller {
static {
initialize();
}
/** Has the joystick been created? */
/** Has the controller been created? */
private static boolean created;
/** The joystick buttons status */
/** The controller buttons status */
private static boolean[] buttons;
/** X position, range -1000 to 1000 */
@ -95,7 +95,7 @@ public class Joystick {
/** Constant specifying westward POV */
public static final int POV_WEST = 9000;
/* Joystick capabilities */
/* Controller capabilities */
public static int buttonCount = -1;
public static boolean hasXAxis = false;
public static boolean hasYAxis = false;
@ -104,9 +104,9 @@ public class Joystick {
public static boolean hasSlider = false;
/**
* Joystick cannot be constructed.
* Controller cannot be constructed.
*/
private Joystick() {
private Controller() {
}
/**
@ -118,8 +118,8 @@ public class Joystick {
}
/**
* "Create" the joystick. The display must first have been created.
* @throws Exception if the joystick could not be created for any reason
* "Create" the controller. The display must first have been created.
* @throws Exception if the controller could not be created for any reason
*/
public static void create() throws Exception {
if (created) {
@ -127,13 +127,13 @@ public class Joystick {
}
if (!nCreate()) {
throw new Exception("The joystick could not be created.");
throw new Exception("The controller could not be created.");
}
created = true;
}
/**
* "Destroy" the joystick
* "Destroy" the controller
*/
public static void destroy() {
if (!created) {
@ -145,39 +145,39 @@ public class Joystick {
}
/**
* Polls the joystick.
* Polls the controller.
*/
public static void poll() {
assert created : "The joystick has not been created.";
assert created : "The controller has not been created.";
nPoll();
}
/**
* See if a particular mouse button is down.
* See if a particular button is down.
*
* @param button The index of the button you wish to test (0..buttonCount)
* @return true if the specified button is down
* @see #buttonCount
*/
public static boolean isButtonDown(int button) {
assert created : "The joystick has not been created.";
assert created : "The controller has not been created.";
return buttons[button];
}
/**
* Native method to poll the joystick
* Native method to poll the controller
*/
private static native void nPoll();
/**
* Native method to create the joystick
* Native method to create the controller
*
* @return true if the joystick was created
* @return true if the controller was created
*/
private static native boolean nCreate();
/**
* Native method the destroy the joystick
* Native method the destroy the controller
*/
private static native void nDestroy();

View file

@ -1,256 +0,0 @@
/*
* Copyright (c) 2002 Lightweight Java Game Library Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'Light Weight Java Game Library' nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.lwjgl.input;
import java.nio.ByteBuffer;
import org.lwjgl.Display;
import org.lwjgl.Sys;
/**
* $Id$
*
* A raw GamePad interface. This can be used to poll the current state of the
* buttons, or read all the gamepad presses / releases since the last read.
* Buffering must be explicitly enabled; the size of the buffer is determined
* by the native implementation at its discretion.
*
* @author cix_foo <cix_foo@users.sourceforge.net>
* @version $Revision$
*/
public class GamePad {
// Button codes
public static int PAD_UP = 1;
public static int PAD_DOWN = 2;
public static int PAD_LEFT = 3;
public static int PAD_RIGHT = 4;
public static int PAD_BUTTON0 = 5;
public static int PAD_BUTTON1 = 6;
public static int PAD_BUTTON2 = 7;
public static int PAD_BUTTON3 = 8;
public static int PAD_BUTTON4 = 9;
public static int PAD_BUTTON5 = 10;
public static int PAD_BUTTON6 = 11;
public static int PAD_BUTTON7 = 12;
public static int PAD_BUTTON8 = 13;
static {
initialize();
}
/** Has the gamepad been created? */
private static boolean created;
/** The buttons status from the last poll */
private static final ByteBuffer buttonDownBuffer = ByteBuffer.allocateDirect(256);
/** Address of the buttonDown buffer */
private static final int buttonDownAddress = Sys.getDirectBufferAddress(buttonDownBuffer);
/**
* The button events from the last read: a sequence of pairs of button number,
* followed by state.
*/
private static ByteBuffer readBuffer;
/** Address of the read buffer */
private static int readBufferAddress;
/** The current gamepad event button being examined */
public static int button;
/** The current state of the button being examined in the event queue */
public static boolean state;
/**
* GamePad cannot be constructed.
*/
private GamePad() {
}
/**
* Static initialization
*/
private static void initialize() {
System.loadLibrary(Sys.getLibraryName());
initIDs();
}
/**
* Register fields with the native library
*/
private static native void initIDs();
/**
* "Create" the gamepad. The display must first have been created. The
* reason for this is so the gamepad has a window to "focus" in.
*
* @throws Exception if the gamepad could not be created for any reason
*/
public static void create() throws Exception {
if (created)
return;
if (!Display.isCreated())
throw new Exception("The display has not yet been created.");
if (!nCreate())
throw new Exception("The gamepad could not be created.");
created = true;
}
/**
* Native method to create the gamepad
*
* @return true if the gamepad was created
*/
private static native boolean nCreate();
/**
* "Destroy" the gamepad
*/
public static void destroy() {
if (!created)
return;
created = false;
nDestroy();
}
/**
* Native method the destroy the gamepad
*/
private static native void nDestroy();
/**
* Polls the gamepad.
*/
public static void poll() {
assert created : "The gamepad has not been created.";
nPoll(buttonDownAddress);
}
/**
* Native method to poll the gamepad.
*
* @param keyDownBufferAddress the address of a 256-byte buffer to place
* key states in.
*/
private static native void nPoll(int keyDownBufferAddress);
/**
* Reads the gamepad buffer. Call next() to read the events one by one.
* @see #next()
*/
public static void read() {
assert created : "The gamepad has not been created.";
assert readBuffer != null : "GamePad buffering has not been enabled.";
readBuffer.clear();
readBuffer.limit(nRead(readBufferAddress) << 1);
}
/**
* Native method to read the gamepad buffer
*
* @param readBufferAddress the address of the gamepad buffer
* @return the number of gamepad events read
*/
private static native int nRead(int readBufferAddress);
/**
* Enable gamepad buffering. Must be called after the gamepad is created.
* @return the size of the gamepad buffer in events, or 0 if no buffering
* can be enabled for any reason
*/
public static int enableBuffer() {
assert created : "The gamepad has not been created.";
return nEnableBuffer();
}
/**
* 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();
/**
* Checks to see if a button is down.
* @param button The button code to check
* @return true if the button is down according to the last poll()
*/
public static boolean isButtonDown(int button) {
assert created : "The gamepad has not been created.";
return buttonDownBuffer.get(button) != 0;
}
/**
* Gets the number of gamepad events waiting after doing a read().
* @return the number of gamepad events
*/
public static int getNumGamePadEvents() {
return readBuffer.limit() >> 1;
}
/**
* Gets the next gamepad event. This is stored in the publicly accessible
* static fields button and state.
* @return true if a gamepad event was read, false otherwise
*/
public static boolean next() {
assert created : "The gamepad has not been created.";
assert readBuffer != null : "GamePad buffering has not been enabled.";
if (readBuffer.hasRemaining()) {
button = readBuffer.get();
state = readBuffer.get() != 0;
return true;
} else
return false;
}
/**
* Queries the number of buttons the gamepad has (excluding up, down, left, right)
* @return the number of buttons the gamepad has
*/
public static int getNumButtons() {
assert created : "The gamepad has not been created.";
return nGetNumButtons();
}
/**
* Native implementation of getNumButtons()
*/
private static native int nGetNumButtons();
}

View file

@ -0,0 +1,151 @@
/*
* Copyright (c) 2002 Lightweight Java Game Library Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'Light Weight Java Game Library' nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.lwjgl.test.input;
import java.awt.*;
import java.awt.event.*;
import org.lwjgl.input.Controller;
/**
* $Id$
* <br>
* Controller test, hwich shows a awt window, printing Controller state
*
* @author Brian Matzon <brian@matzon.dk>
* @version $Revision$
*/
public class ControllerTest extends Panel {
private int counter = 0;
public Thread animationThread;
/** Creates a new instance of ControllerTest */
public ControllerTest() {
try {
Controller.create();
} catch (Exception e) {
e.printStackTrace();
return;
}
animationThread = new Thread() {
public void run() {
while (true) {
paint(getGraphics());
try {
Thread.sleep(250);
} catch (InterruptedException inte) {
inte.printStackTrace();
}
}
}
};
animationThread.setDaemon(true);
}
public void paint(Graphics g) {
if (g == null) {
return;
}
g.setColor(Color.white);
g.fillRect(0, 0, 640, 480);
int y = 100;
int x = 100;
Controller.poll();
g.setColor(Color.blue);
g.drawString("Buttoncount: " + Controller.buttonCount, x, y);
y += 20;
g.drawString("-----------------------------------------------", x, y);
y += 20;
g.drawString("x : " + Controller.x, x, y);
y += 20;
g.drawString("y : " + Controller.y, x, y);
y += 20;
if (Controller.hasZAxis) {
g.drawString("z : " + Controller.z, x, y);
y += 20;
}
if (Controller.hasPOV) {
g.drawString("pov: " + Controller.pov, x, y);
y += 20;
}
//paint buttons
g.drawString("btn: ", x, y);
x += g.getFontMetrics().stringWidth("btn: ");
for (int i = 0; i < Controller.buttonCount; i++) {
if (Controller.isButtonDown(i)) {
g.drawString(i + ", ", x, y);
x += 15;
}
}
}
public void update(Graphics g) {
paint(g);
}
public void disposing() {
Controller.destroy();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
final ControllerTest p = new ControllerTest();
final Frame f = new Frame();
f.setLayout(null);
f.setSize(640, 480);
f.setLocation(100, 100);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
f.hide();
p.disposing();
f.dispose();
}
});
p.setSize(640, 480);
p.setLocation(0, 0);
p.setBackground(Color.RED);
f.add(p);
f.show();
p.animationThread.start();
}
}

View file

@ -1,151 +0,0 @@
/*
* Copyright (c) 2002 Lightweight Java Game Library Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'Light Weight Java Game Library' nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.lwjgl.test.input;
import java.awt.*;
import java.awt.event.*;
import org.lwjgl.input.Joystick;
/**
* $Id$
* <br>
* Joystick test, hwich shows a awt window, printing joystick state
*
* @author Brian Matzon <brian@matzon.dk>
* @version $Revision$
*/
public class JoystickTest extends Panel {
private int counter = 0;
public Thread animationThread;
/** Creates a new instance of JoystickTest */
public JoystickTest() {
try {
Joystick.create();
} catch (Exception e) {
e.printStackTrace();
return;
}
animationThread = new Thread() {
public void run() {
while (true) {
paint(getGraphics());
try {
Thread.sleep(250);
} catch (InterruptedException inte) {
inte.printStackTrace();
}
}
}
};
animationThread.setDaemon(true);
}
public void paint(Graphics g) {
if (g == null) {
return;
}
g.setColor(Color.white);
g.fillRect(0, 0, 640, 480);
int y = 100;
int x = 100;
Joystick.poll();
g.setColor(Color.blue);
g.drawString("Buttoncount: " + Joystick.buttonCount, x, y);
y += 20;
g.drawString("-----------------------------------------------", x, y);
y += 20;
g.drawString("x : " + Joystick.x, x, y);
y += 20;
g.drawString("y : " + Joystick.y, x, y);
y += 20;
if(Joystick.hasZAxis) {
g.drawString("z : " + Joystick.z, x, y);
y += 20;
}
if(Joystick.hasPOV) {
g.drawString("pov: " + Joystick.pov, x, y);
y += 20;
}
//paint buttons
g.drawString("btn: ", x, y);
x += g.getFontMetrics().stringWidth("btn: ");
for(int i=0; i<Joystick.buttonCount; i++) {
if(Joystick.isButtonDown(i)) {
g.drawString(i + ", ", x, y);
x+= 15;
}
}
}
public void update(Graphics g) {
paint(g);
}
public void disposing() {
Joystick.destroy();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
final JoystickTest p = new JoystickTest();
final Frame f = new Frame();
f.setLayout(null);
f.setSize(640, 480);
f.setLocation(100, 100);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
f.hide();
p.disposing();
f.dispose();
}
});
p.setSize(640, 480);
p.setLocation(0, 0);
p.setBackground(Color.RED);
f.add(p);
f.show();
p.animationThread.start();
}
}