| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- using GameAudio = DevilLink2.Presentation.AudioManager;
- using DevilLink2.Presentation;
- using DG.Tweening;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using UnityEngine.UI;
- namespace DevilLink2.UI
- {
- /// <summary>Preserves Button.onClick; owns only its press tween and activation debounce.</summary>
- public sealed class UIFeedbackButton : Button
- {
- public const double RepeatDelay = .18;
- private double lastActivation = double.NegativeInfinity;
- private Tween tween;
- private Vector3 restingScale;
- private bool pressed, hasBaseline;
- // Opt-in for composed modal buttons: an input lock is not a business Disabled state.
- public bool PreserveAppearanceDuringModalLock { get; set; }
- // Composed lobby cards keep their progress text fixed and supply local edge feedback.
- public bool EnablePressScale { get; set; } = true;
- private bool ModalLocked()
- {
- foreach (var effect in GetComponentsInParent<UITransition>()) {
- var group = effect.GetComponent<CanvasGroup>();
- if (!effect.IsShown || effect.IsTransitioning || (group && !group.interactable)) return true;
- }
- return false;
- }
- protected override void DoStateTransition(SelectionState state, bool instant)
- {
- // Preserve actual Disabled for inventory/availability. Only a modal input gate uses Normal.
- if (PreserveAppearanceDuringModalLock && interactable && ModalLocked()) {
- state = SelectionState.Normal; instant = true;
- }
- base.DoStateTransition(state, instant);
- }
- protected override void OnCanvasGroupChanged()
- {
- base.OnCanvasGroupChanged();
- if (!IsInteractable()) CancelPointerFeedback();
- }
- private void CancelPointerFeedback()
- {
- // Clear the Selectable pointer-down latch without changing keyboard focus or navigation.
- base.OnPointerUp(new PointerEventData(EventSystem.current) { button = PointerEventData.InputButton.Left });
- ResetFeedback();
- }
- private bool Available()
- {
- if (!IsActive() || !IsInteractable()) return false;
- foreach (var effect in GetComponentsInParent<UITransition>())
- if (!effect.IsShown || effect.IsTransitioning) return false;
- return true;
- }
- public bool TryActivate(double now)
- {
- if (double.IsNaN(now) || double.IsInfinity(now) || !Available() || now - lastActivation < RepeatDelay) return false;
- lastActivation = now; GameAudio.Sound(AudioCue.UI_Click); onClick.Invoke(); return true;
- }
- public override void OnPointerClick(PointerEventData data)
- { if (data.button == PointerEventData.InputButton.Left) TryActivate(Time.unscaledTimeAsDouble); }
- public override void OnSubmit(BaseEventData data) { TryActivate(Time.unscaledTimeAsDouble); }
- public override void OnPointerDown(PointerEventData data)
- {
- if (data.button != PointerEventData.InputButton.Left || !Available()) return;
- base.OnPointerDown(data);
- if (!EnablePressScale) return;
- if (!hasBaseline) { restingScale = transform.localScale; hasBaseline = true; }
- pressed = true; Animate(restingScale * .98f);
- }
- public override void OnPointerUp(PointerEventData data)
- { base.OnPointerUp(data); Release(); }
- public override void OnPointerExit(PointerEventData data)
- { base.OnPointerExit(data); Release(); }
- private void Release() { if (!pressed) return; if (!Available()) { CancelPointerFeedback(); return; } pressed = false; Animate(restingScale); }
- private void Animate(Vector3 target)
- {
- tween?.Kill(false); tween = null;
- if (!Application.isPlaying) { transform.localScale = target; return; }
- Vector3 from = transform.localScale; float progress = 0;
- tween = DOTween.To(() => progress, value => { progress = value; if (this) transform.localScale = Vector3.Lerp(from, target, value); },
- 1, .10f).SetEase(Ease.OutQuad).SetUpdate(true).SetRecyclable(false);
- }
- private void Update() { if (hasBaseline && !Available()) CancelPointerFeedback(); }
- private void ResetFeedback()
- { tween?.Kill(false); tween = null; if (hasBaseline) transform.localScale = restingScale; pressed = hasBaseline = false; }
- protected override void OnDisable() { ResetFeedback(); lastActivation = double.NegativeInfinity; base.OnDisable(); }
- protected override void OnDestroy() { ResetFeedback(); base.OnDestroy(); }
- }
- }
|