how to wrap cursor around?

Started by mireazma, March 31, 2014, 12:03:01

Previous topic - Next topic

mireazma

I tried this code which works only 20% of time:
mouseX = Mouse.getX();
mouseY = Mouse.getY();
if(mouseX >= DISPLAY_WIDTH) Mouse.setCursorPosition(mouseX - DISPLAY_WIDTH, mouseY);else
if(mouseX <= 0) Mouse.setCursorPosition(mouseX + DISPLAY_WIDTH, mouseY);
if(mouseY >= DISPLAY_HEIGHT) Mouse.setCursorPosition(mouseX, mouseY - DISPLAY_HEIGHT);else
if(mouseY <= 0) Mouse.setCursorPosition(mouseX, mouseY + DISPLAY_HEIGHT);

I tried fiddling with Mouse.setClipMouseCoordinatesToWindow() but I can't tell what it does as it makes no difference to the code above.
Besides, isn't there a built-in function for wrapping cursor? My code looks nothing like elegant.
I want to be able to infinitely move the mouse in a direction (and register that movement), even if it gets out of the display.

quew8

Are you looking for the kind of thing you get in first person shooters? What you want is setMouseGrabbed().

From the docs:

Quote
public static void setGrabbed(boolean grab)
Sets whether or not the mouse has grabbed the cursor (and thus hidden). If grab is false, the getX() and getY() will return delta movement in pixels clamped to the display dimensions, from the center of the display.
Parameters:
grab - whether the mouse should be grabbed

mireazma

Thanks. That's exactly what I wanted. I read about that function before, about mouse grabbing but didn't know it meant wrapping coords. It says setGrabbed sets mouse grabbing (useful explanation) and what it does when set to... false. Who knew mouse grabbing meant cursor wrapping? I thought it had to do with the window taking control of the mouse in which case (in my view) it wouldn't let the cursor get out of the boundaries.
Anyway, thanks again.