Skip to content

Commit 96830cc

Browse files
committed
Handle in-game UI input handling in a different way
1 parent f31f65a commit 96830cc

File tree

1 file changed

+40
-16
lines changed

1 file changed

+40
-16
lines changed

Source/Managers/RenderManager.cpp

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,52 @@ void InitImGui() // Initializes ImGui, alongside the needed fonts.
7474
{
7575
CreateContext();
7676
ImGuiIO& io = GetIO();
77+
// TODO: Figure out how to disable gamepad input to the game when the interface is open, and also make sure gamepad input is consistent.
78+
// TODO: Find a way to add a gamepad hotkey to open the menu.
7779
io.ConfigFlags = ImGuiConfigFlags_NoMouseCursorChange | ImGuiConfigFlags_NavEnableGamepad | ImGuiConfigFlags_NavEnableKeyboard;
7880
LocalizationRm.InitFont(PlayerSettingsRm.INS.dpiScale / 100.0f * PlayerSettingsRm.INS.dpiScaleMultiplier);
7981

8082
ImGui_ImplWin32_Init(window);
8183
ImGui_ImplDX11_Init(pDevice, pContext);
8284
}
8385

84-
// Another function relating to imgui functionality.
86+
// TODO: Figure out why this isn't disabling input from gameplay.
8587
LRESULT __stdcall WndProc(const HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
8688
{
87-
if (true && ImGui_ImplWin32_WndProcHandler(hWnd, uMsg, wParam, lParam)) {
88-
return true;
89+
if (InitHook) {
90+
// Toggle UI visibility with key presses
91+
if (uMsg == WM_KEYUP) {
92+
if (wParam == VK_DELETE)
93+
PlayerSettingsRm.ShowEFUI = !PlayerSettingsRm.ShowEFUI;
94+
else if (wParam == VK_OEM_3)
95+
PlayerSettingsRm.ShowDevConsole = !PlayerSettingsRm.ShowDevConsole;
96+
}
97+
// If UI is active, block input from the game, but allow ImGui to handle it
98+
if (PlayerSettingsRm.ShowEFUI) {
99+
// Pass input to ImGui first
100+
if (ImGui_ImplWin32_WndProcHandler(hWnd, uMsg, wParam, lParam)) {
101+
return true; // ImGui handled the input, return early
102+
}
103+
// If ImGui didn't handle the input, block it from reaching the game
104+
switch (uMsg)
105+
{
106+
case WM_MOUSEMOVE:
107+
case WM_LBUTTONDOWN: case WM_LBUTTONUP:
108+
case WM_RBUTTONDOWN: case WM_RBUTTONUP:
109+
case WM_MBUTTONDOWN: case WM_MBUTTONUP:
110+
case WM_MOUSEWHEEL:
111+
case WM_KEYDOWN: case WM_KEYUP:
112+
case WM_CHAR:
113+
case WM_INPUT: // Block Raw Input if game uses DirectInput
114+
return 0; // Absorb the event (block the game)
115+
}
116+
}
89117
}
90-
return CallWindowProc(oWndProc, hWnd, uMsg, wParam, lParam);
118+
// If UI is not active, pass input to the original window procedure (game)
119+
if (oWndProc)
120+
return CallWindowProc(oWndProc, hWnd, uMsg, wParam, lParam);
121+
else
122+
return DefWindowProc(hWnd, uMsg, wParam, lParam); // Fallback if oWndProc is invalid
91123
}
92124

93125
// An easily callable function to make the CreateTexture2D hook a little cleaner.
@@ -360,8 +392,8 @@ namespace EnigmaFix {
360392
// A hook that contains the needed logic for adding the imgui interface, and forcing flip model presentation.
361393
HRESULT __stdcall RenderManager::hkPresent(IDXGISwapChain *pSwapChain, UINT SyncInterval, UINT Flags) // Here's what happens when the swapchain is ready to be presented.
362394
{
363-
if (!InitHook) { // Checks if the hook hasn't been initalized, and if not, does the needed deeds to hook ImGui.
364-
HRESULT hr = pSwapChain->GetDevice(__uuidof(ID3D11Device), (void**)&pDevice);
395+
if (!InitHook) { // Checks if the hook hasn't been initialized, and if not, does the needed deeds to hook ImGui.
396+
HRESULT hr = pSwapChain->GetDevice(__uuidof(ID3D11Device), reinterpret_cast<void**>(&pDevice));
365397
if (SUCCEEDED(hr))
366398
{
367399
pDevice->GetImmediateContext(&pContext);
@@ -371,27 +403,19 @@ namespace EnigmaFix {
371403
PlayerSettingsRm.INS.dpiScale = Util::GetDPIScaleForWindow(window) * 100.0f;
372404
spdlog::info("RenderManager: Current DPI Scale is {}%.", PlayerSettingsRm.INS.dpiScale);
373405
ID3D11Texture2D* pBackBuffer;
374-
hr = pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);
406+
hr = pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast<LPVOID*>(&pBackBuffer));
375407
if (SUCCEEDED(hr)) {
376408
pDevice->CreateRenderTargetView(pBackBuffer, NULL, &mainRenderTargetView);
377409
pBackBuffer->Release();
378410
}
379411
else { throw std::runtime_error("Failed to get swapchain back buffer."); } // Throw a standard C++ exception with an error message.
380-
oWndProc = (WNDPROC)SetWindowLongPtr(window, GWLP_WNDPROC, (LONG_PTR)WndProc);
412+
oWndProc = reinterpret_cast<WNDPROC>(SetWindowLongPtr(window, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(WndProc)));
381413
InitImGui();
382414
InitHook = true;
383415
}
384416
else { throw std::runtime_error("Failed to get swapchain device."); }
385417
}
386418

387-
if (GetAsyncKeyState(VK_DELETE) & 1) { // Checks if "DEL" key was pressed, and toggles the boolean that shows the UI.
388-
PlayerSettingsRm.ShowEFUI = !PlayerSettingsRm.ShowEFUI;
389-
}
390-
391-
if (GetAsyncKeyState(VK_OEM_3) & 1) { // Checks if "~/`/Tilde" key was pressed, and toggles the developer console UI.
392-
PlayerSettingsRm.ShowDevConsole = !PlayerSettingsRm.ShowDevConsole;
393-
}
394-
395419
// TODO: Implement developer console and find a way to pipe SpdLog and standard logging to it.
396420
PlayerSettingsRm.ShowUI = PlayerSettingsRm.ShowEFUI || PlayerSettingsRm.ShowDevConsole; // Checks if either EFUI or the dev console are enabled, and if so, enable the showUI flag.
397421

0 commit comments

Comments
 (0)