fixed mouse issues, setGrabbed & native cursor now works (afaik ;))

This commit is contained in:
Brian Matzon 2004-04-12 14:46:18 +00:00
parent b21e4a7f93
commit a9506354ee
4 changed files with 83 additions and 74 deletions

View File

@ -181,11 +181,10 @@ public class Mouse {
Cursor oldCursor = currentCursor;
currentCursor = cursor;
if (currentCursor != null) {
if(currentCursor != oldCursor) {
nSetNativeCursor(currentCursor.getHandle());
currentCursor.setTimeout();
x = Window.getWidth() / 2;
y = Window.getHeight() / 2;
isGrabbed = false;
}
} else {
nSetNativeCursor(0);
}
@ -255,6 +254,8 @@ public class Mouse {
created = true;
currentCursor = null;
dx = dy = dwheel = 0;
x = Window.getWidth() / 2;
y = Window.getHeight() / 2;
// set mouse buttons
buttonCount = nGetButtonCount();
@ -553,12 +554,18 @@ public class Mouse {
public static void setGrabbed(boolean 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()) {

View File

@ -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 {
@ -256,28 +251,32 @@ 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<cursor.length; i++) {
cursor[i].destroy();
}
Mouse.destroy();
Window.destroy();
Display.setDisplayMode(mode);
@ -295,7 +293,6 @@ public class HWCursorTest {
glInit();
Keyboard.create();
initNativeCursors();
} catch (Exception e) {
e.printStackTrace();
@ -305,7 +302,6 @@ public class HWCursorTest {
//check for window key
if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
try {
Keyboard.destroy();
try {
Mouse.setNativeCursor(null);
} catch (Exception e) {
@ -315,7 +311,6 @@ public class HWCursorTest {
for(int i=0; i<cursor.length; i++) {
cursor[i].destroy();
}
Mouse.destroy();
Window.destroy();
Display.resetDisplayMode();
@ -323,7 +318,6 @@ public class HWCursorTest {
glInit();
Keyboard.create();
initNativeCursors();
} catch (Exception e) {
e.printStackTrace();
@ -340,16 +334,18 @@ public class HWCursorTest {
if (Keyboard.isKeyDown(Keyboard.KEY_N)) {
try {
cursor[mouse_btn].resetAnimation();
Mouse.setNativeCursor(cursor[mouse_btn]);
} catch (Exception e) {
e.printStackTrace();
}
}
if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
while(Keyboard.next()) {
if(Keyboard.getEventKey() == Keyboard.KEY_SPACE && Keyboard.getEventKeyState()) {
Mouse.setGrabbed(!Mouse.isGrabbed());
System.out.println("Grabbed: " + Mouse.isGrabbed());
//mouse_x = Mouse.getX();
//mouse_y = Mouse.getY();
}
}
}
@ -357,7 +353,6 @@ public class HWCursorTest {
* Cleans up the test
*/
private void cleanup() {
Keyboard.destroy();
try {
Mouse.setNativeCursor(null);
} catch (Exception e) {
@ -367,7 +362,6 @@ public class HWCursorTest {
for(int i=0; i<cursor.length; i++) {
cursor[i].destroy();
}
Mouse.destroy();
Display.resetDisplayMode();
Window.destroy();
}

View File

@ -238,26 +238,31 @@ public class MouseTest {
private void updateState() {
direction = -1;
int dx = Mouse.getDX();
int dy = Mouse.getDY();
int dw = Mouse.getDWheel();
// get out if no movement
if (Mouse.getDX() == Mouse.getDY() && Mouse.getDX() == 0 && Mouse.getDWheel() == 0) {
if (dx == dy && dx == 0 && dw == 0) {
return;
}
// determine direction moved
// ============================
if(Mouse.getDX() > 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,15 +318,18 @@ public class MouseTest {
* Handles the keyboard
*/
private void handleKeyboard() {
while(Keyboard.next()) {
// closing on ESCAPE
if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
if(Keyboard.getEventKey() == Keyboard.KEY_ESCAPE && Keyboard.getEventKeyState()) {
closing = true;
}
if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
if(Keyboard.getEventKey() == Keyboard.KEY_SPACE && Keyboard.getEventKeyState()) {
Mouse.setGrabbed(!Mouse.isGrabbed());
}
}
}
/**
* Does the "model logic"

View File

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