From d6eeec0669aef43cb573821904611503c25a3d09 Mon Sep 17 00:00:00 2001 From: js6pak Date: Sun, 3 Jan 2021 19:53:09 +0100 Subject: [PATCH] Make imgui Window use GUILayout --- Reactor.Debugger/DebuggerPlugin.cs | 2 +- Reactor/Extensions/GUIExtensions.cs | 14 ++++++- Reactor/Unstrip/Patches.cs | 57 ++++++++++++++++++++++++++++- 3 files changed, 70 insertions(+), 3 deletions(-) diff --git a/Reactor.Debugger/DebuggerPlugin.cs b/Reactor.Debugger/DebuggerPlugin.cs index ad2e401..898c610 100644 --- a/Reactor.Debugger/DebuggerPlugin.cs +++ b/Reactor.Debugger/DebuggerPlugin.cs @@ -43,7 +43,7 @@ namespace Reactor.Debugger public DebuggerComponent(IntPtr ptr) : base(ptr) { - TestWindow = new DragWindow(new Rect(20, 20, 120, 120), "Debugger", () => + TestWindow = new DragWindow(new Rect(20, 20, 0, 0), "Debugger", () => { GUILayout.Label("Name: " + SaveManager.PlayerName, new Il2CppReferenceArray(0)); DisableGameEnd = GUILayout.Toggle(DisableGameEnd, "Disable game end", new Il2CppReferenceArray(0)); diff --git a/Reactor/Extensions/GUIExtensions.cs b/Reactor/Extensions/GUIExtensions.cs index 4b24c3d..cdc2340 100644 --- a/Reactor/Extensions/GUIExtensions.cs +++ b/Reactor/Extensions/GUIExtensions.cs @@ -1,4 +1,5 @@ using System; +using UnhollowerBaseLib; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; @@ -29,6 +30,16 @@ namespace Reactor.Extensions return rect; } + /// + /// Reset Rect size + /// + public static Rect ResetSize(this Rect rect) + { + rect.width = rect.height = 0; + + return rect; + } + /// /// Create from /// @@ -103,7 +114,8 @@ namespace Reactor.Extensions { if (Enabled) { - Rect = GUI.Window(Id, Rect, Func, Title); + Rect = Rect.ResetSize(); + Rect = GUILayout.Window(Id, Rect, Func, Title, new Il2CppReferenceArray(0)); } } } diff --git a/Reactor/Unstrip/Patches.cs b/Reactor/Unstrip/Patches.cs index 03ada82..08488ae 100644 --- a/Reactor/Unstrip/Patches.cs +++ b/Reactor/Unstrip/Patches.cs @@ -13,13 +13,68 @@ namespace Reactor.Unstrip [HarmonyPatch(typeof(GUIUtility), nameof(GUIUtility.HasKeyFocus))] public static class HasKeyFocusPatch { - public static bool Prefix(int controlID, ref bool __result) + public static bool Prefix(int controlID, out bool __result) { __result = controlID == GUIUtility.keyboardControl; return false; } } + [HarmonyPatch(typeof(GUILayout), nameof(GUILayout.DoWindow), typeof(int), typeof(Rect), typeof(GUI.WindowFunction), typeof(GUIContent), typeof(GUIStyle), typeof(Il2CppReferenceArray))] + public static class DoWindowPatch + { + private sealed class LayoutedWindow + { + internal LayoutedWindow(GUI.WindowFunction f, Rect screenRect, GUIContent content, GUILayoutOption[] options, GUIStyle style) + { + this.m_Func = f; + this.m_ScreenRect = screenRect; + this.m_Options = options; + this.m_Style = style; + } + + public void DoWindow(int windowID) + { + var topLevel = GUILayoutUtility.current.topLevel; + var type = Event.current.type; + if (type != EventType.Layout) + { + topLevel.ResetCursor(); + } + else + { + topLevel.resetCoords = true; + topLevel.rect = this.m_ScreenRect; + var flag = this.m_Options != null; + if (flag) + { + topLevel.ApplyOptions(this.m_Options); + } + + topLevel.isWindow = true; + topLevel.windowID = windowID; + topLevel.style = this.m_Style; + } + + this.m_Func.Invoke(windowID); + } + + private readonly GUI.WindowFunction m_Func; + private readonly Rect m_ScreenRect; + private readonly GUILayoutOption[] m_Options; + private readonly GUIStyle m_Style; + } + + public static bool Prefix(int id, Rect screenRect, GUI.WindowFunction func, GUIContent content, GUIStyle style, Il2CppReferenceArray options, out Rect __result) + { + GUIUtility.CheckOnGUI(); + var @object = new LayoutedWindow(func, screenRect, content, options, style); + __result = GUI.Window(id, screenRect, (GUI.WindowFunction) @object.DoWindow, content, style); + + return false; + } + } + [HarmonyPatch(typeof(GUILayoutGroup), nameof(GUILayoutGroup.Add))] public static class AddPatch {