< Summary

Class:DCL.SettingsPanelHUD.Controls.SettingsControlView
Assembly:SettingsPanelHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/SettingsPanelHUD/Scripts/ControlsModule/SettingsControlView.cs
Covered lines:57
Uncovered lines:11
Coverable lines:68
Total lines:188
Line coverage:83.8% (57 of 68)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SettingsControlView()0%110100%
Initialize(...)0%770100%
OnDestroy()0%440100%
RefreshControl()0%2100%
ApplySetting(...)0%2100%
OnAnyDisableFlagChange(...)0%550100%
OnAnyDeactivationFlagChange(...)0%9.065045.45%
OnGeneralSettingsChanged(...)0%2100%
OnQualitySettingsChanged(...)0%2100%
OnResetSettingsControl()0%2100%
SetEnabled(...)0%12120100%
SetControlActive(...)0%110100%

File(s)

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

#LineLine coverage
 1using DCL.SettingsPanelHUD.Common;
 2using System.Collections.Generic;
 3using DCL.SettingsCommon;
 4using DCL.SettingsCommon.SettingsControllers.BaseControllers;
 5using TMPro;
 6using UnityEngine;
 7using UnityEngine.UI;
 8using QualitySettings = DCL.SettingsCommon.QualitySettings;
 9
 10namespace DCL.SettingsPanelHUD.Controls
 11{
 12    /// <summary>
 13    /// Base interface to implement a view for a CONTROL.
 14    /// </summary>
 15    public interface ISettingsControlView
 16    {
 17        /// <summary>
 18        /// All the needed base logic to initializes the CONTROL view.
 19        /// </summary>
 20        /// <param name="controlConfig">Model that will contain the configuration of the CONTROL.</param>
 21        /// <param name="settingsControlController">Controller associated to the CONTROL view.</param>
 22        void Initialize(SettingsControlModel controlConfig, SettingsControlController settingsControlController);
 23
 24        /// <summary>
 25        /// This logic should update the CONTROL view with the stored value.
 26        /// </summary>
 27        void RefreshControl();
 28    }
 29
 30    /// <summary>
 31    /// MonoBehaviour that represents the base of a CONTROL view.
 32    /// </summary>
 33    public class SettingsControlView : MonoBehaviour, ISettingsControlView
 34    {
 35        [SerializeField] private TextMeshProUGUI title;
 36        [SerializeField] private Color titleDeactivationColor;
 37        [SerializeField] private GameObject betaIndicator;
 38        [SerializeField] private CanvasGroup canvasGroup;
 39        [SerializeField] private List<TextMeshProUGUI> valueLabels;
 40        [SerializeField] private Color valueLabelDeactivationColor;
 41        [SerializeField] private List<Image> handleImages;
 42        [SerializeField] private Color handlerDeactivationColor;
 43        [SerializeField] private List<CanvasGroup> controlBackgroundCanvasGroups;
 3544        [SerializeField] private float controlBackgroundDeactivationAlpha = 0.5f;
 45
 46        protected SettingsControlController settingsControlController;
 47
 48        private SettingsControlModel controlConfig;
 49        private Color originalTitleColor;
 50        private Color originalLabelColor;
 51        private Color originalHandlerColor;
 52        private float originalControlBackgroundAlpha;
 53
 54        public virtual void Initialize(SettingsControlModel controlConfig, SettingsControlController settingsControlCont
 55        {
 2856            this.controlConfig = controlConfig;
 2857            this.settingsControlController = settingsControlController;
 2858            this.settingsControlController.Initialize();
 2859            title.text = controlConfig.title;
 2860            betaIndicator.SetActive(controlConfig.isBeta);
 2861            originalTitleColor = title.color;
 2862            originalLabelColor = valueLabels.Count > 0 ? valueLabels[0].color : Color.white;
 2863            originalHandlerColor = handleImages.Count > 0 ? handleImages[0].color : Color.white;
 2864            originalControlBackgroundAlpha = controlBackgroundCanvasGroups.Count > 0 ? controlBackgroundCanvasGroups[0].
 65
 9066            foreach (BooleanVariable flag in controlConfig.flagsThatDisableMe)
 67            {
 1768                flag.OnChange += OnAnyDisableFlagChange;
 1769                OnAnyDisableFlagChange(flag.Get(), false);
 70            }
 71
 6272            foreach (BooleanVariable flag in controlConfig.flagsThatDeactivateMe)
 73            {
 374                flag.OnChange += OnAnyDeactivationFlagChange;
 375                OnAnyDeactivationFlagChange(flag.Get(), false);
 76            }
 77
 2878            RefreshControl();
 79
 2880            Settings.i.generalSettings.OnChanged += OnGeneralSettingsChanged;
 2881            Settings.i.qualitySettings.OnChanged += OnQualitySettingsChanged;
 2882            Settings.i.OnResetAllSettings += OnResetSettingsControl;
 2883        }
 84
 85        protected virtual void OnDestroy()
 86        {
 3187            if (controlConfig != null)
 88            {
 9089                foreach (BooleanVariable flag in controlConfig.flagsThatDisableMe)
 90                {
 1791                    flag.OnChange -= OnAnyDisableFlagChange;
 92                }
 93
 6294                foreach (BooleanVariable flag in controlConfig.flagsThatDeactivateMe)
 95                {
 396                    flag.OnChange -= OnAnyDeactivationFlagChange;
 97                }
 98            }
 99
 31100            Settings.i.generalSettings.OnChanged -= OnGeneralSettingsChanged;
 31101            Settings.i.qualitySettings.OnChanged -= OnQualitySettingsChanged;
 31102            Settings.i.OnResetAllSettings -= OnResetSettingsControl;
 31103        }
 104
 0105        public virtual void RefreshControl() { }
 106
 107        /// <summary>
 108        /// It will be triggered when the CONTROL state changes and will execute the main flow of the CONTROL controller
 109        /// </summary>
 110        /// <param name="newValue">Value of the new state. It can be a bool (for toggle controls), a float (for slider c
 111        protected void ApplySetting(object newValue)
 112        {
 0113            settingsControlController.UpdateSetting(newValue);
 0114            settingsControlController.ApplySettings();
 0115        }
 116
 117        private void OnAnyDisableFlagChange(bool current, bool previous)
 118        {
 17119            bool canApplychange = true;
 17120            if (!current)
 121            {
 122                // Check if all the disable flags are false before enable the control
 75123                foreach (var flag in controlConfig.flagsThatDisableMe)
 124                {
 22125                    if (flag.Get() == true)
 126                    {
 1127                        canApplychange = false;
 1128                        break;
 129                    }
 130                }
 131            }
 132
 17133            if (canApplychange)
 16134                SetEnabled(!current);
 17135        }
 136
 137        private void OnAnyDeactivationFlagChange(bool current, bool previous)
 138        {
 3139            bool canApplychange = true;
 3140            if (!current)
 141            {
 142                // Check if all the deactivation flags are false before enable the control
 0143                foreach (var flag in controlConfig.flagsThatDeactivateMe)
 144                {
 0145                    if (flag.Get() == true)
 146                    {
 0147                        canApplychange = false;
 0148                        break;
 149                    }
 150                }
 151            }
 152
 3153            if (canApplychange)
 3154                SetControlActive(!current);
 3155        }
 156
 0157        private void OnGeneralSettingsChanged(GeneralSettings obj) { RefreshControl(); }
 158
 0159        private void OnQualitySettingsChanged(QualitySettings obj) { RefreshControl(); }
 160
 0161        private void OnResetSettingsControl() { RefreshControl(); }
 162
 163        private void SetEnabled(bool enabled)
 164        {
 16165            title.color = enabled ? originalTitleColor : titleDeactivationColor;
 68166            foreach (var text in valueLabels)
 167            {
 18168                text.color = enabled ? originalLabelColor : valueLabelDeactivationColor;
 169            }
 68170            foreach (var image in handleImages)
 171            {
 18172                image.color = enabled ? originalHandlerColor : handlerDeactivationColor;
 173            }
 68174            foreach (var canvasGroup in controlBackgroundCanvasGroups)
 175            {
 18176                canvasGroup.alpha = enabled ? originalControlBackgroundAlpha : controlBackgroundDeactivationAlpha;
 177            }
 16178            canvasGroup.interactable = enabled;
 16179            canvasGroup.blocksRaycasts = enabled;
 16180        }
 181
 182        private void SetControlActive(bool actived)
 183        {
 3184            gameObject.SetActive(actived);
 3185            CommonSettingsPanelEvents.RaiseRefreshAllWidgetsSize();
 3186        }
 187    }
 188}