< 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:55
Uncovered lines:13
Coverable lines:68
Total lines:188
Line coverage:80.8% (55 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%5.155081.82%
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;
 4144        [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        {
 3456            this.controlConfig = controlConfig;
 3457            this.settingsControlController = settingsControlController;
 3458            this.settingsControlController.Initialize();
 3459            title.text = controlConfig.title;
 3460            betaIndicator.SetActive(controlConfig.isBeta);
 3461            originalTitleColor = title.color;
 3462            originalLabelColor = valueLabels.Count > 0 ? valueLabels[0].color : Color.white;
 3463            originalHandlerColor = handleImages.Count > 0 ? handleImages[0].color : Color.white;
 3464            originalControlBackgroundAlpha = controlBackgroundCanvasGroups.Count > 0 ? controlBackgroundCanvasGroups[0].
 65
 7866            foreach (BooleanVariable flag in controlConfig.flagsThatDisableMe)
 67            {
 568                flag.OnChange += OnAnyDisableFlagChange;
 569                OnAnyDisableFlagChange(flag.Get(), false);
 70            }
 71
 7272            foreach (BooleanVariable flag in controlConfig.flagsThatDeactivateMe)
 73            {
 274                flag.OnChange += OnAnyDeactivationFlagChange;
 275                OnAnyDeactivationFlagChange(flag.Get(), false);
 76            }
 77
 3478            RefreshControl();
 79
 3480            Settings.i.generalSettings.OnChanged += OnGeneralSettingsChanged;
 3481            Settings.i.qualitySettings.OnChanged += OnQualitySettingsChanged;
 3482            Settings.i.OnResetAllSettings += OnResetSettingsControl;
 3483        }
 84
 85        protected virtual void OnDestroy()
 86        {
 3787            if (controlConfig != null)
 88            {
 7889                foreach (BooleanVariable flag in controlConfig.flagsThatDisableMe)
 90                {
 591                    flag.OnChange -= OnAnyDisableFlagChange;
 92                }
 93
 7294                foreach (BooleanVariable flag in controlConfig.flagsThatDeactivateMe)
 95                {
 296                    flag.OnChange -= OnAnyDeactivationFlagChange;
 97                }
 98            }
 99
 37100            Settings.i.generalSettings.OnChanged -= OnGeneralSettingsChanged;
 37101            Settings.i.qualitySettings.OnChanged -= OnQualitySettingsChanged;
 37102            Settings.i.OnResetAllSettings -= OnResetSettingsControl;
 37103        }
 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        {
 5119            bool canApplychange = true;
 5120            if (!current)
 121            {
 122                // Check if all the disable flags are false before enable the control
 16123                foreach (var flag in controlConfig.flagsThatDisableMe)
 124                {
 4125                    if (flag.Get() == true)
 126                    {
 0127                        canApplychange = false;
 0128                        break;
 129                    }
 130                }
 131            }
 132
 5133            if (canApplychange)
 5134                SetEnabled(!current);
 5135        }
 136
 137        private void OnAnyDeactivationFlagChange(bool current, bool previous)
 138        {
 2139            bool canApplychange = true;
 2140            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
 2153            if (canApplychange)
 2154                SetControlActive(!current);
 2155        }
 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        {
 5165            title.color = enabled ? originalTitleColor : titleDeactivationColor;
 20166            foreach (var text in valueLabels)
 167            {
 5168                text.color = enabled ? originalLabelColor : valueLabelDeactivationColor;
 169            }
 20170            foreach (var image in handleImages)
 171            {
 5172                image.color = enabled ? originalHandlerColor : handlerDeactivationColor;
 173            }
 20174            foreach (var canvasGroup in controlBackgroundCanvasGroups)
 175            {
 5176                canvasGroup.alpha = enabled ? originalControlBackgroundAlpha : controlBackgroundDeactivationAlpha;
 177            }
 5178            canvasGroup.interactable = enabled;
 5179            canvasGroup.blocksRaycasts = enabled;
 5180        }
 181
 182        private void SetControlActive(bool actived)
 183        {
 2184            gameObject.SetActive(actived);
 2185            CommonSettingsPanelEvents.RaiseRefreshAllWidgetsSize();
 2186        }
 187    }
 188}