Skip to content

Commit

Permalink
Finally fixed the goddamn app menu ... again
Browse files Browse the repository at this point in the history
  • Loading branch information
cheese3660 committed Jan 18, 2024
1 parent d85814d commit 314a175
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
56 changes: 43 additions & 13 deletions src/SpaceWarp.UI/Backend/UI/Appbar/AppbarBackend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,21 +303,28 @@ private static void SetOABTrayState(bool state)
#region KSC App Bar

private static GameObject _kscTray;

// ReSharper disable once InconsistentNaming
private static GameObject KSCTray
{
get
{
if (_kscTray == null)
{
return _kscTray = CreateKSCTray();
{
return _kscTray = CreateKSCTray();
}

return _kscTray;
}
}

private const string KscMenuPath =
"GameManager/Default Game Instance(Clone)/UI Manager(Clone)/Main Canvas/KSCMenu(Clone)/Panel/Window-FacilityMenu/GRP-Body/Content/Menu";

private const string FlyoutName = "LaunchLocationFlyoutHeaderToggle";

private const string TargetName = "LaunchLocationsFlyoutTarget";

// ReSharper disable once InconsistentNaming
private static GameObject CreateKSCTray()
{
Expand All @@ -326,8 +333,8 @@ private static GameObject CreateKSCTray()
// Find the KSC launch locations menu item; it will be used for cloning the app tray

// Get the Launch Pads menu item
var kscMenu = GameObject.Find("GameManager/Default Game Instance(Clone)/UI Manager(Clone)/Main Canvas/KSCMenu(Clone)/LandingPanel/InteriorWindow/MenuButtons/Content/Menu");
var launchLocationsButton = kscMenu != null ? kscMenu.GetChild("LaunchLocationFlyoutHeaderToggle") : null;
var kscMenu = GameObject.Find(KscMenuPath);
var launchLocationsButton = kscMenu != null ? kscMenu.GetChild(FlyoutName) : null;

if (kscMenu == null || launchLocationsButton == null)
{
Expand All @@ -340,13 +347,13 @@ private static GameObject CreateKSCTray()
kscAppTrayButton.name = "KSC-AppTrayButton";

// Set the button icon (use OAB app tray icon)
var image = kscAppTrayButton.GetChild("Header").GetChild("Content").GetChild("Icon Panel").GetChild("icon").GetComponent<Image>();
var image = kscAppTrayButton.GetChild("ICO-Launchpad").GetComponent<Image>();
var tex = AssetManager.GetAsset<Texture2D>($"{SpaceWarpPlugin.ModGuid}/images/oabTrayButton.png");
tex.filterMode = FilterMode.Point;
image.sprite = Sprite.Create(tex, new Rect(0, 0, 32, 32), new Vector2(0.5f, 0.5f));

// Change the text to APPS
var title = kscAppTrayButton.GetChild("Header").GetChild("Content").GetChild("Title");
var title = kscAppTrayButton.GetChild("TXT-Launchpad");
{
// Suppress renaming of the button to Launchpad
var localizer = title.GetComponent<Localize>();
Expand All @@ -359,19 +366,42 @@ private static GameObject CreateKSCTray()
}

// Get the popup tray and rename it
var kscAppTray = kscAppTrayButton.GetChild("LaunchLocationsFlyoutTarget");
var originalFlyout = kscMenu.GetChild(TargetName);
var originalToggle = launchLocationsButton.GetComponent<ToggleExtended>();
var kscAppTray = UnityObject.Instantiate(originalFlyout, kscMenu.transform);
kscAppTray.name = "KSC-AppTray";


// Delete existing buttons and separators in the tray
for (var i = 0; i < kscAppTray.transform.childCount; i++)
{
var child = kscAppTray.transform.GetChild(i);

// Destroy all objects inside the tray, but keep the arrow ("thingy") that points to the menu button
if (!child.name.ToLowerInvariant().Contains("thingy"))
if (!child.name.ToLowerInvariant().Contains("bg-panel"))
UnityObject.Destroy(child.gameObject);
}

var toggleFlyout = kscAppTrayButton.GetComponent<ToggleFlyout>();
toggleFlyout.OnDisable();
toggleFlyout._target = kscAppTray;
toggleFlyout._toggle = kscAppTrayButton.GetComponent<ToggleExtended>();
toggleFlyout._toggleCanvas = kscAppTray.GetComponent<Canvas>();
toggleFlyout.OnEnable();
toggleFlyout._toggleCanvas.enabled = true;
toggleFlyout._toggle.onValueChanged.AddListener(x =>
{
if (!x) return;
if (!originalToggle.isOn) return;
originalToggle.Set(false);
});
originalToggle.onValueChanged.AddListener(x =>
{
if (!x) return;
if (!toggleFlyout._toggle.isOn) return;
toggleFlyout._toggle.Set(false);
});

Logger.LogInfo("Created KSC app tray.");

return kscAppTray;
Expand All @@ -386,7 +416,7 @@ public static void AddKSCButton(string buttonText, Sprite buttonIcon, string but

// Find the Launchpad_1 button.
var kscLaunchLocationsFlyoutTarget = GameObject.Find(
"GameManager/Default Game Instance(Clone)/UI Manager(Clone)/Main Canvas/KSCMenu(Clone)/LandingPanel/InteriorWindow/MenuButtons/Content/Menu/LaunchLocationFlyoutHeaderToggle/LaunchLocationsFlyoutTarget");
$"{KscMenuPath}/{TargetName}");
var launchPadButton = kscLaunchLocationsFlyoutTarget != null ? kscLaunchLocationsFlyoutTarget.GetChild("Launchpad_1") : null;

if (launchPadButton == null)
Expand All @@ -400,7 +430,7 @@ public static void AddKSCButton(string buttonText, Sprite buttonIcon, string but
modButton.name = buttonId;

// Change the text
var modText = modButton.GetChild("Content").GetChild("Text (TMP)").GetComponent<TextMeshProUGUI>();
var modText = modButton.GetChild("TXT-LocationName").GetComponent<TextMeshProUGUI>();
modText.text = buttonText;

// Suppress renaming of the button
Expand All @@ -411,7 +441,7 @@ public static void AddKSCButton(string buttonText, Sprite buttonIcon, string but
}

// Change the icon
var icon = modButton.GetChild("Icon");
var icon = modButton.GetChild("ICO-Location");
var image = icon.GetComponent<Image>();
image.sprite = buttonIcon;

Expand Down
1 change: 1 addition & 0 deletions src/SpaceWarp.UI/Modules/UI.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using HarmonyLib;
using JetBrains.Annotations;
using KSP.UI.Flight;
using SpaceWarp.API.Assets;
using SpaceWarp.API.Configuration;
using SpaceWarp.API.UI.Appbar;
Expand Down

0 comments on commit 314a175

Please sign in to comment.