< Summary

Class:DCL.SettingsPanelHUD.SettingsPanelHUDController
Assembly:SettingsPanelHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/SettingsPanelHUD/Scripts/SettingsPanelHUDController.cs
Covered lines:51
Uncovered lines:20
Coverable lines:71
Total lines:225
Line coverage:71.8% (51 of 71)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SettingsPanelHUDController()0%110100%
Initialize()0%110100%
CreateView()0%110100%
Dispose()0%220100%
SetVisibility(...)0%110100%
OnSettingsPanelVisibleChanged(...)0%110100%
SetVisibility_Internal(...)0%7.237083.33%
AddSection(...)0%330100%
OpenSection(...)0%12300%
OpenSection(...)0%330100%
MarkMenuButtonAsSelected(...)0%220100%
SaveSettings()0%110100%
ResetAllSettings()0%2100%
SetTutorialButtonEnabled(...)0%6200%
AddHelpAndSupportWindow(...)0%3.333066.67%
ConfigureSettingsInFullscreenMenuChanged(...)0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/SettingsPanelHUD/Scripts/SettingsPanelHUDController.cs

#LineLine coverage
 1using DCL.SettingsPanelHUD.Sections;
 2using System.Collections.Generic;
 3using System.Linq;
 4using DCL.SettingsCommon;
 5using UnityEngine;
 6using DCL.HelpAndSupportHUD;
 7
 8namespace 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
 1478        public SettingsPanelHUDView view { get; private set; }
 79
 80        public event System.Action OnOpen;
 81        public event System.Action OnClose;
 82
 1883        public List<ISettingsSectionView> sections { get; } = new List<ISettingsSectionView>();
 84
 185        private List<SettingsButtonEntry> menuButtons = new List<SettingsButtonEntry>();
 86        private HelpAndSupportHUDController helpAndSupportHud;
 87
 488        BaseVariable<bool> settingsPanelVisible => DataStore.i.settings.settingsPanelVisible;
 389        BaseVariable<Transform> configureSettingsInFullscreenMenu => DataStore.i.exploreV2.configureSettingsInFullscreen
 90
 91        public event System.Action OnRestartTutorial;
 92
 93        public void Initialize()
 94        {
 195            view = CreateView();
 196            view.Initialize(this, this);
 97
 198            view.OnRestartTutorial += () =>
 99            {
 0100                OnRestartTutorial?.Invoke();
 0101                DataStore.i.exploreV2.isOpen.Set(false);
 0102            };
 103
 1104            view.OnHelpAndSupportClicked += () => helpAndSupportHud.SetVisibility(true);
 105
 1106            settingsPanelVisible.OnChange += OnSettingsPanelVisibleChanged;
 1107            OnSettingsPanelVisibleChanged(settingsPanelVisible.Get(), false);
 108
 1109            configureSettingsInFullscreenMenu.OnChange += ConfigureSettingsInFullscreenMenuChanged;
 1110            ConfigureSettingsInFullscreenMenuChanged(configureSettingsInFullscreenMenu.Get(), null);
 111
 1112            DataStore.i.settings.isInitialized.Set(true);
 1113        }
 1114        protected virtual SettingsPanelHUDView CreateView() { return SettingsPanelHUDView.Create(); }
 115
 116        public void Dispose()
 117        {
 1118            settingsPanelVisible.OnChange -= OnSettingsPanelVisibleChanged;
 1119            configureSettingsInFullscreenMenu.OnChange -= ConfigureSettingsInFullscreenMenuChanged;
 120
 1121            if (view != null)
 1122                UnityEngine.Object.Destroy(view.gameObject);
 1123        }
 124
 2125        public void SetVisibility(bool visible) { settingsPanelVisible.Set(visible); }
 126
 4127        private void OnSettingsPanelVisibleChanged(bool current, bool previous) { SetVisibility_Internal(current); }
 128
 129        public void SetVisibility_Internal(bool visible)
 130        {
 2131            if (!visible && view.isOpen)
 132            {
 0133                OnClose?.Invoke();
 134            }
 2135            else if (visible && !view.isOpen)
 136            {
 1137                OnOpen?.Invoke();
 138            }
 139
 2140            view.SetVisibility(visible);
 2141        }
 142
 143        public void AddSection(
 144            SettingsButtonEntry newMenuButton,
 145            ISettingsSectionView newSection,
 146            ISettingsSectionController newSectionController,
 147            SettingsSectionModel sectionConfig)
 148        {
 3149            newMenuButton?.Initialize(sectionConfig.icon, sectionConfig.text);
 150
 3151            newSection.Initialize(newSectionController, sectionConfig.widgets.ToList(), sectionConfig.text);
 3152            newSection.SetActive(false);
 3153            sections.Add(newSection);
 154
 3155            newMenuButton?.ConfigureAction(() =>
 156            {
 0157                foreach (var button in menuButtons)
 158                {
 0159                    button.MarkAsSelected(false);
 160                }
 0161                newMenuButton.MarkAsSelected(true);
 162
 0163                OpenSection(newSection);
 0164            });
 165
 3166            menuButtons.Add(newMenuButton);
 3167        }
 168
 169        public void OpenSection(ISettingsSectionView sectionToOpen)
 170        {
 0171            foreach (var section in sections)
 172            {
 0173                section.SetActive(section == sectionToOpen);
 174
 0175                if (section == sectionToOpen)
 0176                    view.SetWorldPreviewActive(section.sectionName.ToLower() == SECTION_TO_ACTIVATE_WORLD_PREVIEW);
 177            }
 0178        }
 179
 180        public void OpenSection(int sectionIndex)
 181        {
 16182            for (int i = 0; i < sections.Count; i++)
 183            {
 6184                var section = sections[i];
 6185                section.SetActive(i == sectionIndex);
 186
 6187                if (i == sectionIndex)
 2188                    view.SetWorldPreviewActive(section.sectionName.ToLower() == SECTION_TO_ACTIVATE_WORLD_PREVIEW);
 189            }
 2190        }
 191
 192        public void MarkMenuButtonAsSelected(int buttonIndex)
 193        {
 16194            for (int i = 0; i < menuButtons.Count; i++)
 195            {
 6196                var button = menuButtons[i];
 6197                button.MarkAsSelected(i == buttonIndex);
 198            }
 2199        }
 200
 2201        public virtual void SaveSettings() { Settings.i.SaveSettings(); }
 202
 0203        public void ResetAllSettings() { Settings.i.ResetAllSettings(); }
 204
 205        public void SetTutorialButtonEnabled(bool isEnabled)
 206        {
 0207            if (view != null)
 0208                view.SetTutorialButtonEnabled(isEnabled);
 0209        }
 210
 211        public void AddHelpAndSupportWindow(HelpAndSupportHUDController controller)
 212        {
 1213            if (controller == null || controller.view == null)
 214            {
 0215                Debug.LogWarning("AddHelpAndSupportWindow >>> Help and Support window doesn't exist yet!");
 0216                return;
 217            }
 218
 1219            helpAndSupportHud = controller;
 1220            view.OnAddHelpAndSupportWindow();
 1221        }
 222
 2223        private void ConfigureSettingsInFullscreenMenuChanged(Transform currentParentTransform, Transform previousParent
 224    }
 225}