| | 1 | | using DCL.Components; |
| | 2 | | using DCL.Controllers; |
| | 3 | | using DCL.Interface; |
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Linq; |
| | 7 | | using UnityEngine; |
| | 8 | |
|
| | 9 | | namespace DCL.ExperiencesViewer |
| | 10 | | { |
| | 11 | | public interface IExperiencesViewerComponentController : IDisposable |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// Initializes the experiences viewer controller. |
| | 15 | | /// </summary> |
| | 16 | | /// <param name="sceneController">Scene controller used to detect when PEX are created/removed.</param> |
| | 17 | | void Initialize(ISceneController sceneController); |
| | 18 | |
|
| | 19 | | /// <summary> |
| | 20 | | /// Set the experience viewer feature as visible or not. |
| | 21 | | /// </summary> |
| | 22 | | /// <param name="visible">True for showing it.</param> |
| | 23 | | void SetVisibility(bool visible); |
| | 24 | | } |
| | 25 | |
|
| | 26 | | public class ExperiencesViewerComponentController : IExperiencesViewerComponentController |
| | 27 | | { |
| 7 | 28 | | internal BaseVariable<Transform> isInitialized => DataStore.i.experiencesViewer.isInitialized; |
| 37 | 29 | | internal BaseVariable<bool> isOpen => DataStore.i.experiencesViewer.isOpen; |
| 15 | 30 | | internal BaseVariable<int> numOfLoadedExperiences => DataStore.i.experiencesViewer.numOfLoadedExperiences; |
| 0 | 31 | | public BaseDictionary<string, WearableItem> wearableCatalog => DataStore.i.common.wearables; |
| | 32 | |
|
| | 33 | | internal IExperiencesViewerComponentView view; |
| | 34 | | internal ISceneController sceneController; |
| | 35 | | internal UserProfile userProfile; |
| | 36 | | internal CatalogController catalog; |
| 7 | 37 | | internal Dictionary<string, IParcelScene> activePEXScenes = new Dictionary<string, IParcelScene>(); |
| 7 | 38 | | internal List<string> pausedPEXScenesIds = new List<string>(); |
| | 39 | | internal List<string> lastDisablePEXSentToKernel; |
| | 40 | |
|
| | 41 | | public void Initialize(ISceneController sceneController) |
| | 42 | | { |
| 7 | 43 | | view = CreateView(); |
| 7 | 44 | | view.onCloseButtonPressed += OnCloseButtonPressed; |
| 7 | 45 | | view.onSomeExperienceUIVisibilityChanged += OnSomeExperienceUIVisibilityChanged; |
| 7 | 46 | | view.onSomeExperienceExecutionChanged += OnSomeExperienceExecutionChanged; |
| | 47 | |
|
| 7 | 48 | | isOpen.OnChange += IsOpenChanged; |
| 7 | 49 | | IsOpenChanged(isOpen.Get(), false); |
| | 50 | |
|
| 7 | 51 | | this.sceneController = sceneController; |
| 7 | 52 | | if (this.sceneController != null) |
| | 53 | | { |
| 7 | 54 | | this.sceneController.OnNewPortableExperienceSceneAdded += OnPEXSceneAdded; |
| 7 | 55 | | this.sceneController.OnNewPortableExperienceSceneRemoved += OnPEXSceneRemoved; |
| | 56 | | } |
| | 57 | |
|
| 7 | 58 | | CheckCurrentActivePortableExperiences(); |
| | 59 | |
|
| 7 | 60 | | userProfile = UserProfile.GetOwnUserProfile(); |
| 7 | 61 | | if (userProfile != null) |
| | 62 | | { |
| 7 | 63 | | userProfile.OnUpdate += OnUserProfileUpdated; |
| 7 | 64 | | OnUserProfileUpdated(userProfile); |
| | 65 | | } |
| | 66 | |
|
| 7 | 67 | | isInitialized.Set(view.experienceViewerTransform); |
| 7 | 68 | | } |
| | 69 | |
|
| | 70 | | public void SetVisibility(bool visible) |
| | 71 | | { |
| 16 | 72 | | view.SetVisible(visible); |
| 16 | 73 | | isOpen.Set(visible); |
| 16 | 74 | | } |
| | 75 | |
|
| | 76 | | public void Dispose() |
| | 77 | | { |
| 7 | 78 | | view.onCloseButtonPressed -= OnCloseButtonPressed; |
| 7 | 79 | | view.onSomeExperienceUIVisibilityChanged -= OnSomeExperienceUIVisibilityChanged; |
| 7 | 80 | | view.onSomeExperienceExecutionChanged -= OnSomeExperienceExecutionChanged; |
| 7 | 81 | | isOpen.OnChange -= IsOpenChanged; |
| | 82 | |
|
| 7 | 83 | | if (sceneController != null) |
| | 84 | | { |
| 7 | 85 | | sceneController.OnNewPortableExperienceSceneAdded -= OnPEXSceneAdded; |
| 7 | 86 | | sceneController.OnNewPortableExperienceSceneRemoved -= OnPEXSceneRemoved; |
| | 87 | | } |
| | 88 | |
|
| 7 | 89 | | if (userProfile != null) |
| 7 | 90 | | userProfile.OnUpdate -= OnUserProfileUpdated; |
| 7 | 91 | | } |
| | 92 | |
|
| 2 | 93 | | internal void OnCloseButtonPressed() { SetVisibility(false); } |
| | 94 | |
|
| | 95 | | internal void OnSomeExperienceUIVisibilityChanged(string pexId, bool isVisible) |
| | 96 | | { |
| 0 | 97 | | activePEXScenes.TryGetValue(pexId, out IParcelScene scene); |
| 0 | 98 | | if (scene != null) |
| | 99 | | { |
| 0 | 100 | | UIScreenSpace sceneUIComponent = scene.GetSharedComponent<UIScreenSpace>(); |
| 0 | 101 | | sceneUIComponent.canvas.enabled = isVisible; |
| | 102 | | } |
| | 103 | |
|
| 0 | 104 | | if (!isVisible) |
| 0 | 105 | | view.ShowUIHiddenToast(); |
| 0 | 106 | | } |
| | 107 | |
|
| | 108 | | internal void OnSomeExperienceExecutionChanged(string pexId, bool isPlaying) |
| | 109 | | { |
| 0 | 110 | | if (isPlaying) |
| | 111 | | { |
| 0 | 112 | | WebInterface.SetDisabledPortableExperiences(pausedPEXScenesIds.Where(x => x != pexId).ToArray()); |
| 0 | 113 | | } |
| | 114 | | else |
| | 115 | | { |
| | 116 | | // We only keep the experience paused in the list if our avatar has the related wearable equipped |
| 0 | 117 | | if (userProfile != null && userProfile.avatar.wearables.Contains(pexId)) |
| | 118 | | { |
| 0 | 119 | | if (!pausedPEXScenesIds.Contains(pexId)) |
| 0 | 120 | | pausedPEXScenesIds.Add(pexId); |
| | 121 | |
|
| 0 | 122 | | WebInterface.SetDisabledPortableExperiences(pausedPEXScenesIds.ToArray()); |
| 0 | 123 | | } |
| | 124 | | else |
| | 125 | | { |
| 0 | 126 | | WebInterface.KillPortableExperience(pexId); |
| | 127 | | } |
| | 128 | | } |
| 0 | 129 | | } |
| | 130 | |
|
| 26 | 131 | | internal void IsOpenChanged(bool current, bool previous) { SetVisibility(current); } |
| | 132 | |
|
| | 133 | | internal void CheckCurrentActivePortableExperiences() |
| | 134 | | { |
| 8 | 135 | | activePEXScenes.Clear(); |
| 8 | 136 | | pausedPEXScenesIds.Clear(); |
| | 137 | |
|
| 8 | 138 | | if (DCL.Environment.i.world.state != null) |
| | 139 | | { |
| 0 | 140 | | List<GlobalScene> activePortableExperiences = WorldStateUtils.GetActivePortableExperienceScenes(); |
| 0 | 141 | | foreach (GlobalScene pexScene in activePortableExperiences) |
| | 142 | | { |
| 0 | 143 | | OnPEXSceneAdded(pexScene); |
| | 144 | | } |
| | 145 | | } |
| | 146 | |
|
| 8 | 147 | | numOfLoadedExperiences.Set(activePEXScenes.Count); |
| 8 | 148 | | } |
| | 149 | |
|
| | 150 | | public void OnPEXSceneAdded(IParcelScene scene) |
| | 151 | | { |
| 0 | 152 | | ExperienceRowComponentView experienceToUpdate = view.GetAvailableExperienceById(scene.sceneData.id); |
| | 153 | |
|
| 0 | 154 | | if (activePEXScenes.ContainsKey(scene.sceneData.id)) |
| | 155 | | { |
| 0 | 156 | | activePEXScenes[scene.sceneData.id] = scene; |
| 0 | 157 | | pausedPEXScenesIds.Remove(scene.sceneData.id); |
| | 158 | |
|
| 0 | 159 | | if (experienceToUpdate != null) |
| 0 | 160 | | experienceToUpdate.SetUIVisibility(true); |
| | 161 | |
|
| 0 | 162 | | return; |
| | 163 | | } |
| | 164 | |
|
| 0 | 165 | | GlobalScene newPortableExperienceScene = scene as GlobalScene; |
| | 166 | |
|
| 0 | 167 | | if (pausedPEXScenesIds.Contains(scene.sceneData.id)) |
| | 168 | | { |
| 0 | 169 | | pausedPEXScenesIds.Remove(scene.sceneData.id); |
| | 170 | |
|
| 0 | 171 | | if (experienceToUpdate != null) |
| 0 | 172 | | experienceToUpdate.SetAsPlaying(true); |
| 0 | 173 | | } |
| | 174 | | else |
| | 175 | | { |
| 0 | 176 | | ExperienceRowComponentModel experienceToAdd = new ExperienceRowComponentModel |
| | 177 | | { |
| | 178 | | id = newPortableExperienceScene.sceneData.id, |
| | 179 | | isPlaying = true, |
| | 180 | | isUIVisible = true, |
| | 181 | | name = newPortableExperienceScene.sceneName, |
| | 182 | | iconUri = newPortableExperienceScene.iconUrl, |
| | 183 | | allowStartStop = userProfile != null && userProfile.avatar.wearables.Contains(newPortableExperienceS |
| | 184 | | }; |
| | 185 | |
|
| 0 | 186 | | view.AddAvailableExperience(experienceToAdd); |
| 0 | 187 | | activePEXScenes.Add(scene.sceneData.id, scene); |
| 0 | 188 | | numOfLoadedExperiences.Set(activePEXScenes.Count); |
| | 189 | | } |
| 0 | 190 | | } |
| | 191 | |
|
| | 192 | | public void OnPEXSceneRemoved(string id) |
| | 193 | | { |
| 0 | 194 | | if (!activePEXScenes.ContainsKey(id)) |
| 0 | 195 | | return; |
| | 196 | |
|
| 0 | 197 | | if (pausedPEXScenesIds.Contains(id)) |
| | 198 | | { |
| 0 | 199 | | ExperienceRowComponentView experienceToUpdate = view.GetAvailableExperienceById(id); |
| 0 | 200 | | if (experienceToUpdate != null) |
| | 201 | | { |
| 0 | 202 | | if (!experienceToUpdate.model.isPlaying) |
| 0 | 203 | | return; |
| | 204 | | } |
| | 205 | | } |
| | 206 | |
|
| 0 | 207 | | view.RemoveAvailableExperience(id); |
| 0 | 208 | | activePEXScenes.Remove(id); |
| 0 | 209 | | pausedPEXScenesIds.Remove(id); |
| 0 | 210 | | numOfLoadedExperiences.Set(activePEXScenes.Count); |
| 0 | 211 | | } |
| | 212 | |
|
| | 213 | | internal void OnUserProfileUpdated(UserProfile userProfile) |
| | 214 | | { |
| 7 | 215 | | List<string> experiencesIdsToRemove = new List<string>(); |
| | 216 | |
|
| 14 | 217 | | foreach (var pex in activePEXScenes) |
| | 218 | | { |
| | 219 | | // We remove from the list all those experiences related to wearables that are not equipped |
| 0 | 220 | | if (wearableCatalog.ContainsKey(pex.Key) && !userProfile.avatar.wearables.Contains(pex.Key)) |
| 0 | 221 | | experiencesIdsToRemove.Add(pex.Key); |
| | 222 | | } |
| | 223 | |
|
| 14 | 224 | | foreach (string pexId in experiencesIdsToRemove) |
| | 225 | | { |
| 0 | 226 | | view.RemoveAvailableExperience(pexId); |
| 0 | 227 | | activePEXScenes.Remove(pexId); |
| 0 | 228 | | pausedPEXScenesIds.Remove(pexId); |
| | 229 | | } |
| | 230 | |
|
| 7 | 231 | | numOfLoadedExperiences.Set(activePEXScenes.Count); |
| | 232 | |
|
| 7 | 233 | | if (lastDisablePEXSentToKernel != pausedPEXScenesIds) |
| | 234 | | { |
| 7 | 235 | | lastDisablePEXSentToKernel = pausedPEXScenesIds; |
| 7 | 236 | | WebInterface.SetDisabledPortableExperiences(pausedPEXScenesIds.ToArray()); |
| | 237 | | } |
| 7 | 238 | | } |
| | 239 | |
|
| 0 | 240 | | internal virtual IExperiencesViewerComponentView CreateView() => ExperiencesViewerComponentView.Create(); |
| | 241 | | } |
| | 242 | | } |