| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL.Builder; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | namespace DCL.Builder |
| | 7 | | { |
| | 8 | | internal enum SectionId |
| | 9 | | { |
| | 10 | | SCENES, |
| | 11 | | PROJECTS, |
| | 12 | | LAND, |
| | 13 | | SETTINGS_PROJECT_GENERAL, |
| | 14 | | SETTINGS_PROJECT_CONTRIBUTORS, |
| | 15 | | SETTINGS_PROJECT_ADMIN |
| | 16 | | } |
| | 17 | |
|
| | 18 | | internal interface ISectionsController : IDisposable |
| | 19 | | { |
| | 20 | | event Action OnSectionContentEmpty; |
| | 21 | | event Action OnSectionContentNotEmpty; |
| | 22 | | event Action<SectionBase> OnSectionLoaded; |
| | 23 | | event Action<SectionBase> OnSectionShow; |
| | 24 | | event Action<SectionBase> OnSectionHide; |
| | 25 | | event Action OnRequestContextMenuHide; |
| | 26 | | event Action OnCreateProjectRequest; |
| | 27 | | event Action<SectionId> OnOpenSectionId; |
| | 28 | | event Action<string, SceneDataUpdatePayload> OnRequestUpdateSceneData; |
| | 29 | | event Action<string, SceneContributorsUpdatePayload> OnRequestUpdateSceneContributors; |
| | 30 | | event Action<string, SceneAdminsUpdatePayload> OnRequestUpdateSceneAdmins; |
| | 31 | | event Action<string, SceneBannedUsersUpdatePayload> OnRequestUpdateSceneBannedUsers; |
| | 32 | | event Action<string> OnRequestOpenUrl; |
| | 33 | | event Action<Vector2Int> OnRequestGoToCoords; |
| | 34 | | event Action<Vector2Int> OnRequestEditSceneAtCoords; |
| | 35 | | void OpenSection(SectionId id); |
| | 36 | | void SetFetchingDataStart(); |
| | 37 | | void SetFetchingDataStart<T>() where T : SectionBase; |
| | 38 | | void SetFetchingDataEnd<T>() where T : SectionBase; |
| | 39 | | } |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// This class is in charge of handling open/close of the different menu sections |
| | 43 | | /// </summary> |
| | 44 | | internal class SectionsController : ISectionsController |
| | 45 | | { |
| | 46 | | public event Action OnSectionContentEmpty; |
| | 47 | | public event Action OnSectionContentNotEmpty; |
| | 48 | | public event Action<SectionBase> OnSectionLoaded; |
| | 49 | | public event Action<SectionBase> OnSectionShow; |
| | 50 | | public event Action<SectionBase> OnSectionHide; |
| | 51 | | public event Action OnRequestContextMenuHide; |
| | 52 | | public event Action OnCreateProjectRequest; |
| | 53 | | public event Action<SectionId> OnOpenSectionId; |
| | 54 | | public event Action<string, SceneDataUpdatePayload> OnRequestUpdateSceneData; |
| | 55 | | public event Action<string, SceneContributorsUpdatePayload> OnRequestUpdateSceneContributors; |
| | 56 | | public event Action<string, SceneAdminsUpdatePayload> OnRequestUpdateSceneAdmins; |
| | 57 | | public event Action<string, SceneBannedUsersUpdatePayload> OnRequestUpdateSceneBannedUsers; |
| | 58 | | public event Action<string> OnRequestOpenUrl; |
| | 59 | | public event Action<Vector2Int> OnRequestGoToCoords; |
| | 60 | | public event Action<Vector2Int> OnRequestEditSceneAtCoords; |
| | 61 | |
|
| 2 | 62 | | private Dictionary<SectionId, SectionBase> loadedSections = new Dictionary<SectionId, SectionBase>(); |
| | 63 | | private Transform sectionsParent; |
| | 64 | | private ISectionFactory sectionFactory; |
| | 65 | | private SectionBase currentOpenSection; |
| | 66 | |
|
| | 67 | | /// <summary> |
| | 68 | | /// Ctor |
| | 69 | | /// </summary> |
| | 70 | | /// <param name="sectionsParent">container for the different sections view</param> |
| 0 | 71 | | public SectionsController(Transform sectionsParent) : this(new SectionFactory(), sectionsParent) { } |
| | 72 | |
|
| | 73 | | /// <summary> |
| | 74 | | /// Ctor |
| | 75 | | /// </summary> |
| | 76 | | /// <param name="sectionFactory">factory that instantiates menu sections</param> |
| | 77 | | /// <param name="sectionsParent">container for the different sections view</param> |
| 2 | 78 | | public SectionsController(ISectionFactory sectionFactory, Transform sectionsParent) |
| | 79 | | { |
| 2 | 80 | | this.sectionsParent = sectionsParent; |
| 2 | 81 | | this.sectionFactory = sectionFactory; |
| 2 | 82 | | } |
| | 83 | |
|
| | 84 | | /// <summary> |
| | 85 | | /// Get (load if not already loaded) the controller for certain menu section id |
| | 86 | | /// </summary> |
| | 87 | | /// <param name="id">id of the controller to get</param> |
| | 88 | | /// <returns></returns> |
| | 89 | | public SectionBase GetOrLoadSection(SectionId id) |
| | 90 | | { |
| 3 | 91 | | if (loadedSections.TryGetValue(id, out SectionBase section)) |
| | 92 | | { |
| 0 | 93 | | return section; |
| | 94 | | } |
| | 95 | |
|
| 3 | 96 | | section = sectionFactory.GetSectionController(id); |
| 3 | 97 | | if (section != null) |
| | 98 | | { |
| 3 | 99 | | section.SetViewContainer(sectionsParent); |
| 3 | 100 | | section.SetFetchingDataState(section.isLoading); |
| 3 | 101 | | SubscribeEvents(section); |
| | 102 | | } |
| | 103 | |
|
| 3 | 104 | | loadedSections.Add(id, section); |
| 3 | 105 | | OnSectionLoaded?.Invoke(section); |
| 3 | 106 | | return section; |
| | 107 | | } |
| | 108 | |
|
| 0 | 109 | | public void ContentEmpty() => OnSectionContentEmpty?.Invoke(); |
| 0 | 110 | | public void ContentNotEmpty() => OnSectionContentNotEmpty?.Invoke(); |
| | 111 | |
|
| | 112 | | /// <summary> |
| | 113 | | /// Opens (make visible) a menu section. It will load it if necessary. |
| | 114 | | /// Closes (hides) the previously open section. |
| | 115 | | /// </summary> |
| | 116 | | /// <param name="id">id of the section to show</param> |
| | 117 | | public void OpenSection(SectionId id) |
| | 118 | | { |
| 3 | 119 | | var section = GetOrLoadSection(id); |
| 3 | 120 | | var success = OpenSection(section); |
| 3 | 121 | | if (success) |
| | 122 | | { |
| 3 | 123 | | OnOpenSectionId?.Invoke(id); |
| | 124 | | } |
| 0 | 125 | | } |
| | 126 | |
|
| 0 | 127 | | public void SetFetchingDataStart(){ SetIsLoading<SectionBase>(true); } |
| 0 | 128 | | public void SetFetchingDataStart<T>() where T : SectionBase { SetIsLoading<T>(true); } |
| | 129 | |
|
| 0 | 130 | | public void SetFetchingDataEnd<T>() where T : SectionBase { SetIsLoading<T>(false); } |
| | 131 | |
|
| | 132 | | private void SetIsLoading<T>(bool isLoading) where T : SectionBase |
| | 133 | | { |
| 0 | 134 | | using (var iterator = loadedSections.GetEnumerator()) |
| | 135 | | { |
| 0 | 136 | | while (iterator.MoveNext()) |
| | 137 | | { |
| 0 | 138 | | if(iterator.Current.Value is T sectionBase) |
| 0 | 139 | | sectionBase.SetFetchingDataState(isLoading); |
| | 140 | | } |
| 0 | 141 | | } |
| 0 | 142 | | } |
| | 143 | |
|
| | 144 | | private bool OpenSection(SectionBase section) |
| | 145 | | { |
| 3 | 146 | | if (currentOpenSection == section) |
| 0 | 147 | | return false; |
| | 148 | |
|
| 3 | 149 | | if (currentOpenSection != null) |
| | 150 | | { |
| 1 | 151 | | currentOpenSection.SetVisible(false); |
| 1 | 152 | | OnSectionHide?.Invoke(currentOpenSection); |
| | 153 | | } |
| | 154 | |
|
| 3 | 155 | | currentOpenSection = section; |
| | 156 | |
|
| 3 | 157 | | if (currentOpenSection != null) |
| | 158 | | { |
| 3 | 159 | | currentOpenSection.SetVisible(true); |
| 3 | 160 | | OnSectionShow?.Invoke(currentOpenSection); |
| | 161 | | } |
| | 162 | |
|
| 3 | 163 | | return true; |
| | 164 | | } |
| | 165 | |
|
| | 166 | | public void Dispose() |
| | 167 | | { |
| 2 | 168 | | using (var iterator = loadedSections.GetEnumerator()) |
| | 169 | | { |
| 5 | 170 | | while (iterator.MoveNext()) |
| | 171 | | { |
| 3 | 172 | | iterator.Current.Value.OnEmptyContent -= ContentEmpty; |
| 3 | 173 | | iterator.Current.Value.OnNotEmptyContent -= ContentNotEmpty; |
| 3 | 174 | | iterator.Current.Value.Dispose(); |
| | 175 | | } |
| 2 | 176 | | } |
| | 177 | |
|
| 2 | 178 | | loadedSections.Clear(); |
| 2 | 179 | | } |
| | 180 | |
|
| 0 | 181 | | private void OnHideContextMenuRequested() { OnRequestContextMenuHide?.Invoke(); } |
| | 182 | |
|
| 0 | 183 | | private void OnUpdateSceneDataRequested(string id, SceneDataUpdatePayload payload) { OnRequestUpdateSceneData?.I |
| | 184 | |
|
| 0 | 185 | | private void OnUpdateSceneContributorsRequested(string id, SceneContributorsUpdatePayload payload) { OnRequestUp |
| | 186 | |
|
| 0 | 187 | | private void OnUpdateSceneAdminsRequested(string id, SceneAdminsUpdatePayload payload) { OnRequestUpdateSceneAdm |
| | 188 | |
|
| 0 | 189 | | private void OnUpdateSceneBannedUsersRequested(string id, SceneBannedUsersUpdatePayload payload) { OnRequestUpda |
| | 190 | |
|
| 0 | 191 | | private void OnOpenUrlRequested(string url) { OnRequestOpenUrl?.Invoke(url); } |
| | 192 | |
|
| 0 | 193 | | private void CreateProjectRequest() {OnCreateProjectRequest?.Invoke(); } |
| | 194 | |
|
| 0 | 195 | | private void OnGoToCoordsRequested(Vector2Int coords) { OnRequestGoToCoords?.Invoke(coords); } |
| | 196 | |
|
| 0 | 197 | | private void OnEditSceneAtCoordsRequested(Vector2Int coords) { OnRequestEditSceneAtCoords?.Invoke(coords); } |
| | 198 | |
|
| | 199 | | private void SubscribeEvents(SectionBase sectionBase) |
| | 200 | | { |
| 3 | 201 | | sectionBase.OnEmptyContent += ContentEmpty; |
| 3 | 202 | | sectionBase.OnNotEmptyContent += ContentNotEmpty; |
| | 203 | |
|
| 3 | 204 | | if (sectionBase is ISectionOpenSectionRequester openSectionRequester) |
| | 205 | | { |
| 0 | 206 | | openSectionRequester.OnRequestOpenSection += OpenSection; |
| | 207 | | } |
| 3 | 208 | | if (sectionBase is ISectionHideContextMenuRequester hideContextMenuRequester) |
| | 209 | | { |
| 0 | 210 | | hideContextMenuRequester.OnRequestContextMenuHide += OnHideContextMenuRequested; |
| | 211 | | } |
| 3 | 212 | | if (sectionBase is ISectionUpdateSceneDataRequester updateSceneDataRequester) |
| | 213 | | { |
| 0 | 214 | | updateSceneDataRequester.OnRequestUpdateSceneData += OnUpdateSceneDataRequested; |
| | 215 | | } |
| 3 | 216 | | if (sectionBase is ISectionUpdateSceneContributorsRequester updateSceneContributorsRequester) |
| | 217 | | { |
| 0 | 218 | | updateSceneContributorsRequester.OnRequestUpdateSceneContributors += OnUpdateSceneContributorsRequested; |
| | 219 | | } |
| 3 | 220 | | if (sectionBase is ISectionUpdateSceneAdminsRequester updateSceneAdminsRequester) |
| | 221 | | { |
| 0 | 222 | | updateSceneAdminsRequester.OnRequestUpdateSceneAdmins += OnUpdateSceneAdminsRequested; |
| | 223 | | } |
| 3 | 224 | | if (sectionBase is ISectionUpdateSceneBannedUsersRequester updateSceneBannedUsersRequester) |
| | 225 | | { |
| 0 | 226 | | updateSceneBannedUsersRequester.OnRequestUpdateSceneBannedUsers += OnUpdateSceneBannedUsersRequested; |
| | 227 | | } |
| 3 | 228 | | if (sectionBase is ISectionOpenURLRequester openURLRequester) |
| | 229 | | { |
| 0 | 230 | | openURLRequester.OnRequestOpenUrl += OnOpenUrlRequested; |
| | 231 | | } |
| 3 | 232 | | if (sectionBase is ISectionGotoCoordsRequester goToRequester) |
| | 233 | | { |
| 0 | 234 | | goToRequester.OnRequestGoToCoords += OnGoToCoordsRequested; |
| | 235 | | } |
| 3 | 236 | | if (sectionBase is ISectionEditSceneAtCoordsRequester editSceneRequester) |
| | 237 | | { |
| 0 | 238 | | editSceneRequester.OnRequestEditSceneAtCoords += OnEditSceneAtCoordsRequested; |
| | 239 | | } |
| 3 | 240 | | if (sectionBase is ISectionProjectController sectionProjectController) |
| | 241 | | { |
| 0 | 242 | | sectionProjectController.OnCreateProjectRequest += CreateProjectRequest; |
| | 243 | | } |
| 3 | 244 | | } |
| | 245 | | } |
| | 246 | |
|
| | 247 | | } |