From 029bc6117ed51ac6fe2fa6fe8eb9afd0899e7239 Mon Sep 17 00:00:00 2001 From: Brian Matzon Date: Wed, 4 Feb 2004 20:44:58 +0000 Subject: [PATCH] using getters new test --- src/java/org/lwjgl/test/input/MouseTest.java | 415 +++++++++++++++---- 1 file changed, 336 insertions(+), 79 deletions(-) diff --git a/src/java/org/lwjgl/test/input/MouseTest.java b/src/java/org/lwjgl/test/input/MouseTest.java index 2efe0577..bd405535 100644 --- a/src/java/org/lwjgl/test/input/MouseTest.java +++ b/src/java/org/lwjgl/test/input/MouseTest.java @@ -31,13 +31,13 @@ */ package org.lwjgl.test.input; -import org.lwjgl.DisplayMode; import org.lwjgl.input.Mouse; import org.lwjgl.input.Keyboard; import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.Window; -import org.lwjgl.opengl.GLU; +import org.lwjgl.opengl.glu.GLU; import org.lwjgl.vector.Vector2f; +import org.lwjgl.vector.Vector3f; /** * $Id$ @@ -48,35 +48,81 @@ import org.lwjgl.vector.Vector2f; * @version $Revision$ */ public class MouseTest { - - /** GLU instance */ - private GLU glu; - - /** position of quad to draw */ - private Vector2f position = new Vector2f(320.0f, 240.0f); + /** Direction mouse has moved */ + private int direction; - /** Display mode selected */ - private DisplayMode displayMode; + /** Last button pressed */ + private int lastButton = 0; + + /** Last direction we scrolled in */ + private int lastScrollDirection = -1; + + /** Width of window */ + private static int WINDOW_WIDTH = 640; + + /** Height of window */ + private static int WINDOW_HEIGHT = 640; + + /** Triangle size (in ½) */ + private Vector2f triangleSize = new Vector2f(120, 100); + + /** Triangle color */ + private Vector3f triangleColor[] = new Vector3f[] { + new Vector3f(1,1,1), + new Vector3f(1,0,0), + new Vector3f(0,1,0), + new Vector3f(0,0,1) + }; + + private Vector3f quadColor[] = new Vector3f[] { + new Vector3f(1,1,1), + new Vector3f(1,0,0), + new Vector3f(0,1,0), + new Vector3f(0,0,1) + }; + + /** Triangles to paint */ + private Vector2f[] triangles = { + new Vector2f(WINDOW_WIDTH/2, WINDOW_HEIGHT - triangleSize.y), + new Vector2f(triangleSize.y, WINDOW_HEIGHT/2), + new Vector2f(WINDOW_WIDTH/2, triangleSize.y), + new Vector2f(WINDOW_WIDTH-triangleSize.y, WINDOW_HEIGHT/2) + }; + /** Whether the test is closing */ + private boolean closing = false; + + /** Fullscreen or not */ + public static final boolean FULLSCREEN = false; + + /** Buffered mouse or not */ + public static final boolean BUFFERED_MOUSE = true; + + private int bufferSize; + /** Creates a new instance of MouseTest */ public MouseTest() { } private void initialize() { // create display and opengl - setupDisplay(false); + setupDisplay(); - try { - Keyboard.create(); - } catch (Exception e) { - e.printStackTrace(); - System.exit(-1); - } + createMouse(); + createKeyboard(); } - private void setupDisplay(boolean fullscreen) { + /** + * Setup display + */ + private void setupDisplay() { try { - Window.create("MouseTest", 50, 50, 640, 480, 16, 0, 0, 0); + if (FULLSCREEN) { + Window.create("MouseTest", 16, 0, 0, 0); + } else { + Window.create("MouseTest", 50, 50, WINDOW_WIDTH, WINDOW_HEIGHT, 16, 0, 0, 0); + } + Window.setVSyncEnabled(true); } catch (Exception e) { e.printStackTrace(); System.exit(-1); @@ -85,23 +131,31 @@ public class MouseTest { initializeOpenGL(); } + /** + * Initializes OpenGL + * + */ private void initializeOpenGL() { GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); - GLU.gluOrtho2D(0.0, 640, 0, 480); + GLU.gluOrtho2D(0.0f, WINDOW_WIDTH, 0, WINDOW_HEIGHT); } + /** + * Executes the actual test + */ public void executeTest() { initialize(); - createMouse(); - - wiggleMouse(); + runTest(); Mouse.destroy(); Keyboard.destroy(); Window.destroy(); } + /** + * Creates the mouse + */ private void createMouse() { try { Mouse.create(); @@ -109,73 +163,276 @@ public class MouseTest { e.printStackTrace(); System.exit(-1); } - } - private void wiggleMouse() { - while (!Window.isCloseRequested()) { - Window.update(); - - if(Window.isMinimized()) { - try { - Thread.sleep(100); - } catch (InterruptedException inte) { - inte.printStackTrace(); - } - continue; + // if compiled for buffered mode, enable that + if(BUFFERED_MOUSE) { + try { + Mouse.enableBuffer(); + } catch (Exception e) { + e.printStackTrace(); + System.exit(-1); } - - Mouse.poll(); - Keyboard.poll(); - - if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) { - return; - } - - position.x += Mouse.dx; - position.y += Mouse.dy; - - if(position.x<0) { - position.x = 0; - } else if (position.x>640-60) { - position.x = 640-60; - } - - if(position.y < 0) { - position.y = 0; - } else if (position.y>480-30) { - position.y = 480-30; - } - - - render(); - - Window.paint(); } } + /** + * Creates the keyboard + */ + private void createKeyboard() { + try { + Keyboard.create(); + } catch (Exception e) { + e.printStackTrace(); + System.exit(-1); + } + } + + /** + * Runs the test + */ + private void runTest() { + // while not exiting + while (!closing) { + handleWindow(); + + // secondary check + if(!closing) { + + // poll and check keyboard and mouse + handleKeyboard(); + handleMouse(); + + + // pause and continue if minimized + if(Window.isMinimized()) { + pause(100); + continue; + } + + // render and flip + logic(); + render(); + Window.paint(); + } + Thread.yield(); + } + } + + /** + * Pauses the current thread for a specified time + * + * @param time milliseconds to pause + */ + private void pause(long time) { + try { + Thread.sleep(time); + } catch (InterruptedException inte) { + inte.printStackTrace(); + } + } + + /** + * Handles the window + */ + private void handleWindow() { + Window.update(); + closing = Window.isCloseRequested(); + } + + /** + * handles the mouse + */ + private void handleMouse() { + if(BUFFERED_MOUSE) { + readBufferedMouse(); + } else { + readUnbufferedMouse(); + } + } + + /** + * reads a mouse in buffered mode + */ + private void readBufferedMouse() { + // poll for current values + Mouse.poll(); + + // read events + Mouse.read(); + + // iterate all events, use the last button down + while(Mouse.next()) { + if(Mouse.getEventButtonState()) { + lastButton = Mouse.getEventButton(); + } + } + + updateState(); + } + + /** + * Reads the mouse in unbuffered mode + */ + private void readUnbufferedMouse() { + // poll for current values + Mouse.poll(); + + // get last button down + for(int i=0;i 0) { + direction = 3; + } + + if(Mouse.getDX() < 0) { + direction = 1; + } + + if(Mouse.getDY() > 0) { + direction = 0; + } + + if(Mouse.getDY() < 0) { + direction = 2; + } + // ---------------------------- + + if(direction > -1) { + + // based on which button was last pushed, update model + switch(lastButton) { + case -1: + break; + case 1: + triangleColor[direction].y = 1; + break; + case 2: + triangleColor[direction].z = 1; + break; + case 3: + triangleColor[direction].x = 1; + triangleColor[direction].y = 1; + triangleColor[direction].z = 1; + break; + case 0: // fall through + default: + triangleColor[direction].x = 1; + break; + } + } + + // get direction to update in + if (Mouse.getDWheel() > 0) { + lastScrollDirection++; + } else if (Mouse.getDWheel() < 0) { + lastScrollDirection--; + } else if (Mouse.getDWheel() == 0) { + return; + } + + // over/underflow + if(lastScrollDirection < 0) { + lastScrollDirection = 3; + } + if(lastScrollDirection > 3) { + lastScrollDirection = 0; + } + + // update colors + quadColor[lastScrollDirection].x = (float) Math.random(); + quadColor[lastScrollDirection].y = (float) Math.random(); + quadColor[lastScrollDirection].z = (float) Math.random(); + } + + /** + * Handles the keyboard + */ + private void handleKeyboard() { + Keyboard.poll(); + + // closing on ESCAPE + if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) { + closing = true; + } + } + + /** + * Does the "model logic" + */ + private void logic() { + // "we fade to black" + // =========================================== + for(int i=0; i