< 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:186
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.SettingsControls;
 2using DCL.SettingsPanelHUD.Common;
 3using System.Collections.Generic;
 4using TMPro;
 5using UnityEngine;
 6using UnityEngine.UI;
 7
 8namespace DCL.SettingsPanelHUD.Controls
 9{
 10    /// <summary>
 11    /// Base interface to implement a view for a CONTROL.
 12    /// </summary>
 13    public interface ISettingsControlView
 14    {
 15        /// <summary>
 16        /// All the needed base logic to initializes the CONTROL view.
 17        /// </summary>
 18        /// <param name="controlConfig">Model that will contain the configuration of the CONTROL.</param>
 19        /// <param name="settingsControlController">Controller associated to the CONTROL view.</param>
 20        void Initialize(SettingsControlModel controlConfig, SettingsControlController settingsControlController);
 21
 22        /// <summary>
 23        /// This logic should update the CONTROL view with the stored value.
 24        /// </summary>
 25        void RefreshControl();
 26    }
 27
 28    /// <summary>
 29    /// MonoBehaviour that represents the base of a CONTROL view.
 30    /// </summary>
 31    public class SettingsControlView : MonoBehaviour, ISettingsControlView
 32    {
 33        [SerializeField] private TextMeshProUGUI title;
 34        [SerializeField] private Color titleDeactivationColor;
 35        [SerializeField] private GameObject betaIndicator;
 36        [SerializeField] private CanvasGroup canvasGroup;
 37        [SerializeField] private List<TextMeshProUGUI> valueLabels;
 38        [SerializeField] private Color valueLabelDeactivationColor;
 39        [SerializeField] private List<Image> handleImages;
 40        [SerializeField] private Color handlerDeactivationColor;
 41        [SerializeField] private List<CanvasGroup> controlBackgroundCanvasGroups;
 3342        [SerializeField] private float controlBackgroundDeactivationAlpha = 0.5f;
 43
 44        protected SettingsControlController settingsControlController;
 45
 46        private SettingsControlModel controlConfig;
 47        private Color originalTitleColor;
 48        private Color originalLabelColor;
 49        private Color originalHandlerColor;
 50        private float originalControlBackgroundAlpha;
 51
 52        public virtual void Initialize(SettingsControlModel controlConfig, SettingsControlController settingsControlCont
 53        {
 2754            this.controlConfig = controlConfig;
 2755            this.settingsControlController = settingsControlController;
 2756            this.settingsControlController.Initialize();
 2757            title.text = controlConfig.title;
 2758            betaIndicator.SetActive(controlConfig.isBeta);
 2759            originalTitleColor = title.color;
 2760            originalLabelColor = valueLabels.Count > 0 ? valueLabels[0].color : Color.white;
 2761            originalHandlerColor = handleImages.Count > 0 ? handleImages[0].color : Color.white;
 2762            originalControlBackgroundAlpha = controlBackgroundCanvasGroups.Count > 0 ? controlBackgroundCanvasGroups[0].
 63
 8864            foreach (BooleanVariable flag in controlConfig.flagsThatDisableMe)
 65            {
 1766                flag.OnChange += OnAnyDisableFlagChange;
 1767                OnAnyDisableFlagChange(flag.Get(), false);
 68            }
 69
 6070            foreach (BooleanVariable flag in controlConfig.flagsThatDeactivateMe)
 71            {
 372                flag.OnChange += OnAnyDeactivationFlagChange;
 373                OnAnyDeactivationFlagChange(flag.Get(), false);
 74            }
 75
 2776            RefreshControl();
 77
 2778            Settings.i.OnGeneralSettingsChanged += OnGeneralSettingsChanged;
 2779            Settings.i.OnQualitySettingsChanged += OnQualitySettingsChanged;
 2780            Settings.i.OnResetAllSettings += OnResetSettingsControl;
 2781        }
 82
 83        protected virtual void OnDestroy()
 84        {
 3085            if (controlConfig != null)
 86            {
 8887                foreach (BooleanVariable flag in controlConfig.flagsThatDisableMe)
 88                {
 1789                    flag.OnChange -= OnAnyDisableFlagChange;
 90                }
 91
 6092                foreach (BooleanVariable flag in controlConfig.flagsThatDeactivateMe)
 93                {
 394                    flag.OnChange -= OnAnyDeactivationFlagChange;
 95                }
 96            }
 97
 3098            Settings.i.OnGeneralSettingsChanged -= OnGeneralSettingsChanged;
 3099            Settings.i.OnQualitySettingsChanged -= OnQualitySettingsChanged;
 30100            Settings.i.OnResetAllSettings -= OnResetSettingsControl;
 30101        }
 102
 0103        public virtual void RefreshControl() { }
 104
 105        /// <summary>
 106        /// It will be triggered when the CONTROL state changes and will execute the main flow of the CONTROL controller
 107        /// </summary>
 108        /// <param name="newValue">Value of the new state. It can be a bool (for toggle controls), a float (for slider c
 109        protected void ApplySetting(object newValue)
 110        {
 0111            settingsControlController.UpdateSetting(newValue);
 0112            settingsControlController.ApplySettings();
 0113        }
 114
 115        private void OnAnyDisableFlagChange(bool current, bool previous)
 116        {
 17117            bool canApplychange = true;
 17118            if (!current)
 119            {
 120                // Check if all the disable flags are false before enable the control
 75121                foreach (var flag in controlConfig.flagsThatDisableMe)
 122                {
 22123                    if (flag.Get() == true)
 124                    {
 1125                        canApplychange = false;
 1126                        break;
 127                    }
 128                }
 129            }
 130
 17131            if (canApplychange)
 16132                SetEnabled(!current);
 17133        }
 134
 135        private void OnAnyDeactivationFlagChange(bool current, bool previous)
 136        {
 3137            bool canApplychange = true;
 3138            if (!current)
 139            {
 140                // Check if all the deactivation flags are false before enable the control
 0141                foreach (var flag in controlConfig.flagsThatDeactivateMe)
 142                {
 0143                    if (flag.Get() == true)
 144                    {
 0145                        canApplychange = false;
 0146                        break;
 147                    }
 148                }
 149            }
 150
 3151            if (canApplychange)
 3152                SetControlActive(!current);
 3153        }
 154
 0155        private void OnGeneralSettingsChanged(SettingsData.GeneralSettings obj) { RefreshControl(); }
 156
 0157        private void OnQualitySettingsChanged(SettingsData.QualitySettings obj) { RefreshControl(); }
 158
 0159        private void OnResetSettingsControl() { RefreshControl(); }
 160
 161        private void SetEnabled(bool enabled)
 162        {
 16163            title.color = enabled ? originalTitleColor : titleDeactivationColor;
 76164            foreach (var text in valueLabels)
 165            {
 22166                text.color = enabled ? originalLabelColor : valueLabelDeactivationColor;
 167            }
 84168            foreach (var image in handleImages)
 169            {
 26170                image.color = enabled ? originalHandlerColor : handlerDeactivationColor;
 171            }
 84172            foreach (var canvasGroup in controlBackgroundCanvasGroups)
 173            {
 26174                canvasGroup.alpha = enabled ? originalControlBackgroundAlpha : controlBackgroundDeactivationAlpha;
 175            }
 16176            canvasGroup.interactable = enabled;
 16177            canvasGroup.blocksRaycasts = enabled;
 16178        }
 179
 180        private void SetControlActive(bool actived)
 181        {
 3182            gameObject.SetActive(actived);
 3183            CommonSettingsPanelEvents.RaiseRefreshAllWidgetsSize();
 3184        }
 185    }
 186}