| | 1 | | using DCL.SettingsPanelHUD.Common; |
| | 2 | | using DCL.SettingsPanelHUD.Sections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | |
|
| | 6 | | namespace DCL.SettingsPanelHUD |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Interface to implement a controller for the main settings panel. |
| | 10 | | /// </summary> |
| | 11 | | public interface ISettingsPanelHUDController |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// List of all SECTIONS added to the main settings panel. |
| | 15 | | /// </summary> |
| | 16 | | List<ISettingsSectionView> sections { get; } |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// All the needed logic to initializes the controller and its associated view, and make them works. |
| | 20 | | /// </summary> |
| | 21 | | void Initialize(); |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// Adds a SECTION into the main settings panel. |
| | 25 | | /// </summary> |
| | 26 | | /// <param name="newMenuButton">MonoBehaviour that will represent the menu button associated to the new SECTION. |
| | 27 | | /// <param name="newSection">New SECTION that will be added.</param> |
| | 28 | | /// <param name="newSectionController">Controller belonging to the new SECTION.</param> |
| | 29 | | /// <param name="sectionConfig">Model that will contain the configuration of the new SECTION.</param> |
| | 30 | | void AddSection(SettingsButtonEntry newMenuButton, ISettingsSectionView newSection, ISettingsSectionController n |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Opens a specific SECTION of the main settings panel. |
| | 34 | | /// </summary> |
| | 35 | | /// <param name="sectionToOpen">SECTION to be opened.</param> |
| | 36 | | void OpenSection(ISettingsSectionView sectionToOpen); |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// Opens a specific SECTION of the main settings panel. |
| | 40 | | /// </summary> |
| | 41 | | /// <param name="sectionIndex">SECTION index to be opened. The index must be less than the section list length.< |
| | 42 | | void OpenSection(int sectionIndex); |
| | 43 | |
|
| | 44 | | /// <summary> |
| | 45 | | /// Mark a specific menu button as selected. |
| | 46 | | /// </summary> |
| | 47 | | /// <param name="buttonIndex">Menu button index to be selected.</param> |
| | 48 | | void MarkMenuButtonAsSelected(int buttonIndex); |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// Save all the current settings values. |
| | 52 | | /// </summary> |
| | 53 | | void SaveSettings(); |
| | 54 | |
|
| | 55 | | /// <summary> |
| | 56 | | /// Reset all the current settings to their default values. |
| | 57 | | /// </summary> |
| | 58 | | void ResetAllSettings(); |
| | 59 | | } |
| | 60 | |
|
| | 61 | | /// <summary> |
| | 62 | | /// This controller is in charge of manage all the logic related to the main settings panel. |
| | 63 | | /// </summary> |
| | 64 | | public class SettingsPanelHUDController : IHUD, ISettingsPanelHUDController |
| | 65 | | { |
| 0 | 66 | | public SettingsPanelHUDView view { get; private set; } |
| | 67 | |
|
| | 68 | | public event System.Action OnOpen; |
| | 69 | | public event System.Action OnClose; |
| | 70 | |
|
| 2 | 71 | | public List<ISettingsSectionView> sections { get; } = new List<ISettingsSectionView>(); |
| | 72 | |
|
| 2 | 73 | | private List<SettingsButtonEntry> menuButtons = new List<SettingsButtonEntry>(); |
| | 74 | |
|
| 6 | 75 | | public SettingsPanelHUDController() { view = SettingsPanelHUDView.Create(); } |
| | 76 | |
|
| | 77 | | public void Dispose() |
| | 78 | | { |
| 6 | 79 | | if (view != null) |
| 2 | 80 | | UnityEngine.Object.Destroy(view.gameObject); |
| 6 | 81 | | } |
| | 82 | |
|
| | 83 | | public void SetVisibility(bool visible) |
| | 84 | | { |
| 1 | 85 | | if (!visible && view.isOpen) |
| | 86 | | { |
| 0 | 87 | | OnClose?.Invoke(); |
| 0 | 88 | | } |
| 1 | 89 | | else if (visible && !view.isOpen) |
| | 90 | | { |
| 1 | 91 | | OnOpen?.Invoke(); |
| | 92 | | } |
| | 93 | |
|
| 1 | 94 | | view.SetVisibility(visible); |
| 1 | 95 | | } |
| | 96 | |
|
| 2 | 97 | | public void Initialize() { view.Initialize(this, this); } |
| | 98 | |
|
| | 99 | | public void AddSection( |
| | 100 | | SettingsButtonEntry newMenuButton, |
| | 101 | | ISettingsSectionView newSection, |
| | 102 | | ISettingsSectionController newSectionController, |
| | 103 | | SettingsSectionModel sectionConfig) |
| | 104 | | { |
| 3 | 105 | | newMenuButton?.Initialize(sectionConfig.icon, sectionConfig.text); |
| | 106 | |
|
| 3 | 107 | | newSection.Initialize(newSectionController, sectionConfig.widgets.ToList()); |
| 3 | 108 | | newSection.SetActive(false); |
| 3 | 109 | | sections.Add(newSection); |
| | 110 | |
|
| 3 | 111 | | newMenuButton?.ConfigureAction(() => |
| | 112 | | { |
| 0 | 113 | | foreach (var button in menuButtons) |
| | 114 | | { |
| 0 | 115 | | button.MarkAsSelected(false); |
| | 116 | | } |
| 0 | 117 | | newMenuButton.MarkAsSelected(true); |
| | 118 | |
|
| 0 | 119 | | OpenSection(newSection); |
| 0 | 120 | | }); |
| | 121 | |
|
| 3 | 122 | | menuButtons.Add(newMenuButton); |
| 3 | 123 | | } |
| | 124 | |
|
| | 125 | | public void OpenSection(ISettingsSectionView sectionToOpen) |
| | 126 | | { |
| 0 | 127 | | foreach (var section in sections) |
| | 128 | | { |
| 0 | 129 | | if (section == sectionToOpen) |
| | 130 | | continue; |
| | 131 | |
|
| 0 | 132 | | section.SetActive(false); |
| | 133 | | } |
| | 134 | |
|
| 0 | 135 | | sectionToOpen.SetActive(true); |
| 0 | 136 | | } |
| | 137 | |
|
| | 138 | | public void OpenSection(int sectionIndex) |
| | 139 | | { |
| 16 | 140 | | foreach (var section in sections) |
| | 141 | | { |
| 6 | 142 | | if (section == sections[sectionIndex]) |
| | 143 | | continue; |
| | 144 | |
|
| 4 | 145 | | section.SetActive(false); |
| | 146 | | } |
| | 147 | |
|
| 2 | 148 | | sections[sectionIndex].SetActive(true); |
| 2 | 149 | | } |
| | 150 | |
|
| | 151 | | public void MarkMenuButtonAsSelected(int buttonIndex) |
| | 152 | | { |
| 16 | 153 | | foreach (var button in menuButtons) |
| | 154 | | { |
| 6 | 155 | | button.MarkAsSelected(false); |
| | 156 | | } |
| | 157 | |
|
| 2 | 158 | | menuButtons[buttonIndex].MarkAsSelected(true); |
| 2 | 159 | | } |
| | 160 | |
|
| 0 | 161 | | public void SaveSettings() { Settings.i.SaveSettings(); } |
| | 162 | |
|
| 0 | 163 | | public void ResetAllSettings() { Settings.i.ResetAllSettings(); } |
| | 164 | | } |
| | 165 | | } |