Skip to content

Commit

Permalink
Clip cursor to game window
Browse files Browse the repository at this point in the history
This addresses a bug where the cursor could move outside the game window
during a flick (a very quick mouse movement to reposition the
crosshair).
  • Loading branch information
wroyca committed Oct 2, 2024
1 parent b0810e4 commit 8d35d49
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/Components/Modules/RawMouse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,6 @@ namespace Components

if (GetForegroundWindow() == Window::GetWindow())
{
if (r_fullscreen.get<bool>())
{
IN_ClampMouseMove();
}

static auto oldX = 0, oldY = 0;

auto dx = MouseRawX - oldX;
Expand All @@ -103,11 +98,14 @@ namespace Components
ScreenToClient(Window::GetWindow(), &curPos);

Gamepad::OnMouseMove(curPos.x, curPos.y, dx, dy);

auto recenterMouse = Game::CL_MouseEvent(curPos.x, curPos.y, dx, dy);

ClipCursor(NULL);

if (recenterMouse)
{
Game::IN_RecenterMouse();
RawMouse::IN_RecenterMouse();
}
}
}
Expand All @@ -134,6 +132,18 @@ namespace Components
IN_RawMouse_Init();
}

BOOL RawMouse::IN_RecenterMouse()
{
RECT clientRect;

GetClientRect(Window::GetWindow(), &clientRect);

ClientToScreen(Window::GetWindow(), std::bit_cast<POINT*>(&clientRect.left));
ClientToScreen(Window::GetWindow(), std::bit_cast<POINT*>(&clientRect.right));

return ClipCursor(&clientRect);
}

void RawMouse::IN_MouseMove()
{
if (M_RawInput.get<bool>())
Expand All @@ -154,6 +164,9 @@ namespace Components
Utils::Hook(0x467C03, IN_Init, HOOK_CALL).install()->quick();
Utils::Hook(0x64D095, IN_Init, HOOK_JUMP).install()->quick();

Utils::Hook(0x473517, IN_RecenterMouse, HOOK_CALL).install()->quick();
Utils::Hook(0x64C520, IN_RecenterMouse, HOOK_CALL).install()->quick();

M_RawInput = Dvar::Register<bool>("m_rawinput", true, Game::DVAR_ARCHIVE, "Use raw mouse input, Improves accuracy & has better support for higher polling rates");

Window::OnWndMessage(WM_INPUT, OnRawInput);
Expand Down
1 change: 1 addition & 0 deletions src/Components/Modules/RawMouse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ namespace Components
static void IN_RawMouseMove();
static void IN_RawMouse_Init();
static void IN_Init();
static BOOL IN_RecenterMouse();
};
}

0 comments on commit 8d35d49

Please sign in to comment.