UITransition.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435
  1. using GameAudio = DevilLink2.Presentation.AudioManager;
  2. using DevilLink2.Presentation;
  3. using UnityEngine;
  4. namespace DevilLink2.UI
  5. {
  6. /// <summary>Synchronous visibility/input gate. Popup motion and completion callbacks are retired.</summary>
  7. [DisallowMultipleComponent]
  8. public sealed class UITransition : MonoBehaviour
  9. {
  10. // Compatibility for existing editor tools. These APIs can no longer enable motion.
  11. public const float EnterSeconds=0,ExitSeconds=0;
  12. public static bool EnablePopupAnimations { get=>false; set{} }
  13. public bool? PopupAnimationOverride => null;
  14. public void SetPopupAnimationOverride(bool? ignored){}
  15. private CanvasGroup group;private Transform content;private bool initialized,inputEnabled=true;
  16. public bool IsShown {get;private set;}
  17. public bool IsTransitioning => false;
  18. public static bool IsVisible(GameObject target){var gate=target?target.GetComponent<UITransition>():null;return gate?gate.IsShown:target&&target.activeSelf;}
  19. public static void Show(GameObject target,bool visible){if(!target)return;var gate=target.GetComponent<UITransition>();if(!gate)gate=target.AddComponent<UITransition>();gate.SetVisible(visible);}
  20. public static void SetInput(GameObject target,bool enabled){if(!target)return;var gate=target.GetComponent<UITransition>();if(gate){gate.inputEnabled=enabled;gate.ApplyInput();}else{var g=target.GetComponent<CanvasGroup>();if(g)g.interactable=enabled;}}
  21. private void Prepare(){if(initialized)return;group=GetComponent<CanvasGroup>();if(!group)group=gameObject.AddComponent<CanvasGroup>();
  22. content=transform.Find("Card");if(!content)content=transform.Find("Border");if(!content)content=transform.Find("RewardCard");
  23. var old=GetComponent<PopupAnimationController>();if(old){old.ResetVisual();old.enabled=false;}
  24. initialized=true;
  25. }
  26. public void SetVisible(bool visible){bool first=!initialized;Prepare();bool changed=IsShown!=visible;IsShown=visible;
  27. Normalize();ApplyInput();gameObject.SetActive(visible);
  28. if(!first&&changed&&GetComponent<UnityEngine.UI.Image>())GameAudio.Sound(visible?AudioCue.UI_Open:AudioCue.UI_Close);
  29. }
  30. private void Normalize(){if(group)group.alpha=1;if(content){content.localScale=Vector3.one;var g=content.GetComponent<CanvasGroup>();if(g)g.alpha=1;}}
  31. private void ApplyInput(){if(!group)return;group.interactable=IsShown&&inputEnabled;group.blocksRaycasts=IsShown;}
  32. private void OnEnable(){if(initialized){Normalize();ApplyInput();}}
  33. private void OnDisable(){if(group){group.alpha=1;group.interactable=false;group.blocksRaycasts=false;}if(content)content.localScale=Vector3.one;}
  34. }
  35. }