From 61ddd625dfa4b7299dbc3e79e61ed54f1f49ea68 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Tue, 12 Apr 2005 11:45:06 +0000 Subject: [PATCH] Added Mouse.setCursorPosition(x, y) --- src/java/org/lwjgl/input/Mouse.java | 20 +++++++++++++++---- .../lwjgl/opengl/DisplayImplementation.java | 3 +++ src/java/org/lwjgl/opengl/LinuxDisplay.java | 7 +++++++ src/java/org/lwjgl/opengl/MacOSXDisplay.java | 4 ++++ src/java/org/lwjgl/opengl/Win32Display.java | 1 + src/native/linux/org_lwjgl_input_Mouse.c | 11 ++++++++++ 6 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/java/org/lwjgl/input/Mouse.java b/src/java/org/lwjgl/input/Mouse.java index dce99440..0927f48f 100644 --- a/src/java/org/lwjgl/input/Mouse.java +++ b/src/java/org/lwjgl/input/Mouse.java @@ -152,10 +152,6 @@ public class Mouse { * * NOTE: The native cursor is not constrained to the window, but * relative events will not be generated if the cursor is outside. - * The initial position of the cursor is the middle of the window, - * that is, {window_width/2, window_height/2}. - * The cursor will be moved to this origin when a - * native cursor is set and the previous cursor is null. * * @param cursor the native cursor object to bind. May be null. * @return The previous Cursor object set, or null. @@ -177,6 +173,21 @@ public class Mouse { return oldCursor; } + /** + * Set the position of the native cursor. This method is only valid when + * the cursor is not grabbed and native cursors are supported. + * + * @param x The x coordinate of the new cursor position in OpenGL coordinates relative + * to the window origin. + * @param y The y coordinate of the new cursor position in OpenGL coordinates relative + * to the window origin. + */ + public static void setCursorPosition(int x, int y) { + if ((Cursor.getCapabilities() & Cursor.CURSOR_ONE_BIT_TRANSPARENCY) == 0) + throw new IllegalStateException("Mouse doesn't support native cursors"); + Display.getImplementation().setCursorPosition(x, y); + } + /** * Static initialization */ @@ -200,6 +211,7 @@ public class Mouse { x = width / 2; y = height / 2; readBuffer.clear(); + setCursorPosition(x, y); } /** diff --git a/src/java/org/lwjgl/opengl/DisplayImplementation.java b/src/java/org/lwjgl/opengl/DisplayImplementation.java index 369154cc..f4d5e083 100644 --- a/src/java/org/lwjgl/opengl/DisplayImplementation.java +++ b/src/java/org/lwjgl/opengl/DisplayImplementation.java @@ -162,6 +162,9 @@ public interface DisplayImplementation { */ int getNativeCursorCapabilities(); + /** Method to set the native cursor position */ + void setCursorPosition(int x, int y); + /** Method to set the native cursor */ void setNativeCursor(Object handle) throws LWJGLException; diff --git a/src/java/org/lwjgl/opengl/LinuxDisplay.java b/src/java/org/lwjgl/opengl/LinuxDisplay.java index 93a3170a..c21cf497 100644 --- a/src/java/org/lwjgl/opengl/LinuxDisplay.java +++ b/src/java/org/lwjgl/opengl/LinuxDisplay.java @@ -268,6 +268,13 @@ final class LinuxDisplay implements DisplayImplementation { } private static native int nReadMouse(IntBuffer buffer, int buffer_position); + public void setCursorPosition(int x, int y) { + lockAWT(); + nSetCursorPosition(x, y); + unlockAWT(); + } + private native void nSetCursorPosition(int x, int y); + public void grabMouse(boolean grab) { lockAWT(); nGrabMouse(grab); diff --git a/src/java/org/lwjgl/opengl/MacOSXDisplay.java b/src/java/org/lwjgl/opengl/MacOSXDisplay.java index 7e4bfaa1..570858f6 100644 --- a/src/java/org/lwjgl/opengl/MacOSXDisplay.java +++ b/src/java/org/lwjgl/opengl/MacOSXDisplay.java @@ -306,6 +306,10 @@ final class MacOSXDisplay implements DisplayImplementation { return 0; } + public void setCursorPosition(int x, int y) { + throw new UnsupportedOperationException(); + } + public void setNativeCursor(Object handle) throws LWJGLException { Cursor awt_cursor = (Cursor)handle; frame.setCursor(awt_cursor); diff --git a/src/java/org/lwjgl/opengl/Win32Display.java b/src/java/org/lwjgl/opengl/Win32Display.java index dd389502..e8f56b60 100644 --- a/src/java/org/lwjgl/opengl/Win32Display.java +++ b/src/java/org/lwjgl/opengl/Win32Display.java @@ -106,6 +106,7 @@ final class Win32Display implements DisplayImplementation { return Cursor.CURSOR_ONE_BIT_TRANSPARENCY; } + public native void setCursorPosition(int x, int y); public native void setNativeCursor(Object handle) throws LWJGLException; public native int getMinCursorSize(); public native int getMaxCursorSize(); diff --git a/src/native/linux/org_lwjgl_input_Mouse.c b/src/native/linux/org_lwjgl_input_Mouse.c index 4cda1aa0..d6a722ff 100644 --- a/src/native/linux/org_lwjgl_input_Mouse.c +++ b/src/native/linux/org_lwjgl_input_Mouse.c @@ -342,6 +342,17 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nReadMouse(JNIEnv *env return copyEvents(&event_queue, buffer_ptr + buffer_position, buffer_size); } +JNIEXPORT void JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nSetCursorPosition(JNIEnv * env, jclass clazz, jint x, jint y) { + XWindowAttributes attributes; + if (!XGetWindowAttributes(getDisplay(), getCurrentWindow(), &attributes)) { + printfDebugJava(env, "XGetWindowAttributes failed"); + return; + } + int transformed_x = attributes.x + x; + int transformed_y = attributes.y + transformY(y); + XWarpPointer(getDisplay(), None, getCurrentWindow(), 0, 0, 0, 0, transformed_x, transformed_y); +} + JNIEXPORT void JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nGrabMouse(JNIEnv * env, jclass clazz, jboolean new_grab) { Window root_return, child_return; int root_x, root_y, win_x, win_y;