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