| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using TMPro; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.EventSystems; |
| | 6 | | using UnityEngine.UI; |
| | 7 | |
|
| | 8 | | public class PublicChatChannelComponentView : BaseComponentView, IChannelChatWindowView, IComponentModelConfig<PublicCha |
| | 9 | | { |
| | 10 | | [SerializeField] internal Button closeButton; |
| | 11 | | [SerializeField] internal Button backButton; |
| | 12 | | [SerializeField] internal TMP_Text nameLabel; |
| | 13 | | [SerializeField] internal TMP_Text descriptionLabel; |
| | 14 | | [SerializeField] internal ChatHUDView chatView; |
| | 15 | | [SerializeField] internal PublicChatChannelModel model; |
| | 16 | | [SerializeField] internal CanvasGroup[] previewCanvasGroup; |
| | 17 | | [SerializeField] internal Vector2 previewModeSize; |
| | 18 | |
|
| | 19 | | private Coroutine alphaRoutine; |
| | 20 | | private Vector2 originalSize; |
| | 21 | |
|
| | 22 | | public event Action OnClose; |
| | 23 | | public event Action OnBack; |
| | 24 | | public event Action<bool> OnFocused |
| | 25 | | { |
| 3 | 26 | | add => onFocused += value; |
| 1 | 27 | | remove => onFocused -= value; |
| | 28 | | } |
| | 29 | | public event Action OnClickOverWindow; |
| | 30 | |
|
| 0 | 31 | | public bool IsActive => gameObject.activeInHierarchy; |
| 1 | 32 | | public IChatHUDComponentView ChatHUD => chatView; |
| 0 | 33 | | public RectTransform Transform => (RectTransform) transform; |
| 0 | 34 | | public bool IsFocused => isFocused; |
| | 35 | |
|
| | 36 | | public static PublicChatChannelComponentView Create() |
| | 37 | | { |
| 12 | 38 | | return Instantiate(Resources.Load<PublicChatChannelComponentView>("SocialBarV1/GeneralChatChannelHUD")); |
| | 39 | | } |
| | 40 | |
|
| | 41 | | public override void Awake() |
| | 42 | | { |
| 12 | 43 | | base.Awake(); |
| 12 | 44 | | originalSize = ((RectTransform) transform).sizeDelta; |
| 13 | 45 | | backButton.onClick.AddListener(() => OnBack?.Invoke()); |
| 13 | 46 | | closeButton.onClick.AddListener(() => OnClose?.Invoke()); |
| 12 | 47 | | } |
| | 48 | |
|
| | 49 | | public override void RefreshControl() |
| | 50 | | { |
| 1 | 51 | | nameLabel.text = $"#{model.name}"; |
| 1 | 52 | | descriptionLabel.text = model.description; |
| 1 | 53 | | } |
| | 54 | |
|
| 1 | 55 | | public void Hide() => gameObject.SetActive(false); |
| | 56 | |
|
| 2 | 57 | | public void Show() => gameObject.SetActive(true); |
| | 58 | |
|
| | 59 | | public void Configure(PublicChatChannelModel model) |
| | 60 | | { |
| 1 | 61 | | this.model = model; |
| 1 | 62 | | RefreshControl(); |
| 1 | 63 | | } |
| | 64 | |
|
| | 65 | | public void ActivatePreview() |
| | 66 | | { |
| | 67 | | const float alphaTarget = 0f; |
| | 68 | |
|
| 1 | 69 | | if (!gameObject.activeInHierarchy) |
| | 70 | | { |
| 0 | 71 | | foreach (var group in previewCanvasGroup) |
| 0 | 72 | | group.alpha = alphaTarget; |
| | 73 | |
|
| 0 | 74 | | return; |
| | 75 | | } |
| | 76 | |
|
| 1 | 77 | | if (alphaRoutine != null) |
| 0 | 78 | | StopCoroutine(alphaRoutine); |
| | 79 | |
|
| 1 | 80 | | alphaRoutine = StartCoroutine(SetAlpha(alphaTarget, 0.5f)); |
| 1 | 81 | | ((RectTransform) transform).sizeDelta = previewModeSize; |
| 1 | 82 | | } |
| | 83 | |
|
| | 84 | | public void ActivatePreviewInstantly() |
| | 85 | | { |
| 2 | 86 | | if (alphaRoutine != null) |
| 0 | 87 | | StopCoroutine(alphaRoutine); |
| | 88 | |
|
| | 89 | | const float alphaTarget = 0f; |
| 32 | 90 | | foreach (var group in previewCanvasGroup) |
| 14 | 91 | | group.alpha = alphaTarget; |
| | 92 | |
|
| 2 | 93 | | ((RectTransform) transform).sizeDelta = previewModeSize; |
| 2 | 94 | | } |
| | 95 | |
|
| | 96 | | public void DeactivatePreview() |
| | 97 | | { |
| | 98 | | const float alphaTarget = 1f; |
| | 99 | |
|
| 1 | 100 | | if (!gameObject.activeInHierarchy) |
| | 101 | | { |
| 0 | 102 | | foreach (var group in previewCanvasGroup) |
| 0 | 103 | | group.alpha = alphaTarget; |
| | 104 | |
|
| 0 | 105 | | return; |
| | 106 | | } |
| | 107 | |
|
| 1 | 108 | | if (alphaRoutine != null) |
| 0 | 109 | | StopCoroutine(alphaRoutine); |
| | 110 | |
|
| 1 | 111 | | alphaRoutine = StartCoroutine(SetAlpha(alphaTarget, 0.5f)); |
| 1 | 112 | | ((RectTransform) transform).sizeDelta = originalSize; |
| 1 | 113 | | } |
| | 114 | |
|
| 1 | 115 | | public void OnPointerDown(PointerEventData eventData) => OnClickOverWindow?.Invoke(); |
| | 116 | |
|
| | 117 | | private IEnumerator SetAlpha(float target, float duration) |
| | 118 | | { |
| 2 | 119 | | var t = 0f; |
| | 120 | |
|
| 225 | 121 | | while (t < duration) |
| | 122 | | { |
| 223 | 123 | | t += Time.deltaTime; |
| | 124 | |
|
| 3568 | 125 | | foreach (var group in previewCanvasGroup) |
| 1561 | 126 | | group.alpha = Mathf.Lerp(group.alpha, target, t / duration); |
| | 127 | |
|
| 223 | 128 | | yield return null; |
| | 129 | | } |
| | 130 | |
|
| 32 | 131 | | foreach (var group in previewCanvasGroup) |
| 14 | 132 | | group.alpha = target; |
| 2 | 133 | | } |
| | 134 | | } |