Flipped cursor coordinates to match OpenGL

This commit is contained in:
Elias Naur 2003-08-11 16:35:45 +00:00
parent 0b55322fc2
commit c8f38ac9b7
2 changed files with 41 additions and 17 deletions

View File

@ -79,6 +79,21 @@ static unsigned char buttons[NUM_BUTTONS];
static Cursor blank_cursor; static Cursor blank_cursor;
static Cursor current_cursor; static Cursor current_cursor;
static int cap(int val, int min, int max) {
if (val < min)
return min;
else if (val > max)
return max;
else
return val;
}
static void setCursorPos(int x, int y) {
y = getWindowHeight() - 1 - y;
current_x = cap(x, 0, getWindowWidth() - 1);
current_y = cap(y, 0, getWindowHeight() - 1);
}
/* /*
* Class: org_lwjgl_input_Mouse * Class: org_lwjgl_input_Mouse
* Method: initIDs * Method: initIDs
@ -200,7 +215,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nSetNativeCursor
if (cursor_handle != 0) { if (cursor_handle != 0) {
Cursor cursor = (Cursor)cursor_handle; Cursor cursor = (Cursor)cursor_handle;
if (!native_cursor) { if (!native_cursor) {
current_x = current_y = 0; setCursorPos(0, 0);
XWarpPointer(getCurrentDisplay(), None, getCurrentWindow(), 0, 0, 0, 0, current_x, current_y); XWarpPointer(getCurrentDisplay(), None, getCurrentWindow(), 0, 0, 0, 0, current_x, current_y);
native_cursor = true; native_cursor = true;
} }
@ -256,7 +271,8 @@ JNIEXPORT jboolean JNICALL Java_org_lwjgl_input_Mouse_nCreate
int i; int i;
env->SetStaticIntField(clazz, fid_button_count, NUM_BUTTONS); env->SetStaticIntField(clazz, fid_button_count, NUM_BUTTONS);
env->SetStaticBooleanField(clazz, fid_has_wheel, JNI_TRUE); env->SetStaticBooleanField(clazz, fid_has_wheel, JNI_TRUE);
current_x = current_y = current_z = last_x = last_y = last_z; setCursorPos(0, 0);
current_z = last_x = last_y = last_z = 0;
for (i = 0; i < NUM_BUTTONS; i++) for (i = 0; i < NUM_BUTTONS; i++)
buttons[i] = JNI_FALSE; buttons[i] = JNI_FALSE;
if (!blankCursor()) { if (!blankCursor()) {
@ -334,10 +350,8 @@ void handleButtonRelease(XButtonEvent *event) {
} }
void handlePointerMotion(XMotionEvent *event) { void handlePointerMotion(XMotionEvent *event) {
if (pointer_grabbed || native_cursor) { if (pointer_grabbed || native_cursor)
current_x = event->x; setCursorPos(event->x, event->y);
current_y = event->y;
}
} }
static void warpPointer(void) { static void warpPointer(void) {
@ -347,8 +361,9 @@ static void warpPointer(void) {
// Reset pointer to middle of screen if inside a certain inner border // Reset pointer to middle of screen if inside a certain inner border
if (current_x < POINTER_WARP_BORDER || current_y < POINTER_WARP_BORDER || if (current_x < POINTER_WARP_BORDER || current_y < POINTER_WARP_BORDER ||
current_x > getWindowWidth() - POINTER_WARP_BORDER || current_y > getWindowHeight() - POINTER_WARP_BORDER) { current_x > getWindowWidth() - POINTER_WARP_BORDER || current_y > getWindowHeight() - POINTER_WARP_BORDER) {
current_x = last_x = getWindowWidth()/2; setCursorPos(getWindowWidth()>>1, getWindowHeight()>>1);
current_y = last_y = getWindowHeight()/2; last_x = current_x;
last_y = current_y;
XWarpPointer(getCurrentDisplay(), None, getCurrentWindow(), 0, 0, 0, 0, current_x, current_y); XWarpPointer(getCurrentDisplay(), None, getCurrentWindow(), 0, 0, 0, 0, current_x, current_y);
XEvent event; XEvent event;
// Try to catch the warp pointer event // Try to catch the warp pointer event

View File

@ -185,9 +185,9 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nSetNativeCursor
RECT clientRect; RECT clientRect;
GetWindowRect(hwnd, &windowRect); GetWindowRect(hwnd, &windowRect);
getScreenClientRect(&clientRect, &windowRect); getScreenClientRect(&clientRect, &windowRect);
SetCursorPos(clientRect.left, clientRect.top);
cursorPos.x = clientRect.left; cursorPos.x = clientRect.left;
cursorPos.y = clientRect.top; cursorPos.y = clientRect.bottom - 1;
SetCursorPos(cursorPos.x, cursorPos.y);
ShowCursor(TRUE); ShowCursor(TRUE);
usingNativeCursor = true; usingNativeCursor = true;
} }
@ -352,6 +352,15 @@ void SetupMouse() {
mCreate_success = true; mCreate_success = true;
} }
static int cap(int val, int min, int max) {
if (val < min)
return min;
else if (val > max)
return max;
else
return val;
}
static void getGDICursorDelta(int* return_dx, int* return_dy) { static void getGDICursorDelta(int* return_dx, int* return_dy) {
int dx = 0; int dx = 0;
int dy = 0; int dy = 0;
@ -366,13 +375,12 @@ static void getGDICursorDelta(int* return_dx, int* return_dy) {
windowRect = newWindowRect; windowRect = newWindowRect;
getScreenClientRect(&clientRect, &windowRect); getScreenClientRect(&clientRect, &windowRect);
// Clip the position to the client rect // Clip the position to the client rect
if (newCursorPos.x < clientRect.right && newCursorPos.x >= clientRect.left && newCursorPos.x = cap(newCursorPos.x, clientRect.left, clientRect.right - 1);
newCursorPos.y < clientRect.bottom && newCursorPos.y >= clientRect.top) { newCursorPos.y = cap(newCursorPos.y, clientRect.top, clientRect.bottom - 1);
dx = newCursorPos.x - cursorPos.x; dx = newCursorPos.x - cursorPos.x;
dy = newCursorPos.y - cursorPos.y; dy = newCursorPos.y - cursorPos.y;
cursorPos.x += dx; cursorPos.x += dx;
cursorPos.y += dy; cursorPos.y += dy;
}
*return_dx = dx; *return_dx = dx;
*return_dy = dy; *return_dy = dy;
} }
@ -415,6 +423,7 @@ void UpdateMouseFields() {
dx = diMouseState.lX; dx = diMouseState.lX;
dy = diMouseState.lY; dy = diMouseState.lY;
} }
dy = -dy;
mEnvironment->SetStaticIntField(clsMouse, fidMDX, (jint)dx); mEnvironment->SetStaticIntField(clsMouse, fidMDX, (jint)dx);
mEnvironment->SetStaticIntField(clsMouse, fidMDY, (jint)dy); mEnvironment->SetStaticIntField(clsMouse, fidMDY, (jint)dy);