diff --git a/Silksong.ModMenu/Elements/DynamicDescriptionChoiceElement.cs b/Silksong.ModMenu/Elements/DynamicDescriptionChoiceElement.cs index ea678ac..69bf968 100644 --- a/Silksong.ModMenu/Elements/DynamicDescriptionChoiceElement.cs +++ b/Silksong.ModMenu/Elements/DynamicDescriptionChoiceElement.cs @@ -136,6 +136,7 @@ private static void HookOnSelect(ILManipulationInfo info) private static void HookOnDeselect(ILManipulationInfo info) { ILCursor cursor = new(info.Context); + int locIndex = 1; while ( cursor.TryGotoNext( diff --git a/Silksong.ModMenu/Elements/TextInput.cs b/Silksong.ModMenu/Elements/TextInput.cs index 91dede6..dcc2b91 100644 --- a/Silksong.ModMenu/Elements/TextInput.cs +++ b/Silksong.ModMenu/Elements/TextInput.cs @@ -48,6 +48,9 @@ public TextInput(LocalizedText label, ITextModel model, LocalizedText descrip InputField.contentType = InputField.ContentType.IntegerNumber; else if (floatTypes.Contains(typeof(T))) InputField.contentType = InputField.ContentType.DecimalNumber; + + input.AddComponent().DescriptionAnimator = + DescriptionText.gameObject.GetComponent(); } /// diff --git a/Silksong.ModMenu/Internal/DescriptionAnimationHelper.cs b/Silksong.ModMenu/Internal/DescriptionAnimationHelper.cs new file mode 100644 index 0000000..5abf588 --- /dev/null +++ b/Silksong.ModMenu/Internal/DescriptionAnimationHelper.cs @@ -0,0 +1,83 @@ +using System.Collections; +using UnityEngine; +using UnityEngine.EventSystems; +using UnityEngine.UI; + +namespace Silksong.ModMenu.Internal; + +internal class DescriptionAnimationHelper + : MonoBehaviour, + ISelectHandler, + IDeselectHandler, + ICancelHandler +{ + private Selectable _selectable; + public Animator? DescriptionAnimator { get; set; } + + void Awake() + { + _selectable = GetComponent(); + } + + public void OnDeselect(BaseEventData eventData) + { + StartCoroutine(ValidateDeselect(eventData, false)); + } + + // My best attempt to imitate the code from MenuSelectable.ValidateDeselect + private IEnumerator ValidateDeselect(BaseEventData eventData, bool force) + { + if (DescriptionAnimator == null) + { + yield break; + } + + GameObject? prevSelectedObject = EventSystem.current.currentSelectedGameObject; + + yield return new WaitForEndOfFrame(); + + if (!(EventSystem.current.currentSelectedGameObject != null || force)) + { + InputHandler ih = ManagerSingleton.Instance; + if (ih && !ih.acceptingInput) + { + while (!ih.acceptingInput) + { + yield return null; + } + } + yield return null; + } + + if (EventSystem.current.currentSelectedGameObject != null || force) + { + DescriptionAnimator.ResetTrigger(MenuSelectable._showPropId); + DescriptionAnimator.SetTrigger(MenuSelectable._hidePropId); + } + else if (prevSelectedObject != null && prevSelectedObject.activeInHierarchy) + { + EventSystem.current.SetSelectedGameObject(prevSelectedObject); + } + } + + public void OnSelect(BaseEventData eventData) + { + if (DescriptionAnimator == null) + { + return; + } + + if (!_selectable.interactable) + { + return; + } + + DescriptionAnimator.ResetTrigger(MenuSelectable._hidePropId); + DescriptionAnimator.SetTrigger(MenuSelectable._showPropId); + } + + public void OnCancel(BaseEventData eventData) + { + StartCoroutine(ValidateDeselect(eventData, true)); + } +}