src/events/SDL_mouse.c
changeset 7107 2fcf7bf1d2b2
parent 7104 4e4ca313000c
child 7191 75360622e65f
equal deleted inserted replaced
7106:31f8acac196b 7107:2fcf7bf1d2b2
   413 
   413 
   414 int
   414 int
   415 SDL_SetRelativeMouseMode(SDL_bool enabled)
   415 SDL_SetRelativeMouseMode(SDL_bool enabled)
   416 {
   416 {
   417     SDL_Mouse *mouse = SDL_GetMouse();
   417     SDL_Mouse *mouse = SDL_GetMouse();
       
   418     SDL_Window *focusWindow = SDL_GetKeyboardFocus();
       
   419     int original_x = mouse->x, original_y = mouse->y;
   418 
   420 
   419     if (enabled == mouse->relative_mode) {
   421     if (enabled == mouse->relative_mode) {
   420         return 0;
   422         return 0;
   421     }
   423     }
   422 
   424 
   423     if (!mouse->SetRelativeMouseMode) {
   425     if (!mouse->SetRelativeMouseMode) {
   424         return SDL_Unsupported();
   426         return SDL_Unsupported();
   425     }
   427     }
   426 
   428 
       
   429     if (enabled && focusWindow) {
       
   430         /* Center it in the focused window to prevent clicks from going through
       
   431          * to background windows.
       
   432          */
       
   433         SDL_SetMouseFocus(focusWindow);
       
   434         SDL_WarpMouseInWindow(focusWindow, focusWindow->w/2, focusWindow->h/2);
       
   435     }
       
   436 
   427     if (mouse->SetRelativeMouseMode(enabled) < 0) {
   437     if (mouse->SetRelativeMouseMode(enabled) < 0) {
   428         return -1;
   438         return -1;
   429     }
   439     }
   430 
   440 
   431     /* Set the relative mode */
   441     /* Set the relative mode */
   432     mouse->relative_mode = enabled;
   442     mouse->relative_mode = enabled;
   433 
   443 
   434     if (enabled) {
   444     if (enabled) {
   435         /* Save the expected mouse position */
   445         /* Save the expected mouse position */
   436         mouse->original_x = mouse->x;
   446         mouse->original_x = original_x;
   437         mouse->original_y = mouse->y;
   447         mouse->original_y = original_y;
   438     } else if (mouse->focus) {
   448     } else if (mouse->focus) {
   439         /* Restore the expected mouse position */
   449         /* Restore the expected mouse position */
   440         SDL_WarpMouseInWindow(mouse->focus, mouse->original_x, mouse->original_y);
   450         SDL_WarpMouseInWindow(mouse->focus, mouse->original_x, mouse->original_y);
   441     }
   451     }
   442 
   452