Added Mouse.setCursorPosition(x, y)

This commit is contained in:
Elias Naur 2005-04-12 11:45:06 +00:00
parent f3d3ce7ce5
commit 61ddd625df
6 changed files with 42 additions and 4 deletions

View File

@ -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);
}
/**

View File

@ -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;

View File

@ -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);

View File

@ -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);

View File

@ -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();

View File

@ -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;