Fix cursor locking bounds calculation
This commit is contained in:
@@ -123,34 +123,32 @@ void Screen::SetCursorLock(CursorLockMode mode)
|
||||
#if USE_EDITOR
|
||||
const auto win = Editor::Managed->GetGameWindow(true);
|
||||
Rectangle bounds(Editor::Managed->GameViewportToScreen(Float2::Zero), Editor::Managed->GetGameWindowSize());
|
||||
if (win)
|
||||
bounds = Rectangle(win->ScreenToClient(bounds.GetTopLeft()), bounds.Size);
|
||||
#else
|
||||
const auto win = Engine::MainWindow;
|
||||
Rectangle bounds = win != nullptr ? win->GetClientBounds() : Rectangle();
|
||||
#endif
|
||||
bool inRelativeMode = Input::Mouse->IsRelative();
|
||||
if (win && mode == CursorLockMode::Clipped)
|
||||
win->StartClippingCursor(bounds);
|
||||
else if (win && mode == CursorLockMode::Locked)
|
||||
if (win)
|
||||
{
|
||||
// Use mouse clip region to restrict the cursor in one spot
|
||||
Rectangle centerBounds;
|
||||
auto mousePosition = win->GetMousePosition();
|
||||
if (bounds.Contains(mousePosition))
|
||||
centerBounds = Rectangle(mousePosition, Float2(1, 1));
|
||||
else
|
||||
centerBounds = Rectangle(bounds.GetCenter(), Float2(1, 1));
|
||||
win->StartClippingCursor(centerBounds);
|
||||
}
|
||||
else if (win && (CursorLock == CursorLockMode::Locked || CursorLock == CursorLockMode::Clipped))
|
||||
win->EndClippingCursor();
|
||||
CursorLock = mode;
|
||||
bool inRelativeMode = Input::Mouse->IsRelative();
|
||||
if (mode == CursorLockMode::Clipped)
|
||||
win->StartClippingCursor(bounds);
|
||||
else if (mode == CursorLockMode::Locked)
|
||||
{
|
||||
// Use mouse clip region to restrict the cursor in one spot
|
||||
win->StartClippingCursor(Rectangle(bounds.GetCenter(), Float2(1, 1)));
|
||||
}
|
||||
else if (CursorLock == CursorLockMode::Locked || CursorLock == CursorLockMode::Clipped)
|
||||
win->EndClippingCursor();
|
||||
|
||||
// Enable relative mode when cursor is restricted
|
||||
bool focused = win && Engine::HasGameViewportFocus();
|
||||
if (win && CursorLock != CursorLockMode::None)
|
||||
Input::Mouse->SetRelativeMode(true, win);
|
||||
else if (win && CursorLock == CursorLockMode::None && inRelativeMode)
|
||||
Input::Mouse->SetRelativeMode(false, win);
|
||||
// Enable relative mode when cursor is restricted
|
||||
if (mode != CursorLockMode::None)
|
||||
Input::Mouse->SetRelativeMode(true, win);
|
||||
else if (mode == CursorLockMode::None && inRelativeMode)
|
||||
Input::Mouse->SetRelativeMode(false, win);
|
||||
}
|
||||
CursorLock = mode;
|
||||
}
|
||||
|
||||
GameWindowMode Screen::GetGameWindowMode()
|
||||
|
||||
@@ -521,6 +521,17 @@ public:
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the mouse position in window coordinates.
|
||||
/// </summary>
|
||||
API_PROPERTY() virtual Float2 GetMousePosition() const;
|
||||
|
||||
/// <summary>
|
||||
/// Sets the mouse position in window coordinates.
|
||||
/// </summary>
|
||||
/// <param name="position">Mouse position to set on</param>
|
||||
API_PROPERTY() virtual void SetMousePosition(const Float2& position) const;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the mouse cursor.
|
||||
/// </summary>
|
||||
@@ -717,16 +728,6 @@ public:
|
||||
API_FUNCTION() bool GetKeyUp(KeyboardKeys key) const;
|
||||
|
||||
public:
|
||||
/// <summary>
|
||||
/// Gets the mouse position in window coordinates.
|
||||
/// </summary>
|
||||
API_PROPERTY() Float2 GetMousePosition() const;
|
||||
|
||||
/// <summary>
|
||||
/// Sets the mouse position in window coordinates.
|
||||
/// </summary>
|
||||
/// <param name="position">Mouse position to set on</param>
|
||||
API_PROPERTY() void SetMousePosition(const Float2& position) const;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the mouse position change during the last frame.
|
||||
|
||||
@@ -399,6 +399,7 @@ public:
|
||||
|
||||
void SetRelativeMode(bool relativeMode, Window* window) final override
|
||||
{
|
||||
ASSERT(window != nullptr);
|
||||
if (relativeMode == _relativeMode)
|
||||
return;
|
||||
|
||||
|
||||
@@ -820,16 +820,8 @@ void SDLWindow::StartClippingCursor(const Rectangle& bounds)
|
||||
return;
|
||||
|
||||
#if PLATFORM_LINUX
|
||||
{
|
||||
auto oldValue = SDL_GetHint(SDL_HINT_MOUSE_RELATIVE_WARP_MOTION);
|
||||
SDL_SetHint(SDL_HINT_MOUSE_RELATIVE_WARP_MOTION, "1");
|
||||
|
||||
// The cursor is not fully constrained when positioned outside of the clip region...
|
||||
Float2 center = bounds.GetCenter();
|
||||
SDL_WarpMouseInWindow(_window, center.X, center.Y);
|
||||
|
||||
SDL_SetHint(SDL_HINT_MOUSE_RELATIVE_WARP_MOTION, oldValue);
|
||||
}
|
||||
// The cursor is not fully constrained when positioned outside the clip region
|
||||
SetMousePosition(bounds.GetCenter());
|
||||
#endif
|
||||
|
||||
_isClippingCursor = true;
|
||||
@@ -847,6 +839,17 @@ void SDLWindow::EndClippingCursor()
|
||||
SDL_SetWindowMouseRect(_window, nullptr);
|
||||
}
|
||||
|
||||
void SDLWindow::SetMousePosition(const Float2& position) const
|
||||
{
|
||||
if (!_settings.AllowInput || !_focused)
|
||||
return;
|
||||
|
||||
SDL_WarpMouseInWindow(_window, position.X, position.Y);
|
||||
|
||||
Float2 screenPosition = ClientToScreen(position);
|
||||
Input::Mouse->OnMouseMoved(screenPosition);
|
||||
}
|
||||
|
||||
void SDLWindow::SetCursor(CursorType type)
|
||||
{
|
||||
CursorType oldCursor = _cursor;
|
||||
|
||||
@@ -113,6 +113,7 @@ public:
|
||||
void EndTrackingMouse() override;
|
||||
void StartClippingCursor(const Rectangle& bounds) override;
|
||||
void EndClippingCursor() override;
|
||||
void SetMousePosition(const Float2& position) const override;
|
||||
void SetCursor(CursorType type) override;
|
||||
|
||||
#if USE_EDITOR && PLATFORM_WINDOWS
|
||||
|
||||
Reference in New Issue
Block a user