| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using TMPro; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.UI; |
| | 8 | |
|
| | 9 | | namespace DCL.ExperiencesViewer |
| | 10 | | { |
| | 11 | | public class ExperiencesViewerComponentView : BaseComponentView, IExperiencesViewerComponentView |
| | 12 | | { |
| | 13 | | private const int EXPERIENCES_POOL_PREWARM = 10; |
| | 14 | | private const float UI_HIDDEN_TOAST_SHOWING_TIME = 2f; |
| | 15 | | private const string EXPERIENCES_POOL_NAME = "ExperiencesViewer_ExperienceRowsPool"; |
| | 16 | |
|
| | 17 | | [Header("Assets References")] |
| | 18 | | [SerializeField] internal ExperienceRowComponentView experienceRowPrefab; |
| | 19 | |
|
| | 20 | | [Header("Prefab References")] |
| | 21 | | [SerializeField] internal Button closeButton; |
| | 22 | | [SerializeField] internal GridContainerComponentView availableExperiences; |
| | 23 | | [SerializeField] internal ShowHideAnimator toastAnimator; |
| | 24 | | [SerializeField] internal TMP_Text toastLabel; |
| | 25 | |
|
| | 26 | | private Pool experiencesPool; |
| | 27 | | private Coroutine toastRoutine; |
| | 28 | |
|
| | 29 | | public Color rowsBackgroundColor; |
| | 30 | | public Color rowsOnHoverColor; |
| | 31 | |
|
| | 32 | | public event Action OnCloseButtonPressed; |
| | 33 | | public event Action<string, bool> OnExperienceUiVisibilityChanged; |
| | 34 | | public event Action<string, bool> OnExperienceExecutionChanged; |
| | 35 | |
|
| 0 | 36 | | public Transform ExperienceViewerTransform => transform; |
| | 37 | |
|
| | 38 | | public static ExperiencesViewerComponentView Create() |
| | 39 | | { |
| 0 | 40 | | ExperiencesViewerComponentView experiencesViewerComponentView = Instantiate(Resources.Load<GameObject>("Expe |
| 0 | 41 | | experiencesViewerComponentView.name = "_ExperiencesViewer"; |
| | 42 | |
|
| 0 | 43 | | return experiencesViewerComponentView; |
| | 44 | | } |
| | 45 | |
|
| | 46 | | public override void Awake() |
| | 47 | | { |
| 9 | 48 | | base.Awake(); |
| | 49 | |
|
| 9 | 50 | | closeButton.onClick.AddListener(() => OnCloseButtonPressed?.Invoke()); |
| | 51 | |
|
| 9 | 52 | | ConfigurePool(); |
| 9 | 53 | | } |
| | 54 | |
|
| | 55 | | public override void RefreshControl() |
| | 56 | | { |
| 0 | 57 | | availableExperiences.RefreshControl(); |
| 0 | 58 | | } |
| | 59 | |
|
| | 60 | | public void SetAvailableExperiences(List<ExperienceRowComponentModel> experiences) |
| | 61 | | { |
| 4 | 62 | | availableExperiences.ExtractItems(); |
| 4 | 63 | | experiencesPool.ReleaseAll(); |
| | 64 | |
|
| 24 | 65 | | foreach (ExperienceRowComponentModel exp in experiences) |
| 8 | 66 | | AddAvailableExperience(exp); |
| 4 | 67 | | } |
| | 68 | |
|
| | 69 | | public void AddAvailableExperience(ExperienceRowComponentModel experience) |
| | 70 | | { |
| 11 | 71 | | experience.backgroundColor = rowsBackgroundColor; |
| 11 | 72 | | experience.onHoverColor = rowsOnHoverColor; |
| | 73 | |
|
| 11 | 74 | | ExperienceRowComponentView newRealmRow = experiencesPool.Get().gameObject.GetComponent<ExperienceRowComponen |
| 11 | 75 | | newRealmRow.Configure(experience); |
| 11 | 76 | | newRealmRow.onShowPEXUI -= OnSomeExperienceUIVisibilityChanged; |
| 11 | 77 | | newRealmRow.onShowPEXUI += OnSomeExperienceUIVisibilityChanged; |
| 11 | 78 | | newRealmRow.onStartPEX -= OnSomeExperienceExecutionChanged; |
| 11 | 79 | | newRealmRow.onStartPEX += OnSomeExperienceExecutionChanged; |
| | 80 | |
|
| 11 | 81 | | availableExperiences.AddItemWithResize(newRealmRow); |
| 11 | 82 | | } |
| | 83 | |
|
| | 84 | | public void RemoveAvailableExperience(string id) |
| | 85 | | { |
| 1 | 86 | | ExperienceRowComponentView experienceToRemove = availableExperiences.GetItems() |
| 1 | 87 | | .Select(x => x as ExperienceRowComponentView) |
| 1 | 88 | | .FirstOrDefault(x => x.model.id == id); |
| | 89 | |
|
| 1 | 90 | | if (experienceToRemove != null) |
| 1 | 91 | | availableExperiences.RemoveItem(experienceToRemove); |
| 1 | 92 | | } |
| | 93 | |
|
| | 94 | | public List<ExperienceRowComponentView> GetAllAvailableExperiences() |
| | 95 | | { |
| 0 | 96 | | return availableExperiences.GetItems() |
| 0 | 97 | | .Select(x => x as ExperienceRowComponentView) |
| | 98 | | .ToList(); |
| | 99 | | } |
| | 100 | |
|
| | 101 | | public ExperienceRowComponentView GetAvailableExperienceById(string id) |
| | 102 | | { |
| 1 | 103 | | return availableExperiences.GetItems() |
| 2 | 104 | | .Select(x => x as ExperienceRowComponentView) |
| 2 | 105 | | .FirstOrDefault(x => x.model.id == id); |
| | 106 | | } |
| | 107 | |
|
| 4 | 108 | | public void SetVisible(bool isActive) { gameObject.SetActive(isActive); } |
| | 109 | |
|
| | 110 | | public void ShowUiHiddenToast(string pxName) |
| | 111 | | { |
| 1 | 112 | | if (toastRoutine != null) |
| | 113 | | { |
| 0 | 114 | | StopCoroutine(toastRoutine); |
| 0 | 115 | | toastRoutine = null; |
| | 116 | | } |
| | 117 | |
|
| 1 | 118 | | pxName = string.IsNullOrEmpty(pxName) ? "Experience" : pxName; |
| | 119 | |
|
| 1 | 120 | | toastLabel.text = $"<b>{pxName}</b> UI hidden"; |
| 1 | 121 | | toastRoutine = StartCoroutine(ShowToastRoutine()); |
| 1 | 122 | | } |
| | 123 | |
|
| | 124 | | public void ShowUiShownToast(string pxName) |
| | 125 | | { |
| 0 | 126 | | if (toastRoutine != null) |
| | 127 | | { |
| 0 | 128 | | StopCoroutine(toastRoutine); |
| 0 | 129 | | toastRoutine = null; |
| | 130 | | } |
| | 131 | |
|
| 0 | 132 | | pxName = string.IsNullOrEmpty(pxName) ? "Experience" : pxName; |
| | 133 | |
|
| 0 | 134 | | toastLabel.text = $"<b>{pxName}</b> UI shown"; |
| 0 | 135 | | toastRoutine = StartCoroutine(ShowToastRoutine()); |
| 0 | 136 | | } |
| | 137 | |
|
| | 138 | | public void ShowEnabledToast(string pxName) |
| | 139 | | { |
| 0 | 140 | | if (toastRoutine != null) |
| | 141 | | { |
| 0 | 142 | | StopCoroutine(toastRoutine); |
| 0 | 143 | | toastRoutine = null; |
| | 144 | | } |
| | 145 | |
|
| 0 | 146 | | pxName = string.IsNullOrEmpty(pxName) ? "Experience" : pxName; |
| | 147 | |
|
| 0 | 148 | | toastLabel.text = $"<b>{pxName}</b> activated"; |
| 0 | 149 | | toastRoutine = StartCoroutine(ShowToastRoutine()); |
| 0 | 150 | | } |
| | 151 | |
|
| | 152 | | public void ShowDisabledToast(string pxName) |
| | 153 | | { |
| 0 | 154 | | if (toastRoutine != null) |
| | 155 | | { |
| 0 | 156 | | StopCoroutine(toastRoutine); |
| 0 | 157 | | toastRoutine = null; |
| | 158 | | } |
| | 159 | |
|
| 0 | 160 | | pxName = string.IsNullOrEmpty(pxName) ? "Experience" : pxName; |
| | 161 | |
|
| 0 | 162 | | toastLabel.text = $"<b>{pxName}</b> deactivated"; |
| 0 | 163 | | toastRoutine = StartCoroutine(ShowToastRoutine()); |
| 0 | 164 | | } |
| | 165 | |
|
| | 166 | | public override void Dispose() |
| | 167 | | { |
| 18 | 168 | | base.Dispose(); |
| | 169 | |
|
| 18 | 170 | | closeButton.onClick.RemoveAllListeners(); |
| 18 | 171 | | } |
| | 172 | |
|
| | 173 | | private void OnSomeExperienceUIVisibilityChanged(string pexId, bool isUIVisible) => |
| 1 | 174 | | OnExperienceUiVisibilityChanged?.Invoke(pexId, isUIVisible); |
| | 175 | |
|
| | 176 | | private void OnSomeExperienceExecutionChanged(string pexId, bool isPlaying) => |
| 3 | 177 | | OnExperienceExecutionChanged?.Invoke(pexId, isPlaying); |
| | 178 | |
|
| | 179 | | private void ConfigurePool() |
| | 180 | | { |
| 9 | 181 | | experiencesPool = PoolManager.i.GetPool(EXPERIENCES_POOL_NAME); |
| | 182 | |
|
| 17 | 183 | | if (experiencesPool != null) return; |
| | 184 | |
|
| 1 | 185 | | experiencesPool = PoolManager.i.AddPool( |
| | 186 | | EXPERIENCES_POOL_NAME, |
| | 187 | | Instantiate(experienceRowPrefab).gameObject, |
| | 188 | | maxPrewarmCount: EXPERIENCES_POOL_PREWARM, |
| | 189 | | isPersistent: true); |
| | 190 | |
|
| 1 | 191 | | experiencesPool.ForcePrewarm(); |
| 1 | 192 | | } |
| | 193 | |
|
| | 194 | | private IEnumerator ShowToastRoutine() |
| | 195 | | { |
| 1 | 196 | | toastAnimator.Show(); |
| 1 | 197 | | yield return new WaitForSeconds(UI_HIDDEN_TOAST_SHOWING_TIME); |
| 0 | 198 | | toastAnimator.Hide(); |
| 0 | 199 | | } |
| | 200 | | } |
| | 201 | | } |