| | 1 | | using DCL.SettingsPanelHUD.Sections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using DCL.SettingsCommon; |
| | 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 | |
|
| | 75 | | public void Initialize() |
| | 76 | | { |
| 2 | 77 | | view = CreateView(); |
| 2 | 78 | | view.Initialize(this, this); |
| 2 | 79 | | } |
| | 80 | | protected virtual SettingsPanelHUDView CreateView() |
| | 81 | | { |
| 2 | 82 | | return SettingsPanelHUDView.Create(); |
| | 83 | | } |
| | 84 | |
|
| | 85 | | public void Dispose() |
| | 86 | | { |
| 6 | 87 | | if (view != null) |
| 2 | 88 | | UnityEngine.Object.Destroy(view.gameObject); |
| 6 | 89 | | } |
| | 90 | |
|
| | 91 | | public void SetVisibility(bool visible) |
| | 92 | | { |
| 1 | 93 | | if (!visible && view.isOpen) |
| | 94 | | { |
| 0 | 95 | | OnClose?.Invoke(); |
| 0 | 96 | | } |
| 1 | 97 | | else if (visible && !view.isOpen) |
| | 98 | | { |
| 1 | 99 | | OnOpen?.Invoke(); |
| | 100 | | } |
| | 101 | |
|
| 1 | 102 | | view.SetVisibility(visible); |
| 1 | 103 | | } |
| | 104 | |
|
| | 105 | | public void AddSection( |
| | 106 | | SettingsButtonEntry newMenuButton, |
| | 107 | | ISettingsSectionView newSection, |
| | 108 | | ISettingsSectionController newSectionController, |
| | 109 | | SettingsSectionModel sectionConfig) |
| | 110 | | { |
| 3 | 111 | | newMenuButton?.Initialize(sectionConfig.icon, sectionConfig.text); |
| | 112 | |
|
| 3 | 113 | | newSection.Initialize(newSectionController, sectionConfig.widgets.ToList()); |
| 3 | 114 | | newSection.SetActive(false); |
| 3 | 115 | | sections.Add(newSection); |
| | 116 | |
|
| 3 | 117 | | newMenuButton?.ConfigureAction(() => |
| | 118 | | { |
| 0 | 119 | | foreach (var button in menuButtons) |
| | 120 | | { |
| 0 | 121 | | button.MarkAsSelected(false); |
| | 122 | | } |
| 0 | 123 | | newMenuButton.MarkAsSelected(true); |
| | 124 | |
|
| 0 | 125 | | OpenSection(newSection); |
| 0 | 126 | | }); |
| | 127 | |
|
| 3 | 128 | | menuButtons.Add(newMenuButton); |
| 3 | 129 | | } |
| | 130 | |
|
| | 131 | | public void OpenSection(ISettingsSectionView sectionToOpen) |
| | 132 | | { |
| 0 | 133 | | foreach (var section in sections) |
| | 134 | | { |
| 0 | 135 | | section.SetActive(section == sectionToOpen); |
| | 136 | | } |
| 0 | 137 | | } |
| | 138 | |
|
| | 139 | | public void OpenSection(int sectionIndex) |
| | 140 | | { |
| 18 | 141 | | for (int i = 0; i < sections.Count; i++) |
| | 142 | | { |
| 6 | 143 | | var section = sections[i]; |
| 6 | 144 | | section.SetActive(i == sectionIndex); |
| | 145 | | } |
| 3 | 146 | | } |
| | 147 | |
|
| | 148 | | public void MarkMenuButtonAsSelected(int buttonIndex) |
| | 149 | | { |
| 18 | 150 | | for (int i = 0; i < menuButtons.Count; i++) |
| | 151 | | { |
| 6 | 152 | | var button = menuButtons[i]; |
| 6 | 153 | | button.MarkAsSelected(i == buttonIndex); |
| | 154 | | } |
| 3 | 155 | | } |
| | 156 | |
|
| 0 | 157 | | public virtual void SaveSettings() { Settings.i.SaveSettings(); } |
| | 158 | |
|
| 0 | 159 | | public void ResetAllSettings() { Settings.i.ResetAllSettings(); } |
| | 160 | | } |
| | 161 | | } |