| | 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 action will be called when the user want to edit a project |
| | 17 | | /// </summary> |
| | 18 | | event Action<ProjectData> OnEditorPressed; |
| | 19 | |
|
| | 20 | | /// <summary> |
| | 21 | | /// This action will be called each time that we changes the project list |
| | 22 | | /// </summary> |
| | 23 | | event Action<Dictionary<string, IProjectCardView>> OnProjectsSet; |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// When the user click on expand or collapse the project |
| | 27 | | /// </summary> |
| | 28 | | event Action OnExpandMenuPressed; |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// This will updated the deployments of the projects looking at the lands deployments |
| | 32 | | /// </summary> |
| | 33 | | void UpdateDeploymentStatus(); |
| | 34 | |
|
| | 35 | | /// <summary> |
| | 36 | | /// This will set the project list |
| | 37 | | /// </summary> |
| | 38 | | /// <param name="projects"></param> |
| | 39 | | void SetProjects(ProjectData[] projects); |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// This will add a listener that will be responsible to add/remove projects |
| | 43 | | /// </summary> |
| | 44 | | /// <param name="listener"></param> |
| | 45 | | void AddListener(IProjectsListener listener); |
| | 46 | |
|
| | 47 | | /// <summary> |
| | 48 | | /// This will remove a listener that will be responsible to add/remove projects |
| | 49 | | /// </summary> |
| | 50 | | /// <param name="listener"></param> |
| | 51 | | void RemoveListener(IProjectsListener listener); |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// This will get all projects |
| | 55 | | /// </summary> |
| | 56 | | /// <returns>Dictionary of all the projects indexed by their id</returns> |
| | 57 | | Dictionary<string, IProjectCardView> GetProjects(); |
| | 58 | | } |
| | 59 | |
|
| | 60 | | internal class ProjectsController : IProjectsController |
| | 61 | | { |
| | 62 | | public event Action<ProjectData> OnEditorPressed; |
| | 63 | | public event Action<Dictionary<string, IProjectCardView>> OnProjectsSet; |
| | 64 | |
|
| | 65 | | public event Action OnExpandMenuPressed; |
| | 66 | |
|
| 3 | 67 | | private readonly ISectionSearchHandler sceneSearchHandler = new SectionSearchHandler(); |
| | 68 | |
|
| 3 | 69 | | internal Dictionary<string, IProjectCardView> projects = new Dictionary<string, IProjectCardView>(); |
| | 70 | | private readonly ProjectCardView projectCardViewPrefab; |
| | 71 | | private readonly Transform defaultParent; |
| | 72 | |
|
| | 73 | | /// <summary> |
| | 74 | | /// Ctor |
| | 75 | | /// </summary> |
| | 76 | | /// <param name="projectCardViewPrefab">prefab for project's card</param> |
| | 77 | | /// <param name="defaultParent">default parent for scene's card</param> |
| 3 | 78 | | public ProjectsController(ProjectCardView projectCardViewPrefab, Transform defaultParent = null) |
| | 79 | | { |
| 3 | 80 | | this.projectCardViewPrefab = projectCardViewPrefab; |
| 3 | 81 | | this.defaultParent = defaultParent; |
| 3 | 82 | | } |
| | 83 | |
|
| | 84 | | public void SetProjects(ProjectData[] projects) |
| | 85 | | { |
| 2 | 86 | | foreach (var projectCard in this.projects.Values) |
| | 87 | | { |
| 0 | 88 | | DestroyCardView(projectCard); |
| | 89 | | } |
| | 90 | |
|
| 1 | 91 | | this.projects = new Dictionary<string, IProjectCardView>(); |
| 4 | 92 | | foreach (var project in projects) |
| | 93 | | { |
| 1 | 94 | | this.projects.Add(project.id, CreateCardView(project)); |
| | 95 | | } |
| 2 | 96 | | sceneSearchHandler.SetSearchableList(this.projects.Values.Select(scene => scene.searchInfo).ToList()); |
| 1 | 97 | | OnProjectsSet?.Invoke(this.projects); |
| 1 | 98 | | } |
| | 99 | |
|
| | 100 | | public void UpdateDeploymentStatus() |
| | 101 | | { |
| 4 | 102 | | foreach (var project in projects) |
| | 103 | | { |
| 1 | 104 | | List<Scene> scenesList = new List<Scene>(); |
| 2 | 105 | | foreach (LandWithAccess landWithAccess in DataStore.i.builderInWorld.landsWithAccess.Get()) |
| | 106 | | { |
| 0 | 107 | | foreach (var scene in landWithAccess.scenes) |
| | 108 | | { |
| 0 | 109 | | if (scene.projectId == project.Key) |
| 0 | 110 | | scenesList.Add(scene); |
| | 111 | | } |
| | 112 | | } |
| | 113 | |
|
| 1 | 114 | | project.Value.SetScenes(scenesList); |
| | 115 | | } |
| 1 | 116 | | } |
| | 117 | |
|
| | 118 | | public void AddListener(IProjectsListener listener) |
| | 119 | | { |
| 0 | 120 | | OnProjectsSet += listener.OnSetProjects; |
| 0 | 121 | | listener.OnSetProjects(projects); |
| 0 | 122 | | } |
| | 123 | |
|
| | 124 | | public void RemoveListener(IProjectsListener listener) |
| | 125 | | { |
| 0 | 126 | | OnProjectsSet -= listener.OnSetProjects; |
| 0 | 127 | | } |
| | 128 | |
|
| 0 | 129 | | public Dictionary<string, IProjectCardView> GetProjects() { return projects; } |
| | 130 | |
|
| | 131 | | internal void ExpandMenuPressed() |
| | 132 | | { |
| 1 | 133 | | OnExpandMenuPressed?.Invoke(); |
| 1 | 134 | | } |
| | 135 | |
|
| | 136 | | private IProjectCardView CreateCardView(ProjectData data) |
| | 137 | | { |
| 1 | 138 | | var card = CreateCardView(); |
| 1 | 139 | | card.Setup(data); |
| 1 | 140 | | return card; |
| | 141 | | } |
| | 142 | |
|
| | 143 | | private IProjectCardView CreateCardView() |
| | 144 | | { |
| 1 | 145 | | ProjectCardView projectCardView = GameObject.Instantiate(projectCardViewPrefab).GetComponent<ProjectCardView>(); |
| | 146 | | IProjectCardView view = projectCardView; |
| | 147 | |
|
| 1 | 148 | | view.SetActive(false); |
| 1 | 149 | | view.ConfigureDefaultParent(defaultParent); |
| 1 | 150 | | view.SetToDefaultParent(); |
| | 151 | |
|
| 1 | 152 | | view.OnEditorPressed += OnEditorPressed; |
| 1 | 153 | | view.OnSettingsPressed += OnSceneSettingsPressed; |
| 1 | 154 | | view.OnExpandMenuPressed += ExpandMenuPressed; |
| | 155 | |
|
| 1 | 156 | | return view; |
| | 157 | | } |
| | 158 | |
|
| | 159 | | private void DestroyCardView(IProjectCardView projectCardView) |
| | 160 | | { |
| | 161 | | // NOTE: there is actually no need to unsubscribe here, but, just in case... |
| 2 | 162 | | projectCardView.OnEditorPressed -= OnEditorPressed; |
| 2 | 163 | | projectCardView.OnSettingsPressed -= OnSceneSettingsPressed; |
| 2 | 164 | | projectCardView.OnExpandMenuPressed -= ExpandMenuPressed; |
| | 165 | |
|
| 2 | 166 | | projectCardView.Dispose(); |
| 2 | 167 | | } |
| | 168 | |
|
| 0 | 169 | | private void OnSceneSettingsPressed(ProjectData sceneData) { } |
| | 170 | |
|
| | 171 | | public void Dispose() |
| | 172 | | { |
| 10 | 173 | | foreach (var projectCard in this.projects.Values) |
| | 174 | | { |
| 2 | 175 | | DestroyCardView(projectCard); |
| | 176 | | } |
| 3 | 177 | | } |
| | 178 | | } |