From 606e2e0b9f93d8ad68c68474957b47c3657f2690 Mon Sep 17 00:00:00 2001 From: pas2704 Date: Sun, 24 Nov 2024 02:28:22 -0500 Subject: [PATCH] Fix input handling Keyboard input is blocked when a menu is receiving text input Mouse inputs are blocked when the mouse is visible and hovering over an imgui item --- CringeLauncher/ImGuiHandler.cs | 11 +++++++++- CringeLauncher/Patches/InputPatch.cs | 33 ++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 CringeLauncher/Patches/InputPatch.cs diff --git a/CringeLauncher/ImGuiHandler.cs b/CringeLauncher/ImGuiHandler.cs index 9590dcf..fbca287 100644 --- a/CringeLauncher/ImGuiHandler.cs +++ b/CringeLauncher/ImGuiHandler.cs @@ -18,6 +18,11 @@ internal class ImGuiHandler : IDisposable private DeviceContext? _deviceContext; private static nint _wndproc; + public bool BlockMouse { get; private set; } + public bool BlockKeys { get; private set; } + public bool DrawMouse { get; private set; } + + public static ImGuiHandler? Instance; public static RenderTargetView? Rtv; @@ -65,7 +70,11 @@ internal class ImGuiHandler : IDisposable ImGui_ImplDX11_NewFrame(); ImGui_ImplWin32_NewFrame(); NewFrame(); - + + var io = GetIO(); + BlockMouse = io.WantCaptureMouse; + BlockKeys = io.WantTextInput; + DrawMouse = io.MouseDrawCursor; _renderHandler.OnFrame(); Render(); diff --git a/CringeLauncher/Patches/InputPatch.cs b/CringeLauncher/Patches/InputPatch.cs new file mode 100644 index 0000000..934c0b3 --- /dev/null +++ b/CringeLauncher/Patches/InputPatch.cs @@ -0,0 +1,33 @@ +using HarmonyLib; +using VRage; +using VRage.Input; +using VRage.Input.Keyboard; +using VRage.Platform.Windows.Input; + +namespace CringeLauncher.Patches; + +[HarmonyPatch] +internal static class InputPatch +{ + [HarmonyPrefix, HarmonyPatch(typeof(MyDirectInput), nameof(MyDirectInput.GetMouseState))] + private static bool GetMouseStatePrefix(ref MyMouseState state) + { + if (ImGuiHandler.Instance?.BlockMouse == true && (MyVRage.Platform.Input.ShowCursor || ImGuiHandler.Instance.DrawMouse)) + { + state = default; + return false; + } + return true; + } + + [HarmonyPrefix, HarmonyPatch("VRage.Input.Keyboard.MyGuiLocalizedKeyboardState", "GetCurrentState")] + private static bool GetKeyboardStatePrefix(ref MyKeyboardState __result) + { + if (ImGuiHandler.Instance?.BlockKeys == true) + { + __result = default; + return false; + } + return true; + } +}