| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | public interface ISectionSelectorComponentView |
| | 6 | | { |
| | 7 | | /// <summary> |
| | 8 | | /// Set the sections of the selector. |
| | 9 | | /// </summary> |
| | 10 | | /// <param name="sections">List of UI components.</param> |
| | 11 | | void SetSections(List<SectionToggleModel> sections); |
| | 12 | |
|
| | 13 | | /// <summary> |
| | 14 | | /// Get a section of the section selector. |
| | 15 | | /// </summary> |
| | 16 | | /// <param name="index">Index of the list of sections.</param> |
| | 17 | | /// <returns>A specific section toggle.</returns> |
| | 18 | | ISectionToggle GetSection(int index); |
| | 19 | |
|
| | 20 | | /// <summary> |
| | 21 | | /// Get all the sections of the section selector. |
| | 22 | | /// </summary> |
| | 23 | | /// <returns>The list of sections.</returns> |
| | 24 | | List<ISectionToggle> GetAllSections(); |
| | 25 | | } |
| | 26 | |
|
| | 27 | | public class SectionSelectorComponentView : BaseComponentView, ISectionSelectorComponentView, IComponentModelConfig |
| | 28 | | { |
| | 29 | | [Header("Prefab References")] |
| | 30 | | [SerializeField] internal SectionToggle sectionToggleTemplate; |
| | 31 | |
|
| | 32 | | [Header("Configuration")] |
| | 33 | | [SerializeField] internal SectionSelectorComponentModel model; |
| | 34 | |
|
| 101 | 35 | | internal List<ISectionToggle> instantiatedSections = new List<ISectionToggle>(); |
| | 36 | |
|
| | 37 | | public override void Awake() |
| | 38 | | { |
| 58 | 39 | | base.Awake(); |
| | 40 | |
|
| 58 | 41 | | RegisterCurrentInstantiatedSections(); |
| 58 | 42 | | } |
| | 43 | |
|
| | 44 | | public void Configure(BaseComponentModel newModel) |
| | 45 | | { |
| 1 | 46 | | model = (SectionSelectorComponentModel)newModel; |
| 1 | 47 | | RefreshControl(); |
| 1 | 48 | | } |
| | 49 | |
|
| | 50 | | public override void RefreshControl() |
| | 51 | | { |
| 16 | 52 | | if (model == null) |
| 0 | 53 | | return; |
| | 54 | |
|
| 16 | 55 | | SetSections(model.sections); |
| 16 | 56 | | } |
| | 57 | |
|
| | 58 | | public void SetSections(List<SectionToggleModel> sections) |
| | 59 | | { |
| 20 | 60 | | model.sections = sections; |
| | 61 | |
|
| 20 | 62 | | RemoveAllInstantiatedSections(); |
| | 63 | |
|
| 226 | 64 | | for (int i = 0; i < sections.Count; i++) |
| | 65 | | { |
| 93 | 66 | | CreateSection(sections[i], $"Section_{i}"); |
| | 67 | | } |
| | 68 | |
|
| 20 | 69 | | if (instantiatedSections.Count > 0) |
| 20 | 70 | | instantiatedSections[0].SelectToggle(); |
| 20 | 71 | | } |
| | 72 | |
|
| | 73 | | public ISectionToggle GetSection(int index) |
| | 74 | | { |
| 793 | 75 | | if (index >= instantiatedSections.Count) |
| 0 | 76 | | return null; |
| | 77 | |
|
| 793 | 78 | | return instantiatedSections[index]; |
| | 79 | | } |
| | 80 | |
|
| 0 | 81 | | public List<ISectionToggle> GetAllSections() { return instantiatedSections; } |
| | 82 | |
|
| | 83 | | internal void CreateSection(SectionToggleModel newSectionModel, string name) |
| | 84 | | { |
| 94 | 85 | | if (sectionToggleTemplate == null) |
| 0 | 86 | | return; |
| | 87 | |
|
| 94 | 88 | | SectionToggle newGO = Instantiate(sectionToggleTemplate, transform); |
| 94 | 89 | | newGO.name = name; |
| 94 | 90 | | newGO.gameObject.SetActive(true); |
| 94 | 91 | | newGO.SetInfo(newSectionModel); |
| 94 | 92 | | newGO.SelectToggle(); |
| 94 | 93 | | instantiatedSections.Add(newGO); |
| 94 | 94 | | } |
| | 95 | |
|
| | 96 | | internal void RemoveAllInstantiatedSections() |
| | 97 | | { |
| 250 | 98 | | foreach (Transform child in transform) |
| | 99 | | { |
| 104 | 100 | | if (child.gameObject == sectionToggleTemplate.gameObject) |
| | 101 | | continue; |
| | 102 | |
|
| 83 | 103 | | if (Application.isPlaying) |
| | 104 | | { |
| 83 | 105 | | Destroy(child.gameObject); |
| 83 | 106 | | } |
| | 107 | | else |
| | 108 | | { |
| 0 | 109 | | if (isActiveAndEnabled) |
| 0 | 110 | | StartCoroutine(DestroyGameObjectOnEditor(child.gameObject)); |
| | 111 | | } |
| | 112 | | } |
| | 113 | |
|
| 21 | 114 | | instantiatedSections.Clear(); |
| 21 | 115 | | } |
| | 116 | |
|
| | 117 | | internal IEnumerator DestroyGameObjectOnEditor(GameObject go) |
| | 118 | | { |
| 0 | 119 | | yield return null; |
| 0 | 120 | | DestroyImmediate(go); |
| 0 | 121 | | } |
| | 122 | |
|
| | 123 | | internal void RegisterCurrentInstantiatedSections() |
| | 124 | | { |
| 58 | 125 | | instantiatedSections.Clear(); |
| | 126 | |
|
| 784 | 127 | | foreach (Transform child in transform) |
| | 128 | | { |
| 334 | 129 | | if (child.gameObject == sectionToggleTemplate.gameObject || |
| | 130 | | child.name.Contains("TooltipRef")) |
| | 131 | | continue; |
| | 132 | |
|
| 276 | 133 | | SectionToggle existingSection = child.GetComponent<SectionToggle>(); |
| 276 | 134 | | if (existingSection != null) |
| 276 | 135 | | instantiatedSections.Add(existingSection); |
| | 136 | | else |
| 0 | 137 | | Destroy(child.gameObject); |
| | 138 | | } |
| 58 | 139 | | } |
| | 140 | | } |