From 620919ecd6e9c8735477f3c700a13875ff521311 Mon Sep 17 00:00:00 2001 From: Falki <72734856+Falki-git@users.noreply.github.com> Date: Tue, 5 Sep 2023 14:04:45 +0200 Subject: [PATCH 1/5] Initial scaffolding --- .../Backend/UI/Appbar/AppbarBackend.cs | 140 +++++++++++++++++- 1 file changed, 139 insertions(+), 1 deletion(-) diff --git a/SpaceWarp.UI/Backend/UI/Appbar/AppbarBackend.cs b/SpaceWarp.UI/Backend/UI/Appbar/AppbarBackend.cs index 8f2ef2b0..c7c4d215 100644 --- a/SpaceWarp.UI/Backend/UI/Appbar/AppbarBackend.cs +++ b/SpaceWarp.UI/Backend/UI/Appbar/AppbarBackend.cs @@ -30,6 +30,7 @@ internal static class AppbarBackend public static readonly UnityEvent AppBarOABSubscriber = new(); public static readonly UnityEvent AppBarInFlightSubscriber = new(); + public static readonly UnityEvent AppBarKSCSubscriber = new(); internal static void SubscriberSchedulePing(AppbarEvent type) { @@ -42,6 +43,7 @@ internal static void SubscriberSchedulePing(AppbarEvent type) { AppbarEvent.Flight => AppBarInFlightSubscriber, AppbarEvent.OAB => AppBarOABSubscriber, + AppbarEvent.KSC => AppBarKSCSubscriber, _ => waiterObject.CreationEvent }; @@ -52,7 +54,8 @@ internal static void SubscriberSchedulePing(AppbarEvent type) internal enum AppbarEvent { Flight, - OAB + OAB, + KSC } #region Flight App Bar @@ -287,6 +290,141 @@ private static void SetOABTrayState(bool state) } #endregion + + #region KSC App Bar + + private static GameObject _kscTray; + + private static GameObject KSCTray + { + get + { + if (_oabTray == null) + { + return _oabTray = CreateKSCTray(); + } + + return _oabTray; + } + } + + private static Property _kscState; + + private static GameObject CreateKSCTray() + { + Logger.LogInfo("Creating KSC app tray..."); + + // 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; + + if (kscMenu == null || launchLocationsButton == null) + { + Logger.LogError("Couldn't find KSC tray."); + return null; + } + + // Clone it, add it to the menu and rename it + var kscAppTrayButton = UnityEngine.Object.Instantiate(launchLocationsButton, kscMenu.transform); + kscAppTrayButton.name = "KSC-AppTrayButton"; + + // Set the button icon + var image = kscAppTrayButton.GetChild("Header").GetChild("Content").GetChild("Icon Panel").GetChild("icon").GetComponent(); + var tex = AssetManager.GetAsset($"{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 text = kscAppTrayButton.GetChild("Header").GetChild("Content").GetChild("Title").GetComponent(); + text.text = "Apps"; + + // Get the tray and rename it + var kscAppTray = kscAppTrayButton.GetChild("LaunchLocationsFlyoutTarget"); + kscAppTray.name = "KSC-AppTray"; + + // Delete existing buttons in the tray. + for (var i = 0; i < kscAppTray.transform.childCount; i++) + { + var child = kscAppTray.transform.GetChild(i); + + UnityEngine.Object.Destroy(child.gameObject); + } + + Logger.LogInfo("Created KSC app tray."); + + return kscAppTray; + } + + public static void AddKSCButton(string buttonText, Sprite buttonIcon, string buttonId, Action function) + { + throw new NotImplementedException(); + + // TODO: + + Logger.LogInfo($"Adding OAB app bar button: {buttonId}."); + + // Find the resource manager button. + var list = GameObject.Find( + "GameManager/Default Game Instance(Clone)/UI Manager(Clone)/Scaled Popup Canvas/Container/ButtonBar/BTN-App-Tray/appbar-others-group"); + var resourceManger = list != null ? list.GetChild("BTN-Resource-Manager") : null; + + if (resourceManger == null) + { + Logger.LogError("Couldn't find the appbar."); + return; + } + + // Clone the resource manager button. + var appButton = UnityObject.Instantiate(resourceManger, OABTray.transform); + appButton.name = buttonId; + + // Change the text. + var text = appButton.GetChild("Content").GetChild("TXT-title").GetComponent(); + text.text = buttonText; + + var localizer = text.gameObject.GetComponent(); + if (localizer) + { + UnityObject.Destroy(localizer); + } + + // Change the icon. + var icon = appButton.GetChild("Content").GetChild("GRP-icon"); + var image = icon.GetChild("ICO-asset").GetComponent(); + image.sprite = buttonIcon; + + // Add our function call to the toggle. + var utoggle = appButton.GetComponent(); + utoggle.onValueChanged.AddListener(state => + { + Logger.LogInfo($"{buttonId}({state})"); + function(state); + }); + + // Set the initial state of the button. + var toggle = appButton.GetComponent(); + toggle.BindValue(new Property(false)); + + // Bind the action to close the tray after pressing the button. + void Action() => SetKSCTrayState(false); + appButton.GetComponent().BindAction(new DelegateAction((Action)Action)); + + Logger.LogInfo($"Added OAB appbar button: {buttonId}"); + } + + private static void SetKSCTrayState(bool state) + { + if (_kscTray == null) + { + return; + } + + _kscState.SetValue(state); + } + + #endregion } internal class ToolbarBackendObject : KerbalBehavior From f2f14e29552f61a69b01bd94acb54656a4772121 Mon Sep 17 00:00:00 2001 From: Falki-git <72734856+Falki-git@users.noreply.github.com> Date: Wed, 6 Sep 2023 00:23:17 +0200 Subject: [PATCH 2/5] Add KSC app tray and button registration --- SpaceWarp.UI/API/UI/Appbar/Appbar.cs | 37 ++++- .../Backend/UI/Appbar/AppbarBackend.cs | 135 +++++++++++++----- SpaceWarp.UI/Modules/UI.cs | 1 + 3 files changed, 135 insertions(+), 38 deletions(-) diff --git a/SpaceWarp.UI/API/UI/Appbar/Appbar.cs b/SpaceWarp.UI/API/UI/Appbar/Appbar.cs index 435ec8ab..af009f94 100644 --- a/SpaceWarp.UI/API/UI/Appbar/Appbar.cs +++ b/SpaceWarp.UI/API/UI/Appbar/Appbar.cs @@ -14,8 +14,9 @@ public static class Appbar { private static readonly List<(string text, Sprite icon, string ID, Action action)> ButtonsToBeLoaded = new(); - private static readonly List<(string text, Sprite icon, string ID, Action action)> OABButtonsToBeLoaded = - new(); + private static readonly List<(string text, Sprite icon, string ID, Action action)> OABButtonsToBeLoaded = new(); + + private static readonly List<(string text, Sprite icon, string ID, Action action)> KSCButtonsToBeLoaded = new(); /// /// Register an appbar menu for the game @@ -101,6 +102,30 @@ public static void RegisterOABAppButton(string text, string id, Texture2D icon, RegisterOABAppButton(text, id, GetAppBarIconFromTexture(icon), func); } + /// + /// Register a button on the KSC AppBar + /// + /// The text in the appbar menu + /// A unique id for the appbar menu eg: "BTN-ExampleKSC" + /// A Sprite for the icon in the appbar + /// The function to be called when this button is clicked + public static void RegisterKSCAppButton(string text, string id, Sprite icon, Action func) + { + KSCButtonsToBeLoaded.Add((text, icon, id, func)); + } + + /// + /// Register a button on the KSC AppBar + /// + /// The text in the appbar menu + /// A unique id for the appbar menu eg: "BTN-ExampleKSC" + /// A Texture2D for the icon in the appbar + /// The function to be called when this button is clicked + public static void RegisterKSCAppButton(string text, string id, Texture2D icon, Action func) + { + RegisterKSCAppButton(text, id, GetAppBarIconFromTexture(icon), func); + } + /// /// Convert a Texture2D to a Sprite /// @@ -149,4 +174,12 @@ internal static void LoadOABButtons() AppbarBackend.AddOABButton(button.text, button.icon, button.ID, button.action); } } + + internal static void LoadKSCButtons() + { + foreach (var button in KSCButtonsToBeLoaded) + { + AppbarBackend.AddKSCButton(button.text, button.icon, button.ID, button.action); + } + } } \ No newline at end of file diff --git a/SpaceWarp.UI/Backend/UI/Appbar/AppbarBackend.cs b/SpaceWarp.UI/Backend/UI/Appbar/AppbarBackend.cs index c7c4d215..6597c5b7 100644 --- a/SpaceWarp.UI/Backend/UI/Appbar/AppbarBackend.cs +++ b/SpaceWarp.UI/Backend/UI/Appbar/AppbarBackend.cs @@ -12,6 +12,7 @@ using KSP.Game; using KSP.OAB; using KSP.Sim.impl; +using KSP.UI; using KSP.UI.Binding; using KSP.UI.Flight; using SpaceWarp.API.Assets; @@ -299,12 +300,12 @@ private static GameObject KSCTray { get { - if (_oabTray == null) + if (_kscTray == null) { - return _oabTray = CreateKSCTray(); + return _kscTray = CreateKSCTray(); } - return _oabTray; + return _kscTray; } } @@ -337,8 +338,17 @@ private static GameObject CreateKSCTray() image.sprite = Sprite.Create(tex, new Rect(0, 0, 32, 32), new Vector2(0.5f, 0.5f)); // Change the text to APPS - var text = kscAppTrayButton.GetChild("Header").GetChild("Content").GetChild("Title").GetComponent(); - text.text = "Apps"; + var title = kscAppTrayButton.GetChild("Header").GetChild("Content").GetChild("Title"); + { + // Suppress renaming of button to Launchpad + var localizer = title.GetComponent(); + if (localizer) + { + UnityObject.Destroy(localizer); + } + var text = title.GetComponent(); + text.text = "Apps"; + } // Get the tray and rename it var kscAppTray = kscAppTrayButton.GetChild("LaunchLocationsFlyoutTarget"); @@ -349,7 +359,8 @@ private static GameObject CreateKSCTray() { var child = kscAppTray.transform.GetChild(i); - UnityEngine.Object.Destroy(child.gameObject); + if (!child.name.ToLowerInvariant().Contains("thingy")) + UnityEngine.Object.Destroy(child.gameObject); } Logger.LogInfo("Created KSC app tray."); @@ -357,63 +368,104 @@ private static GameObject CreateKSCTray() return kscAppTray; } - public static void AddKSCButton(string buttonText, Sprite buttonIcon, string buttonId, Action function) + public static void AddKSCButton(string buttonText, Sprite buttonIcon, string buttonId, Action function) { - throw new NotImplementedException(); + Logger.LogInfo($"Adding KSC appbar button: {buttonId}."); - // TODO: + //var list = GameObject.Find( + // "GameManager/Default Game Instance(Clone)/UI Manager(Clone)/Scaled Popup Canvas/Container/ButtonBar/BTN-App-Tray/appbar-others-group"); + //var resourceManger = list != null ? list.GetChild("BTN-Resource-Manager") : null; - Logger.LogInfo($"Adding OAB app bar button: {buttonId}."); + //if (resourceManger == null) + //{ + // Logger.LogError("Couldn't find the appbar."); + // return; + //} - // Find the resource manager button. - var list = GameObject.Find( - "GameManager/Default Game Instance(Clone)/UI Manager(Clone)/Scaled Popup Canvas/Container/ButtonBar/BTN-App-Tray/appbar-others-group"); - var resourceManger = list != null ? list.GetChild("BTN-Resource-Manager") : null; + // 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"); + var launchPadButton = kscLaunchLocationsFlyoutTarget != null ? kscLaunchLocationsFlyoutTarget.GetChild("Launchpad_1") : null; - if (resourceManger == null) + if (launchPadButton == null) { - Logger.LogError("Couldn't find the appbar."); + Logger.LogError("Couldn't find the KSC Launchpad_1 button."); return; } + // Clone the resource manager button. - var appButton = UnityObject.Instantiate(resourceManger, OABTray.transform); - appButton.name = buttonId; + //var appButton = UnityObject.Instantiate(resourceManger, KSCTray.transform); + //appButton.name = buttonId; + + var modButton = UnityObject.Instantiate(launchPadButton, KSCTray.transform); + modButton.name = buttonId; + // Change the text. - var text = appButton.GetChild("Content").GetChild("TXT-title").GetComponent(); - text.text = buttonText; + //var text = appButton.GetChild("Content").GetChild("TXT-title").GetComponent(); + //text.text = buttonText; - var localizer = text.gameObject.GetComponent(); + var modText = modButton.GetChild("Content").GetChild("Text (TMP)").GetComponent(); + modText.text = buttonText; + + //var localizer = text.gameObject.GetComponent(); + //if (localizer) + //{ + // UnityObject.Destroy(localizer); + //} + + var localizer = modText.gameObject.GetComponent(); if (localizer) { UnityObject.Destroy(localizer); } // Change the icon. - var icon = appButton.GetChild("Content").GetChild("GRP-icon"); - var image = icon.GetChild("ICO-asset").GetComponent(); + //var icon = appButton.GetChild("Content").GetChild("GRP-icon"); + //var image = icon.GetChild("ICO-asset").GetComponent(); + //image.sprite = buttonIcon; + + var icon = modButton.GetChild("Icon"); + var image = icon.GetComponent(); image.sprite = buttonIcon; // Add our function call to the toggle. - var utoggle = appButton.GetComponent(); - utoggle.onValueChanged.AddListener(state => - { - Logger.LogInfo($"{buttonId}({state})"); - function(state); - }); + //var utoggle = appButton.GetComponent(); + //utoggle.onValueChanged.AddListener(state => + //{ + // Logger.LogInfo($"{buttonId}({state})"); + // function(state); + //}); // Set the initial state of the button. - var toggle = appButton.GetComponent(); - toggle.BindValue(new Property(false)); + //var toggle = appButton.GetComponent(); + //toggle.BindValue(new Property(false)); - // Bind the action to close the tray after pressing the button. - void Action() => SetKSCTrayState(false); - appButton.GetComponent().BindAction(new DelegateAction((Action)Action)); + //// Bind the action to close the tray after pressing the button. + //void Action() => SetKSCTrayState(false); + //appButton.GetComponent().BindAction(new DelegateAction((Action)Action)); - Logger.LogInfo($"Added OAB appbar button: {buttonId}"); + var buttonExtended = modButton.GetComponent(); + var previousListeners = modButton.GetComponent(); + if (previousListeners) + { + UnityObject.Destroy(previousListeners); + } + buttonExtended.onClick.AddListener(() => + { + Logger.LogInfo($"Mod button {buttonId} clicked."); + function(); + + // Hide the tray + var toggle = KSCTray.GetComponentInParent(); + toggle.isOn = false; + }); + + Logger.LogInfo($"Added KSC appbar button: {buttonId}."); } + // TODO: delete? private static void SetKSCTrayState(bool state) { if (_kscTray == null) @@ -442,7 +494,8 @@ private IEnumerator Awaiter() yield return Type switch { AppbarEvent.Flight => new WaitForSeconds(1), - AppbarEvent.OAB => new WaitForFixedUpdate(), + AppbarEvent.OAB => new WaitForSeconds(1), + AppbarEvent.KSC => new WaitForFixedUpdate(), _ => new WaitForSeconds(1) }; @@ -471,3 +524,13 @@ public static void Postfix() SubscriberSchedulePing(AppbarEvent.OAB); } } + +[HarmonyPatch(typeof(KSCMenuManager))] +[HarmonyPatch("Start")] +internal class ToolbarBackendKSCPatcher +{ + public static void Postfix() + { + SubscriberSchedulePing(AppbarEvent.KSC); + } +} diff --git a/SpaceWarp.UI/Modules/UI.cs b/SpaceWarp.UI/Modules/UI.cs index b272e57c..9ec81c5e 100644 --- a/SpaceWarp.UI/Modules/UI.cs +++ b/SpaceWarp.UI/Modules/UI.cs @@ -53,6 +53,7 @@ public override void LoadModule() { AppbarBackend.AppBarInFlightSubscriber.AddListener(Appbar.LoadAllButtons); AppbarBackend.AppBarOABSubscriber.AddListener(Appbar.LoadOABButtons); + AppbarBackend.AppBarKSCSubscriber.AddListener(Appbar.LoadKSCButtons); Instance = this; ConfigErrorColor = new(ModuleConfiguration.Bind("Debug Console", "Color Error", Color.red, "The color for log messages that have the level: Error/Fatal (bolded)")); From d25b194e8a9ce1d58fb1ba34e0560aa84036e829 Mon Sep 17 00:00:00 2001 From: Falki <72734856+Falki-git@users.noreply.github.com> Date: Thu, 7 Sep 2023 19:32:59 +0200 Subject: [PATCH 3/5] Cleanup --- .../Backend/UI/Appbar/AppbarBackend.cs | 29 ++++++------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/SpaceWarp.UI/Backend/UI/Appbar/AppbarBackend.cs b/SpaceWarp.UI/Backend/UI/Appbar/AppbarBackend.cs index 6597c5b7..97b3ca4f 100644 --- a/SpaceWarp.UI/Backend/UI/Appbar/AppbarBackend.cs +++ b/SpaceWarp.UI/Backend/UI/Appbar/AppbarBackend.cs @@ -309,8 +309,6 @@ private static GameObject KSCTray } } - private static Property _kscState; - private static GameObject CreateKSCTray() { Logger.LogInfo("Creating KSC app tray..."); @@ -323,15 +321,15 @@ private static GameObject CreateKSCTray() if (kscMenu == null || launchLocationsButton == null) { - Logger.LogError("Couldn't find KSC tray."); + Logger.LogError("Couldn't find Launch Pads menu item for cloning the KSC app tray."); return null; } // Clone it, add it to the menu and rename it - var kscAppTrayButton = UnityEngine.Object.Instantiate(launchLocationsButton, kscMenu.transform); + var kscAppTrayButton = UnityObject.Instantiate(launchLocationsButton, kscMenu.transform); kscAppTrayButton.name = "KSC-AppTrayButton"; - // Set the button icon + // Set the button icon (use OAB app tray icon) var image = kscAppTrayButton.GetChild("Header").GetChild("Content").GetChild("Icon Panel").GetChild("icon").GetComponent(); var tex = AssetManager.GetAsset($"{SpaceWarpPlugin.ModGuid}/images/oabTrayButton.png"); tex.filterMode = FilterMode.Point; @@ -350,17 +348,17 @@ private static GameObject CreateKSCTray() text.text = "Apps"; } - // Get the tray and rename it + // Get the popup tray and rename it var kscAppTray = kscAppTrayButton.GetChild("LaunchLocationsFlyoutTarget"); kscAppTray.name = "KSC-AppTray"; - // Delete existing buttons in the tray. + // Delete existing buttons in the tray for (var i = 0; i < kscAppTray.transform.childCount; i++) { var child = kscAppTray.transform.GetChild(i); if (!child.name.ToLowerInvariant().Contains("thingy")) - UnityEngine.Object.Destroy(child.gameObject); + UnityObject.Destroy(child.gameObject); } Logger.LogInfo("Created KSC app tray."); @@ -372,6 +370,8 @@ public static void AddKSCButton(string buttonText, Sprite buttonIcon, string but { Logger.LogInfo($"Adding KSC appbar button: {buttonId}."); + // Grab the Launchpad_1 button, clone it and convert it to a mod launching button + //var list = GameObject.Find( // "GameManager/Default Game Instance(Clone)/UI Manager(Clone)/Scaled Popup Canvas/Container/ButtonBar/BTN-App-Tray/appbar-others-group"); //var resourceManger = list != null ? list.GetChild("BTN-Resource-Manager") : null; @@ -465,17 +465,6 @@ public static void AddKSCButton(string buttonText, Sprite buttonIcon, string but Logger.LogInfo($"Added KSC appbar button: {buttonId}."); } - // TODO: delete? - private static void SetKSCTrayState(bool state) - { - if (_kscTray == null) - { - return; - } - - _kscState.SetValue(state); - } - #endregion } @@ -494,7 +483,7 @@ private IEnumerator Awaiter() yield return Type switch { AppbarEvent.Flight => new WaitForSeconds(1), - AppbarEvent.OAB => new WaitForSeconds(1), + AppbarEvent.OAB => new WaitForFixedUpdate(), AppbarEvent.KSC => new WaitForFixedUpdate(), _ => new WaitForSeconds(1) }; From 2504a622e3a581e9e75797e74ef6ed95d9d46746 Mon Sep 17 00:00:00 2001 From: Falki-git <72734856+Falki-git@users.noreply.github.com> Date: Thu, 7 Sep 2023 20:03:49 +0200 Subject: [PATCH 4/5] Remove unused code and add comments --- .../Backend/UI/Appbar/AppbarBackend.cs | 58 +++---------------- 1 file changed, 9 insertions(+), 49 deletions(-) diff --git a/SpaceWarp.UI/Backend/UI/Appbar/AppbarBackend.cs b/SpaceWarp.UI/Backend/UI/Appbar/AppbarBackend.cs index 97b3ca4f..77713bff 100644 --- a/SpaceWarp.UI/Backend/UI/Appbar/AppbarBackend.cs +++ b/SpaceWarp.UI/Backend/UI/Appbar/AppbarBackend.cs @@ -338,7 +338,7 @@ private static GameObject CreateKSCTray() // Change the text to APPS var title = kscAppTrayButton.GetChild("Header").GetChild("Content").GetChild("Title"); { - // Suppress renaming of button to Launchpad + // Suppress renaming of the button to Launchpad var localizer = title.GetComponent(); if (localizer) { @@ -352,11 +352,12 @@ private static GameObject CreateKSCTray() var kscAppTray = kscAppTrayButton.GetChild("LaunchLocationsFlyoutTarget"); kscAppTray.name = "KSC-AppTray"; - // Delete existing buttons in the tray + // 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")) UnityObject.Destroy(child.gameObject); } @@ -372,16 +373,6 @@ public static void AddKSCButton(string buttonText, Sprite buttonIcon, string but // Grab the Launchpad_1 button, clone it and convert it to a mod launching button - //var list = GameObject.Find( - // "GameManager/Default Game Instance(Clone)/UI Manager(Clone)/Scaled Popup Canvas/Container/ButtonBar/BTN-App-Tray/appbar-others-group"); - //var resourceManger = list != null ? list.GetChild("BTN-Resource-Manager") : null; - - //if (resourceManger == null) - //{ - // Logger.LogError("Couldn't find the appbar."); - // return; - //} - // 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"); @@ -393,59 +384,28 @@ public static void AddKSCButton(string buttonText, Sprite buttonIcon, string but return; } - - // Clone the resource manager button. - //var appButton = UnityObject.Instantiate(resourceManger, KSCTray.transform); - //appButton.name = buttonId; - + // Clone the button, add it to the popup tray and rename it var modButton = UnityObject.Instantiate(launchPadButton, KSCTray.transform); modButton.name = buttonId; - // Change the text. - //var text = appButton.GetChild("Content").GetChild("TXT-title").GetComponent(); - //text.text = buttonText; - + // Change the text var modText = modButton.GetChild("Content").GetChild("Text (TMP)").GetComponent(); modText.text = buttonText; - //var localizer = text.gameObject.GetComponent(); - //if (localizer) - //{ - // UnityObject.Destroy(localizer); - //} - + // Suppress renaming of the button var localizer = modText.gameObject.GetComponent(); if (localizer) { UnityObject.Destroy(localizer); } - // Change the icon. - //var icon = appButton.GetChild("Content").GetChild("GRP-icon"); - //var image = icon.GetChild("ICO-asset").GetComponent(); - //image.sprite = buttonIcon; - + // Change the icon var icon = modButton.GetChild("Icon"); var image = icon.GetComponent(); image.sprite = buttonIcon; - // Add our function call to the toggle. - //var utoggle = appButton.GetComponent(); - //utoggle.onValueChanged.AddListener(state => - //{ - // Logger.LogInfo($"{buttonId}({state})"); - // function(state); - //}); - - // Set the initial state of the button. - //var toggle = appButton.GetComponent(); - //toggle.BindValue(new Property(false)); - - //// Bind the action to close the tray after pressing the button. - //void Action() => SetKSCTrayState(false); - //appButton.GetComponent().BindAction(new DelegateAction((Action)Action)); - + // Remove previous onclick listeners and add the function that mod will use var buttonExtended = modButton.GetComponent(); var previousListeners = modButton.GetComponent(); if (previousListeners) @@ -457,7 +417,7 @@ public static void AddKSCButton(string buttonText, Sprite buttonIcon, string but Logger.LogInfo($"Mod button {buttonId} clicked."); function(); - // Hide the tray + // Hide the popup tray after the button is clicked var toggle = KSCTray.GetComponentInParent(); toggle.isOn = false; }); From b5c42b4d3cf16fc3c2a135dbcbf5c0b5782a25ae Mon Sep 17 00:00:00 2001 From: Falki-git <72734856+Falki-git@users.noreply.github.com> Date: Thu, 7 Sep 2023 20:08:56 +0200 Subject: [PATCH 5/5] Cleanup --- SpaceWarp.UI/Backend/UI/Appbar/AppbarBackend.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/SpaceWarp.UI/Backend/UI/Appbar/AppbarBackend.cs b/SpaceWarp.UI/Backend/UI/Appbar/AppbarBackend.cs index 77713bff..abdd99ca 100644 --- a/SpaceWarp.UI/Backend/UI/Appbar/AppbarBackend.cs +++ b/SpaceWarp.UI/Backend/UI/Appbar/AppbarBackend.cs @@ -388,7 +388,6 @@ public static void AddKSCButton(string buttonText, Sprite buttonIcon, string but var modButton = UnityObject.Instantiate(launchPadButton, KSCTray.transform); modButton.name = buttonId; - // Change the text var modText = modButton.GetChild("Content").GetChild("Text (TMP)").GetComponent(); modText.text = buttonText;