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