< 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:60
Uncovered lines:13
Coverable lines:73
Total lines:187
Line coverage:82.1% (60 of 73)
Covered branches:0
Total branches:0
Covered methods:8
Total methods:17
Method coverage:47% (8 of 17)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SettingsControlView()0%110100%
Initialize(...)0%14140100%
OnDestroy()0%550100%
RefreshControl()0%110100%
ApplySetting(...)0%2100%
OnInfoButtonClicked()0%6200%
OnGeneralSettingsChanged(...)0%2100%
OnQualitySettingsChanged(...)0%2100%
OnAnyDeactivationFlagChange(...)0%2100%
OnAnyDisableFlagChange(...)0%2100%
OnAnyOverrideFlagChange(...)0%2100%
AllFlagsAreDisabled(...)0%12300%
AnyFlagIsEnabled(...)0%12300%
SwitchVisibility(...)0%110100%
SwitchInteractibility(...)0%330100%
SetOverriden(...)0%110100%
SwitchUIControlInteractibility(...)0%10100100%

File(s)

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

#LineLine coverage
 1using DCL.SettingsCommon;
 2using DCL.SettingsCommon.SettingsControllers.BaseControllers;
 3using DCL.SettingsPanelHUD.Common;
 4using System.Collections.Generic;
 5using System.Linq;
 6using TMPro;
 7using UIComponents.Scripts.Components;
 8using UIComponents.Scripts.Components.Tooltip;
 9using UnityEngine;
 10using UnityEngine.UI;
 11using QualitySettings = DCL.SettingsCommon.QualitySettings;
 12
 13namespace DCL.SettingsPanelHUD.Controls
 14{
 15    /// <summary>
 16    /// MonoBehaviour that represents the base of a CONTROL view.
 17    /// </summary>
 18    public class SettingsControlView : MonoBehaviour, ISettingsControlView
 19    {
 20        [SerializeField] private CanvasGroup canvasGroup;
 21        [SerializeField] private CanvasGroup controlCanvasGroup;
 22
 23        [Space]
 24        [SerializeField] private TextMeshProUGUI title;
 25        [SerializeField] private Color titleDeactivationColor;
 26
 27        [Space]
 28        [SerializeField] private GameObject betaIndicator;
 29        [SerializeField] private ButtonComponentView infoButton;
 30        [SerializeField] private TooltipComponentView tooltip;
 31
 32        [Space]
 33        [SerializeField] private List<TextMeshProUGUI> valueLabels;
 34        [SerializeField] private Color valueLabelDeactivationColor;
 35
 36        [Space]
 37        [SerializeField] private List<Image> handleImages;
 38        [SerializeField] private Color handlerDeactivationColor;
 39
 40        [Space]
 41        [SerializeField] private List<CanvasGroup> controlBackgroundCanvasGroups;
 5042        [SerializeField] private float controlBackgroundDeactivationAlpha = 0.5f;
 43
 44        private 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 model, SettingsControlController controller)
 53        {
 3754            controlConfig = model;
 3755            settingsControlController = controller;
 3756            settingsControlController.Initialize();
 57
 3758            betaIndicator.SetActive(model.isBeta);
 59
 3760            string tooltipMessage = !string.IsNullOrEmpty(model.infoTooltipMessage) ? model.infoTooltipMessage : "This s
 3761            tooltip.SetModel(new TooltipComponentModel(tooltipMessage));
 62
 3763            infoButton.onClick.AddListener(OnInfoButtonClicked);
 3764            infoButton.gameObject.SetActive(model.infoButtonEnabled);
 65
 3766            title.text = model.title;
 3767            originalTitleColor = title.color;
 3768            originalLabelColor = valueLabels.Count > 0 ? valueLabels[0].color : Color.white;
 3769            originalHandlerColor = handleImages.Count > 0 ? handleImages[0].color : Color.white;
 3770            originalControlBackgroundAlpha = controlBackgroundCanvasGroups.Count > 0 ? controlBackgroundCanvasGroups[0].
 71
 4372            SwitchInteractibility(isInteractable: model.flagsThatDisableMe.All(flag => flag.Get() == false));
 4173            SwitchVisibility(isVisible: model.flagsThatDeactivateMe.All(flag => flag.Get() == false));
 74
 3775            if(!model.infoButtonEnabled)
 3876                SetOverriden(@override: model.flagsThatOverrideMe.Any(flag => flag.Get()));
 77
 3778            RefreshControl();
 79
 8680            foreach (BooleanVariable flag in model.flagsThatDisableMe)
 681                flag.OnChange += OnAnyDisableFlagChange;
 82
 8283            foreach (BooleanVariable flag in model.flagsThatDeactivateMe)
 484                flag.OnChange += OnAnyDeactivationFlagChange;
 85
 7886            foreach (BooleanVariable flag in model.flagsThatOverrideMe)
 287                flag.OnChange += OnAnyOverrideFlagChange;
 88
 3789            Settings.i.generalSettings.OnChanged += OnGeneralSettingsChanged;
 3790            Settings.i.qualitySettings.OnChanged += OnQualitySettingsChanged;
 3791            Settings.i.OnResetAllSettings += RefreshControl;
 3792        }
 93
 94        protected virtual void OnDestroy()
 95        {
 4396            if (controlConfig != null)
 97            {
 8698                foreach (BooleanVariable flag in controlConfig.flagsThatDisableMe)
 699                    flag.OnChange -= OnAnyDisableFlagChange;
 100
 82101                foreach (BooleanVariable flag in controlConfig.flagsThatDeactivateMe)
 4102                    flag.OnChange -= OnAnyDeactivationFlagChange;
 103
 78104                foreach (BooleanVariable flag in controlConfig.flagsThatOverrideMe)
 2105                    flag.OnChange -= OnAnyOverrideFlagChange;
 106            }
 107
 43108            infoButton.onClick.RemoveListener(OnInfoButtonClicked);
 109
 43110            Settings.i.generalSettings.OnChanged -= OnGeneralSettingsChanged;
 43111            Settings.i.qualitySettings.OnChanged -= OnQualitySettingsChanged;
 43112            Settings.i.OnResetAllSettings -= RefreshControl;
 43113        }
 114
 37115        public virtual void RefreshControl() { }
 116
 117        /// <summary>
 118        /// It will be triggered when the CONTROL state changes and will execute the main flow of the CONTROL controller
 119        /// </summary>
 120        /// <param name="newValue">Value of the new state. It can be a bool (for toggle controls), a float (for slider c
 121        protected void ApplySetting(object newValue)
 122        {
 0123            settingsControlController.UpdateSetting(newValue);
 0124            settingsControlController.ApplySettings();
 0125        }
 126
 127        private void OnInfoButtonClicked()
 128        {
 0129            if (!tooltip.gameObject.activeSelf)
 0130                tooltip.Show();
 0131        }
 132
 133        private void OnGeneralSettingsChanged(GeneralSettings _) =>
 0134            RefreshControl();
 135
 136        private void OnQualitySettingsChanged(QualitySettings _) =>
 0137            RefreshControl();
 138
 139        private void OnAnyDeactivationFlagChange(bool deactivateFlag, bool _ = false) =>
 0140            SwitchVisibility(isVisible: AllFlagsAreDisabled(deactivateFlag, controlConfig.flagsThatDeactivateMe));
 141
 142        private void OnAnyDisableFlagChange(bool disableFlag, bool _ = false) =>
 0143            SwitchInteractibility(isInteractable: AllFlagsAreDisabled(disableFlag, controlConfig.flagsThatDisableMe));
 144
 145        private void OnAnyOverrideFlagChange(bool overrideFlag, bool _ = false) =>
 0146            SetOverriden(@override: AnyFlagIsEnabled(overrideFlag, controlConfig.flagsThatOverrideMe));
 147
 148        private static bool AllFlagsAreDisabled(bool flagEnabled, List<BooleanVariable> flags) =>
 0149            !flagEnabled && flags.All(flag => flag.Get() == false);
 150
 151        private static bool AnyFlagIsEnabled(bool flagEnabled, List<BooleanVariable> flags) =>
 0152            flagEnabled || flags.Any(flag => flag.Get());
 153
 154        private void SwitchVisibility(bool isVisible)
 155        {
 37156            gameObject.SetActive(isVisible);
 37157            CommonSettingsPanelEvents.RaiseRefreshAllWidgetsSize();
 37158        }
 159
 160        private void SwitchInteractibility(bool isInteractable)
 161        {
 37162            title.color = isInteractable ? originalTitleColor : titleDeactivationColor;
 37163            SwitchUIControlInteractibility(isInteractable, canvasGroup);
 37164        }
 165
 166        private void SetOverriden(bool @override)
 167        {
 36168            infoButton.gameObject.SetActive(@override);
 36169            SwitchUIControlInteractibility(isInteractable: !@override, controlCanvasGroup);
 36170        }
 171
 172        private void SwitchUIControlInteractibility(bool isInteractable, CanvasGroup group)
 173        {
 300174            foreach (TextMeshProUGUI text in valueLabels)
 77175                text.color = isInteractable ? originalLabelColor : valueLabelDeactivationColor;
 176
 308177            foreach (Image image in handleImages)
 81178                image.color = isInteractable ? originalHandlerColor : handlerDeactivationColor;
 179
 308180            foreach (CanvasGroup bkgCanvasGroup in controlBackgroundCanvasGroups)
 81181                bkgCanvasGroup.alpha = isInteractable ? originalControlBackgroundAlpha : controlBackgroundDeactivationAl
 182
 73183            group.interactable = isInteractable;
 73184            group.blocksRaycasts = isInteractable;
 73185        }
 186    }
 187}