From a9506354ee25d2d4d85a6af18ff805262e56e6c3 Mon Sep 17 00:00:00 2001 From: Brian Matzon Date: Mon, 12 Apr 2004 14:46:18 +0000 Subject: [PATCH] fixed mouse issues, setGrabbed & native cursor now works (afaik ;)) --- src/java/org/lwjgl/input/Mouse.java | 25 +++++--- .../org/lwjgl/test/input/HWCursorTest.java | 58 +++++++++---------- src/java/org/lwjgl/test/input/MouseTest.java | 36 +++++++----- src/native/win32/org_lwjgl_input_Mouse.cpp | 38 ++++++------ 4 files changed, 83 insertions(+), 74 deletions(-) diff --git a/src/java/org/lwjgl/input/Mouse.java b/src/java/org/lwjgl/input/Mouse.java index 9028284d..39857b83 100644 --- a/src/java/org/lwjgl/input/Mouse.java +++ b/src/java/org/lwjgl/input/Mouse.java @@ -181,11 +181,10 @@ public class Mouse { Cursor oldCursor = currentCursor; currentCursor = cursor; if (currentCursor != null) { - nSetNativeCursor(currentCursor.getHandle()); - currentCursor.setTimeout(); - x = Window.getWidth() / 2; - y = Window.getHeight() / 2; - isGrabbed = false; + if(currentCursor != oldCursor) { + nSetNativeCursor(currentCursor.getHandle()); + currentCursor.setTimeout(); + } } else { nSetNativeCursor(0); } @@ -255,7 +254,9 @@ public class Mouse { created = true; currentCursor = null; dx = dy = dwheel = 0; - + x = Window.getWidth() / 2; + y = Window.getHeight() / 2; + // set mouse buttons buttonCount = nGetButtonCount(); buttons = BufferUtils.createByteBuffer(buttonCount); @@ -351,7 +352,7 @@ public class Mouse { dx += poll_dx; dy += poll_dy; dwheel += poll_dwheel; - + // if window has been created, clamp to edges if (Window.isCreated()) { // clamp x, y @@ -551,14 +552,20 @@ public class Mouse { * (and thus hidden). */ public static void setGrabbed(boolean grab) { - isGrabbed = grab; + isGrabbed = grab; nGrabMouse(isGrabbed); + + if(!grab) { + x = Window.getWidth() / 2; + y = Window.getHeight() / 2; + } } private static native void nGrabMouse(boolean grab); /** * Updates the cursor, so that animation can be changed if needed. - * This method is called automatically by the window on its update. + * This method is called automatically by the window on its update, and + * shouldn't be called otherwise */ public static void updateCursor() { if (Display.getPlatform() == Display.PLATFORM_WGL && currentCursor != null && currentCursor.hasTimedOut()) { diff --git a/src/java/org/lwjgl/test/input/HWCursorTest.java b/src/java/org/lwjgl/test/input/HWCursorTest.java index ea53fc9b..40863df1 100644 --- a/src/java/org/lwjgl/test/input/HWCursorTest.java +++ b/src/java/org/lwjgl/test/input/HWCursorTest.java @@ -89,8 +89,6 @@ public class HWCursorTest { glInit(); - Keyboard.create(); - Mouse.create(); initNativeCursors(); } catch (Exception e) { @@ -106,10 +104,6 @@ public class HWCursorTest { cursor = new Cursor[3]; - // center - mouse_x = 400; - mouse_y = 300; - int cursorImageCount = 1; int cursorWidth = Mouse.getMaxCursorSize(); int cursorHeight = cursorWidth; @@ -214,6 +208,7 @@ public class HWCursorTest { if (Window.isVisible()) { // check keyboard input processKeyboard(); + processMouse(); render(); } else { @@ -255,29 +250,33 @@ public class HWCursorTest { } GL11.glPopMatrix(); } + + private void processMouse() { + int dx = Mouse.getDX(); + int dy = Mouse.getDY(); + + if (dx != 0 || dy != 0) { + //mouse_x += dx; + //mouse_y += dy; + } + mouse_x = Mouse.getX(); + mouse_y = Mouse.getY(); + + while(Mouse.next()) { + if(Mouse.getEventButtonState() && Mouse.getEventButton() < 3) { + mouse_btn = Mouse.getEventButton(); + } + } + } /** * Processes keyboard input */ private void processKeyboard() { - if (Mouse.getDX() != 0 || Mouse.getDY() != 0) { - mouse_x += Mouse.getDX() / 2; - mouse_y += Mouse.getDY() / 2; - } - - if(Mouse.isButtonDown(0)) { - mouse_btn = 0; - } else if(Mouse.isButtonDown(1)) { - mouse_btn = 1; - } else if(Mouse.isButtonDown(2)) { - mouse_btn = 2; - } - //check for fullscreen key if (Keyboard.isKeyDown(Keyboard.KEY_F)) { try { - Keyboard.destroy(); try { Mouse.setNativeCursor(null); } catch (Exception e) { @@ -287,7 +286,6 @@ public class HWCursorTest { for(int i=0; i 0) { + if(dx > 0) { direction = 3; } - if(Mouse.getDX() < 0) { + if(dx < 0) { direction = 1; } - if(Mouse.getDY() > 0) { + if(dy > 0) { direction = 0; } - if(Mouse.getDY() < 0) { + if(dy < 0) { direction = 2; } @@ -287,11 +292,11 @@ public class MouseTest { } // get direction to update in - if (Mouse.getDWheel() > 0) { + if (dw > 0) { lastScrollDirection++; - } else if (Mouse.getDWheel() < 0) { + } else if (dw < 0) { lastScrollDirection--; - } else if (Mouse.getDWheel() == 0) { + } else if (dw == 0) { return; } @@ -313,13 +318,16 @@ public class MouseTest { * Handles the keyboard */ private void handleKeyboard() { - // closing on ESCAPE - if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) { - closing = true; - } - if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)) { - Mouse.setGrabbed(!Mouse.isGrabbed()); + while(Keyboard.next()) { + // closing on ESCAPE + if(Keyboard.getEventKey() == Keyboard.KEY_ESCAPE && Keyboard.getEventKeyState()) { + closing = true; + } + + if(Keyboard.getEventKey() == Keyboard.KEY_SPACE && Keyboard.getEventKeyState()) { + Mouse.setGrabbed(!Mouse.isGrabbed()); + } } } diff --git a/src/native/win32/org_lwjgl_input_Mouse.cpp b/src/native/win32/org_lwjgl_input_Mouse.cpp index f59a8877..7f34794f 100644 --- a/src/native/win32/org_lwjgl_input_Mouse.cpp +++ b/src/native/win32/org_lwjgl_input_Mouse.cpp @@ -227,32 +227,13 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nSetNativeCursor SetClassLong(hwnd, GCL_HCURSOR, (LONG)cursor); SetCursor(cursor); if (!usingNativeCursor) { - mDIDevice->Unacquire(); - if(mDIDevice->SetCooperativeLevel(hwnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND) != DI_OK) { - throwException(env, "Could not set the CooperativeLevel."); - return; - } - /* Reset cursor position to middle of the window */ - RECT clientRect; - GetWindowRect(hwnd, &windowRect); - getScreenClientRect(&clientRect, &windowRect); - cursorPos.x = (clientRect.left + clientRect.right)/2; - cursorPos.y = clientRect.bottom - 1 - (clientRect.bottom - clientRect.top)/2; - SetCursorPos(cursorPos.x, cursorPos.y); usingNativeCursor = true; } } else { if (usingNativeCursor) { SetClassLong(hwnd, GCL_HCURSOR, (LONG)NULL); SetCursor(LoadCursor(NULL, IDC_ARROW)); - mDIDevice->Unacquire(); - if(mDIDevice->SetCooperativeLevel(hwnd, mouseMask) != DI_OK) { - throwException(env, "Could not set the CooperativeLevel."); - return; - } usingNativeCursor = false; - mDIDevice->Acquire(); - ShowCursor(TRUE); } } } @@ -308,8 +289,18 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nGrabMouse if(grab) { mouseMask = DISCL_EXCLUSIVE | DISCL_FOREGROUND; + ShowCursor(false); } else { mouseMask = DISCL_NONEXCLUSIVE | DISCL_FOREGROUND; + ShowCursor(true); + + /* Reset cursor position to middle of the window */ + RECT clientRect; + GetWindowRect(hwnd, &windowRect); + getScreenClientRect(&clientRect, &windowRect); + cursorPos.x = (clientRect.left + clientRect.right)/2; + cursorPos.y = clientRect.bottom - 1 - (clientRect.bottom - clientRect.top)/2; + SetCursorPos(cursorPos.x, cursorPos.y); } mDIDevice->Unacquire(); if(mDIDevice->SetCooperativeLevel(hwnd, mouseMask) != DI_OK) { @@ -408,6 +399,14 @@ void SetupMouse() { return; } mCreate_success = true; + + /* Reset cursor position to middle of the window */ + RECT clientRect; + GetWindowRect(hwnd, &windowRect); + getScreenClientRect(&clientRect, &windowRect); + cursorPos.x = (clientRect.left + clientRect.right)/2; + cursorPos.y = clientRect.bottom - 1 - (clientRect.bottom - clientRect.top)/2; + SetCursorPos(cursorPos.x, cursorPos.y); } static int cap(int val, int min, int max) { @@ -487,6 +486,7 @@ static void UpdateMouseFields(JNIEnv *env, jclass clsMouse, jobject coord_buffer coords[0] = dx; coords[1] = dy; coords[2] = diMouseState.lZ; + for (int i = 0; i < mButtoncount; i++) { if (diMouseState.rgbButtons[i] != 0) { diMouseState.rgbButtons[i] = JNI_TRUE;