Fix restoring locked cursor state when window gains focus again

This commit is contained in:
2025-03-31 23:25:38 +03:00
parent d355f9eff5
commit c1f76abc22
5 changed files with 22 additions and 2 deletions

View File

@@ -1179,9 +1179,10 @@ namespace FlaxEditor.Windows
_cursorVisible = Screen.CursorVisible;
_cursorLockMode = Screen.CursorLock;
// Restore cursor visibility (could be hidden by the game)
// Restore cursor state, could be hidden or locked by the game
if (!_cursorVisible)
Screen.CursorVisible = true;
Screen.CursorLock = CursorLockMode.None;
}
}

View File

@@ -119,7 +119,8 @@ public:
/// <summary>
/// Gets the current state of mouse relative mode.
/// </summary>
API_FUNCTION() FORCE_INLINE bool IsRelative() const
/// <param name="window">The window to check against, or null to check for any window.</param>
virtual API_FUNCTION() bool IsRelative(Window* window = nullptr) const
{
return _relativeMode;
}

View File

@@ -45,6 +45,7 @@ protected:
bool _isHorizontalFlippingMouse = false;
bool _isVerticalFlippingMouse = false;
bool _isClippingCursor = false;
bool _restoreRelativeMode = false;
explicit WindowBase(const CreateWindowSettings& settings);
virtual ~WindowBase();

View File

@@ -432,6 +432,13 @@ public:
if (!SDL_SetWindowRelativeMouseMode(windowHandle, relativeMode))
LOG(Error, "Failed to set mouse relative mode: {0}", String(SDL_GetError()));
}
bool IsRelative(Window* window) const override
{
if (window == nullptr)
return _relativeMode;
return _relativeModeWindow == window && _relativeMode;
}
};
/// <summary>

View File

@@ -440,6 +440,9 @@ void SDLWindow::HandleEvent(SDL_Event& event)
if (inRelativeMode)
Input::Mouse->SetRelativeMode(true, this);
}
else if (_restoreRelativeMode)
Input::Mouse->SetRelativeMode(true, this);
_restoreRelativeMode = false;
return;
}
case SDL_EVENT_WINDOW_FOCUS_LOST:
@@ -448,6 +451,13 @@ void SDLWindow::HandleEvent(SDL_Event& event)
SDL_StopTextInput(_window);
if (_isClippingCursor)
SDL_SetWindowMouseRect(_window, nullptr);
if (Input::Mouse->IsRelative(this))
{
Input::Mouse->SetRelativeMode(false, this);
_restoreRelativeMode = true;
}
OnLostFocus();
return;
}