| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using UnityEngine; |
| | 5 | | using Object = UnityEngine.Object; |
| | 6 | |
|
| | 7 | | internal class SectionProjectScenesController : SectionBase, IProjectSceneListener, ISectionHideContextMenuRequester |
| | 8 | | { |
| | 9 | | public const string VIEW_PREFAB_PATH = "BuilderProjectsPanelMenuSections/SectionProjectScenesView"; |
| | 10 | |
|
| | 11 | | public event Action OnRequestContextMenuHide; |
| | 12 | |
|
| 1 | 13 | | public override ISectionSearchHandler searchHandler => sceneSearchHandler; |
| | 14 | |
|
| | 15 | | private readonly SectionProjectScenesView view; |
| | 16 | |
|
| 1 | 17 | | private readonly ISectionSearchHandler sceneSearchHandler = new SectionSearchHandler(); |
| | 18 | | private Dictionary<string, ISceneCardView> scenesViews; |
| | 19 | |
|
| 0 | 20 | | public SectionProjectScenesController() : this( |
| | 21 | | Object.Instantiate(Resources.Load<SectionProjectScenesView>(VIEW_PREFAB_PATH)) |
| 0 | 22 | | ) { } |
| | 23 | |
|
| 1 | 24 | | public SectionProjectScenesController(SectionProjectScenesView view) |
| | 25 | | { |
| 1 | 26 | | this.view = view; |
| | 27 | |
|
| 1 | 28 | | view.OnScrollRectValueChanged += OnRequestContextMenuHide; |
| 1 | 29 | | sceneSearchHandler.OnResult += OnSearchResult; |
| 1 | 30 | | } |
| | 31 | |
|
| 0 | 32 | | public override void SetViewContainer(Transform viewContainer) { view.SetParent(viewContainer); } |
| | 33 | |
|
| | 34 | | public override void Dispose() |
| | 35 | | { |
| 1 | 36 | | view.OnScrollRectValueChanged -= OnRequestContextMenuHide; |
| 1 | 37 | | view.Dispose(); |
| 1 | 38 | | } |
| | 39 | |
|
| 0 | 40 | | protected override void OnShow() { view.SetActive(true); } |
| | 41 | |
|
| 0 | 42 | | protected override void OnHide() { view.SetActive(false); } |
| | 43 | |
|
| | 44 | | void IProjectSceneListener.OnSetScenes(Dictionary<string, ISceneCardView> scenes) |
| | 45 | | { |
| 1 | 46 | | scenesViews = new Dictionary<string, ISceneCardView>(scenes); |
| 11 | 47 | | sceneSearchHandler.SetSearchableList(scenes.Values.Select(scene => scene.searchInfo).ToList()); |
| 1 | 48 | | } |
| | 49 | |
|
| | 50 | | void IProjectSceneListener.OnSceneAdded(ISceneCardView scene) |
| | 51 | | { |
| 0 | 52 | | scenesViews.Add(scene.sceneData.id, scene); |
| 0 | 53 | | sceneSearchHandler.AddItem(scene.searchInfo); |
| 0 | 54 | | } |
| | 55 | |
|
| | 56 | | void IProjectSceneListener.OnSceneRemoved(ISceneCardView scene) |
| | 57 | | { |
| 0 | 58 | | scenesViews.Remove(scene.sceneData.id); |
| 0 | 59 | | scene.SetActive(false); |
| 0 | 60 | | } |
| | 61 | |
|
| | 62 | | private void OnSearchResult(List<ISearchInfo> searchInfoScenes) |
| | 63 | | { |
| 2 | 64 | | if (scenesViews == null) |
| 1 | 65 | | return; |
| | 66 | |
|
| 1 | 67 | | using (var iterator = scenesViews.GetEnumerator()) |
| | 68 | | { |
| 11 | 69 | | while (iterator.MoveNext()) |
| | 70 | | { |
| 10 | 71 | | iterator.Current.Value.SetParent(view.scenesCardContainer); |
| 10 | 72 | | iterator.Current.Value.SetActive(false); |
| | 73 | | } |
| 1 | 74 | | } |
| | 75 | |
|
| 22 | 76 | | for (int i = 0; i < searchInfoScenes.Count; i++) |
| | 77 | | { |
| 10 | 78 | | if (!scenesViews.TryGetValue(searchInfoScenes[i].id, out ISceneCardView cardView)) |
| | 79 | | continue; |
| | 80 | |
|
| 10 | 81 | | cardView.SetActive(true); |
| 10 | 82 | | cardView.SetSiblingIndex(i); |
| | 83 | | } |
| 1 | 84 | | view.ResetScrollRect(); |
| 1 | 85 | | } |
| | 86 | | } |