Fix mouse events not being fired when the mouse is ungrabbed

This commit is contained in:
LJ Sonic 2023-10-18 16:50:06 +02:00
parent 4196252ea9
commit 9f116c7c9e

View file

@ -382,10 +382,8 @@ static INT32 Impl_SDL_Scancode_To_Keycode(SDL_Scancode code)
return 0; return 0;
} }
static boolean IgnoreMouse(void) static boolean ShouldIgnoreMouse(void)
{ {
if (cv_alwaysgrabmouse.value)
return false;
if (menuactive) if (menuactive)
return !M_MouseNeeded(); return !M_MouseNeeded();
if (paused || con_destlines || chat_on) if (paused || con_destlines || chat_on)
@ -393,11 +391,20 @@ static boolean IgnoreMouse(void)
if (gamestate != GS_LEVEL && gamestate != GS_INTERMISSION && if (gamestate != GS_LEVEL && gamestate != GS_INTERMISSION &&
gamestate != GS_CONTINUING && gamestate != GS_CUTSCENE) gamestate != GS_CONTINUING && gamestate != GS_CUTSCENE)
return true; return true;
if (!mousegrabbedbylua)
return true;
return false; return false;
} }
static boolean ShouldGrabMouse(void)
{
if (cv_alwaysgrabmouse.value)
return true;
if (ShouldIgnoreMouse())
return false;
if (!mousegrabbedbylua)
return false;
return true;
}
static void SDLdoGrabMouse(void) static void SDLdoGrabMouse(void)
{ {
SDL_ShowCursor(SDL_DISABLE); SDL_ShowCursor(SDL_DISABLE);
@ -424,7 +431,7 @@ void I_UpdateMouseGrab(void)
{ {
if (SDL_WasInit(SDL_INIT_VIDEO) == SDL_INIT_VIDEO && window != NULL if (SDL_WasInit(SDL_INIT_VIDEO) == SDL_INIT_VIDEO && window != NULL
&& SDL_GetMouseFocus() == window && SDL_GetKeyboardFocus() == window && SDL_GetMouseFocus() == window && SDL_GetKeyboardFocus() == window
&& USE_MOUSEINPUT && !IgnoreMouse()) && USE_MOUSEINPUT && ShouldGrabMouse())
SDLdoGrabMouse(); SDLdoGrabMouse();
} }
@ -640,7 +647,7 @@ static void Impl_HandleWindowEvent(SDL_WindowEvent evt)
} }
//else firsttimeonmouse = SDL_FALSE; //else firsttimeonmouse = SDL_FALSE;
if (USE_MOUSEINPUT && !IgnoreMouse()) if (USE_MOUSEINPUT && ShouldGrabMouse())
SDLdoGrabMouse(); SDLdoGrabMouse();
} }
else if (!mousefocus && !kbfocus) else if (!mousefocus && !kbfocus)
@ -692,7 +699,7 @@ static void Impl_HandleMouseMotionEvent(SDL_MouseMotionEvent evt)
if (USE_MOUSEINPUT) if (USE_MOUSEINPUT)
{ {
if ((SDL_GetMouseFocus() != window && SDL_GetKeyboardFocus() != window) || (IgnoreMouse() && !firstmove)) if ((SDL_GetMouseFocus() != window && SDL_GetKeyboardFocus() != window) || (!ShouldGrabMouse() && !firstmove))
{ {
SDLdoUngrabMouse(); SDLdoUngrabMouse();
firstmove = false; firstmove = false;
@ -745,7 +752,7 @@ static void Impl_HandleMouseButtonEvent(SDL_MouseButtonEvent evt, Uint32 type)
// this apparently makes a mouse button down event but not a mouse button up event, // this apparently makes a mouse button down event but not a mouse button up event,
// resulting in whatever key was pressed down getting "stuck" if we don't ignore it. // resulting in whatever key was pressed down getting "stuck" if we don't ignore it.
// -- Monster Iestyn (28/05/18) // -- Monster Iestyn (28/05/18)
if (SDL_GetMouseFocus() != window || IgnoreMouse()) if (SDL_GetMouseFocus() != window || ShouldIgnoreMouse())
return; return;
/// \todo inputEvent.button.which /// \todo inputEvent.button.which
@ -1127,7 +1134,7 @@ void I_StartupMouse(void)
} }
else else
firsttimeonmouse = SDL_FALSE; firsttimeonmouse = SDL_FALSE;
if (cv_usemouse.value && !IgnoreMouse()) if (cv_usemouse.value && ShouldGrabMouse())
SDLdoGrabMouse(); SDLdoGrabMouse();
else else
SDLdoUngrabMouse(); SDLdoUngrabMouse();