new test, based on MouseTest
This commit is contained in:
parent
b7a151c48b
commit
c04fe10bac
|
@ -13,7 +13,7 @@
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
* documentation and/or other materials provided with the distribution.
|
* documentation and/or other materials provided with the distribution.
|
||||||
*
|
*
|
||||||
* * Neither the name of 'Light Weight Java Game Library' nor the names of
|
* * Neither the name of 'Lightweight Java Game Library' nor the names of
|
||||||
* its contributors may be used to endorse or promote products derived
|
* its contributors may be used to endorse or promote products derived
|
||||||
* from this software without specific prior written permission.
|
* from this software without specific prior written permission.
|
||||||
*
|
*
|
||||||
|
@ -31,149 +31,176 @@
|
||||||
*/
|
*/
|
||||||
package org.lwjgl.test.input;
|
package org.lwjgl.test.input;
|
||||||
|
|
||||||
import java.awt.*;
|
import org.lwjgl.DisplayMode;
|
||||||
import java.awt.event.*;
|
|
||||||
import org.lwjgl.input.Controller;
|
import org.lwjgl.input.Controller;
|
||||||
|
import org.lwjgl.input.Keyboard;
|
||||||
|
import org.lwjgl.opengl.GL;
|
||||||
|
import org.lwjgl.opengl.GLU;
|
||||||
|
import org.lwjgl.vector.Vector2f;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* $Id$
|
* $Id$
|
||||||
* <br>
|
* <br>
|
||||||
* Controller test, hwich shows a awt window, printing Controller state
|
* Controller test
|
||||||
*
|
*
|
||||||
* @author Brian Matzon <brian@matzon.dk>
|
* @author Brian Matzon <brian@matzon.dk>
|
||||||
* @version $Revision$
|
* @version $Revision$
|
||||||
*/
|
*/
|
||||||
public class ControllerTest extends Panel {
|
public class ControllerTest {
|
||||||
|
|
||||||
private int counter = 0;
|
/** OpenGL instance */
|
||||||
|
private GL gl;
|
||||||
|
|
||||||
public Thread animationThread;
|
/** GLU instance */
|
||||||
|
private GLU glu;
|
||||||
|
|
||||||
|
/** position of quad to draw */
|
||||||
|
private Vector2f position = new Vector2f(320.0f, 240.0f);
|
||||||
|
|
||||||
|
/** Display mode selected */
|
||||||
|
private DisplayMode displayMode;
|
||||||
|
|
||||||
/** Creates a new instance of ControllerTest */
|
/** Creates a new instance of ControllerTest */
|
||||||
public ControllerTest() {
|
public ControllerTest() {
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initialize() {
|
||||||
|
// create display and opengl
|
||||||
|
setupDisplay(false);
|
||||||
|
|
||||||
|
try {
|
||||||
|
Keyboard.create();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
System.exit(-1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setupDisplay(boolean fullscreen) {
|
||||||
|
try {
|
||||||
|
gl = new GL("ControllerTest", 50, 50, 640, 480, 16, 0, 0, 0);
|
||||||
|
gl.create();
|
||||||
|
|
||||||
|
glu = new GLU(gl);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
System.exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
initializeOpenGL();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initializeOpenGL() {
|
||||||
|
gl.clearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
||||||
|
glu.ortho2D(0.0, 640, 0, 480);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void executeTest() {
|
||||||
|
initialize();
|
||||||
|
|
||||||
|
createController();
|
||||||
|
|
||||||
|
wiggleController();
|
||||||
|
|
||||||
|
Controller.destroy();
|
||||||
|
Keyboard.destroy();
|
||||||
|
gl.destroy();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createController() {
|
||||||
try {
|
try {
|
||||||
Controller.create();
|
Controller.create();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return;
|
System.exit(-1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
animationThread = new Thread() {
|
private void wiggleController() {
|
||||||
public void run() {
|
while (!gl.isCloseRequested()) {
|
||||||
while (true) {
|
gl.tick();
|
||||||
paint(getGraphics());
|
|
||||||
|
|
||||||
|
if(gl.isMinimized()) {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(250);
|
Thread.sleep(100);
|
||||||
} catch (InterruptedException inte) {
|
} catch (InterruptedException inte) {
|
||||||
inte.printStackTrace();
|
inte.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
continue;
|
||||||
}
|
|
||||||
};
|
|
||||||
animationThread.setDaemon(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void paint(Graphics g) {
|
Controller.poll();
|
||||||
if (g == null) {
|
Keyboard.poll();
|
||||||
|
|
||||||
|
if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
g.setColor(Color.white);
|
if (Controller.x > 200) {
|
||||||
g.fillRect(0, 0, 640, 480);
|
position.x += 1;
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
if(Controller.hasXAxis) {
|
|
||||||
g.drawString("x : " + Controller.x, x, y);
|
|
||||||
y += 20;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(Controller.hasRXAxis) {
|
if (Controller.x < -200) {
|
||||||
g.drawString("rx : " + Controller.rx, x, y);
|
position.x -= 1;
|
||||||
y += 20;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(Controller.hasYAxis) {
|
if (Controller.y < -200) {
|
||||||
g.drawString("y : " + Controller.y, x, y);
|
position.y += 1;
|
||||||
y += 20;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(Controller.hasRYAxis) {
|
if (Controller.y > 200) {
|
||||||
g.drawString("ry : " + Controller.ry, x, y);
|
position.y -= 1;
|
||||||
y += 20;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Controller.hasZAxis) {
|
if(position.x<0) {
|
||||||
g.drawString("z : " + Controller.z, x, y);
|
position.x = 0;
|
||||||
y += 20;
|
} else if (position.x>640-60) {
|
||||||
|
position.x = 640-60;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Controller.hasRZAxis) {
|
if(position.y < 0) {
|
||||||
g.drawString("rz : " + Controller.rz, x, y);
|
position.y = 0;
|
||||||
y += 20;
|
} else if (position.y>480-30) {
|
||||||
|
position.y = 480-30;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Controller.hasPOV) {
|
|
||||||
g.drawString("pov: " + Controller.pov, x, y);
|
|
||||||
y += 20;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Controller.hasSlider) {
|
render();
|
||||||
g.drawString("slder: " + Controller.slider, x, y);
|
|
||||||
y += 20;
|
|
||||||
}
|
|
||||||
|
|
||||||
//paint buttons
|
gl.paint();
|
||||||
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) {
|
private void render() {
|
||||||
paint(g);
|
gl.clear(GL.COLOR_BUFFER_BIT);
|
||||||
}
|
|
||||||
|
|
||||||
public void disposing() {
|
gl.begin(GL.POLYGON);
|
||||||
Controller.destroy();
|
{
|
||||||
|
float color = 1.0f;
|
||||||
|
int buttonDown = 0;
|
||||||
|
|
||||||
|
for(int i=0;i<Controller.buttonCount; i++) {
|
||||||
|
if(Controller.isButtonDown(i)) {
|
||||||
|
color = (1.0f / Controller.buttonCount) * (i+1);
|
||||||
|
System.out.println("Button " + i + " down");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
gl.color3f(color, color, color);
|
||||||
|
|
||||||
|
gl.vertex2f(position.x + 0.0f, position.y + 0.0f);
|
||||||
|
gl.vertex2f(position.x + 0.0f, position.y + 30.0f);
|
||||||
|
gl.vertex2f(position.x + 40.0f, position.y + 30.0f);
|
||||||
|
gl.vertex2f(position.x + 60.0f, position.y + 15.f);
|
||||||
|
gl.vertex2f(position.x + 40.0f, position.y + 0.0f);
|
||||||
|
}
|
||||||
|
gl.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param args the command line arguments
|
* @param args the command line arguments
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
final ControllerTest p = new ControllerTest();
|
ControllerTest ct = new ControllerTest();
|
||||||
final Frame f = new Frame();
|
ct.executeTest();
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue