| | 1 | | using DCL.Helpers; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.EventSystems; |
| | 6 | | using Object = UnityEngine.Object; |
| | 7 | |
|
| | 8 | | internal class SortDropdownView : MonoBehaviour, IDeselectHandler |
| | 9 | | { |
| | 10 | | public event Action<string> OnSortTypeSelected; |
| | 11 | |
|
| | 12 | | [SerializeField] private Transform buttonsContainer; |
| | 13 | | [SerializeField] private SortDropdownButton dropdownButtonBase; |
| | 14 | | [SerializeField] private ShowHideAnimator showHideAnimator; |
| | 15 | |
|
| 26 | 16 | | private readonly Queue<SortDropdownButton> buttonsPool = new Queue<SortDropdownButton>(); |
| 26 | 17 | | internal readonly List<SortDropdownButton> activeButtons = new List<SortDropdownButton>(); |
| | 18 | |
|
| | 19 | | private void Awake() |
| | 20 | | { |
| 11 | 21 | | dropdownButtonBase.gameObject.SetActive(false); |
| 11 | 22 | | dropdownButtonBase.OnSelected += OnSortButtonPressed; |
| 11 | 23 | | buttonsPool.Enqueue(dropdownButtonBase); |
| | 24 | |
|
| 11 | 25 | | gameObject.SetActive(false); |
| 11 | 26 | | } |
| | 27 | |
|
| 0 | 28 | | public int GetSortTypesCount() { return activeButtons.Count; } |
| | 29 | |
|
| | 30 | | public void Show() |
| | 31 | | { |
| 1 | 32 | | if (!gameObject.activeSelf) |
| | 33 | | { |
| 1 | 34 | | gameObject.SetActive(true); |
| | 35 | | } |
| | 36 | |
|
| 1 | 37 | | if (EventSystem.current != null) |
| | 38 | | { |
| 0 | 39 | | EventSystem.current.SetSelectedGameObject(gameObject); |
| | 40 | | } |
| | 41 | |
|
| 1 | 42 | | showHideAnimator.Show(); |
| 1 | 43 | | } |
| | 44 | |
|
| 0 | 45 | | public void Hide() { showHideAnimator.Hide(); } |
| | 46 | |
|
| | 47 | | public void AddSortType(string[] texts) |
| | 48 | | { |
| 24 | 49 | | for (int i = 0; i < texts.Length; i++) |
| | 50 | | { |
| 5 | 51 | | AddSortType(texts[i]); |
| | 52 | | } |
| 7 | 53 | | } |
| | 54 | |
|
| | 55 | | public void AddSortType(string text) |
| | 56 | | { |
| | 57 | | SortDropdownButton button; |
| 5 | 58 | | if (buttonsPool.Count > 0) |
| | 59 | | { |
| 3 | 60 | | button = buttonsPool.Dequeue(); |
| 3 | 61 | | } |
| | 62 | | else |
| | 63 | | { |
| 2 | 64 | | button = Object.Instantiate(dropdownButtonBase, buttonsContainer); |
| 2 | 65 | | button.transform.ResetLocalTRS(); |
| 2 | 66 | | button.OnSelected += OnSortButtonPressed; |
| | 67 | | } |
| 5 | 68 | | button.SetText(text); |
| 5 | 69 | | button.gameObject.SetActive(true); |
| 5 | 70 | | activeButtons.Add(button); |
| 5 | 71 | | } |
| | 72 | |
|
| | 73 | | public void Clear() |
| | 74 | | { |
| 8 | 75 | | for (int i = 0; i < activeButtons.Count; i++) |
| | 76 | | { |
| 0 | 77 | | activeButtons[i].gameObject.SetActive(false); |
| 0 | 78 | | buttonsPool.Enqueue(activeButtons[i]); |
| | 79 | | } |
| 4 | 80 | | activeButtons.Clear(); |
| 4 | 81 | | } |
| | 82 | |
|
| 2 | 83 | | private void OnSortButtonPressed(string sortType) { OnSortTypeSelected?.Invoke(sortType); } |
| | 84 | |
|
| 0 | 85 | | void IDeselectHandler.OnDeselect(BaseEventData eventData) { Hide(); } |
| | 86 | | } |