Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow app launcher to collapse/expand (even in SC UIs) #53

Merged
merged 1 commit into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions GameData/KSPCommunityFixes/Settings.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
// ##########################
Expand Down
1 change: 1 addition & 0 deletions KSPCommunityFixes/KSPCommunityFixes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
<Compile Include="Modding\ReflectionTypeLoadExceptionHandler.cs" />
<Compile Include="Performance\PQSUpdateNoMemoryAlloc.cs" />
<Compile Include="QoL\AutostrutActions.cs" />
<Compile Include="QoL\AppLauncherCollapseShow.cs" />
<Compile Include="QoL\DisableNewGameIntro.cs" />
<Compile Include="QoL\NoIVA.cs" />
<Compile Include="Performance\OnDemandPartBuoyancy.cs" />
Expand Down
223 changes: 223 additions & 0 deletions KSPCommunityFixes/QoL/AppLauncherCollapseShow.cs
Original file line number Diff line number Diff line change
@@ -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<PatchInfo> 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>();
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>();
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>();
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<Transform> allChilds = __instance.launcherSpace.GetComponentsInChildren<Transform>(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<VerticalLayoutGroup>();
lG.childAlignment = appList.GetComponent<VerticalLayoutGroup>().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<HorizontalLayoutGroup>();
lG.childAlignment = appList.GetComponent<HorizontalLayoutGroup>().childAlignment;
lG.childForceExpandWidth = false;
}

expand.name = "BtnExpand";
expand.transform.SetParent(__instance.currentLayout.GetGameObject().transform, false);
expand.SetActive(true);
GameObject.DestroyImmediate(expand.GetComponent<KSP.UI.PointerClickAndHoldHandler>());
var exButton = expand.GetComponent<Button>();
exButton.onClick = new Button.ButtonClickedEvent();
exButton.onClick.AddListener(ExpandAppLauncher);
expand.SetActive(false);

collapse.name = "BtnCollapse";
collapse.transform.SetParent(appList.transform, false);
if (__instance.IsPositionedAtTop)
collapse.transform.SetSiblingIndex(0);
collapse.SetActive(true);
GameObject.DestroyImmediate(collapse.GetComponent<KSP.UI.PointerClickAndHoldHandler>());
var colButton = collapse.GetComponent<Button>();
colButton.onClick = new Button.ButtonClickedEvent();
colButton.onClick.AddListener(CollapseAppLauncher);
}
static void ApplicationLauncher_StartupSequence_Postfix(ApplicationLauncher __instance)
{
if (__instance.currentLayout == null)
return;

Transform[] childs = ApplicationLauncher.Instance.launcherSpace.GetComponentsInChildren<Transform>(true);
if (HighLogic.LoadedScene == GameScenes.EDITOR)
{
LayoutElement topRightSpacer = __instance.currentLayout.GetTopRightSpacer();
topRightSpacer.transform.SetAsLastSibling();

LayoutElement newSpacer;
var newSpacerTransform = childs.FirstOrDefault(t => t.name == "ExpandSpacer");
if (newSpacerTransform == null)
{
newSpacer = GameObject.Instantiate(topRightSpacer);
newSpacer.name = "ExpandSpacer";
newSpacer.transform.SetParent(__instance.currentLayout.GetGameObject().transform, false);
}
else
{
newSpacer = newSpacerTransform.gameObject.GetComponent<LayoutElement>();
}
newSpacer.preferredHeight = topRightSpacer.preferredHeight;
newSpacer.preferredWidth = topRightSpacer.preferredWidth;
newSpacer.gameObject.SetActive(childs.First(t => t.name == "BtnExpand").gameObject.activeSelf);
}
else
{
var newSpacerTransform = childs.FirstOrDefault(t => t.name == "ExpandSpacer");
if (newSpacerTransform != null)
newSpacerTransform.gameObject.SetActive(false);
}
}

private static void ToggleAppLauncher(bool show)
{
Transform[] childs = ApplicationLauncher.Instance.launcherSpace.GetComponentsInChildren<Transform>(true);
var expand = childs.FirstOrDefault(t => t.name == "BtnExpand");
var list = ApplicationLauncher.Instance.currentLayout.GetGameObject().transform.GetChild(0);
if (expand == null || list == null)
{
Debug.LogError($"Couldn't find button (null? {expand == null}) or list (null? {list == null})");
ApplicationLauncher.Instance.launcherSpace.gameObject.SetActive(show);
return;
}
expand.gameObject.SetActive(!show);
list.gameObject.SetActive(show);
var spacer = childs.FirstOrDefault(t => t.name == "ExpandSpacer");
if (spacer != null)
spacer.gameObject.SetActive(!show);
}

public static void ExpandAppLauncher()
{
ToggleAppLauncher(true);
ApplicationLauncher.Ready = true;
ApplicationLauncher.Instance.onShow();
}

public static void CollapseAppLauncher()
{
ToggleAppLauncher(false);
ApplicationLauncher.Instance.onHide();
ApplicationLauncher.Ready = false;
}
}
}