| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL.ExperiencesViewer |
| | 8 | | { |
| | 9 | | public interface IExperiencesViewerComponentView |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// It will be triggered when the close button is clicked. |
| | 13 | | /// </summary> |
| | 14 | | event Action onCloseButtonPressed; |
| | 15 | |
|
| | 16 | | /// <summary> |
| | 17 | | /// It will be triggered when the UI visibility of some experience changes. |
| | 18 | | /// </summary> |
| | 19 | | event Action<string, bool> onSomeExperienceUIVisibilityChanged; |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// It will be triggered when the execution of some experience changes. |
| | 23 | | /// </summary> |
| | 24 | | event Action<string, bool> onSomeExperienceExecutionChanged; |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// It represents the container transform of the component. |
| | 28 | | /// </summary> |
| | 29 | | Transform experienceViewerTransform { get; } |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// Set the available experiences component with a list of experiences. |
| | 33 | | /// </summary> |
| | 34 | | /// <param name="experiences">List of experiences (model) to be loaded.</param> |
| | 35 | | void SetAvailableExperiences(List<ExperienceRowComponentModel> experiences); |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Add an experience into the available experiences component. |
| | 39 | | /// </summary> |
| | 40 | | /// <param name="experience">Experience to add.</param> |
| | 41 | | void AddAvailableExperience(ExperienceRowComponentModel experience); |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Remove an experience from the available experiences component. |
| | 45 | | /// </summary> |
| | 46 | | /// <param name="id">Experience id to remove.</param> |
| | 47 | | void RemoveAvailableExperience(string id); |
| | 48 | |
|
| | 49 | | /// <summary> |
| | 50 | | /// Get all experiences. |
| | 51 | | /// </summary> |
| | 52 | | /// <returns>A list of experiences.</returns> |
| | 53 | | List<ExperienceRowComponentView> GetAllAvailableExperiences(); |
| | 54 | |
|
| | 55 | | /// <summary> |
| | 56 | | /// Get a specific experience. |
| | 57 | | /// </summary> |
| | 58 | | /// <param name="id">Id of the experience to search.</param> |
| | 59 | | /// <returns>An experience.</returns> |
| | 60 | | ExperienceRowComponentView GetAvailableExperienceById(string id); |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// Shows/Hides the game object of the Experiences Viewer. |
| | 64 | | /// </summary> |
| | 65 | | /// <param name="isActive">True to show it.</param> |
| | 66 | | void SetVisible(bool isActive); |
| | 67 | |
|
| | 68 | | /// <summary> |
| | 69 | | /// Shows the info toast for when the UI of an experience is hidden. |
| | 70 | | /// </summary> |
| | 71 | | void ShowUIHiddenToast(); |
| | 72 | | } |
| | 73 | |
|
| | 74 | | public class ExperiencesViewerComponentView : BaseComponentView, IExperiencesViewerComponentView |
| | 75 | | { |
| | 76 | | internal const string EXPERIENCES_POOL_NAME = "ExperiencesViewer_ExperienceRowsPool"; |
| | 77 | | internal const int EXPERIENCES_POOL_PREWARM = 10; |
| | 78 | | internal const float UI_HIDDEN_TOAST_SHOWING_TIME = 2f; |
| | 79 | |
|
| | 80 | | [Header("Assets References")] |
| | 81 | | [SerializeField] internal ExperienceRowComponentView experienceRowPrefab; |
| | 82 | |
|
| | 83 | | [Header("Prefab References")] |
| | 84 | | [SerializeField] internal ButtonComponentView closeButton; |
| | 85 | | [SerializeField] internal GridContainerComponentView availableExperiences; |
| | 86 | | [SerializeField] internal ShowHideAnimator hiddenUIToastAnimator; |
| | 87 | | public Color rowsBackgroundColor; |
| | 88 | | public Color rowsOnHoverColor; |
| | 89 | |
|
| | 90 | | public event Action onCloseButtonPressed; |
| | 91 | | public event Action<string, bool> onSomeExperienceUIVisibilityChanged; |
| | 92 | | public event Action<string, bool> onSomeExperienceExecutionChanged; |
| | 93 | |
|
| | 94 | | internal Pool experiencesPool; |
| | 95 | | internal Coroutine uiHiddenToastCoroutine; |
| | 96 | |
|
| 0 | 97 | | public Transform experienceViewerTransform => transform; |
| | 98 | |
|
| | 99 | | public override void Awake() |
| | 100 | | { |
| 10 | 101 | | base.Awake(); |
| | 102 | |
|
| 10 | 103 | | closeButton.onClick.AddListener(() => onCloseButtonPressed?.Invoke()); |
| | 104 | |
|
| 10 | 105 | | ConfigurePEXPool(); |
| 10 | 106 | | } |
| | 107 | |
|
| | 108 | | public override void RefreshControl() |
| | 109 | | { |
| 0 | 110 | | availableExperiences.RefreshControl(); |
| 0 | 111 | | } |
| | 112 | |
|
| | 113 | | public void SetAvailableExperiences(List<ExperienceRowComponentModel> experiences) |
| | 114 | | { |
| 4 | 115 | | availableExperiences.ExtractItems(); |
| 4 | 116 | | experiencesPool.ReleaseAll(); |
| | 117 | |
|
| 4 | 118 | | List<BaseComponentView> experiencesToAdd = new List<BaseComponentView>(); |
| 24 | 119 | | foreach (ExperienceRowComponentModel exp in experiences) |
| | 120 | | { |
| 8 | 121 | | AddAvailableExperience(exp); |
| | 122 | | } |
| 4 | 123 | | } |
| | 124 | |
|
| | 125 | | public void AddAvailableExperience(ExperienceRowComponentModel experience) |
| | 126 | | { |
| 9 | 127 | | experience.backgroundColor = rowsBackgroundColor; |
| 9 | 128 | | experience.onHoverColor = rowsOnHoverColor; |
| | 129 | |
|
| 9 | 130 | | ExperienceRowComponentView newRealmRow = experiencesPool.Get().gameObject.GetComponent<ExperienceRowComponen |
| 9 | 131 | | newRealmRow.Configure(experience); |
| 9 | 132 | | newRealmRow.onShowPEXUI -= OnSomeExperienceUIVisibilityChanged; |
| 9 | 133 | | newRealmRow.onShowPEXUI += OnSomeExperienceUIVisibilityChanged; |
| 9 | 134 | | newRealmRow.onStartPEX -= OnSomeExperienceExecutionChanged; |
| 9 | 135 | | newRealmRow.onStartPEX += OnSomeExperienceExecutionChanged; |
| | 136 | |
|
| 9 | 137 | | availableExperiences.AddItem(newRealmRow); |
| 9 | 138 | | } |
| | 139 | |
|
| | 140 | | public void RemoveAvailableExperience(string id) |
| | 141 | | { |
| 1 | 142 | | ExperienceRowComponentView experienceToRemove = availableExperiences.GetItems() |
| 1 | 143 | | .Select(x => x as ExperienceRowComponentView) |
| 1 | 144 | | .FirstOrDefault(x => x.model.id == id); |
| | 145 | |
|
| 1 | 146 | | if (experienceToRemove != null) |
| 1 | 147 | | availableExperiences.RemoveItem(experienceToRemove); |
| 1 | 148 | | } |
| | 149 | |
|
| | 150 | | public List<ExperienceRowComponentView> GetAllAvailableExperiences() |
| | 151 | | { |
| 0 | 152 | | return availableExperiences.GetItems() |
| 0 | 153 | | .Select(x => x as ExperienceRowComponentView) |
| | 154 | | .ToList(); |
| | 155 | | } |
| | 156 | |
|
| | 157 | | public ExperienceRowComponentView GetAvailableExperienceById(string id) |
| | 158 | | { |
| 1 | 159 | | return availableExperiences.GetItems() |
| 2 | 160 | | .Select(x => x as ExperienceRowComponentView) |
| 2 | 161 | | .FirstOrDefault(x => x.model.id == id); |
| | 162 | | } |
| | 163 | |
|
| 4 | 164 | | public void SetVisible(bool isActive) { gameObject.SetActive(isActive); } |
| | 165 | |
|
| | 166 | | public void ShowUIHiddenToast() |
| | 167 | | { |
| 1 | 168 | | if (uiHiddenToastCoroutine != null) |
| | 169 | | { |
| 0 | 170 | | StopCoroutine(uiHiddenToastCoroutine); |
| 0 | 171 | | uiHiddenToastCoroutine = null; |
| | 172 | | } |
| | 173 | |
|
| 1 | 174 | | uiHiddenToastCoroutine = StartCoroutine(ShowUIHiddenToastCoroutine()); |
| 1 | 175 | | } |
| | 176 | |
|
| | 177 | | public override void Dispose() |
| | 178 | | { |
| 20 | 179 | | base.Dispose(); |
| | 180 | |
|
| 20 | 181 | | closeButton.onClick.RemoveAllListeners(); |
| 20 | 182 | | } |
| | 183 | |
|
| | 184 | | internal void OnSomeExperienceUIVisibilityChanged(string pexId, bool isUIVisible) |
| | 185 | | { |
| 1 | 186 | | onSomeExperienceUIVisibilityChanged?.Invoke(pexId, isUIVisible); |
| 1 | 187 | | } |
| | 188 | |
|
| | 189 | | internal void OnSomeExperienceExecutionChanged(string pexId, bool isPlaying) |
| | 190 | | { |
| 1 | 191 | | onSomeExperienceExecutionChanged?.Invoke(pexId, isPlaying); |
| 1 | 192 | | } |
| | 193 | |
|
| | 194 | | internal void ConfigurePEXPool() |
| | 195 | | { |
| 11 | 196 | | experiencesPool = PoolManager.i.GetPool(EXPERIENCES_POOL_NAME); |
| 11 | 197 | | if (experiencesPool == null) |
| | 198 | | { |
| 1 | 199 | | experiencesPool = PoolManager.i.AddPool( |
| | 200 | | EXPERIENCES_POOL_NAME, |
| | 201 | | GameObject.Instantiate(experienceRowPrefab).gameObject, |
| | 202 | | maxPrewarmCount: EXPERIENCES_POOL_PREWARM, |
| | 203 | | isPersistent: true); |
| | 204 | |
|
| 1 | 205 | | experiencesPool.ForcePrewarm(); |
| | 206 | | } |
| 11 | 207 | | } |
| | 208 | |
|
| | 209 | | internal IEnumerator ShowUIHiddenToastCoroutine() |
| | 210 | | { |
| 1 | 211 | | hiddenUIToastAnimator.Show(); |
| 1 | 212 | | yield return new WaitForSeconds(UI_HIDDEN_TOAST_SHOWING_TIME); |
| 0 | 213 | | hiddenUIToastAnimator.Hide(); |
| 0 | 214 | | } |
| | 215 | |
|
| | 216 | | internal static ExperiencesViewerComponentView Create() |
| | 217 | | { |
| 0 | 218 | | ExperiencesViewerComponentView experiencesViewerComponentView = Instantiate(Resources.Load<GameObject>("Expe |
| 0 | 219 | | experiencesViewerComponentView.name = "_ExperiencesViewer"; |
| | 220 | |
|
| 0 | 221 | | return experiencesViewerComponentView; |
| | 222 | | } |
| | 223 | | } |
| | 224 | | } |