| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using System; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.UI; |
| | 5 | | using DG.Tweening; |
| | 6 | | using System.Threading; |
| | 7 | |
|
| | 8 | | namespace DCL.Social.Passports |
| | 9 | | { |
| | 10 | | public class PlayerPassportHUDView : BaseComponentView, IPlayerPassportHUDView |
| | 11 | | { |
| | 12 | | [SerializeField] internal Button hideCardButton; |
| | 13 | | [SerializeField] internal Button hideCardButtonGuest; |
| | 14 | | [SerializeField] internal Button backgroundButton; |
| | 15 | | [SerializeField] internal GameObject container; |
| | 16 | | [SerializeField] internal RectTransform passportMask; |
| | 17 | | [SerializeField] internal Canvas passportCanvas; |
| | 18 | | [SerializeField] internal CanvasGroup passportCanvasGroup; |
| | 19 | |
|
| 0 | 20 | | public int PassportCurrentSortingOrder => passportCanvas.sortingOrder; |
| | 21 | |
|
| | 22 | | public event Action OnClose; |
| | 23 | |
|
| 0 | 24 | | private CancellationTokenSource animationCancellationToken = new CancellationTokenSource(); |
| | 25 | | private MouseCatcher mouseCatcher; |
| | 26 | |
|
| | 27 | | public static PlayerPassportHUDView CreateView() => |
| 0 | 28 | | Instantiate(Resources.Load<GameObject>("PlayerPassport")).GetComponent<PlayerPassportHUDView>(); |
| | 29 | |
|
| | 30 | | public void Initialize(MouseCatcher mouseCatcher) |
| | 31 | | { |
| 0 | 32 | | hideCardButton.onClick.RemoveAllListeners(); |
| 0 | 33 | | hideCardButton.onClick.AddListener(ClosePassport); |
| 0 | 34 | | hideCardButtonGuest.onClick.RemoveAllListeners(); |
| 0 | 35 | | hideCardButtonGuest.onClick.AddListener(ClosePassport); |
| 0 | 36 | | backgroundButton.onClick.RemoveAllListeners(); |
| 0 | 37 | | backgroundButton.onClick.AddListener(ClosePassport); |
| 0 | 38 | | this.mouseCatcher = mouseCatcher; |
| | 39 | |
|
| 0 | 40 | | if (mouseCatcher != null) |
| 0 | 41 | | mouseCatcher.OnMouseDown += ClosePassport; |
| 0 | 42 | | } |
| | 43 | |
|
| | 44 | | public void SetVisibility(bool visible) |
| | 45 | | { |
| 0 | 46 | | gameObject.SetActive(visible); |
| 0 | 47 | | } |
| | 48 | |
|
| | 49 | | public void SetPassportPanelVisibility(bool visible) |
| | 50 | | { |
| 0 | 51 | | if (visible && mouseCatcher != null) |
| | 52 | | { |
| 0 | 53 | | mouseCatcher.UnlockCursor(); |
| | 54 | | } |
| | 55 | |
|
| 0 | 56 | | animationCancellationToken.Cancel(); |
| 0 | 57 | | animationCancellationToken = new CancellationTokenSource(); |
| | 58 | |
|
| 0 | 59 | | if (visible) |
| | 60 | | { |
| 0 | 61 | | container.SetActive(true); |
| 0 | 62 | | ShowPassportAnimation(animationCancellationToken.Token).Forget(); |
| | 63 | | } |
| | 64 | | else |
| | 65 | | { |
| 0 | 66 | | HidePassportAnimation(animationCancellationToken.Token); |
| | 67 | | } |
| | 68 | |
|
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | private async UniTaskVoid ShowPassportAnimation(CancellationToken cancellationToken) |
| | 72 | | { |
| 0 | 73 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 74 | | passportMask.anchoredPosition = new Vector2(0, -180); |
| 0 | 75 | | passportCanvasGroup.alpha = 0; |
| 0 | 76 | | passportCanvasGroup.DOFade(1, 0.3f) |
| | 77 | | .SetEase(Ease.Linear); |
| | 78 | | try |
| | 79 | | { |
| 0 | 80 | | Vector2 endPosition = new Vector2(0, 0); |
| 0 | 81 | | Vector2 currentPosition = passportMask.anchoredPosition; |
| 0 | 82 | | DOTween.To(() => currentPosition, x => currentPosition = x, endPosition, 0.3f).SetEase(Ease.OutBack); |
| 0 | 83 | | while (passportMask.anchoredPosition.y < 0) |
| | 84 | | { |
| 0 | 85 | | passportMask.anchoredPosition = currentPosition; |
| 0 | 86 | | await UniTask.NextFrame(cancellationToken); |
| | 87 | | } |
| 0 | 88 | | } |
| 0 | 89 | | catch (OperationCanceledException) { } |
| 0 | 90 | | } |
| | 91 | |
|
| | 92 | | private async UniTaskVoid HidePassportAnimation(CancellationToken cancellationToken) |
| | 93 | | { |
| 0 | 94 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 95 | | passportMask.anchoredPosition = new Vector2(0, 0); |
| 0 | 96 | | passportCanvasGroup.alpha = 1; |
| 0 | 97 | | passportCanvasGroup.DOFade(0, 0.3f) |
| | 98 | | .SetEase(Ease.Linear); |
| | 99 | | try |
| | 100 | | { |
| 0 | 101 | | Vector2 endPosition = new Vector2(0, -180); |
| 0 | 102 | | Vector2 currentPosition = passportMask.anchoredPosition; |
| 0 | 103 | | DOTween.To(() => currentPosition, x => currentPosition = x, endPosition, 0.3f).SetEase(Ease.InBack); |
| 0 | 104 | | while (passportMask.anchoredPosition.y > -180) |
| | 105 | | { |
| 0 | 106 | | passportMask.anchoredPosition = currentPosition; |
| 0 | 107 | | await UniTask.NextFrame(cancellationToken); |
| | 108 | | } |
| 0 | 109 | | container.SetActive(false); |
| 0 | 110 | | } |
| 0 | 111 | | catch (OperationCanceledException) |
| | 112 | | { |
| 0 | 113 | | Debug.LogError("Cancelled show passport animation"); |
| 0 | 114 | | } |
| 0 | 115 | | } |
| | 116 | |
|
| | 117 | | public override void RefreshControl() |
| | 118 | | { |
| 0 | 119 | | } |
| | 120 | |
|
| | 121 | | public override void Dispose() |
| | 122 | | { |
| 0 | 123 | | if (mouseCatcher != null) |
| 0 | 124 | | mouseCatcher.OnMouseDown -= ClosePassport; |
| | 125 | |
|
| 0 | 126 | | animationCancellationToken?.Cancel(); |
| 0 | 127 | | animationCancellationToken?.Dispose(); |
| 0 | 128 | | animationCancellationToken = null; |
| 0 | 129 | | } |
| | 130 | |
|
| | 131 | | private void ClosePassport() |
| | 132 | | { |
| 0 | 133 | | OnClose?.Invoke(); |
| 0 | 134 | | } |
| | 135 | | } |
| | 136 | | } |