From 78364c4d7b1298c8873bf33dcd8e580413fbbb20 Mon Sep 17 00:00:00 2001 From: NathanKell Date: Sun, 3 Jul 2022 20:47:56 -0700 Subject: [PATCH] Allow app launcher to collapse/expand, and merely collapse rather than hide it in SC UIs like MC/RnD/etc. Remaining wrinkle is that the Contract and Messages icons lose their white overlays. --- GameData/KSPCommunityFixes/Settings.cfg | 5 + KSPCommunityFixes/KSPCommunityFixes.csproj | 1 + .../QoL/AppLauncherCollapseShow.cs | 223 ++++++++++++++++++ 3 files changed, 229 insertions(+) create mode 100644 KSPCommunityFixes/QoL/AppLauncherCollapseShow.cs diff --git a/GameData/KSPCommunityFixes/Settings.cfg b/GameData/KSPCommunityFixes/Settings.cfg index 44214b0..4ce9c24 100644 --- a/GameData/KSPCommunityFixes/Settings.cfg +++ b/GameData/KSPCommunityFixes/Settings.cfg @@ -169,6 +169,11 @@ KSP_COMMUNITY_FIXES // upon creating a new career game. Disabled by default. DisableNewGameIntro = false + // Allows the AppLauncher (stock toolbar) to be collapsed and expanded via a button + // prior to the Messages app icon. This button is shown even in Space Center UIs + // like Mission Control, so it can be accessed there too. + AppLauncherCollapseShow = true + // ########################## // Performance tweaks // ########################## diff --git a/KSPCommunityFixes/KSPCommunityFixes.csproj b/KSPCommunityFixes/KSPCommunityFixes.csproj index c75a560..9430762 100644 --- a/KSPCommunityFixes/KSPCommunityFixes.csproj +++ b/KSPCommunityFixes/KSPCommunityFixes.csproj @@ -121,6 +121,7 @@ + diff --git a/KSPCommunityFixes/QoL/AppLauncherCollapseShow.cs b/KSPCommunityFixes/QoL/AppLauncherCollapseShow.cs new file mode 100644 index 0000000..5f571dd --- /dev/null +++ b/KSPCommunityFixes/QoL/AppLauncherCollapseShow.cs @@ -0,0 +1,223 @@ +using HarmonyLib; +using System; +using System.Collections.Generic; +using KSP.UI.Screens; +using UnityEngine; +using UniLinq; +using UnityEngine.UI; + +namespace KSPCommunityFixes.QoL +{ + class AppLauncherCollapseShow : BasePatch + { + protected override Version VersionMin => new Version(1, 8, 0); + + protected override void ApplyPatches(List patches) + { + patches.Add(new PatchInfo( + PatchMethodType.Postfix, + AccessTools.Method(typeof(ApplicationLauncher), nameof(ApplicationLauncher.SpawnSimpleLayout)), + this)); + + patches.Add(new PatchInfo( + PatchMethodType.Postfix, + AccessTools.Method(typeof(ApplicationLauncher), nameof(ApplicationLauncher.StartupSequence)), + this)); + + patches.Add(new PatchInfo( + PatchMethodType.Prefix, + AccessTools.Method(typeof(ApplicationLauncher), nameof(ApplicationLauncher.Show)), + this)); + + patches.Add(new PatchInfo( + PatchMethodType.Prefix, + AccessTools.Method(typeof(ApplicationLauncher), nameof(ApplicationLauncher.Hide)), + this)); + + patches.Add(new PatchInfo( + PatchMethodType.Prefix, + AccessTools.Method(typeof(ApplicationLauncher), nameof(ApplicationLauncher.ShouldItShow)), + this)); + + patches.Add(new PatchInfo( + PatchMethodType.Prefix, + AccessTools.Method(typeof(ApplicationLauncher), nameof(ApplicationLauncher.ShouldItHide)), + this)); + + patches.Add(new PatchInfo( + PatchMethodType.Prefix, + AccessTools.Method(typeof(ApplicationLauncher), nameof(ApplicationLauncher.ShouldItHide), new Type[] { typeof(GameEvents.VesselSpawnInfo) }), + this, + "ApplicationLauncher_ShouldItHideVesselSpawn_Prefix")); + } + static bool ApplicationLauncher_ShouldItShow_Prefix(ApplicationLauncher __instance) + { + if (__instance.ShouldBeVisible()) + { + Canvas canvas = ApplicationLauncher.Instance.GetComponent(); + canvas.sortingLayerName = "Default"; + canvas.sortingOrder = 0; + canvas.overrideSorting = false; + __instance.Show(); + } + return false; + } + + static bool ApplicationLauncher_ShouldItHide_Prefix(ApplicationLauncher __instance) + { + if (__instance.ShouldBeVisible()) + { + Canvas canvas = ApplicationLauncher.Instance.GetComponent(); + canvas.overrideSorting = true; + canvas.sortingLayerName = "Actions"; + canvas.sortingOrder = 20; + __instance.Hide(); + } + return false; + } + + static bool ApplicationLauncher_ShouldItHideVesselSpawn_Prefix(ApplicationLauncher __instance) + { + if (__instance.ShouldBeVisible()) + { + Canvas canvas = ApplicationLauncher.Instance.GetComponent(); + canvas.overrideSorting = true; + canvas.sortingLayerName = "Actions"; + canvas.sortingOrder = 20; + __instance.Hide(); + } + return false; + } + + static bool ApplicationLauncher_Show_Prefix(ApplicationLauncher __instance) + { + if (!__instance.launcherSpace.gameObject.activeSelf) + __instance.launcherSpace.gameObject.SetActive(true); + + ExpandAppLauncher(); + return false; + } + + static bool ApplicationLauncher_Hide_Prefix(ApplicationLauncher __instance) + { + if (__instance.ShouldBeVisible()) + { + CollapseAppLauncher(); + return false; + } + + __instance.launcherSpace.gameObject.SetActive(false); + __instance.onHide(); + ApplicationLauncher.Ready = false; + return false; + } + + static void ApplicationLauncher_SpawnSimpleLayout_Postfix(ApplicationLauncher __instance) + { + List allChilds = __instance.launcherSpace.GetComponentsInChildren(true).ToList(); + GameObject expand, collapse; + Transform appList = __instance.currentLayout.GetGameObject().transform.GetChild(0); + if (__instance.IsPositionedAtTop) + { + expand = GameObject.Instantiate(allChilds.First(t => t.name == "BtnArrowDown").gameObject); + collapse = GameObject.Instantiate(allChilds.First(t => t.name == "BtnArrowUp").gameObject); + var lG = __instance.currentLayout.GetGameObject().AddComponent(); + lG.childAlignment = appList.GetComponent().childAlignment; + lG.childForceExpandHeight = false; + } + else + { + expand = GameObject.Instantiate(allChilds.First(t => t.name == "BtnArrowLeft").gameObject); + collapse = GameObject.Instantiate(allChilds.First(t => t.name == "BtnArrowRight").gameObject); + var lG = __instance.currentLayout.GetGameObject().AddComponent(); + lG.childAlignment = appList.GetComponent().childAlignment; + lG.childForceExpandWidth = false; + } + + expand.name = "BtnExpand"; + expand.transform.SetParent(__instance.currentLayout.GetGameObject().transform, false); + expand.SetActive(true); + GameObject.DestroyImmediate(expand.GetComponent()); + var exButton = expand.GetComponent