gurkenlabs / litiengine

LITIENGINE 🕹 The pure 2D java game engine.
https://litiengine.com/
MIT License
731 stars 94 forks source link

Mouse Moves in Wrong Direction When Turning GrabMouse On #810

Open Hades948 opened 7 months ago

Hades948 commented 7 months ago

Describe the bug When I call Input.mouse().setGrabMouse(true), the mouse moves in the wrong direction.

I want the player to be able to turn grab mouse on and off, so they can get out of the window without completely closing the game. When I turn grab mouse off, I figure out where the game mouse is and I use a robot to move the actual mouse to that location after calling setGrabMouse(false). This works fine. But when I turn grab mouse on, I'd expect the game mouse to stay where it is (or at the very least, return to the center of the screen). But, instead, it seems to be moving the distance between it and the center of the window, but in the wrong direction.

I believe this is happening in Mouse.setLocation(MouseEvent): image

This is, of course, the correct behavior when grab mouse is on. But I think it's incorrect when grab mouse was off during the last update, but is now on. Again, in that case, I think I'd expect the game mouse's position to just stay where it is (even if the mouse is moving back to the center of the screen).

Here's a GIF where I try to demo it, but it's a little hard to show. Details on how to reproduce are below. java_4ZbXZ7ULbt

To Reproduce Steps to reproduce the behavior:

  1. Throw this code in your main() or somewhere: image VirtualMouse.instance().getLocationOnScreen() does the following: image Utils.robot is just a normal Robot object.

  2. Run the game and press Alt to release the mouse.

  3. Click somewhere on the screen. Closer to the middle is easier to see what's happening. The game mouse will jump away from the center of the screen.

Your System:

Btw, I welcome feedback on this code. I haven't worked with the mouse and robots much.

nightm4re94 commented 7 months ago

Thanks for the elaborate example! Will check this out when I'm back from vacation in March.