| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using UnityEngine; |
| | 5 | | using Object = UnityEngine.Object; |
| | 6 | |
|
| | 7 | | namespace DCL.Builder |
| | 8 | | { |
| | 9 | | internal class SectionScenesController : SectionBase, ISceneListener, ISectionHideContextMenuRequester |
| | 10 | | { |
| | 11 | | public const string VIEW_PREFAB_PATH = "BuilderProjectsPanelMenuSections/SectionDeployedScenesView"; |
| | 12 | |
|
| | 13 | | public event Action OnRequestContextMenuHide; |
| | 14 | |
|
| 1 | 15 | | public override ISectionSearchHandler searchHandler => sceneSearchHandler; |
| | 16 | |
|
| | 17 | | private readonly SectionPlacesView view; |
| | 18 | |
|
| 4 | 19 | | private readonly ISectionSearchHandler sceneSearchHandler = new SectionSearchHandler(); |
| | 20 | | private Dictionary<string, ISceneCardView> scenesViews; |
| | 21 | |
|
| 2 | 22 | | public SectionScenesController() : this( |
| | 23 | | Object.Instantiate(Resources.Load<SectionPlacesView>(VIEW_PREFAB_PATH)) |
| 2 | 24 | | ) { } |
| | 25 | |
|
| 4 | 26 | | public SectionScenesController(SectionPlacesView view) |
| | 27 | | { |
| 4 | 28 | | this.view = view; |
| | 29 | |
|
| 4 | 30 | | view.OnScrollRectValueChanged += OnRequestContextMenuHide; |
| 4 | 31 | | sceneSearchHandler.OnResult += OnSearchResult; |
| 4 | 32 | | } |
| | 33 | |
|
| 0 | 34 | | public override void SetViewContainer(Transform viewContainer) { view.SetParent(viewContainer); } |
| | 35 | |
|
| | 36 | | public override void Dispose() |
| | 37 | | { |
| 2 | 38 | | view.OnScrollRectValueChanged -= OnRequestContextMenuHide; |
| 2 | 39 | | view.Dispose(); |
| 2 | 40 | | } |
| | 41 | |
|
| 0 | 42 | | protected override void OnShow() { view.SetActive(true); } |
| | 43 | |
|
| 0 | 44 | | protected override void OnHide() { view.SetActive(false); } |
| | 45 | |
|
| | 46 | | void ISceneListener.SetScenes(Dictionary<string, ISceneCardView> scenes) |
| | 47 | | { |
| 5 | 48 | | scenesViews = new Dictionary<string, ISceneCardView>(scenes); |
| 18 | 49 | | sceneSearchHandler.SetSearchableList(scenes.Values.Select(scene => scene.searchInfo).ToList()); |
| 5 | 50 | | } |
| | 51 | |
|
| | 52 | | void ISceneListener.SceneAdded(ISceneCardView scene) |
| | 53 | | { |
| 0 | 54 | | scenesViews.Add(scene.SceneData.id, scene); |
| 0 | 55 | | sceneSearchHandler.AddItem(scene.searchInfo); |
| 0 | 56 | | } |
| | 57 | |
|
| | 58 | | void ISceneListener.SceneRemoved(ISceneCardView scene) |
| | 59 | | { |
| 0 | 60 | | scenesViews.Remove(scene.SceneData.id); |
| 0 | 61 | | scene.SetActive(false); |
| 0 | 62 | | } |
| | 63 | |
|
| | 64 | | private void OnSearchResult(List<ISearchInfo> searchInfoScenes) |
| | 65 | | { |
| 6 | 66 | | if (scenesViews == null) |
| 1 | 67 | | return; |
| | 68 | |
|
| 5 | 69 | | using (var iterator = scenesViews.GetEnumerator()) |
| | 70 | | { |
| 18 | 71 | | while (iterator.MoveNext()) |
| | 72 | | { |
| 13 | 73 | | iterator.Current.Value.SetParent(view.GetCardsContainer()); |
| 13 | 74 | | iterator.Current.Value.SetActive(false); |
| | 75 | | } |
| 5 | 76 | | } |
| | 77 | |
|
| 36 | 78 | | for (int i = 0; i < searchInfoScenes.Count; i++) |
| | 79 | | { |
| 13 | 80 | | if (!scenesViews.TryGetValue(searchInfoScenes[i].id, out ISceneCardView cardView)) |
| | 81 | | continue; |
| | 82 | |
|
| 10 | 83 | | cardView.SetActive(true); |
| 10 | 84 | | cardView.SetSiblingIndex(i); |
| | 85 | | } |
| | 86 | |
|
| 5 | 87 | | if (scenesViews.Count == 0) |
| | 88 | | { |
| 2 | 89 | | if (isLoading) |
| | 90 | | { |
| 1 | 91 | | view.SetLoading(); |
| 1 | 92 | | } |
| | 93 | | else |
| | 94 | | { |
| 1 | 95 | | view.SetEmpty(); |
| | 96 | | } |
| 1 | 97 | | } |
| 3 | 98 | | else if (searchInfoScenes.Count == 0) |
| | 99 | | { |
| 0 | 100 | | view.SetNoSearchResult(); |
| 0 | 101 | | } |
| | 102 | | else |
| | 103 | | { |
| 3 | 104 | | view.SetFilled(); |
| | 105 | | } |
| 3 | 106 | | } |
| | 107 | | } |
| | 108 | | } |