Skip to content

Commit

Permalink
Add menu input and input hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
TheIndra55 committed Jan 14, 2024
1 parent 8e3adb9 commit 0d085e4
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 7 deletions.
21 changes: 19 additions & 2 deletions src/Hook.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#include <MinHook.h>

#include "Hook.h"
#include "input/MessageHook.h"

#include "cdc/render/PCDeviceManager.h"

using namespace std::placeholders;

static bool(*s_D3D_Init)();

static bool D3D_Init()
Expand All @@ -28,13 +31,27 @@ void Hook::Initialize()
MH_EnableHook(MH_ALL_HOOKS);
}

void Hook::PostInitialize()
{
// Create the menu
m_menu = std::make_unique<Menu>();

// Register the message hook
MessageHook::OnMessage(std::bind(&Hook::OnMessage, this, _1, _2, _3, _4));
}

void Hook::OnMessage(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
m_menu->OnMessage(hWnd, msg, wParam, lParam);
}

void Hook::OnDevice()
{
// Assign the DeviceManager instance
cdc::PCDeviceManager::s_pInstance = *(cdc::PCDeviceManager**)0xA6669C;

// Creat the menu
m_menu = std::make_unique<Menu>();
// Initialize the hook
PostInitialize();
}

Hook& Hook::GetInstance()
Expand Down
3 changes: 3 additions & 0 deletions src/Hook.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ class Hook
std::unique_ptr<Menu> m_menu;

void Initialize();
void PostInitialize();

void OnMessage(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

public:
Hook();
Expand Down
6 changes: 6 additions & 0 deletions src/input/Input.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "Input.h"

void Input::DisableInput(bool disable)
{
*(bool*)0x8551A9 = disable;
}
7 changes: 7 additions & 0 deletions src/input/Input.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

class Input
{
public:
static void DisableInput(bool disable);
};
24 changes: 24 additions & 0 deletions src/input/MessageHook.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "MessageHook.h"

#include "cdc/render/PCDeviceManager.h"

static std::function<void(HWND, UINT, WPARAM, LPARAM)> s_callback;
static WNDPROC s_original;

static LRESULT WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
s_callback(hWnd, msg, wParam, lParam);

return CallWindowProc(s_original, hWnd, msg, wParam, lParam);
}

void MessageHook::OnMessage(std::function<void(HWND, UINT, WPARAM, LPARAM)> callback)
{
// Get the window handle
auto hWnd = cdc::PCDeviceManager::s_pInstance->GetWindow();

// Set the new message handler to our handler
s_original = (WNDPROC)SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LONG_PTR)WndProc);

s_callback = callback;
}
10 changes: 10 additions & 0 deletions src/input/MessageHook.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include <Windows.h>
#include <functional>

class MessageHook
{
public:
static void OnMessage(std::function<void(HWND, UINT, WPARAM, LPARAM)> callback);
};
28 changes: 28 additions & 0 deletions src/input/MouseHook.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <Windows.h>
#include <MinHook.h>

#include "MouseHook.h"

static bool s_disabled;
static BOOL(WINAPI* s_SetCursorPos)(int, int);

BOOL WINAPI SetCursorPosHook(int X, int Y)
{
if (s_disabled)
{
return TRUE;
}

return s_SetCursorPos(X, Y);
}

void MouseHook::Init()
{
MH_CreateHookApi(L"user32", "SetCursorPos", SetCursorPosHook, (void**)&s_SetCursorPos);
MH_EnableHook(MH_ALL_HOOKS);
}

void MouseHook::DisableCursorLock(bool disable)
{
s_disabled = disable;
}
8 changes: 8 additions & 0 deletions src/input/MouseHook.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

class MouseHook
{
public:
static void Init();
static void DisableCursorLock(bool disable);
};
52 changes: 47 additions & 5 deletions src/menu/Menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@
#include <imgui_impl_dx9.h>
#include <imgui_impl_win32.h>

#include <MinHook.h>

#include "Menu.h"
#include "render/RenderContext.h"
#include "input/MouseHook.h"
#include "input/Input.h"

#include "cdc/render/PCDeviceManager.h"

extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

Menu::Menu() : PCInternalResource()
{
ImGui::CreateContext();
Expand All @@ -15,7 +21,8 @@ Menu::Menu() : PCInternalResource()
ImGui_ImplWin32_Init(cdc::PCDeviceManager::s_pInstance->GetWindow());

// Add present callback to draw the UI
RenderContext::OnPresent([this] { OnPresent(); });
RenderContext::OnPresent(std::bind(&Menu::OnPresent, this));
MouseHook::Init();

OnConstruct();
}
Expand Down Expand Up @@ -44,13 +51,20 @@ void Menu::OnDestroyDevice()

void Menu::OnPresent()
{
// Set the mouse cursor visible when we have focus
ImGui::GetIO().MouseDrawCursor = m_focus;

// Start the frame
ImGui_ImplDX9_NewFrame();
ImGui_ImplWin32_NewFrame();

ImGui::NewFrame();

// Draw the menu
Draw();
if (m_focus)
{
Draw();
}

// End the frame
ImGui::EndFrame();
Expand All @@ -59,9 +73,37 @@ void Menu::OnPresent()
ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData());
}

void Menu::OnMessage(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if (msg == WM_KEYUP && wParam == VK_F8)
{
SetFocus(!m_focus);
}

if (m_focus)
{
ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam);
}
}

void Menu::Draw()
{
ImGui::Begin("Menu");
ImGui::Text("Hello, World!");
ImGui::End();
if (ImGui::BeginMainMenuBar())
{
ImGui::EndMainMenuBar();
}
}

void Menu::SetFocus(bool focus)
{
m_focus = focus;

// Disable the cursor lock and game input
MouseHook::DisableCursorLock(focus);
Input::DisableInput(focus);
}

bool Menu::HasFocus()
{
return m_focus;
}
7 changes: 7 additions & 0 deletions src/menu/Menu.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
#pragma once

#include <Windows.h>

#include "cdc/render/PCInternalResource.h"

class Menu : public cdc::PCInternalResource
{
private:
bool m_initialized = false;
bool m_focus = false;

public:
Menu();

void OnPresent();
void OnMessage(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

bool OnCreateDevice();
void OnDestroyDevice();

void SetFocus(bool focus);
bool HasFocus();

private:
void Draw();
};

0 comments on commit 0d085e4

Please sign in to comment.