|
| 1 | +#if !DUMMY |
| 2 | +using HarmonyLib; |
| 3 | +using System.Collections; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Reflection; |
| 6 | +using UnityEngine; |
| 7 | +using UnityEngine.EventSystems; |
| 8 | +using UnityEngine.UI; |
| 9 | + |
| 10 | +// CTB's existing InputLockManager / EditorLogic-based locks block IMGUI-on-IMGUI and |
| 11 | +// IMGUI-on-KSP-input click-through, but they don't reliably block clicks from reaching |
| 12 | +// Unity's uGUI EventSystem or the stock Part Action Window. These Harmony patches |
| 13 | +// fill that gap by tracking every IMGUI window drawn this frame and short-circuiting |
| 14 | +// the click-dispatch paths when the cursor is over any of them. Windows that haven't |
| 15 | +// adopted CTB (MechJeb etc., which use plain GUILayout.Window) are protected too. |
| 16 | + |
| 17 | +namespace ClickThroughFix |
| 18 | +{ |
| 19 | + [KSPAddon(KSPAddon.Startup.Instantly, true)] |
| 20 | + internal class CTBHarmonyLoader : MonoBehaviour |
| 21 | + { |
| 22 | + private const string HarmonyId = "ClickThroughFix.CTB"; |
| 23 | + private const string UniversalLockId = "CTB_universal_IMGUI"; |
| 24 | + private static bool _patched; |
| 25 | + private bool _flightLocked; |
| 26 | + private bool _editorLocked; |
| 27 | + |
| 28 | + internal void Awake() |
| 29 | + { |
| 30 | + DontDestroyOnLoad(this); |
| 31 | + if (_patched) return; |
| 32 | + _patched = true; |
| 33 | + try |
| 34 | + { |
| 35 | + new Harmony(HarmonyId).PatchAll(typeof(CTBHarmonyLoader).Assembly); |
| 36 | + } |
| 37 | + catch (System.Exception e) |
| 38 | + { |
| 39 | + Log.Error("CTB Harmony patching failed: " + e); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + // KSP's editor part-pick (single + double click to move) and various flight |
| 44 | + // controls poll Input.GetMouseButton directly and honor InputLockManager / |
| 45 | + // EditorLogic locks. uGUI raycaster suppression doesn't reach those paths. |
| 46 | + // Set a universal lock whenever the cursor is over any tracked IMGUI window |
| 47 | + // so a click meant for the mod window doesn't also grab a part in the editor. |
| 48 | + internal void Update() |
| 49 | + { |
| 50 | + // Sync the static enable flag from the per-save CTB settings each frame so |
| 51 | + // toggling the option in the stock settings UI takes effect immediately. |
| 52 | + if (HighLogic.CurrentGame != null) |
| 53 | + ClearInputLocks.universalClickBlocking = |
| 54 | + HighLogic.CurrentGame.Parameters.CustomParams<CTB>().universalClickBlocking; |
| 55 | + |
| 56 | + bool over = ClearInputLocks.universalClickBlocking && IMGUIWindowTracker.MouseOverAnyWindow(); |
| 57 | + bool wantEditor = over && HighLogic.LoadedSceneIsEditor && EditorLogic.fetch != null; |
| 58 | + bool wantFlight = over && (HighLogic.LoadedSceneIsFlight || HighLogic.LoadedSceneHasPlanetarium); |
| 59 | + |
| 60 | + if (wantEditor != _editorLocked) |
| 61 | + { |
| 62 | + if (wantEditor) EditorLogic.fetch.Lock(true, true, true, UniversalLockId); |
| 63 | + else if (EditorLogic.fetch != null) EditorLogic.fetch.Unlock(UniversalLockId); |
| 64 | + _editorLocked = wantEditor; |
| 65 | + } |
| 66 | + if (wantFlight != _flightLocked) |
| 67 | + { |
| 68 | + if (wantFlight) InputLockManager.SetControlLock(ControlTypes.ALLBUTCAMERAS, UniversalLockId); |
| 69 | + else InputLockManager.RemoveControlLock(UniversalLockId); |
| 70 | + _flightLocked = wantFlight; |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // Records the screen-space rect of every IMGUI window drawn this frame. |
| 76 | + // Accumulates within a Unity frame across all OnGUI iterations (Layout, Repaint, |
| 77 | + // MouseDown, …) — Unity dispatches MouseDown only to the focused window's body, |
| 78 | + // so per-iter clearing would lose the rest of the visible windows. Swap to |
| 79 | + // _lastRects at each new Unity frame so EventSystem.RaycastAll (running in |
| 80 | + // Update before this frame's OnGUI) sees the previous frame's full set. |
| 81 | + internal static class IMGUIWindowTracker |
| 82 | + { |
| 83 | + private static List<Rect> _curRects = new List<Rect>(); |
| 84 | + private static List<Rect> _lastRects = new List<Rect>(); |
| 85 | + private static int _frame = -1; |
| 86 | + |
| 87 | + // Transform the returned window rect through the current GUI.matrix so we store |
| 88 | + // actual screen-pixel bounds. KSP applies a UI_SCALE matrix that some mods (KAC) |
| 89 | + // live inside; others (MechJeb) override GUI.matrix and draw in screen coords |
| 90 | + // directly. By transforming through the matrix in effect at return time, the |
| 91 | + // stored rect is always in screen-space and Contains(Input.mousePosition) works |
| 92 | + // for both kinds of mods. |
| 93 | + public static void RecordScreenSpace(Rect r) |
| 94 | + { |
| 95 | + if (!ClearInputLocks.universalClickBlocking) return; |
| 96 | + var m = GUI.matrix; |
| 97 | + Vector3 tl = m.MultiplyPoint3x4(new Vector3(r.xMin, r.yMin, 0f)); |
| 98 | + Vector3 br = m.MultiplyPoint3x4(new Vector3(r.xMax, r.yMax, 0f)); |
| 99 | + EnsureFrame(); |
| 100 | + _curRects.Add(Rect.MinMaxRect( |
| 101 | + Mathf.Min(tl.x, br.x), |
| 102 | + Mathf.Min(tl.y, br.y), |
| 103 | + Mathf.Max(tl.x, br.x), |
| 104 | + Mathf.Max(tl.y, br.y))); |
| 105 | + } |
| 106 | + |
| 107 | + public static bool MouseOverAnyWindow() |
| 108 | + { |
| 109 | + if (!ClearInputLocks.universalClickBlocking) return false; |
| 110 | + EnsureFrame(); |
| 111 | + Vector2 mp = Input.mousePosition; |
| 112 | + mp.y = Screen.height - mp.y; |
| 113 | + for (int i = 0; i < _curRects.Count; i++) |
| 114 | + if (_curRects[i].Contains(mp)) return true; |
| 115 | + for (int i = 0; i < _lastRects.Count; i++) |
| 116 | + if (_lastRects[i].Contains(mp)) return true; |
| 117 | + return false; |
| 118 | + } |
| 119 | + |
| 120 | + private static void EnsureFrame() |
| 121 | + { |
| 122 | + int frame = Time.frameCount; |
| 123 | + if (frame == _frame) return; |
| 124 | + var tmp = _lastRects; |
| 125 | + _lastRects = _curRects; |
| 126 | + _curRects = tmp; |
| 127 | + _curRects.Clear(); |
| 128 | + _frame = frame; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + // Hook every GUI.Window / GUILayout.Window overload so we catch every IMGUI |
| 133 | + // window regardless of which entry point the mod used. Double-records for |
| 134 | + // GUILayout-routed windows (which internally call GUI.Window) are harmless — |
| 135 | + // MouseOverAnyWindow only checks Contains. |
| 136 | + [HarmonyPatch] |
| 137 | + internal class CTB_TrackIMGUIWindows |
| 138 | + { |
| 139 | + [HarmonyTargetMethods] |
| 140 | + internal static IEnumerable<MethodBase> Targets() |
| 141 | + { |
| 142 | + foreach (var t in new[] { typeof(GUI), typeof(GUILayout) }) |
| 143 | + foreach (var m in t.GetMethods(BindingFlags.Public | BindingFlags.Static)) |
| 144 | + if (m.Name == "Window") yield return m; |
| 145 | + } |
| 146 | + |
| 147 | + [HarmonyPostfix] |
| 148 | + internal static void Postfix(Rect __result) => IMGUIWindowTracker.RecordScreenSpace(__result); |
| 149 | + } |
| 150 | + |
| 151 | + // Suppress a uGUI raycaster's hits when the cursor is over a tracked IMGUI mod |
| 152 | + // window. Stock KSP uGUI canvases overlap each other constantly (MainCanvas, |
| 153 | + // AppCanvas, Editor are all near-full-screen), so any further "is another canvas |
| 154 | + // covering us" heuristic just blocks legitimate stock UI clicks. |
| 155 | + [HarmonyPatch(typeof(GraphicRaycaster))] |
| 156 | + internal class CTB_GraphicRaycaster_Raycast |
| 157 | + { |
| 158 | + [HarmonyPrefix] |
| 159 | + [HarmonyPatch("Raycast")] |
| 160 | + [HarmonyPatch(new[] { typeof(PointerEventData), typeof(List<RaycastResult>) })] |
| 161 | + internal static bool Prefix() |
| 162 | + { |
| 163 | + return !ClickThruBlocker.MouseOverAnyWindow(); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + // Right-click on a part runs UIPartActionController.MouseClickCoroutine which opens |
| 168 | + // or closes the PAW. Bail when the cursor is over a registered CTB window so a |
| 169 | + // right-click meant for the mod window doesn't also pop up the PAW behind it. |
| 170 | + [HarmonyPatch(typeof(UIPartActionController))] |
| 171 | + internal class CTB_UIPartActionController_MouseClickCoroutine |
| 172 | + { |
| 173 | + [HarmonyPrefix] |
| 174 | + [HarmonyPatch("MouseClickCoroutine")] |
| 175 | + internal static bool Prefix(ref IEnumerator __result) |
| 176 | + { |
| 177 | + if (ClickThruBlocker.MouseOverAnyWindow()) |
| 178 | + { |
| 179 | + __result = NoOp(); |
| 180 | + return false; |
| 181 | + } |
| 182 | + return true; |
| 183 | + } |
| 184 | + |
| 185 | + private static IEnumerator NoOp() { yield break; } |
| 186 | + } |
| 187 | +} |
| 188 | +#endif |
0 commit comments