|
| 1 | +using System.IO; |
| 2 | +using System.Text; |
| 3 | +using TMPro; |
| 4 | +using UnityEngine; |
| 5 | +using UnityEngine.UI; |
| 6 | + |
| 7 | +namespace HUDReplacer.UI; |
| 8 | + |
| 9 | +internal class DebugWindow : MonoBehaviour |
| 10 | +{ |
| 11 | + private const string WindowTitle = "HUDReplacer"; |
| 12 | + |
| 13 | + private static GameObject _prefab; |
| 14 | + private static DebugWindow _instance; |
| 15 | + |
| 16 | + internal static DebugWindow Instance => _instance; |
| 17 | + |
| 18 | + internal static void Toggle() |
| 19 | + { |
| 20 | + if (_instance == null) |
| 21 | + { |
| 22 | + if (_prefab == null) |
| 23 | + _prefab = BuildPrefab(); |
| 24 | + |
| 25 | + var go = Object.Instantiate(_prefab, MainCanvasUtil.MainCanvas.transform); |
| 26 | + _instance = go.GetComponent<DebugWindow>(); |
| 27 | + go.SetActive(true); |
| 28 | + } |
| 29 | + else |
| 30 | + { |
| 31 | + _instance.gameObject.SetActive(!_instance.gameObject.activeSelf); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + private static GameObject BuildPrefab() |
| 36 | + { |
| 37 | + // Root window |
| 38 | + var windowGo = UIBuilder.CreateWindow( |
| 39 | + null, |
| 40 | + "HUDReplacerDebugWindow", |
| 41 | + new Vector2(100, -100), |
| 42 | + new Vector2(400, 300) |
| 43 | + ); |
| 44 | + |
| 45 | + // Let the window grow vertically to fit content |
| 46 | + var windowFitter = windowGo.AddOrGetComponent<ContentSizeFitter>(); |
| 47 | + windowFitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize; |
| 48 | + |
| 49 | + // Title bar with drag support |
| 50 | + var titleBarGo = UIBuilder.CreateTitleBar(windowGo.transform, WindowTitle); |
| 51 | + |
| 52 | + // Close button |
| 53 | + var closeGo = UIBuilder.CreateButton(titleBarGo.transform, "X", fontSize: 12); |
| 54 | + var closeLE = closeGo.AddOrGetComponent<LayoutElement>(); |
| 55 | + closeLE.preferredWidth = 24; |
| 56 | + closeLE.preferredHeight = 24; |
| 57 | + closeLE.flexibleWidth = 0; |
| 58 | + |
| 59 | + // Content area |
| 60 | + var contentGo = UIBuilder.CreateVerticalLayout(windowGo.transform, "Content"); |
| 61 | + var contentLE = contentGo.AddComponent<LayoutElement>(); |
| 62 | + contentLE.flexibleHeight = 1; |
| 63 | + contentLE.flexibleWidth = 1; |
| 64 | + |
| 65 | + // Toggle Debug button |
| 66 | + var toggleRow = CreateButtonRow(contentGo.transform); |
| 67 | + var toggleGo = ToggleDebugButton.Create(toggleRow.transform); |
| 68 | + toggleGo.AddOrGetComponent<LayoutElement>().flexibleWidth = 1; |
| 69 | + UIBuilder.CreateHelpButton( |
| 70 | + toggleRow.transform, |
| 71 | + "Enable debug keyboard shortcuts:\n" |
| 72 | + + "D - Dump textures under cursor\n" |
| 73 | + + "E - Dump all loaded textures\n" |
| 74 | + + "Q - Reload textures" |
| 75 | + ); |
| 76 | + |
| 77 | + // Reload Textures button |
| 78 | + var reloadRow = CreateButtonRow(contentGo.transform); |
| 79 | + var reloadGo = ReloadTexturesButton.Create(reloadRow.transform); |
| 80 | + reloadGo.AddOrGetComponent<LayoutElement>().flexibleWidth = 1; |
| 81 | + UIBuilder.CreateHelpButton( |
| 82 | + reloadRow.transform, |
| 83 | + "Reload all replacement textures and re-apply them." |
| 84 | + ); |
| 85 | + |
| 86 | + // Dump Textures button |
| 87 | + var dumpRow = CreateButtonRow(contentGo.transform); |
| 88 | + var dumpTexturesGo = UIBuilder.CreateButton(dumpRow.transform, "Dump Loaded Textures"); |
| 89 | + dumpTexturesGo.AddOrGetComponent<LayoutElement>().flexibleWidth = 1; |
| 90 | + var dumpTexturesBtn = dumpTexturesGo.AddComponent<DumpTexturesButton>(); |
| 91 | + dumpTexturesBtn.button = dumpTexturesGo.GetComponent<Button>(); |
| 92 | + UIBuilder.CreateHelpButton( |
| 93 | + dumpRow.transform, |
| 94 | + "Dump the names and sizes of all loaded textures to KSP.log." |
| 95 | + ); |
| 96 | + |
| 97 | + // Dump Skin Textures button |
| 98 | + var skinRow = CreateButtonRow(contentGo.transform); |
| 99 | + var skinTexturesGo = UIBuilder.CreateButton(skinRow.transform, "Dump IMGUI Skin Textures"); |
| 100 | + skinTexturesGo.AddOrGetComponent<LayoutElement>().flexibleWidth = 1; |
| 101 | + var skinTexturesBtn = skinTexturesGo.AddComponent<DumpSkinTexturesButton>(); |
| 102 | + skinTexturesBtn.button = skinTexturesGo.GetComponent<Button>(); |
| 103 | + UIBuilder.CreateHelpButton( |
| 104 | + skinRow.transform, |
| 105 | + "Dump names and sizes of all textures used by the KSP IMGUI skin.\nOverride these to style <i>all</i> IMGUI UIs created by mods or KSP." |
| 106 | + ); |
| 107 | + |
| 108 | + // Textures under cursor panel |
| 109 | + TexturesUnderCursor.Create(contentGo.transform); |
| 110 | + |
| 111 | + // Attach the DebugWindow component |
| 112 | + windowGo.AddComponent<DebugWindow>(); |
| 113 | + |
| 114 | + // Wire close button to destroy the window |
| 115 | + var closeButton = closeGo.AddComponent<CloseButton>(); |
| 116 | + closeButton.button = closeGo.GetComponent<Button>(); |
| 117 | + closeButton.target = windowGo; |
| 118 | + |
| 119 | + return windowGo; |
| 120 | + } |
| 121 | + |
| 122 | + private static GameObject CreateButtonRow(Transform parent) |
| 123 | + { |
| 124 | + var row = UIBuilder.CreateHorizontalLayout( |
| 125 | + parent, |
| 126 | + "ButtonRow", |
| 127 | + padding: new RectOffset(0, 0, 0, 0), |
| 128 | + spacing: 4 |
| 129 | + ); |
| 130 | + var layout = row.GetComponent<HorizontalLayoutGroup>(); |
| 131 | + layout.childForceExpandHeight = false; |
| 132 | + layout.childAlignment = TextAnchor.MiddleLeft; |
| 133 | + return row; |
| 134 | + } |
| 135 | + |
| 136 | + internal static void DumpWindow() |
| 137 | + { |
| 138 | + if (_instance == null) |
| 139 | + { |
| 140 | + Debug.Log("HUDReplacer: No active debug window to dump."); |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + var sb = new StringBuilder(); |
| 145 | + DumpGameObject(sb, _instance.gameObject, 0); |
| 146 | + |
| 147 | + var path = Path.Combine(KSPUtil.ApplicationRootPath, "Logs/HUDReplacer"); |
| 148 | + Directory.CreateDirectory(path); |
| 149 | + File.WriteAllText(Path.Combine(path, "DebugWindow.txt"), sb.ToString()); |
| 150 | + Debug.Log("HUDReplacer: Debug window dump written to Logs/HUDReplacer/DebugWindow.txt"); |
| 151 | + } |
| 152 | + |
| 153 | + internal static void DumpPopupDialogBase() |
| 154 | + { |
| 155 | + var prefab = PopupDialogController.Instance?.popupDialogBase; |
| 156 | + if (prefab == null) |
| 157 | + { |
| 158 | + Debug.Log("HUDReplacer: PopupDialogController not available."); |
| 159 | + return; |
| 160 | + } |
| 161 | + |
| 162 | + var sb = new StringBuilder(); |
| 163 | + DumpGameObject(sb, prefab.gameObject, 0); |
| 164 | + |
| 165 | + var path = Path.Combine(KSPUtil.ApplicationRootPath, "Logs/HUDReplacer"); |
| 166 | + Directory.CreateDirectory(path); |
| 167 | + File.WriteAllText(Path.Combine(path, "PopupDialogBase.txt"), sb.ToString()); |
| 168 | + Debug.Log( |
| 169 | + "HUDReplacer: PopupDialogBase dump written to Logs/HUDReplacer/PopupDialogBase.txt" |
| 170 | + ); |
| 171 | + } |
| 172 | + |
| 173 | + internal static void DumpPrefabs() |
| 174 | + { |
| 175 | + var sb = new StringBuilder(); |
| 176 | + |
| 177 | + foreach (var prefab in UISkinManager.fetch.prefabs) |
| 178 | + { |
| 179 | + if (prefab == null) |
| 180 | + continue; |
| 181 | + DumpGameObject(sb, prefab, 0); |
| 182 | + sb.AppendLine(); |
| 183 | + } |
| 184 | + |
| 185 | + var path = Path.Combine(KSPUtil.ApplicationRootPath, "Logs/HUDReplacer"); |
| 186 | + Directory.CreateDirectory(path); |
| 187 | + File.WriteAllText(Path.Combine(path, "Prefabs.txt"), sb.ToString()); |
| 188 | + Debug.Log($"HUDReplacer: Prefab dump written to Logs/HUDReplacer/Prefabs.txt"); |
| 189 | + } |
| 190 | + |
| 191 | + private static void DumpGameObject(StringBuilder sb, GameObject go, int depth) |
| 192 | + { |
| 193 | + var indent = new string(' ', depth * 2); |
| 194 | + var rect = go.GetComponent<RectTransform>(); |
| 195 | + var rectInfo = ""; |
| 196 | + if (rect != null) |
| 197 | + { |
| 198 | + var sd = rect.sizeDelta; |
| 199 | + var amin = rect.anchorMin; |
| 200 | + var amax = rect.anchorMax; |
| 201 | + var piv = rect.pivot; |
| 202 | + var ap = rect.anchoredPosition; |
| 203 | + rectInfo = |
| 204 | + $" [size={sd.x}x{sd.y} anchor=({amin.x},{amin.y})-({amax.x},{amax.y}) pivot=({piv.x},{piv.y}) pos=({ap.x},{ap.y})]"; |
| 205 | + } |
| 206 | + |
| 207 | + sb.AppendLine($"{indent}{go.name} (active={go.activeSelf}){rectInfo}"); |
| 208 | + |
| 209 | + foreach (var comp in go.GetComponents<Component>()) |
| 210 | + { |
| 211 | + if (comp == null) |
| 212 | + continue; |
| 213 | + if (comp is Transform or RectTransform) |
| 214 | + continue; |
| 215 | + var compInfo = ""; |
| 216 | + if (comp is TextMeshProUGUI tmp) |
| 217 | + compInfo = |
| 218 | + $" alignment={tmp.alignment} overflowMode={tmp.overflowMode} enableWordWrapping={tmp.enableWordWrapping}"; |
| 219 | + sb.AppendLine($"{indent} + {comp.GetType().Name}{compInfo}"); |
| 220 | + } |
| 221 | + |
| 222 | + for (int i = 0; i < go.transform.childCount; i++) |
| 223 | + DumpGameObject(sb, go.transform.GetChild(i).gameObject, depth + 1); |
| 224 | + } |
| 225 | +} |
0 commit comments