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