| | 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 a specific experience. |
| | 51 | | /// </summary> |
| | 52 | | /// <param name="id">Id of the experience to search.</param> |
| | 53 | | /// <returns>An experience.</returns> |
| | 54 | | ExperienceRowComponentView GetAvailableExperienceById(string id); |
| | 55 | |
|
| | 56 | | /// <summary> |
| | 57 | | /// Shows/Hides the game object of the Experiences Viewer. |
| | 58 | | /// </summary> |
| | 59 | | /// <param name="isActive">True to show it.</param> |
| | 60 | | void SetVisible(bool isActive); |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// Shows the info toast for when the UI of an experience is hidden. |
| | 64 | | /// </summary> |
| | 65 | | void ShowUIHiddenToast(); |
| | 66 | | } |
| | 67 | |
|
| | 68 | | public class ExperiencesViewerComponentView : BaseComponentView, IExperiencesViewerComponentView |
| | 69 | | { |
| | 70 | | internal const string EXPERIENCES_POOL_NAME = "ExperiencesViewer_ExperienceRowsPool"; |
| | 71 | | internal const int EXPERIENCES_POOL_PREWARM = 10; |
| | 72 | | internal const float UI_HIDDEN_TOAST_SHOWING_TIME = 2f; |
| | 73 | |
|
| | 74 | | [Header("Assets References")] |
| | 75 | | [SerializeField] internal ExperienceRowComponentView experienceRowPrefab; |
| | 76 | |
|
| | 77 | | [Header("Prefab References")] |
| | 78 | | [SerializeField] internal ButtonComponentView closeButton; |
| | 79 | | [SerializeField] internal GridContainerComponentView availableExperiences; |
| | 80 | | [SerializeField] internal ShowHideAnimator hiddenUIToastAnimator; |
| | 81 | | public Color rowsBackgroundColor; |
| | 82 | | public Color rowsOnHoverColor; |
| | 83 | |
|
| | 84 | | public event Action onCloseButtonPressed; |
| | 85 | | public event Action<string, bool> onSomeExperienceUIVisibilityChanged; |
| | 86 | | public event Action<string, bool> onSomeExperienceExecutionChanged; |
| | 87 | |
|
| | 88 | | internal Pool experiencesPool; |
| | 89 | | internal Coroutine uiHiddenToastCoroutine; |
| | 90 | |
|
| 0 | 91 | | public Transform experienceViewerTransform => transform; |
| | 92 | |
|
| | 93 | | public override void Awake() |
| | 94 | | { |
| 10 | 95 | | base.Awake(); |
| | 96 | |
|
| 10 | 97 | | closeButton.onClick.AddListener(() => onCloseButtonPressed?.Invoke()); |
| | 98 | |
|
| 10 | 99 | | ConfigurePEXPool(); |
| 10 | 100 | | } |
| | 101 | |
|
| | 102 | | public override void RefreshControl() |
| | 103 | | { |
| 0 | 104 | | availableExperiences.RefreshControl(); |
| 0 | 105 | | } |
| | 106 | |
|
| | 107 | | public void SetAvailableExperiences(List<ExperienceRowComponentModel> experiences) |
| | 108 | | { |
| 4 | 109 | | availableExperiences.ExtractItems(); |
| 4 | 110 | | experiencesPool.ReleaseAll(); |
| | 111 | |
|
| 4 | 112 | | List<BaseComponentView> experiencesToAdd = new List<BaseComponentView>(); |
| 24 | 113 | | foreach (ExperienceRowComponentModel exp in experiences) |
| | 114 | | { |
| 8 | 115 | | AddAvailableExperience(exp); |
| | 116 | | } |
| 4 | 117 | | } |
| | 118 | |
|
| | 119 | | public void AddAvailableExperience(ExperienceRowComponentModel experience) |
| | 120 | | { |
| 9 | 121 | | experience.backgroundColor = rowsBackgroundColor; |
| 9 | 122 | | experience.onHoverColor = rowsOnHoverColor; |
| | 123 | |
|
| 9 | 124 | | ExperienceRowComponentView newRealmRow = experiencesPool.Get().gameObject.GetComponent<ExperienceRowComponen |
| 9 | 125 | | newRealmRow.Configure(experience); |
| 9 | 126 | | newRealmRow.onShowPEXUI -= OnSomeExperienceUIVisibilityChanged; |
| 9 | 127 | | newRealmRow.onShowPEXUI += OnSomeExperienceUIVisibilityChanged; |
| 9 | 128 | | newRealmRow.onStartPEX -= OnSomeExperienceExecutionChanged; |
| 9 | 129 | | newRealmRow.onStartPEX += OnSomeExperienceExecutionChanged; |
| | 130 | |
|
| 9 | 131 | | availableExperiences.AddItem(newRealmRow); |
| 9 | 132 | | } |
| | 133 | |
|
| | 134 | | public void RemoveAvailableExperience(string id) |
| | 135 | | { |
| 1 | 136 | | ExperienceRowComponentView experienceToRemove = availableExperiences.GetItems() |
| 1 | 137 | | .Select(x => x as ExperienceRowComponentView) |
| 1 | 138 | | .FirstOrDefault(x => x.model.id == id); |
| | 139 | |
|
| 1 | 140 | | if (experienceToRemove != null) |
| 1 | 141 | | availableExperiences.RemoveItem(experienceToRemove); |
| 1 | 142 | | } |
| | 143 | |
|
| | 144 | | public ExperienceRowComponentView GetAvailableExperienceById(string id) |
| | 145 | | { |
| 1 | 146 | | return availableExperiences.GetItems() |
| 2 | 147 | | .Select(x => x as ExperienceRowComponentView) |
| 2 | 148 | | .FirstOrDefault(x => x.model.id == id); |
| | 149 | | } |
| | 150 | |
|
| 4 | 151 | | public void SetVisible(bool isActive) { gameObject.SetActive(isActive); } |
| | 152 | |
|
| | 153 | | public void ShowUIHiddenToast() |
| | 154 | | { |
| 1 | 155 | | if (uiHiddenToastCoroutine != null) |
| | 156 | | { |
| 0 | 157 | | StopCoroutine(uiHiddenToastCoroutine); |
| 0 | 158 | | uiHiddenToastCoroutine = null; |
| | 159 | | } |
| | 160 | |
|
| 1 | 161 | | uiHiddenToastCoroutine = StartCoroutine(ShowUIHiddenToastCoroutine()); |
| 1 | 162 | | } |
| | 163 | |
|
| | 164 | | public override void Dispose() |
| | 165 | | { |
| 20 | 166 | | base.Dispose(); |
| | 167 | |
|
| 20 | 168 | | closeButton.onClick.RemoveAllListeners(); |
| 20 | 169 | | } |
| | 170 | |
|
| | 171 | | internal void OnSomeExperienceUIVisibilityChanged(string pexId, bool isUIVisible) |
| | 172 | | { |
| 1 | 173 | | onSomeExperienceUIVisibilityChanged?.Invoke(pexId, isUIVisible); |
| 1 | 174 | | } |
| | 175 | |
|
| | 176 | | internal void OnSomeExperienceExecutionChanged(string pexId, bool isPlaying) |
| | 177 | | { |
| 1 | 178 | | onSomeExperienceExecutionChanged?.Invoke(pexId, isPlaying); |
| 1 | 179 | | } |
| | 180 | |
|
| | 181 | | internal void ConfigurePEXPool() |
| | 182 | | { |
| 11 | 183 | | experiencesPool = PoolManager.i.GetPool(EXPERIENCES_POOL_NAME); |
| 11 | 184 | | if (experiencesPool == null) |
| | 185 | | { |
| 1 | 186 | | experiencesPool = PoolManager.i.AddPool( |
| | 187 | | EXPERIENCES_POOL_NAME, |
| | 188 | | GameObject.Instantiate(experienceRowPrefab).gameObject, |
| | 189 | | maxPrewarmCount: EXPERIENCES_POOL_PREWARM, |
| | 190 | | isPersistent: true); |
| | 191 | |
|
| 1 | 192 | | experiencesPool.ForcePrewarm(); |
| | 193 | | } |
| 11 | 194 | | } |
| | 195 | |
|
| | 196 | | internal IEnumerator ShowUIHiddenToastCoroutine() |
| | 197 | | { |
| 1 | 198 | | hiddenUIToastAnimator.Show(); |
| 1 | 199 | | yield return new WaitForSeconds(UI_HIDDEN_TOAST_SHOWING_TIME); |
| 0 | 200 | | hiddenUIToastAnimator.Hide(); |
| 0 | 201 | | } |
| | 202 | |
|
| | 203 | | internal static ExperiencesViewerComponentView Create() |
| | 204 | | { |
| 0 | 205 | | ExperiencesViewerComponentView experiencesViewerComponentView = Instantiate(Resources.Load<GameObject>("Expe |
| 0 | 206 | | experiencesViewerComponentView.name = "_ExperiencesViewer"; |
| | 207 | |
|
| 0 | 208 | | return experiencesViewerComponentView; |
| | 209 | | } |
| | 210 | | } |
| | 211 | | } |