subsoap / defos

Extra native OS functions for games written using the Defold game engine
Creative Commons Zero v1.0 Universal
106 stars 14 forks source link

Lock/unlock mouse cursor to window #32

Closed subsoap closed 6 years ago

subsoap commented 6 years ago

todo

https://msdn.microsoft.com/en-us/library/windows/desktop/ms648383(v=vs.85).aspx

https://github.com/d954mas/Defold-lock-cursor/blob/master/lock/extension/src/LockMouse.cpp

subsoap commented 6 years ago

Locking cursor to center of the window, used in FPS shooters where you want the user to be able to continuously turn around with the mouse. mouse dx and mouse dy are used to get rotation. Generally you want to show a graphic which is centered in the screen and hide the mouse cursor. If you don't hide the mouse cursor as you turn around you'll still see the mouse dx/dy distances from center which is weird.

is_cursor_locked lock_cursor unlock_cursor

Clips the cursor to the borders of the current window so that it cannot exit the window bounds. Useful in games like Diablo where you might want to play in window mode with lots of fast clicking and not accidentally click outside of the game window.

is_cursor_clipped clip_cursor unclip_cursor

Generally if playing in window mode if the user presses ESC you want to open a pause overlay menu of some kind and unclip the cursor so that it can leave the window bounds.

For FPS game you want similar behavior where pressing ESC displays the normal cursor again which is allowed to move around the window freely while the overlay is visible..

rgrams commented 6 years ago

The way d954mas's Lock extension works is it just sets the mouse position back to some preset position every time you call the function and reports dx and dy. So all you really need is a set_mouse_position function. You can get dx and dy from input events normally, and DefOS already has the show/hide cursor functions.

"Capture"ing the mouse to a window should be done with this I think? (no, I was wrong)

subsoap commented 6 years ago

@rgrams Should test making a first person shooter type camera with Rendercam. Hide mouse cursor, always set mouse x,y to center of screen, use mouse dx/dy to rotate fps camera.

rgrams commented 6 years ago

@subsoap I have. It was pretty darn easy. I used d954mas's Lock extension from his Save Shelter game for the native extension part. (it's Windows & HTML only) If DefOS gets a 'set mouse position' function then I would use that instead.

chaosddp commented 6 years ago

Done for Windows at #57 with clipping

subsoap commented 6 years ago

I'll test in next few days unless someone else does before.

dapetcu21 commented 6 years ago

I think we can close this, as it’s been implemented for all platforms in #57 and #60 (minus clipping for HTML5, but as far as I know that’s not possible).