Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Move cursor into window when enabling relative mode or gaining focus …

…in relative mode.

This prevents wonky behavior where the clicks won't go to the window
because the cursor was outside it when we enabled relative mode.
  • Loading branch information
jorgenpt committed Apr 24, 2013
1 parent 06f0751 commit 1a2db6c030038302bd9b2fc8a3e78c9bc3b16c82

File 1 of 2 in 1a2db6c

@@ -415,6 +415,8 @@ int
SDL_SetRelativeMouseMode(SDL_bool enabled)
{
SDL_Mouse *mouse = SDL_GetMouse();
SDL_Window *focusWindow = SDL_GetKeyboardFocus();
int original_x = mouse->x, original_y = mouse->y;

if (enabled == mouse->relative_mode) {
return 0;
@@ -424,6 +426,14 @@ SDL_SetRelativeMouseMode(SDL_bool enabled)
return SDL_Unsupported();
}

if (enabled && focusWindow) {
/* Center it in the focused window to prevent clicks from going through
* to background windows.
*/
SDL_SetMouseFocus(focusWindow);
SDL_WarpMouseInWindow(focusWindow, focusWindow->w/2, focusWindow->h/2);
}

if (mouse->SetRelativeMouseMode(enabled) < 0) {
return -1;
}
@@ -433,8 +443,8 @@ SDL_SetRelativeMouseMode(SDL_bool enabled)

if (enabled) {
/* Save the expected mouse position */
mouse->original_x = mouse->x;
mouse->original_y = mouse->y;
mouse->original_x = original_x;
mouse->original_y = original_y;
} else if (mouse->focus) {
/* Restore the expected mouse position */
SDL_WarpMouseInWindow(mouse->focus, mouse->original_x, mouse->original_y);

0 comments on commit 1a2db6c

Please sign in to comment.