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