| | 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; |
| | 52 | |
|
| 7 | 53 | | DataStore.i.world.portableExperienceIds.OnAdded += OnPEXSceneAdded; |
| 7 | 54 | | DataStore.i.world.portableExperienceIds.OnRemoved += OnPEXSceneRemoved; |
| | 55 | |
|
| 7 | 56 | | CheckCurrentActivePortableExperiences(); |
| | 57 | |
|
| 7 | 58 | | userProfile = UserProfile.GetOwnUserProfile(); |
| 7 | 59 | | if (userProfile != null) |
| | 60 | | { |
| 7 | 61 | | userProfile.OnUpdate += OnUserProfileUpdated; |
| 7 | 62 | | OnUserProfileUpdated(userProfile); |
| | 63 | | } |
| | 64 | |
|
| 7 | 65 | | isInitialized.Set(view.experienceViewerTransform); |
| 7 | 66 | | } |
| | 67 | |
|
| | 68 | | public void SetVisibility(bool visible) |
| | 69 | | { |
| 16 | 70 | | view.SetVisible(visible); |
| 16 | 71 | | isOpen.Set(visible); |
| 16 | 72 | | } |
| | 73 | |
|
| | 74 | | public void Dispose() |
| | 75 | | { |
| 7 | 76 | | view.onCloseButtonPressed -= OnCloseButtonPressed; |
| 7 | 77 | | view.onSomeExperienceUIVisibilityChanged -= OnSomeExperienceUIVisibilityChanged; |
| 7 | 78 | | view.onSomeExperienceExecutionChanged -= OnSomeExperienceExecutionChanged; |
| 7 | 79 | | isOpen.OnChange -= IsOpenChanged; |
| | 80 | |
|
| 7 | 81 | | DataStore.i.world.portableExperienceIds.OnAdded -= OnPEXSceneAdded; |
| 7 | 82 | | DataStore.i.world.portableExperienceIds.OnRemoved -= OnPEXSceneRemoved; |
| | 83 | |
|
| 7 | 84 | | if (userProfile != null) |
| 7 | 85 | | userProfile.OnUpdate -= OnUserProfileUpdated; |
| 7 | 86 | | } |
| | 87 | |
|
| 2 | 88 | | internal void OnCloseButtonPressed() { SetVisibility(false); } |
| | 89 | |
|
| | 90 | | internal void OnSomeExperienceUIVisibilityChanged(string pexId, bool isVisible) |
| | 91 | | { |
| 0 | 92 | | activePEXScenes.TryGetValue(pexId, out IParcelScene scene); |
| 0 | 93 | | if (scene != null) |
| | 94 | | { |
| 0 | 95 | | UIScreenSpace sceneUIComponent = scene.componentsManagerLegacy.GetSceneSharedComponent<UIScreenSpace>(); |
| 0 | 96 | | sceneUIComponent.canvas.enabled = isVisible; |
| | 97 | | } |
| | 98 | |
|
| 0 | 99 | | if (!isVisible) |
| 0 | 100 | | view.ShowUIHiddenToast(); |
| 0 | 101 | | } |
| | 102 | |
|
| | 103 | | internal void OnSomeExperienceExecutionChanged(string pexId, bool isPlaying) |
| | 104 | | { |
| 0 | 105 | | if (isPlaying) |
| | 106 | | { |
| 0 | 107 | | WebInterface.SetDisabledPortableExperiences(pausedPEXScenesIds.Where(x => x != pexId).ToArray()); |
| 0 | 108 | | } |
| | 109 | | else |
| | 110 | | { |
| | 111 | | // We only keep the experience paused in the list if our avatar has the related wearable equipped |
| 0 | 112 | | if (userProfile != null && userProfile.avatar.wearables.Contains(pexId)) |
| | 113 | | { |
| 0 | 114 | | if (!pausedPEXScenesIds.Contains(pexId)) |
| 0 | 115 | | pausedPEXScenesIds.Add(pexId); |
| | 116 | |
|
| 0 | 117 | | WebInterface.SetDisabledPortableExperiences(pausedPEXScenesIds.ToArray()); |
| 0 | 118 | | } |
| | 119 | | else |
| | 120 | | { |
| 0 | 121 | | WebInterface.KillPortableExperience(pexId); |
| | 122 | | } |
| | 123 | | } |
| 0 | 124 | | } |
| | 125 | |
|
| 26 | 126 | | internal void IsOpenChanged(bool current, bool previous) { SetVisibility(current); } |
| | 127 | |
|
| | 128 | | internal void CheckCurrentActivePortableExperiences() |
| | 129 | | { |
| 8 | 130 | | activePEXScenes.Clear(); |
| 8 | 131 | | pausedPEXScenesIds.Clear(); |
| | 132 | |
|
| 8 | 133 | | if (DCL.Environment.i.world.state != null) |
| | 134 | | { |
| 0 | 135 | | List<GlobalScene> activePortableExperiences = |
| | 136 | | PortableExperienceUtils.GetActivePortableExperienceScenes(); |
| 0 | 137 | | foreach (GlobalScene pexScene in activePortableExperiences) |
| | 138 | | { |
| 0 | 139 | | OnPEXSceneAdded(pexScene.sceneData.id); |
| | 140 | | } |
| | 141 | | } |
| | 142 | |
|
| 8 | 143 | | numOfLoadedExperiences.Set(activePEXScenes.Count); |
| 8 | 144 | | } |
| | 145 | |
|
| | 146 | | public void OnPEXSceneAdded(string id) |
| | 147 | | { |
| 0 | 148 | | IParcelScene scene = Environment.i.world.state.GetScene(id); |
| 0 | 149 | | ExperienceRowComponentView experienceToUpdate = view.GetAvailableExperienceById(scene.sceneData.id); |
| | 150 | |
|
| 0 | 151 | | if (activePEXScenes.ContainsKey(scene.sceneData.id)) |
| | 152 | | { |
| 0 | 153 | | activePEXScenes[scene.sceneData.id] = scene; |
| 0 | 154 | | pausedPEXScenesIds.Remove(scene.sceneData.id); |
| | 155 | |
|
| 0 | 156 | | if (experienceToUpdate != null) |
| 0 | 157 | | experienceToUpdate.SetUIVisibility(true); |
| | 158 | |
|
| 0 | 159 | | return; |
| | 160 | | } |
| | 161 | |
|
| 0 | 162 | | GlobalScene newPortableExperienceScene = scene as GlobalScene; |
| 0 | 163 | | DataStore.i.experiencesViewer.activeExperience.Get().Add(scene.sceneData.id); |
| | 164 | |
|
| 0 | 165 | | if (pausedPEXScenesIds.Contains(scene.sceneData.id)) |
| | 166 | | { |
| 0 | 167 | | pausedPEXScenesIds.Remove(scene.sceneData.id); |
| | 168 | |
|
| 0 | 169 | | if (experienceToUpdate != null) |
| 0 | 170 | | experienceToUpdate.SetAsPlaying(true); |
| 0 | 171 | | } |
| | 172 | | else |
| | 173 | | { |
| 0 | 174 | | ExperienceRowComponentModel experienceToAdd = new ExperienceRowComponentModel |
| | 175 | | { |
| | 176 | | id = newPortableExperienceScene.sceneData.id, |
| | 177 | | isPlaying = true, |
| | 178 | | isUIVisible = true, |
| | 179 | | name = newPortableExperienceScene.sceneName, |
| | 180 | | iconUri = newPortableExperienceScene.iconUrl, |
| | 181 | | allowStartStop = userProfile != null && userProfile.avatar.wearables.Contains(newPortableExperienceS |
| | 182 | | }; |
| | 183 | |
|
| 0 | 184 | | view.AddAvailableExperience(experienceToAdd); |
| 0 | 185 | | activePEXScenes.Add(scene.sceneData.id, scene); |
| 0 | 186 | | numOfLoadedExperiences.Set(activePEXScenes.Count); |
| | 187 | | } |
| 0 | 188 | | } |
| | 189 | |
|
| | 190 | | public void OnPEXSceneRemoved(string id) |
| | 191 | | { |
| 0 | 192 | | if (!activePEXScenes.ContainsKey(id)) |
| 0 | 193 | | return; |
| | 194 | |
|
| 0 | 195 | | DataStore.i.experiencesViewer.activeExperience.Get().Remove(id); |
| 0 | 196 | | if (pausedPEXScenesIds.Contains(id)) |
| | 197 | | { |
| 0 | 198 | | ExperienceRowComponentView experienceToUpdate = view.GetAvailableExperienceById(id); |
| 0 | 199 | | if (experienceToUpdate != null) |
| | 200 | | { |
| 0 | 201 | | if (!experienceToUpdate.model.isPlaying) |
| 0 | 202 | | return; |
| | 203 | | } |
| | 204 | | } |
| | 205 | |
|
| 0 | 206 | | view.RemoveAvailableExperience(id); |
| 0 | 207 | | activePEXScenes.Remove(id); |
| 0 | 208 | | pausedPEXScenesIds.Remove(id); |
| 0 | 209 | | numOfLoadedExperiences.Set(activePEXScenes.Count); |
| 0 | 210 | | } |
| | 211 | |
|
| | 212 | | internal void OnUserProfileUpdated(UserProfile userProfile) |
| | 213 | | { |
| 7 | 214 | | List<string> experiencesIdsToRemove = new List<string>(); |
| | 215 | |
|
| 14 | 216 | | foreach (var pex in activePEXScenes) |
| | 217 | | { |
| | 218 | | // We remove from the list all those experiences related to wearables that are not equipped |
| 0 | 219 | | if (wearableCatalog.ContainsKey(pex.Key) && !userProfile.avatar.wearables.Contains(pex.Key)) |
| 0 | 220 | | experiencesIdsToRemove.Add(pex.Key); |
| | 221 | | } |
| | 222 | |
|
| 14 | 223 | | foreach (string pexId in experiencesIdsToRemove) |
| | 224 | | { |
| 0 | 225 | | view.RemoveAvailableExperience(pexId); |
| 0 | 226 | | activePEXScenes.Remove(pexId); |
| 0 | 227 | | pausedPEXScenesIds.Remove(pexId); |
| | 228 | | } |
| | 229 | |
|
| 7 | 230 | | numOfLoadedExperiences.Set(activePEXScenes.Count); |
| | 231 | |
|
| 7 | 232 | | if (lastDisablePEXSentToKernel != pausedPEXScenesIds) |
| | 233 | | { |
| 7 | 234 | | lastDisablePEXSentToKernel = pausedPEXScenesIds; |
| 7 | 235 | | WebInterface.SetDisabledPortableExperiences(pausedPEXScenesIds.ToArray()); |
| | 236 | | } |
| | 237 | |
|
| 7 | 238 | | List<ExperienceRowComponentView> loadedExperiences = view.GetAllAvailableExperiences(); |
| 14 | 239 | | for (int i = 0; i < loadedExperiences.Count; i++) |
| | 240 | | { |
| 0 | 241 | | loadedExperiences[i].SetAllowStartStop(userProfile.avatar.wearables.Contains(loadedExperiences[i].model. |
| | 242 | | } |
| 7 | 243 | | } |
| | 244 | |
|
| 0 | 245 | | internal virtual IExperiencesViewerComponentView CreateView() => ExperiencesViewerComponentView.Create(); |
| | 246 | | } |
| | 247 | | } |