From 96d81be8617a78153fa6af86373647cce12ec9d1 Mon Sep 17 00:00:00 2001 From: agm-114 Date: Fri, 1 Nov 2024 22:43:20 +0530 Subject: [PATCH] Fix button errors. --- AGMLIB/Dynamic Systems/DynamicButtonState.cs | 3 +- AGMLIB/Dynamic Systems/UI/ShipInfoButton.cs | 51 ++++++++++++++------ 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/AGMLIB/Dynamic Systems/DynamicButtonState.cs b/AGMLIB/Dynamic Systems/DynamicButtonState.cs index 055e2c5..55379b6 100644 --- a/AGMLIB/Dynamic Systems/DynamicButtonState.cs +++ b/AGMLIB/Dynamic Systems/DynamicButtonState.cs @@ -29,7 +29,7 @@ class ForcedButtonState protected void Start() { - + for (int i = 0; i < LockButtonNames.Count; i++) { ForcedButtonState newstate = new(); @@ -76,6 +76,7 @@ protected override void FixedUpdate() } if (forcedButtonState.Index < 0 && forcedButtonState.State.Length > 0) forcedButtonState.Index = button.States.FindIndex(a => a == forcedButtonState.State); + //Debug.LogError("forcing state change"); button.ForceButtonChange(forcedButtonState.Index); } diff --git a/AGMLIB/Dynamic Systems/UI/ShipInfoButton.cs b/AGMLIB/Dynamic Systems/UI/ShipInfoButton.cs index b7e7b64..a7f3c07 100644 --- a/AGMLIB/Dynamic Systems/UI/ShipInfoButton.cs +++ b/AGMLIB/Dynamic Systems/UI/ShipInfoButton.cs @@ -1,4 +1,5 @@ -using static UI.SequentialButton; +using static Game.Orders.Tasks.OrderTask; +using static UI.SequentialButton; using TooltipTrigger = UI.TooltipTrigger; public class ShipInfoButton : ShipState @@ -28,6 +29,8 @@ public void HandleButtonChanged(int value) => SelectedOption = value; public void ForceButtonChange(int value) { + if (value == SelectedOption) + return; if (value < 0) value = SelectedOption; HandleButtonChanged(value); @@ -46,29 +49,31 @@ public class ButtonData { public GameObject GameObject; public ShipInfoButton ButtonState; - public SequentialButton Button => GameObject.GetComponentInChildren(); + public SequentialButton? Button => FindButton(); SequentialButtonEvent OnValueChanged => Button.OnValueChanged; //RectTransform parentTransform => GameObject.transform.parent.GetComponent(); - LayoutElement Layout => GameObject.transform.parent.GetComponent(); + //LayoutElement Layout => GameObject.transform.parent.GetComponent(); TextMeshProUGUI Text => GameObject.GetComponentInChildren(); TooltipTrigger Tooltip => Common.GetVal(Button, "_tooltip") ?? GameObject.GetComponentInChildren(); - public ButtonData(SequentialButton template, ShipInfoButton buttondata) + public SequentialButton? FindButton() { - ButtonState = buttondata; - Transform templatetransform = template.gameObject.transform.parent; - GameObject = UnityEngine.Object.Instantiate(templatetransform.gameObject, templatetransform.parent); - Setup(); + if (GameObject == null) + return null; + return GameObject.GetComponentInChildren(); } - void Setup() + public void Setup(SequentialButton template, ShipInfoButton buttondata) { + ButtonState = buttondata; + Transform templatetransform = template.gameObject.transform.parent; + GameObject = UnityEngine.Object.Instantiate(templatetransform.gameObject, templatetransform.parent); ButtonState.Data = this; Text.text = ButtonState.ButtonName; GameObject.name = ButtonState.ButtonName; Tooltip.Text = ButtonState.TooltipString; - Layout.minWidth = 550 / 3 * 4; + //Layout.minWidth = 550 / 3 * 4; OnValueChanged.RemoveAllListeners(); Common.SetVal(Button, "_options", ButtonState.Options); Common.SetVal(Button, "_originalTooltip", ButtonState.TooltipString); @@ -76,6 +81,13 @@ void Setup() //Button.SetOptionWithoutNotify(ButtonState.SelectedOption); OnValueChanged.AddListener(ButtonState.HandleButtonChanged); } + + public static void Resize(SequentialButton template, int extrabuttons = 0) + { + LayoutElement Layout = template.gameObject.transform.parent.GetComponent(); + if(Layout != null) + Layout.minWidth = 550 / 3 * (3 + (int)Math.Ceiling(extrabuttons / 2.0)); + } } //Button @@ -84,19 +96,26 @@ void Setup() [HarmonyPatch(typeof(ShipInfoBar), "MatchAllButtons")] class ShipInfoBarMatchAllButtons { - static Dictionary buttons = new(); + static Dictionary uibuttons = new(); static void Prefix(ShipInfoBar __instance, SequentialButton ____battleshort) { ShipController _primaryShip = Common.GetVal(__instance, "_primaryShip"); - foreach (var button in buttons) + foreach (var button in uibuttons) if (button.Value != null && button.Value.GameObject != null) UnityEngine.Object.Destroy(button.Value.GameObject); - buttons.Clear(); - foreach (ShipInfoButton infobut in _primaryShip.GetComponentsInChildren()) + uibuttons.Clear(); + + ShipInfoButton[] shipbuttons = _primaryShip.GetComponentsInChildren(); + + ButtonData.Resize(____battleshort, shipbuttons.Count()); + + foreach (ShipInfoButton infobut in shipbuttons) { - ButtonData newbutton = new(____battleshort, infobut); - buttons.Add(newbutton.ButtonState.ButtonName, newbutton); + ButtonData newbutton = new(); + newbutton.Setup(____battleshort, infobut); + uibuttons.Add(newbutton.ButtonState.ButtonName, newbutton); } + } } \ No newline at end of file