| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using DCL; |
| | 6 | | using DCL.Builder; |
| | 7 | | using UnityEngine; |
| | 8 | |
|
| | 9 | | /// <summary> |
| | 10 | | /// This interface is responsible to handle the projects of builder in world panel. |
| | 11 | | /// It will handle the listeners that will add/remove projects to the list |
| | 12 | | /// </summary> |
| | 13 | | internal interface IProjectsController |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// This will be released when a project start the publish flow |
| | 17 | | /// </summary> |
| | 18 | | event Action<ProjectData> OnPublishProject; |
| | 19 | |
|
| | 20 | | /// <summary> |
| | 21 | | /// This will be released when a project must be deleted |
| | 22 | | /// </summary> |
| | 23 | | event Action<ProjectData> OnDeleteProject; |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// This will be released when a project must be duplicated |
| | 27 | | /// </summary> |
| | 28 | | event Action<ProjectData> OnDuplicateProject; |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// This action will be called when the user want to edit a project |
| | 32 | | /// </summary> |
| | 33 | | event Action<ProjectData> OnEditorPressed; |
| | 34 | |
|
| | 35 | | /// <summary> |
| | 36 | | /// This action will be called each time that we changes the project list |
| | 37 | | /// </summary> |
| | 38 | | event Action<Dictionary<string, IProjectCardView>> OnProjectsSet; |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// When the user click on expand or collapse the project |
| | 42 | | /// </summary> |
| | 43 | | event Action OnExpandMenuPressed; |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// This will updated the deployments of the projects looking at the lands deployments |
| | 47 | | /// </summary> |
| | 48 | | void UpdateDeploymentStatus(); |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// This will set the project list |
| | 52 | | /// </summary> |
| | 53 | | /// <param name="projects"></param> |
| | 54 | | void SetProjects(ProjectData[] projects); |
| | 55 | |
|
| | 56 | | /// <summary> |
| | 57 | | /// This will set the handler for the context menu of the scenes |
| | 58 | | /// </summary> |
| | 59 | | /// <param name="sceneContextMenuHandler"></param> |
| | 60 | | void SetSceneContextMenuHandler(SceneContextMenuHandler sceneContextMenuHandler); |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// This will add a listener that will be responsible to add/remove projects |
| | 64 | | /// </summary> |
| | 65 | | /// <param name="listener"></param> |
| | 66 | | void AddListener(IProjectsListener listener); |
| | 67 | |
|
| | 68 | | /// <summary> |
| | 69 | | /// This will remove a listener that will be responsible to add/remove projects |
| | 70 | | /// </summary> |
| | 71 | | /// <param name="listener"></param> |
| | 72 | | void RemoveListener(IProjectsListener listener); |
| | 73 | |
|
| | 74 | | /// <summary> |
| | 75 | | /// This will get all projects |
| | 76 | | /// </summary> |
| | 77 | | /// <returns>Dictionary of all the projects indexed by their id</returns> |
| | 78 | | Dictionary<string, IProjectCardView> GetProjects(); |
| | 79 | |
|
| | 80 | | void Dispose(); |
| | 81 | | } |
| | 82 | |
|
| | 83 | | internal class ProjectsController : IProjectsController |
| | 84 | | { |
| | 85 | | public event Action<ProjectData> OnDeleteProject; |
| | 86 | | public event Action<ProjectData> OnDuplicateProject; |
| | 87 | | public event Action<ProjectData> OnPublishProject; |
| | 88 | | public event Action<ProjectData> OnEditorPressed; |
| | 89 | | public event Action<Dictionary<string, IProjectCardView>> OnProjectsSet; |
| | 90 | |
|
| | 91 | | public event Action OnExpandMenuPressed; |
| | 92 | |
|
| 3 | 93 | | private readonly ISectionSearchHandler sceneSearchHandler = new SectionSearchHandler(); |
| | 94 | |
|
| 3 | 95 | | internal Dictionary<string, IProjectCardView> projects = new Dictionary<string, IProjectCardView>(); |
| | 96 | | private readonly ProjectCardView projectCardViewPrefab; |
| | 97 | | private readonly IProjectContextMenuView contextMenuView; |
| | 98 | | private readonly Transform defaultParent; |
| | 99 | | private SceneContextMenuHandler sceneContextMenuHandler; |
| | 100 | |
|
| | 101 | | /// <summary> |
| | 102 | | /// Ctor |
| | 103 | | /// </summary> |
| | 104 | | /// <param name="projectCardViewPrefab">prefab for project's card</param> |
| | 105 | | /// <param name="defaultParent">default parent for scene's card</param> |
| 3 | 106 | | public ProjectsController(ProjectCardView projectCardViewPrefab, IProjectContextMenuView contextMenuView, Transform |
| | 107 | | { |
| 3 | 108 | | this.contextMenuView = contextMenuView; |
| 3 | 109 | | this.projectCardViewPrefab = projectCardViewPrefab; |
| 3 | 110 | | this.defaultParent = defaultParent; |
| | 111 | |
|
| 3 | 112 | | this.contextMenuView.OnDeletePressed += DeleteProject; |
| 3 | 113 | | this.contextMenuView.OnDuplicatePressed += DuplicateProject; |
| 3 | 114 | | this.contextMenuView.OnPublishPressed += PublishProject; |
| 3 | 115 | | } |
| | 116 | |
|
| 0 | 117 | | private void PublishProject(IProjectCardView projectCardView) { OnPublishProject?.Invoke(projectCardView.projectData |
| | 118 | |
|
| 0 | 119 | | private void DuplicateProject(IProjectCardView projectCardView) { OnDuplicateProject?.Invoke(projectCardView.project |
| | 120 | |
|
| 0 | 121 | | private void DeleteProject(IProjectCardView projectCardView) { OnDeleteProject?.Invoke(projectCardView.projectData); |
| | 122 | |
|
| | 123 | | public void SetProjects(ProjectData[] projects) |
| | 124 | | { |
| 2 | 125 | | foreach (var projectCard in this.projects.Values) |
| | 126 | | { |
| 0 | 127 | | DestroyCardView(projectCard); |
| | 128 | | } |
| | 129 | |
|
| 1 | 130 | | this.projects = new Dictionary<string, IProjectCardView>(); |
| 4 | 131 | | foreach (var project in projects) |
| | 132 | | { |
| 1 | 133 | | this.projects.Add(project.id, CreateCardView(project)); |
| | 134 | | } |
| 2 | 135 | | sceneSearchHandler.SetSearchableList(this.projects.Values.Select(scene => scene.searchInfo).ToList()); |
| 1 | 136 | | OnProjectsSet?.Invoke(this.projects); |
| 1 | 137 | | } |
| | 138 | |
|
| 0 | 139 | | public void SetSceneContextMenuHandler(SceneContextMenuHandler sceneContextMenuHandler) { this.sceneContextMenuHandl |
| | 140 | |
|
| | 141 | | public void UpdateDeploymentStatus() |
| | 142 | | { |
| 4 | 143 | | foreach (var project in projects) |
| | 144 | | { |
| 1 | 145 | | List<Scene> scenesList = new List<Scene>(); |
| 2 | 146 | | foreach (LandWithAccess landWithAccess in DataStore.i.builderInWorld.landsWithAccess.Get()) |
| | 147 | | { |
| 0 | 148 | | foreach (var scene in landWithAccess.scenes) |
| | 149 | | { |
| 0 | 150 | | if (scene.projectId == project.Key && !scenesList.Contains(scene)) |
| 0 | 151 | | scenesList.Add(scene); |
| | 152 | | } |
| | 153 | | } |
| | 154 | |
|
| 1 | 155 | | project.Value.SetScenes(scenesList); |
| | 156 | | } |
| 1 | 157 | | } |
| | 158 | |
|
| | 159 | | public void AddListener(IProjectsListener listener) |
| | 160 | | { |
| 0 | 161 | | OnProjectsSet += listener.OnSetProjects; |
| 0 | 162 | | listener.OnSetProjects(projects); |
| 0 | 163 | | } |
| | 164 | |
|
| 0 | 165 | | public void RemoveListener(IProjectsListener listener) { OnProjectsSet -= listener.OnSetProjects; } |
| | 166 | |
|
| 0 | 167 | | public Dictionary<string, IProjectCardView> GetProjects() { return projects; } |
| | 168 | |
|
| 2 | 169 | | internal void ExpandMenuPressed() { OnExpandMenuPressed?.Invoke(); } |
| | 170 | |
|
| | 171 | | private IProjectCardView CreateCardView(ProjectData data) |
| | 172 | | { |
| 1 | 173 | | var card = CreateCardView(); |
| 1 | 174 | | card.Setup(data); |
| 1 | 175 | | return card; |
| | 176 | | } |
| | 177 | |
|
| | 178 | | private IProjectCardView CreateCardView() |
| | 179 | | { |
| 1 | 180 | | ProjectCardView projectCardView = GameObject.Instantiate(projectCardViewPrefab).GetComponent<ProjectCardView>(); |
| | 181 | | IProjectCardView view = projectCardView; |
| | 182 | |
|
| 1 | 183 | | view.SetActive(false); |
| 1 | 184 | | view.ConfigureDefaultParent(defaultParent); |
| 1 | 185 | | view.SetToDefaultParent(); |
| | 186 | |
|
| 1 | 187 | | view.OnEditorPressed += OnEditorPressed; |
| 1 | 188 | | view.OnSettingsPressed += OnProjectSettingsPressed; |
| 1 | 189 | | view.OnSceneCardSettingsPressed += OnSceneCardSettingsPressed; |
| 1 | 190 | | view.OnExpandMenuPressed += ExpandMenuPressed; |
| | 191 | |
|
| 1 | 192 | | return view; |
| | 193 | | } |
| | 194 | |
|
| | 195 | | private void DestroyCardView(IProjectCardView projectCardView) |
| | 196 | | { |
| | 197 | | // NOTE: there is actually no need to unsubscribe here, but, just in case... |
| 2 | 198 | | projectCardView.OnEditorPressed -= OnEditorPressed; |
| 2 | 199 | | projectCardView.OnSettingsPressed -= OnProjectSettingsPressed; |
| 2 | 200 | | projectCardView.OnExpandMenuPressed -= ExpandMenuPressed; |
| | 201 | |
|
| 2 | 202 | | projectCardView.Dispose(); |
| 2 | 203 | | } |
| | 204 | |
|
| 0 | 205 | | private void OnProjectSettingsPressed(IProjectCardView sceneData) { contextMenuView.ShowOnCard(sceneData); } |
| 0 | 206 | | private void OnSceneCardSettingsPressed(IProjectSceneCardView sceneData) { sceneContextMenuHandler.OnContextMenuOpen |
| | 207 | |
|
| | 208 | | public void Dispose() |
| | 209 | | { |
| 3 | 210 | | contextMenuView.OnDeletePressed -= DeleteProject; |
| 3 | 211 | | contextMenuView.OnDuplicatePressed -= DuplicateProject; |
| 3 | 212 | | contextMenuView.OnPublishPressed -= PublishProject; |
| | 213 | |
|
| 10 | 214 | | foreach (var projectCard in this.projects.Values) |
| | 215 | | { |
| 2 | 216 | | DestroyCardView(projectCard); |
| | 217 | | } |
| 3 | 218 | | } |
| | 219 | | } |