| | 1 | | using DCL.SettingsPanelHUD.Sections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using DCL.SettingsCommon; |
| | 5 | | using UnityEngine; |
| | 6 | | using DCL.HelpAndSupportHUD; |
| | 7 | |
|
| | 8 | | namespace DCL.SettingsPanelHUD |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Interface to implement a controller for the main settings panel. |
| | 12 | | /// </summary> |
| | 13 | | public interface ISettingsPanelHUDController |
| | 14 | | { |
| | 15 | | event System.Action OnRestartTutorial; |
| | 16 | |
|
| | 17 | | /// <summary> |
| | 18 | | /// List of all SECTIONS added to the main settings panel. |
| | 19 | | /// </summary> |
| | 20 | | List<ISettingsSectionView> sections { get; } |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// All the needed logic to initializes the controller and its associated view, and make them works. |
| | 24 | | /// </summary> |
| | 25 | | void Initialize(); |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Adds a SECTION into the main settings panel. |
| | 29 | | /// </summary> |
| | 30 | | /// <param name="newMenuButton">MonoBehaviour that will represent the menu button associated to the new SECTION. |
| | 31 | | /// <param name="newSection">New SECTION that will be added.</param> |
| | 32 | | /// <param name="newSectionController">Controller belonging to the new SECTION.</param> |
| | 33 | | /// <param name="sectionConfig">Model that will contain the configuration of the new SECTION.</param> |
| | 34 | | void AddSection(SettingsButtonEntry newMenuButton, ISettingsSectionView newSection, ISettingsSectionController n |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// Opens a specific SECTION of the main settings panel. |
| | 38 | | /// </summary> |
| | 39 | | /// <param name="sectionToOpen">SECTION to be opened.</param> |
| | 40 | | void OpenSection(ISettingsSectionView sectionToOpen); |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// Opens a specific SECTION of the main settings panel. |
| | 44 | | /// </summary> |
| | 45 | | /// <param name="sectionIndex">SECTION index to be opened. The index must be less than the section list length.< |
| | 46 | | void OpenSection(int sectionIndex); |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// Mark a specific menu button as selected. |
| | 50 | | /// </summary> |
| | 51 | | /// <param name="buttonIndex">Menu button index to be selected.</param> |
| | 52 | | void MarkMenuButtonAsSelected(int buttonIndex); |
| | 53 | |
|
| | 54 | | /// <summary> |
| | 55 | | /// Save all the current settings values. |
| | 56 | | /// </summary> |
| | 57 | | void SaveSettings(); |
| | 58 | |
|
| | 59 | | /// <summary> |
| | 60 | | /// Reset all the current settings to their default values. |
| | 61 | | /// </summary> |
| | 62 | | void ResetAllSettings(); |
| | 63 | |
|
| | 64 | | /// <summary> |
| | 65 | | /// Set the tutorial button as enabled/disabled. |
| | 66 | | /// </summary> |
| | 67 | | /// <param name="isEnabled">True for set it as enabled.</param> |
| | 68 | | void SetTutorialButtonEnabled(bool isEnabled); |
| | 69 | | } |
| | 70 | |
|
| | 71 | | /// <summary> |
| | 72 | | /// This controller is in charge of manage all the logic related to the main settings panel. |
| | 73 | | /// </summary> |
| | 74 | | public class SettingsPanelHUDController : IHUD, ISettingsPanelHUDController |
| | 75 | | { |
| | 76 | | private const string SECTION_TO_ACTIVATE_WORLD_PREVIEW = "graphics"; |
| | 77 | |
|
| 0 | 78 | | public SettingsPanelHUDView view { get; private set; } |
| | 79 | |
|
| | 80 | | public event System.Action OnOpen; |
| | 81 | | public event System.Action OnClose; |
| | 82 | |
|
| 1 | 83 | | public List<ISettingsSectionView> sections { get; } = new List<ISettingsSectionView>(); |
| | 84 | |
|
| 1 | 85 | | private List<SettingsButtonEntry> menuButtons = new List<SettingsButtonEntry>(); |
| | 86 | | private HelpAndSupportHUDController helpAndSupportHud; |
| | 87 | |
|
| 4 | 88 | | BaseVariable<bool> settingsPanelVisible => DataStore.i.settings.settingsPanelVisible; |
| 3 | 89 | | BaseVariable<Transform> configureSettingsInFullscreenMenu => DataStore.i.exploreV2.configureSettingsInFullscreen |
| | 90 | |
|
| | 91 | | public event System.Action OnRestartTutorial; |
| | 92 | |
|
| | 93 | | public void Initialize() |
| | 94 | | { |
| 1 | 95 | | view = CreateView(); |
| 1 | 96 | | view.Initialize(this, this); |
| | 97 | |
|
| 1 | 98 | | view.OnRestartTutorial += () => |
| | 99 | | { |
| 0 | 100 | | OnRestartTutorial?.Invoke(); |
| 0 | 101 | | DataStore.i.exploreV2.isOpen.Set(false); |
| 0 | 102 | | }; |
| | 103 | |
|
| 1 | 104 | | view.OnHelpAndSupportClicked += () => helpAndSupportHud.SetVisibility(true); |
| | 105 | |
|
| 1 | 106 | | settingsPanelVisible.OnChange += OnSettingsPanelVisibleChanged; |
| 1 | 107 | | OnSettingsPanelVisibleChanged(settingsPanelVisible.Get(), false); |
| | 108 | |
|
| 1 | 109 | | configureSettingsInFullscreenMenu.OnChange += ConfigureSettingsInFullscreenMenuChanged; |
| 1 | 110 | | ConfigureSettingsInFullscreenMenuChanged(configureSettingsInFullscreenMenu.Get(), null); |
| | 111 | |
|
| 1 | 112 | | DataStore.i.settings.isInitialized.Set(true); |
| 1 | 113 | | } |
| 1 | 114 | | protected virtual SettingsPanelHUDView CreateView() { return SettingsPanelHUDView.Create(); } |
| | 115 | |
|
| | 116 | | public void Dispose() |
| | 117 | | { |
| 1 | 118 | | settingsPanelVisible.OnChange -= OnSettingsPanelVisibleChanged; |
| 1 | 119 | | configureSettingsInFullscreenMenu.OnChange -= ConfigureSettingsInFullscreenMenuChanged; |
| | 120 | |
|
| 1 | 121 | | if (view != null) |
| 1 | 122 | | UnityEngine.Object.Destroy(view.gameObject); |
| 1 | 123 | | } |
| | 124 | |
|
| 2 | 125 | | public void SetVisibility(bool visible) { settingsPanelVisible.Set(visible); } |
| | 126 | |
|
| 4 | 127 | | private void OnSettingsPanelVisibleChanged(bool current, bool previous) { SetVisibility_Internal(current); } |
| | 128 | |
|
| | 129 | | public void SetVisibility_Internal(bool visible) |
| | 130 | | { |
| 2 | 131 | | if (!visible && view.isOpen) |
| | 132 | | { |
| 0 | 133 | | OnClose?.Invoke(); |
| 0 | 134 | | } |
| 2 | 135 | | else if (visible && !view.isOpen) |
| | 136 | | { |
| 1 | 137 | | OnOpen?.Invoke(); |
| | 138 | | } |
| | 139 | |
|
| 2 | 140 | | view.SetVisibility(visible); |
| 2 | 141 | | } |
| | 142 | |
|
| | 143 | | public void AddSection( |
| | 144 | | SettingsButtonEntry newMenuButton, |
| | 145 | | ISettingsSectionView newSection, |
| | 146 | | ISettingsSectionController newSectionController, |
| | 147 | | SettingsSectionModel sectionConfig) |
| | 148 | | { |
| 3 | 149 | | newMenuButton?.Initialize(sectionConfig.icon, sectionConfig.text); |
| | 150 | |
|
| 3 | 151 | | newSection.Initialize(newSectionController, sectionConfig.widgets.ToList(), sectionConfig.text); |
| 3 | 152 | | newSection.SetActive(false); |
| 3 | 153 | | sections.Add(newSection); |
| | 154 | |
|
| 3 | 155 | | newMenuButton?.ConfigureAction(() => |
| | 156 | | { |
| 0 | 157 | | foreach (var button in menuButtons) |
| | 158 | | { |
| 0 | 159 | | button.MarkAsSelected(false); |
| | 160 | | } |
| 0 | 161 | | newMenuButton.MarkAsSelected(true); |
| | 162 | |
|
| 0 | 163 | | OpenSection(newSection); |
| 0 | 164 | | }); |
| | 165 | |
|
| 3 | 166 | | menuButtons.Add(newMenuButton); |
| 3 | 167 | | } |
| | 168 | |
|
| | 169 | | public void OpenSection(ISettingsSectionView sectionToOpen) |
| | 170 | | { |
| 0 | 171 | | foreach (var section in sections) |
| | 172 | | { |
| 0 | 173 | | section.SetActive(section == sectionToOpen); |
| | 174 | |
|
| 0 | 175 | | if (section == sectionToOpen) |
| 0 | 176 | | view.SetWorldPreviewActive(section.sectionName.ToLower() == SECTION_TO_ACTIVATE_WORLD_PREVIEW); |
| | 177 | | } |
| 0 | 178 | | } |
| | 179 | |
|
| | 180 | | public void OpenSection(int sectionIndex) |
| | 181 | | { |
| 16 | 182 | | for (int i = 0; i < sections.Count; i++) |
| | 183 | | { |
| 6 | 184 | | var section = sections[i]; |
| 6 | 185 | | section.SetActive(i == sectionIndex); |
| | 186 | |
|
| 6 | 187 | | if (i == sectionIndex) |
| 2 | 188 | | view.SetWorldPreviewActive(section.sectionName.ToLower() == SECTION_TO_ACTIVATE_WORLD_PREVIEW); |
| | 189 | | } |
| 2 | 190 | | } |
| | 191 | |
|
| | 192 | | public void MarkMenuButtonAsSelected(int buttonIndex) |
| | 193 | | { |
| 16 | 194 | | for (int i = 0; i < menuButtons.Count; i++) |
| | 195 | | { |
| 6 | 196 | | var button = menuButtons[i]; |
| 6 | 197 | | button.MarkAsSelected(i == buttonIndex); |
| | 198 | | } |
| 2 | 199 | | } |
| | 200 | |
|
| 2 | 201 | | public virtual void SaveSettings() { Settings.i.SaveSettings(); } |
| | 202 | |
|
| 0 | 203 | | public void ResetAllSettings() { Settings.i.ResetAllSettings(); } |
| | 204 | |
|
| | 205 | | public void SetTutorialButtonEnabled(bool isEnabled) |
| | 206 | | { |
| 0 | 207 | | if (view != null) |
| 0 | 208 | | view.SetTutorialButtonEnabled(isEnabled); |
| 0 | 209 | | } |
| | 210 | |
|
| | 211 | | public void AddHelpAndSupportWindow(HelpAndSupportHUDController controller) |
| | 212 | | { |
| 1 | 213 | | if (controller == null || controller.view == null) |
| | 214 | | { |
| 0 | 215 | | Debug.LogWarning("AddHelpAndSupportWindow >>> Help and Support window doesn't exist yet!"); |
| 0 | 216 | | return; |
| | 217 | | } |
| | 218 | |
|
| 1 | 219 | | helpAndSupportHud = controller; |
| 1 | 220 | | view.OnAddHelpAndSupportWindow(); |
| 1 | 221 | | } |
| | 222 | |
|
| 2 | 223 | | private void ConfigureSettingsInFullscreenMenuChanged(Transform currentParentTransform, Transform previousParent |
| | 224 | | } |
| | 225 | | } |