UIFeedbackButton.cs 4.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using GameAudio = DevilLink2.Presentation.AudioManager;
  2. using DevilLink2.Presentation;
  3. using DG.Tweening;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.UI;
  7. namespace DevilLink2.UI
  8. {
  9. /// <summary>Preserves Button.onClick; owns only its press tween and activation debounce.</summary>
  10. public sealed class UIFeedbackButton : Button
  11. {
  12. public const double RepeatDelay = .18;
  13. private double lastActivation = double.NegativeInfinity;
  14. private Tween tween;
  15. private Vector3 restingScale;
  16. private bool pressed, hasBaseline;
  17. // Opt-in for composed modal buttons: an input lock is not a business Disabled state.
  18. public bool PreserveAppearanceDuringModalLock { get; set; }
  19. // Composed lobby cards keep their progress text fixed and supply local edge feedback.
  20. public bool EnablePressScale { get; set; } = true;
  21. private bool ModalLocked()
  22. {
  23. foreach (var effect in GetComponentsInParent<UITransition>()) {
  24. var group = effect.GetComponent<CanvasGroup>();
  25. if (!effect.IsShown || effect.IsTransitioning || (group && !group.interactable)) return true;
  26. }
  27. return false;
  28. }
  29. protected override void DoStateTransition(SelectionState state, bool instant)
  30. {
  31. // Preserve actual Disabled for inventory/availability. Only a modal input gate uses Normal.
  32. if (PreserveAppearanceDuringModalLock && interactable && ModalLocked()) {
  33. state = SelectionState.Normal; instant = true;
  34. }
  35. base.DoStateTransition(state, instant);
  36. }
  37. protected override void OnCanvasGroupChanged()
  38. {
  39. base.OnCanvasGroupChanged();
  40. if (!IsInteractable()) CancelPointerFeedback();
  41. }
  42. private void CancelPointerFeedback()
  43. {
  44. // Clear the Selectable pointer-down latch without changing keyboard focus or navigation.
  45. base.OnPointerUp(new PointerEventData(EventSystem.current) { button = PointerEventData.InputButton.Left });
  46. ResetFeedback();
  47. }
  48. private bool Available()
  49. {
  50. if (!IsActive() || !IsInteractable()) return false;
  51. foreach (var effect in GetComponentsInParent<UITransition>())
  52. if (!effect.IsShown || effect.IsTransitioning) return false;
  53. return true;
  54. }
  55. public bool TryActivate(double now)
  56. {
  57. if (double.IsNaN(now) || double.IsInfinity(now) || !Available() || now - lastActivation < RepeatDelay) return false;
  58. lastActivation = now; GameAudio.Sound(AudioCue.UI_Click); onClick.Invoke(); return true;
  59. }
  60. public override void OnPointerClick(PointerEventData data)
  61. { if (data.button == PointerEventData.InputButton.Left) TryActivate(Time.unscaledTimeAsDouble); }
  62. public override void OnSubmit(BaseEventData data) { TryActivate(Time.unscaledTimeAsDouble); }
  63. public override void OnPointerDown(PointerEventData data)
  64. {
  65. if (data.button != PointerEventData.InputButton.Left || !Available()) return;
  66. base.OnPointerDown(data);
  67. if (!EnablePressScale) return;
  68. if (!hasBaseline) { restingScale = transform.localScale; hasBaseline = true; }
  69. pressed = true; Animate(restingScale * .98f);
  70. }
  71. public override void OnPointerUp(PointerEventData data)
  72. { base.OnPointerUp(data); Release(); }
  73. public override void OnPointerExit(PointerEventData data)
  74. { base.OnPointerExit(data); Release(); }
  75. private void Release() { if (!pressed) return; if (!Available()) { CancelPointerFeedback(); return; } pressed = false; Animate(restingScale); }
  76. private void Animate(Vector3 target)
  77. {
  78. tween?.Kill(false); tween = null;
  79. if (!Application.isPlaying) { transform.localScale = target; return; }
  80. Vector3 from = transform.localScale; float progress = 0;
  81. tween = DOTween.To(() => progress, value => { progress = value; if (this) transform.localScale = Vector3.Lerp(from, target, value); },
  82. 1, .10f).SetEase(Ease.OutQuad).SetUpdate(true).SetRecyclable(false);
  83. }
  84. private void Update() { if (hasBaseline && !Available()) CancelPointerFeedback(); }
  85. private void ResetFeedback()
  86. { tween?.Kill(false); tween = null; if (hasBaseline) transform.localScale = restingScale; pressed = hasBaseline = false; }
  87. protected override void OnDisable() { ResetFeedback(); lastActivation = double.NegativeInfinity; base.OnDisable(); }
  88. protected override void OnDestroy() { ResetFeedback(); base.OnDestroy(); }
  89. }
  90. }