Finish RawMouse component, also enable dpi awareness for game window;

This commit is contained in:
GEEKiDoS 2022-04-29 22:26:14 +08:00
parent 08d658644e
commit 237a89fa71
8 changed files with 213 additions and 66 deletions

View File

@ -105,6 +105,7 @@ namespace Components
Loader::Register(new Elevators()); Loader::Register(new Elevators());
Loader::Register(new ClientCommand()); Loader::Register(new ClientCommand());
Loader::Register(new ScriptExtension()); Loader::Register(new ScriptExtension());
Loader::Register(new RawMouse());
Loader::Pregame = false; Loader::Pregame = false;
} }

View File

@ -2,7 +2,9 @@
namespace Components namespace Components
{ {
void IN_ClampMouseMove() Dvar::Var RawMouse::m_rawinput;
void RawMouse::IN_ClampMouseMove()
{ {
tagRECT rc; tagRECT rc;
tagPOINT curPos; tagPOINT curPos;
@ -43,7 +45,36 @@ namespace Components
} }
} }
void IN_RawMouseMove() int RawMouse::mouseRawX = 0;
int RawMouse::mouseRawY = 0;
BOOL RawMouse::OnRawInput(LPARAM lParam, WPARAM)
{
UINT dwSize = sizeof(RAWINPUT);
static BYTE lpb[sizeof(RAWINPUT)];
GetRawInputData((HRAWINPUT)lParam, RID_INPUT, lpb, &dwSize, sizeof(RAWINPUTHEADER));
auto* raw = reinterpret_cast<RAWINPUT*>(lpb);
if (raw->header.dwType == RIM_TYPEMOUSE)
{
// Is there's really absolute mouse on earth?
if (raw->data.mouse.usFlags & MOUSE_MOVE_ABSOLUTE)
{
mouseRawX = raw->data.mouse.lLastX;
mouseRawY = raw->data.mouse.lLastY;
}
else
{
mouseRawX += raw->data.mouse.lLastX;
mouseRawY += raw->data.mouse.lLastY;
}
}
return TRUE;
}
void RawMouse::IN_RawMouseMove()
{ {
static Game::dvar_t* r_fullscreen = Game::Dvar_FindVar("r_fullscreen"); static Game::dvar_t* r_fullscreen = Game::Dvar_FindVar("r_fullscreen");
@ -52,15 +83,61 @@ namespace Components
if (r_fullscreen->current.enabled) if (r_fullscreen->current.enabled)
IN_ClampMouseMove(); IN_ClampMouseMove();
static int oldX = 0, oldY = 0;
} int dx = mouseRawX - oldX;
} int dy = mouseRawY - oldY;
Dvar::Var Mouse_RawInput; oldX = mouseRawX;
oldY = mouseRawY;
void IN_MouseMove() // Don't use raw input for menu?
// Because it needs to call the ScreenToClient
static tagPOINT curPos;
GetCursorPos(&curPos);
Game::s_wmv->oldPos = curPos;
ScreenToClient(Game::g_wv->hWnd, &curPos);
Game::g_wv->recenterMouse = Game::CL_MouseEvent(curPos.x, curPos.y, dx, dy);
if (Game::g_wv->recenterMouse)
{ {
if (Mouse_RawInput.get<bool>()) Game::IN_RecenterMouse();
}
}
}
void RawMouse::IN_RawMouse_Init()
{
static bool init = false;
if (Game::g_wv->hWnd && !init && m_rawinput.get<bool>())
{
#ifdef DEBUG
Logger::Print("Raw Mouse Init.\n");
#endif
RAWINPUTDEVICE Rid[1];
Rid[0].usUsagePage = 0x01; // HID_USAGE_PAGE_GENERIC
Rid[0].usUsage = 0x02; // HID_USAGE_GENERIC_MOUSE
Rid[0].dwFlags = RIDEV_INPUTSINK;
Rid[0].hwndTarget = Game::g_wv->hWnd;
RegisterRawInputDevices(Rid, ARRAYSIZE(Rid), sizeof(Rid[0]));
init = true;
}
}
void RawMouse::IN_Init()
{
Game::IN_Init();
IN_RawMouse_Init();
}
void RawMouse::IN_MouseMove()
{
if (m_rawinput.get<bool>())
{ {
IN_RawMouseMove(); IN_RawMouseMove();
} }
@ -72,10 +149,17 @@ namespace Components
RawMouse::RawMouse() RawMouse::RawMouse()
{ {
Utils::Hook(0x475E65, IN_MouseMove, HOOK_JUMP).install()->quick(); Utils::Hook(0x475E65, RawMouse::IN_MouseMove, HOOK_JUMP).install()->quick();
Utils::Hook(0x475E8D, IN_MouseMove, HOOK_JUMP).install()->quick(); Utils::Hook(0x475E8D, RawMouse::IN_MouseMove, HOOK_JUMP).install()->quick();
Utils::Hook(0x475E9E, IN_MouseMove, HOOK_JUMP).install()->quick(); Utils::Hook(0x475E9E, RawMouse::IN_MouseMove, HOOK_JUMP).install()->quick();
Mouse_RawInput = Dvar::Register<bool>("m_rawinput", true, Game::dvar_flag::DVAR_ARCHIVE, "Use raw mouse input."); Utils::Hook(0x467C03, RawMouse::IN_Init, HOOK_CALL).install()->quick();
Utils::Hook(0x64D095, RawMouse::IN_Init, HOOK_JUMP).install()->quick();
m_rawinput = Dvar::Register<bool>("m_rawinput", true, Game::dvar_flag::DVAR_ARCHIVE, "Use raw mouse input, use in_restart to take effect if not enabled.");
Window::OnWndMessage(WM_INPUT, RawMouse::OnRawInput);
Window::OnCreate(RawMouse::IN_RawMouse_Init);
} }
} }

View File

@ -6,5 +6,15 @@ namespace Components
{ {
public: public:
RawMouse(); RawMouse();
private:
static Dvar::Var m_rawinput;
static int mouseRawX, mouseRawY;
static void IN_ClampMouseMove();
static BOOL OnRawInput(LPARAM lParam, WPARAM);
static void IN_RawMouseMove();
static void IN_RawMouse_Init();
static void IN_Init();
static void IN_MouseMove();
}; };
} }

View File

@ -7,6 +7,8 @@ namespace Components
HWND Window::MainWindow = nullptr; HWND Window::MainWindow = nullptr;
BOOL Window::CursorVisible = TRUE; BOOL Window::CursorVisible = TRUE;
std::unordered_map<UINT, Utils::Slot<Window::WndProcCallback>> Window::WndMessageCallbacks;
Utils::Signal<Window::CreateCallback> Window::CreateSignals;
int Window::Width() int Window::Width()
{ {
@ -66,6 +68,16 @@ namespace Components
return Window::MainWindow; return Window::MainWindow;
} }
void Window::OnWndMessage(UINT Msg, Utils::Slot<Window::WndProcCallback> callback)
{
WndMessageCallbacks.emplace(Msg, callback);
}
void Window::OnCreate(Utils::Slot<CreateCallback> callback)
{
CreateSignals.connect(callback);
}
int Window::IsNoBorder() int Window::IsNoBorder()
{ {
return Window::NoBorder.get<bool>(); return Window::NoBorder.get<bool>();
@ -121,6 +133,9 @@ namespace Components
HWND WINAPI Window::CreateMainWindow(DWORD dwExStyle, LPCSTR lpClassName, LPCSTR lpWindowName, DWORD dwStyle, int X, int Y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam) HWND WINAPI Window::CreateMainWindow(DWORD dwExStyle, LPCSTR lpClassName, LPCSTR lpWindowName, DWORD dwStyle, int X, int Y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam)
{ {
Window::MainWindow = CreateWindowExA(dwExStyle, lpClassName, lpWindowName, dwStyle, X, Y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam); Window::MainWindow = CreateWindowExA(dwExStyle, lpClassName, lpWindowName, dwStyle, X, Y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam);
CreateSignals();
return Window::MainWindow; return Window::MainWindow;
} }
@ -132,15 +147,21 @@ namespace Components
BOOL WINAPI Window::MessageHandler(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) BOOL WINAPI Window::MessageHandler(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{ {
if (Msg == WM_SETCURSOR) if (WndMessageCallbacks.find(Msg) != WndMessageCallbacks.end())
{ {
Window::ApplyCursor(); return WndMessageCallbacks[Msg](lParam, wParam);
return TRUE;
} }
return Utils::Hook::Call<BOOL(__stdcall)(HWND, UINT, WPARAM, LPARAM)>(0x4731F0)(hWnd, Msg, wParam, lParam); return Utils::Hook::Call<BOOL(__stdcall)(HWND, UINT, WPARAM, LPARAM)>(0x4731F0)(hWnd, Msg, wParam, lParam);
} }
void Window::EnableDpiAwareness()
{
const Utils::Library user32{ "user32.dll" };
user32.invokePascal<void>("SetProcessDpiAwarenessContext", DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
}
Window::Window() Window::Window()
{ {
// Borderless window // Borderless window
@ -184,5 +205,13 @@ namespace Components
// Use custom message handler // Use custom message handler
Utils::Hook::Set(0x64D298, Window::MessageHandler); Utils::Hook::Set(0x64D298, Window::MessageHandler);
Window::OnWndMessage(WM_SETCURSOR, [](WPARAM, LPARAM)
{
Window::ApplyCursor();
return TRUE;
});
Window::EnableDpiAwareness();
} }
} }

View File

@ -5,6 +5,9 @@ namespace Components
class Window : public Component class Window : public Component
{ {
public: public:
typedef BOOL(WndProcCallback)(WPARAM wParam, LPARAM lParam);
typedef void(CreateCallback)();
Window(); Window();
static int Width(); static int Width();
@ -18,10 +21,15 @@ namespace Components
static HWND GetWindow(); static HWND GetWindow();
static void OnWndMessage(UINT Msg, Utils::Slot<WndProcCallback> callback);
static void OnCreate(Utils::Slot<CreateCallback> callback);
private: private:
static BOOL CursorVisible; static BOOL CursorVisible;
static Dvar::Var NoBorder; static Dvar::Var NoBorder;
static Dvar::Var NativeCursor; static Dvar::Var NativeCursor;
static std::unordered_map<UINT, Utils::Slot<WndProcCallback>> WndMessageCallbacks;
static Utils::Signal<CreateCallback> CreateSignals;
static HWND MainWindow; static HWND MainWindow;
@ -36,5 +44,7 @@ namespace Components
static void StyleHookStub(); static void StyleHookStub();
static HWND WINAPI CreateMainWindow(DWORD dwExStyle, LPCSTR lpClassName, LPCSTR lpWindowName, DWORD dwStyle, int X, int Y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam); static HWND WINAPI CreateMainWindow(DWORD dwExStyle, LPCSTR lpClassName, LPCSTR lpWindowName, DWORD dwStyle, int X, int Y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam);
static void EnableDpiAwareness();
}; };
} }

View File

@ -425,6 +425,8 @@ namespace Game
IN_RecenterMouse_t IN_RecenterMouse = IN_RecenterMouse_t(0x463D80); IN_RecenterMouse_t IN_RecenterMouse = IN_RecenterMouse_t(0x463D80);
IN_MouseMove_t IN_MouseMove = IN_MouseMove_t(0x64C490); IN_MouseMove_t IN_MouseMove = IN_MouseMove_t(0x64C490);
IN_Init_t IN_Init = IN_Init_t(0x45D620);
IN_Shutdown_t IN_Shutdown = IN_Shutdown_t(0x426360);
XAssetHeader* DB_XAssetPool = reinterpret_cast<XAssetHeader*>(0x7998A8); XAssetHeader* DB_XAssetPool = reinterpret_cast<XAssetHeader*>(0x7998A8);
unsigned int* g_poolSize = reinterpret_cast<unsigned int*>(0x7995E8); unsigned int* g_poolSize = reinterpret_cast<unsigned int*>(0x7995E8);
@ -551,6 +553,9 @@ namespace Game
WinVars_t* g_wv = reinterpret_cast<WinVars_t*>(0x64A3AC8); WinVars_t* g_wv = reinterpret_cast<WinVars_t*>(0x64A3AC8);
WinMouseVars_t* s_wmv = reinterpret_cast<WinMouseVars_t*>(0x649D640); WinMouseVars_t* s_wmv = reinterpret_cast<WinMouseVars_t*>(0x649D640);
int* window_center_x = reinterpret_cast<int*>(0x649D638);
int* window_center_y = reinterpret_cast<int*>(0x649D630);
void Sys_LockRead(FastCriticalSection* critSect) void Sys_LockRead(FastCriticalSection* critSect)
{ {
InterlockedIncrement(&critSect->readCount); InterlockedIncrement(&critSect->readCount);

View File

@ -1014,6 +1014,12 @@ namespace Game
typedef void(*IN_MouseMove_t)(); typedef void(*IN_MouseMove_t)();
extern IN_MouseMove_t IN_MouseMove; extern IN_MouseMove_t IN_MouseMove;
typedef void(*IN_Init_t)();
extern IN_Init_t IN_Init;
typedef void(*IN_Shutdown_t)();
extern IN_Shutdown_t IN_Shutdown;
extern XAssetHeader* DB_XAssetPool; extern XAssetHeader* DB_XAssetPool;
extern unsigned int* g_poolSize; extern unsigned int* g_poolSize;
@ -1145,6 +1151,10 @@ namespace Game
extern WinVars_t* g_wv; extern WinVars_t* g_wv;
extern WinMouseVars_t* s_wmv; extern WinMouseVars_t* s_wmv;
extern int* window_center_x;
extern int* window_center_y;
void Sys_LockRead(FastCriticalSection* critSect); void Sys_LockRead(FastCriticalSection* critSect);
void Sys_UnlockRead(FastCriticalSection* critSect); void Sys_UnlockRead(FastCriticalSection* critSect);

View File

@ -7479,8 +7479,6 @@ namespace Game
int reflib_active; int reflib_active;
HWND hWnd; HWND hWnd;
HINSTANCE hInstance; HINSTANCE hInstance;
int activeApp;
int isMinimized;
int hasFocus; int hasFocus;
int activationStateChanged; int activationStateChanged;
int recenterMouse; int recenterMouse;