| | 1 | | using System; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using UnityEngine; |
| | 6 | | using Object = UnityEngine.Object; |
| | 7 | |
|
| | 8 | | internal class SectionScenesController : SectionBase, IDeployedSceneListener, IProjectSceneListener, ISectionOpenSection |
| | 9 | | { |
| | 10 | | public event Action<SectionId> OnRequestOpenSection; |
| | 11 | |
|
| | 12 | | internal const int MAX_CARDS = 3; |
| | 13 | | internal readonly SectionScenesView view; |
| | 14 | |
|
| | 15 | | private bool hasScenes = false; |
| | 16 | |
|
| 0 | 17 | | public override ISectionSearchHandler searchHandler => hasScenes ? sceneSearchHandler : null; |
| 0 | 18 | | public override SearchBarConfig searchBarConfig => new SearchBarConfig() |
| | 19 | | { |
| | 20 | | showFilterContributor = false, |
| | 21 | | showFilterOperator = false, |
| | 22 | | showFilterOwner = false, |
| | 23 | | showResultLabel = false |
| | 24 | | }; |
| | 25 | |
|
| 3 | 26 | | private readonly ISectionSearchHandler sceneSearchHandler = new SectionSearchHandler(); |
| | 27 | |
|
| | 28 | | internal Dictionary<string, ISceneCardView> deployedViews; |
| | 29 | | internal Dictionary<string, ISceneCardView> projectViews; |
| 3 | 30 | | private List<ISearchInfo> searchList = new List<ISearchInfo>(); |
| | 31 | |
|
| 3 | 32 | | public SectionScenesController() |
| | 33 | | { |
| 3 | 34 | | var prefab = Resources.Load<SectionScenesView>("BuilderProjectsPanelMenuSections/SectionScenesView"); |
| 3 | 35 | | view = Object.Instantiate(prefab); |
| | 36 | |
|
| 3 | 37 | | view.btnProjectsViewAll.onClick.AddListener(() => OnRequestOpenSection?.Invoke(SectionId.SCENES_PROJECT)); |
| 3 | 38 | | view.btnInWorldViewAll.onClick.AddListener(() => OnRequestOpenSection?.Invoke(SectionId.SCENES_DEPLOYED)); |
| | 39 | |
|
| 3 | 40 | | sceneSearchHandler.OnResult += OnSearchResult; |
| 3 | 41 | | } |
| | 42 | |
|
| | 43 | | public override void SetViewContainer(Transform viewContainer) |
| | 44 | | { |
| 0 | 45 | | view.transform.SetParent(viewContainer); |
| 0 | 46 | | view.transform.ResetLocalTRS(); |
| 0 | 47 | | } |
| | 48 | |
|
| 6 | 49 | | public override void Dispose() { view.Dispose(); } |
| | 50 | |
|
| 0 | 51 | | protected override void OnShow() { view.gameObject.SetActive(true); } |
| | 52 | |
|
| | 53 | | protected override void OnHide() |
| | 54 | | { |
| 0 | 55 | | view.gameObject.SetActive(false); |
| 0 | 56 | | searchList.Clear(); |
| 0 | 57 | | deployedViews?.Clear(); |
| 0 | 58 | | projectViews?.Clear(); |
| 0 | 59 | | } |
| | 60 | |
|
| | 61 | | private void ViewDirty() |
| | 62 | | { |
| 18 | 63 | | bool hasDeployedScenes = view.deployedSceneContainer.childCount > 0; |
| 18 | 64 | | bool hasProjectScenes = view.projectSceneContainer.childCount > 0; |
| 18 | 65 | | hasScenes = hasDeployedScenes || hasProjectScenes; |
| | 66 | |
|
| 18 | 67 | | view.contentScreen.SetActive(hasScenes); |
| 18 | 68 | | view.emptyScreen.SetActive(!hasScenes); |
| 18 | 69 | | view.inWorldContainer.SetActive(hasDeployedScenes); |
| 18 | 70 | | view.projectsContainer.SetActive(hasProjectScenes); |
| 18 | 71 | | } |
| | 72 | |
|
| | 73 | | void IDeployedSceneListener.OnSetScenes(Dictionary<string, ISceneCardView> scenes) |
| | 74 | | { |
| 6 | 75 | | UpdateDictionary(ref deployedViews, scenes); |
| 10 | 76 | | searchList.AddRange(scenes.Values.Select(scene => scene.searchInfo)); |
| 6 | 77 | | sceneSearchHandler.SetSearchableList(searchList); |
| 6 | 78 | | } |
| | 79 | |
|
| | 80 | | void IProjectSceneListener.OnSetScenes(Dictionary<string, ISceneCardView> scenes) |
| | 81 | | { |
| 5 | 82 | | UpdateDictionary(ref projectViews, scenes); |
| 6 | 83 | | searchList.AddRange(scenes.Values.Select(scene => scene.searchInfo)); |
| 5 | 84 | | sceneSearchHandler.SetSearchableList(searchList); |
| 5 | 85 | | } |
| | 86 | |
|
| | 87 | | void IDeployedSceneListener.OnSceneAdded(ISceneCardView scene) |
| | 88 | | { |
| 1 | 89 | | deployedViews.Add(scene.sceneData.id, scene); |
| 1 | 90 | | sceneSearchHandler.AddItem(scene.searchInfo); |
| 1 | 91 | | } |
| | 92 | |
|
| | 93 | | void IProjectSceneListener.OnSceneAdded(ISceneCardView scene) |
| | 94 | | { |
| 0 | 95 | | projectViews.Add(scene.sceneData.id, scene); |
| 0 | 96 | | sceneSearchHandler.AddItem(scene.searchInfo); |
| 0 | 97 | | } |
| | 98 | |
|
| | 99 | | void IDeployedSceneListener.OnSceneRemoved(ISceneCardView scene) |
| | 100 | | { |
| 5 | 101 | | scene.SetToDefaultParent(); |
| 5 | 102 | | deployedViews.Remove(scene.sceneData.id); |
| 5 | 103 | | sceneSearchHandler.RemoveItem(scene.searchInfo); |
| 5 | 104 | | } |
| | 105 | |
|
| | 106 | | void IProjectSceneListener.OnSceneRemoved(ISceneCardView scene) |
| | 107 | | { |
| 1 | 108 | | scene.SetToDefaultParent(); |
| 1 | 109 | | projectViews.Remove(scene.sceneData.id); |
| 1 | 110 | | sceneSearchHandler.RemoveItem(scene.searchInfo); |
| 1 | 111 | | } |
| | 112 | |
|
| | 113 | | private void OnSearchResult(List<ISearchInfo> searchInfoScenes) |
| | 114 | | { |
| 18 | 115 | | if (deployedViews != null) |
| 10 | 116 | | SetResult(deployedViews, searchInfoScenes, view.deployedSceneContainer); |
| | 117 | |
|
| 18 | 118 | | if (projectViews != null) |
| 11 | 119 | | SetResult(projectViews, searchInfoScenes, view.projectSceneContainer); |
| | 120 | |
|
| 18 | 121 | | ViewDirty(); |
| 18 | 122 | | } |
| | 123 | |
|
| | 124 | | private void SetResult(Dictionary<string, ISceneCardView> scenesViews, List<ISearchInfo> searchInfoScenes, |
| | 125 | | Transform parent) |
| | 126 | | { |
| 21 | 127 | | int count = 0; |
| | 128 | |
|
| 124 | 129 | | for (int i = 0; i < searchInfoScenes.Count; i++) |
| | 130 | | { |
| 41 | 131 | | if (!scenesViews.TryGetValue(searchInfoScenes[i].id, out ISceneCardView sceneView)) |
| | 132 | | { |
| | 133 | | continue; |
| | 134 | | } |
| | 135 | |
|
| 21 | 136 | | sceneView.SetParent(parent); |
| 21 | 137 | | sceneView.SetSiblingIndex(count); |
| 21 | 138 | | sceneView.SetActive(false); |
| 21 | 139 | | count++; |
| | 140 | | } |
| | 141 | |
|
| 84 | 142 | | for (int i = 0; i < parent.childCount; i++) |
| | 143 | | { |
| 21 | 144 | | parent.GetChild(i).gameObject.SetActive(i < count && i < MAX_CARDS); |
| | 145 | | } |
| 21 | 146 | | } |
| | 147 | |
|
| | 148 | | private void UpdateDictionary(ref Dictionary<string, ISceneCardView> target, Dictionary<string, ISceneCardView> newD |
| | 149 | | { |
| 11 | 150 | | if (newData.Count == 0) |
| 8 | 151 | | return; |
| | 152 | |
|
| 3 | 153 | | if (target == null) |
| | 154 | | { |
| 2 | 155 | | target = new Dictionary<string, ISceneCardView>(newData); |
| 2 | 156 | | return; |
| | 157 | | } |
| | 158 | |
|
| 1 | 159 | | using (var iterator = newData.GetEnumerator()) |
| | 160 | | { |
| 2 | 161 | | while (iterator.MoveNext()) |
| | 162 | | { |
| 1 | 163 | | if (target.ContainsKey(iterator.Current.Key)) |
| | 164 | | continue; |
| | 165 | |
|
| 1 | 166 | | target.Add(iterator.Current.Key, iterator.Current.Value); |
| | 167 | | } |
| 1 | 168 | | } |
| 1 | 169 | | } |
| | 170 | | } |