| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL.Builder; |
| | 4 | | using UnityEngine; |
| | 5 | | using Object = UnityEngine.Object; |
| | 6 | |
|
| | 7 | | internal interface IScenesViewController : IDisposable |
| | 8 | | { |
| | 9 | | event Action<Dictionary<string, ISceneCardView>> OnScenesSet; |
| | 10 | | event Action<ISceneCardView> OnSceneAdded; |
| | 11 | | event Action<ISceneCardView> OnSceneRemoved; |
| | 12 | | event Action<Dictionary<string, ISceneCardView>> OnProjectScenesSet; |
| | 13 | | event Action<ISceneCardView> OnProjectSceneAdded; |
| | 14 | | event Action<ISceneCardView> OnProjectSceneRemoved; |
| | 15 | | event Action<ISceneCardView> OnProjectSelected; |
| | 16 | | event Action<Vector2Int> OnJumpInPressed; |
| | 17 | | event Action<Vector2Int> OnEditorPressed; |
| | 18 | | event Action<ISceneData, ISceneCardView> OnContextMenuPressed; |
| | 19 | | event Action<string> OnRequestOpenUrl; |
| | 20 | | void SetScenes(ISceneData[] scenesData); |
| | 21 | | void SelectScene(string id); |
| | 22 | | void AddListener(ISceneListener listener); |
| | 23 | | void AddListener(ISelectSceneListener listener); |
| | 24 | | void RemoveListener(ISceneListener listener); |
| | 25 | | void RemoveListener(ISelectSceneListener listener); |
| | 26 | | } |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// This class is responsible for receiving a list of scenes and merge it with the previous list |
| | 30 | | /// discriminating deployed and project scenes and triggering events when a new set of scenes arrive or |
| | 31 | | /// when new scenes are added or removed. |
| | 32 | | /// It instantiate and hold all the SceneCardViews to make them re-utilizable in every menu section screen. |
| | 33 | | /// </summary> |
| | 34 | | internal class ScenesViewController : IScenesViewController |
| | 35 | | { |
| | 36 | | public event Action<Dictionary<string, ISceneCardView>> OnScenesSet; |
| | 37 | | public event Action<ISceneCardView> OnSceneAdded; |
| | 38 | | public event Action<ISceneCardView> OnSceneRemoved; |
| | 39 | | public event Action<Dictionary<string, ISceneCardView>> OnProjectScenesSet; |
| | 40 | | public event Action<ISceneCardView> OnProjectSceneAdded; |
| | 41 | | public event Action<ISceneCardView> OnProjectSceneRemoved; |
| | 42 | | public event Action<ISceneCardView> OnProjectSelected; |
| | 43 | | public event Action<Vector2Int> OnJumpInPressed; |
| | 44 | | public event Action<Vector2Int> OnEditorPressed; |
| | 45 | | public event Action<ISceneData, ISceneCardView> OnContextMenuPressed; |
| | 46 | | public event Action<string> OnRequestOpenUrl; |
| | 47 | |
|
| 1 | 48 | | private Dictionary<string, ISceneCardView> scenes = new Dictionary<string, ISceneCardView>(); |
| 1 | 49 | | private Dictionary<string, ISceneCardView> projects = new Dictionary<string, ISceneCardView>(); |
| | 50 | |
|
| | 51 | | private ISceneCardView selectedScene; |
| | 52 | |
|
| 1 | 53 | | private readonly ScenesRefreshHelper scenesRefreshHelper = new ScenesRefreshHelper(); |
| | 54 | | private readonly SceneCardView sceneCardViewPrefab; |
| | 55 | | private readonly Transform defaultParent; |
| | 56 | |
|
| | 57 | | /// <summary> |
| | 58 | | /// Ctor |
| | 59 | | /// </summary> |
| | 60 | | /// <param name="sceneCardViewPrefab">prefab for scene's card</param> |
| | 61 | | /// <param name="defaultParent">default parent for scene's card</param> |
| 1 | 62 | | public ScenesViewController(SceneCardView sceneCardViewPrefab, Transform defaultParent = null) |
| | 63 | | { |
| 1 | 64 | | this.sceneCardViewPrefab = sceneCardViewPrefab; |
| 1 | 65 | | this.defaultParent = defaultParent; |
| 1 | 66 | | } |
| | 67 | |
|
| | 68 | | /// <summary> |
| | 69 | | /// Set current user scenes (deployed and projects) |
| | 70 | | /// </summary> |
| | 71 | | /// <param name="scenesData">list of scenes</param> |
| | 72 | | void IScenesViewController.SetScenes(ISceneData[] scenesData) |
| | 73 | | { |
| 5 | 74 | | scenesRefreshHelper.Set(scenes, projects); |
| | 75 | |
|
| 5 | 76 | | scenes = new Dictionary<string, ISceneCardView>(); |
| 5 | 77 | | projects = new Dictionary<string, ISceneCardView>(); |
| | 78 | |
|
| | 79 | | // update or create new scenes view |
| 28 | 80 | | for (int i = 0; i < scenesData.Length; i++) |
| | 81 | | { |
| 9 | 82 | | SetScene(scenesData[i]); |
| | 83 | | } |
| | 84 | |
|
| | 85 | | // remove old deployed scenes |
| 5 | 86 | | if (scenesRefreshHelper.oldDeployedScenes != null) |
| | 87 | | { |
| 5 | 88 | | using (var iterator = scenesRefreshHelper.oldDeployedScenes.GetEnumerator()) |
| | 89 | | { |
| 5 | 90 | | while (iterator.MoveNext()) |
| | 91 | | { |
| 0 | 92 | | OnSceneRemoved?.Invoke(iterator.Current.Value); |
| 0 | 93 | | DestroyCardView(iterator.Current.Value); |
| | 94 | | } |
| 5 | 95 | | } |
| | 96 | | } |
| | 97 | |
|
| | 98 | | // remove old project scenes |
| 5 | 99 | | if (scenesRefreshHelper.oldProjectsScenes != null) |
| | 100 | | { |
| 5 | 101 | | using (var iterator = scenesRefreshHelper.oldProjectsScenes.GetEnumerator()) |
| | 102 | | { |
| 5 | 103 | | while (iterator.MoveNext()) |
| | 104 | | { |
| 0 | 105 | | OnProjectSceneRemoved?.Invoke(iterator.Current.Value); |
| 0 | 106 | | DestroyCardView(iterator.Current.Value); |
| | 107 | | } |
| 5 | 108 | | } |
| | 109 | | } |
| | 110 | |
|
| | 111 | | // notify scenes set if needed |
| 5 | 112 | | if (scenesRefreshHelper.isOldDeployedScenesEmpty) |
| | 113 | | { |
| 1 | 114 | | OnScenesSet?.Invoke(scenes); |
| | 115 | | } |
| | 116 | |
|
| 5 | 117 | | if (scenesRefreshHelper.isOldProjectScenesEmpty) |
| | 118 | | { |
| 3 | 119 | | OnProjectScenesSet?.Invoke(projects); |
| | 120 | | } |
| 2 | 121 | | } |
| | 122 | |
|
| | 123 | | /// <summary> |
| | 124 | | /// Set selected scene |
| | 125 | | /// </summary> |
| | 126 | | /// <param name="id">scene id</param> |
| | 127 | | void IScenesViewController.SelectScene(string id) |
| | 128 | | { |
| 0 | 129 | | ISceneCardView sceneCardView = null; |
| 0 | 130 | | if (scenes.TryGetValue(id, out ISceneCardView deployedSceneCardView)) |
| | 131 | | { |
| 0 | 132 | | sceneCardView = deployedSceneCardView; |
| 0 | 133 | | } |
| 0 | 134 | | else if (projects.TryGetValue(id, out ISceneCardView projectSceneCardView)) |
| | 135 | | { |
| 0 | 136 | | sceneCardView = projectSceneCardView; |
| | 137 | | } |
| | 138 | |
|
| 0 | 139 | | selectedScene = sceneCardView; |
| | 140 | |
|
| 0 | 141 | | if (sceneCardView != null) |
| | 142 | | { |
| 0 | 143 | | OnProjectSelected?.Invoke(sceneCardView); |
| | 144 | | } |
| 0 | 145 | | } |
| | 146 | |
|
| | 147 | | void IScenesViewController.AddListener(ISceneListener listener) |
| | 148 | | { |
| 0 | 149 | | OnSceneAdded += listener.SceneAdded; |
| 0 | 150 | | OnSceneRemoved += listener.SceneRemoved; |
| 0 | 151 | | OnScenesSet += listener.SetScenes; |
| 0 | 152 | | listener.SetScenes(scenes); |
| 0 | 153 | | } |
| | 154 | |
|
| | 155 | | void IScenesViewController.AddListener(ISelectSceneListener listener) |
| | 156 | | { |
| 0 | 157 | | OnProjectSelected += listener.OnSelectScene; |
| 0 | 158 | | listener.OnSelectScene(selectedScene); |
| 0 | 159 | | } |
| | 160 | |
|
| | 161 | | void IScenesViewController.RemoveListener(ISceneListener listener) |
| | 162 | | { |
| 0 | 163 | | OnSceneAdded -= listener.SceneAdded; |
| 0 | 164 | | OnSceneRemoved -= listener.SceneRemoved; |
| 0 | 165 | | OnScenesSet -= listener.SetScenes; |
| 0 | 166 | | } |
| | 167 | |
|
| 0 | 168 | | void IScenesViewController.RemoveListener(ISelectSceneListener listener) { OnProjectSelected -= listener.OnSelectSce |
| | 169 | |
|
| | 170 | | public void Dispose() |
| | 171 | | { |
| 1 | 172 | | using (var iterator = scenes.GetEnumerator()) |
| | 173 | | { |
| 1 | 174 | | while (iterator.MoveNext()) |
| | 175 | | { |
| 0 | 176 | | iterator.Current.Value.Dispose(); |
| | 177 | | } |
| 1 | 178 | | } |
| | 179 | |
|
| 1 | 180 | | scenes.Clear(); |
| | 181 | |
|
| 1 | 182 | | using (var iterator = projects.GetEnumerator()) |
| | 183 | | { |
| 3 | 184 | | while (iterator.MoveNext()) |
| | 185 | | { |
| 2 | 186 | | iterator.Current.Value.Dispose(); |
| | 187 | | } |
| 1 | 188 | | } |
| | 189 | |
|
| 1 | 190 | | projects.Clear(); |
| 1 | 191 | | } |
| | 192 | |
|
| | 193 | | private void SetScene(ISceneData sceneData) |
| | 194 | | { |
| 9 | 195 | | bool shouldNotifyAdd = (sceneData.isDeployed && !scenesRefreshHelper.isOldDeployedScenesEmpty) || |
| | 196 | | (!sceneData.isDeployed && !scenesRefreshHelper.isOldProjectScenesEmpty); |
| | 197 | |
|
| 9 | 198 | | if (scenesRefreshHelper.IsSceneDeployStatusChanged(sceneData)) |
| | 199 | | { |
| 2 | 200 | | ChangeSceneDeployStatus(sceneData, shouldNotifyAdd); |
| 2 | 201 | | } |
| 7 | 202 | | else if (scenesRefreshHelper.IsSceneUpdate(sceneData)) |
| | 203 | | { |
| 5 | 204 | | UpdateScene(sceneData); |
| 5 | 205 | | } |
| | 206 | | else |
| | 207 | | { |
| 2 | 208 | | CreateScene(sceneData, shouldNotifyAdd); |
| | 209 | | } |
| 2 | 210 | | } |
| | 211 | |
|
| | 212 | | private void ChangeSceneDeployStatus(ISceneData sceneData, bool shouldNotifyAdd) |
| | 213 | | { |
| 2 | 214 | | var cardView = RemoveScene(sceneData, true); |
| 2 | 215 | | cardView.Setup(sceneData); |
| 2 | 216 | | AddScene(sceneData, cardView, shouldNotifyAdd); |
| 2 | 217 | | } |
| | 218 | |
|
| | 219 | | private void UpdateScene(ISceneData sceneData) |
| | 220 | | { |
| 5 | 221 | | var cardView = RemoveScene(sceneData, false); |
| 5 | 222 | | cardView.Setup(sceneData); |
| 5 | 223 | | AddScene(sceneData, cardView, false); |
| 5 | 224 | | } |
| | 225 | |
|
| | 226 | | private void CreateScene(ISceneData sceneData, bool shouldNotifyAdd) |
| | 227 | | { |
| 2 | 228 | | var cardView = CreateCardView(); |
| 2 | 229 | | cardView.Setup(sceneData); |
| 2 | 230 | | AddScene(sceneData, cardView, shouldNotifyAdd); |
| 2 | 231 | | } |
| | 232 | |
|
| | 233 | | private ISceneCardView RemoveScene(ISceneData sceneData, bool notify) |
| | 234 | | { |
| 7 | 235 | | bool wasDeployed = scenesRefreshHelper.WasDeployedScene(sceneData); |
| 7 | 236 | | var dictionary = wasDeployed ? scenesRefreshHelper.oldDeployedScenes : scenesRefreshHelper.oldProjectsScenes; |
| | 237 | |
|
| 7 | 238 | | if (dictionary.TryGetValue(sceneData.id, out ISceneCardView sceneCardView)) |
| | 239 | | { |
| 7 | 240 | | dictionary.Remove(sceneData.id); |
| | 241 | |
|
| 7 | 242 | | if (notify) |
| | 243 | | { |
| 2 | 244 | | if (wasDeployed) |
| 2 | 245 | | OnSceneRemoved?.Invoke(sceneCardView); |
| | 246 | | else |
| 0 | 247 | | OnProjectSceneRemoved?.Invoke(sceneCardView); |
| | 248 | | } |
| | 249 | |
|
| 7 | 250 | | return sceneCardView; |
| | 251 | | } |
| | 252 | |
|
| 0 | 253 | | return null; |
| | 254 | | } |
| | 255 | |
|
| | 256 | | private void AddScene(ISceneData sceneData, ISceneCardView sceneCardView, bool notify) |
| | 257 | | { |
| 9 | 258 | | var dictionary = sceneData.isDeployed ? scenes : projects; |
| 9 | 259 | | if (dictionary.ContainsKey(sceneData.id)) |
| 0 | 260 | | return; |
| 9 | 261 | | dictionary.Add(sceneData.id, sceneCardView); |
| | 262 | |
|
| 9 | 263 | | if (notify) |
| | 264 | | { |
| 2 | 265 | | if (sceneData.isDeployed) |
| 1 | 266 | | OnSceneAdded?.Invoke(sceneCardView); |
| | 267 | | else |
| 1 | 268 | | OnProjectSceneAdded?.Invoke(sceneCardView); |
| | 269 | | } |
| 7 | 270 | | } |
| | 271 | |
|
| | 272 | | private ISceneCardView CreateCardView() |
| | 273 | | { |
| 2 | 274 | | SceneCardView sceneCardView = Object.Instantiate(sceneCardViewPrefab); |
| | 275 | | ISceneCardView view = sceneCardView; |
| | 276 | |
|
| 2 | 277 | | view.SetActive(false); |
| 2 | 278 | | view.ConfigureDefaultParent(defaultParent); |
| 2 | 279 | | view.SetToDefaultParent(); |
| | 280 | |
|
| 2 | 281 | | view.OnEditorPressed += OnEditorPressed; |
| 2 | 282 | | view.OnContextMenuPressed += OnContextMenuPressed; |
| 2 | 283 | | view.OnJumpInPressed += OnJumpInPressed; |
| 2 | 284 | | view.OnSettingsPressed += OnSceneSettingsPressed; |
| | 285 | |
|
| 2 | 286 | | return view; |
| | 287 | | } |
| | 288 | |
|
| | 289 | | private void DestroyCardView(ISceneCardView sceneCardView) |
| | 290 | | { |
| | 291 | | // NOTE: there is actually no need to unsubscribe here, but, just in case... |
| 0 | 292 | | sceneCardView.OnEditorPressed -= OnEditorPressed; |
| 0 | 293 | | sceneCardView.OnContextMenuPressed -= OnContextMenuPressed; |
| 0 | 294 | | sceneCardView.OnJumpInPressed -= OnJumpInPressed; |
| 0 | 295 | | sceneCardView.OnSettingsPressed -= OnSceneSettingsPressed; |
| | 296 | |
|
| 0 | 297 | | sceneCardView.Dispose(); |
| 0 | 298 | | } |
| | 299 | |
|
| 0 | 300 | | private void OnSceneSettingsPressed(ISceneData sceneData) { OnRequestOpenUrl?.Invoke($"https://builder.decentraland. |
| | 301 | | } |