< Summary

Class:DCL.SettingsPanelHUD.SettingsPanelHUDView
Assembly:SettingsPanelHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/SettingsPanelHUD/Scripts/SettingsPanelHUDView.cs
Covered lines:75
Uncovered lines:27
Coverable lines:102
Total lines:240
Line coverage:73.5% (75 of 102)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Create()0%110100%
Initialize(...)0%110100%
Initialize(...)0%110100%
OnDestroy()0%220100%
CreateSections()0%330100%
ShowResetAllConfirmation()0%2100%
HideResetAllConfirmation()0%110100%
ResetAllSettings()0%2100%
CloseSettingsPanel()0%2100%
SetVisibility(...)0%220100%
SetAsFullScreenMenuMode(...)0%4.312016.67%
SetTutorialButtonEnabled(...)0%2100%
OnAddHelpAndSupportWindow()0%110100%
SetWorldPreviewActive(...)0%440100%
OpenAction_OnTriggered(...)0%2100%
CloseResetAllAction_OnTriggered(...)0%2100%
OnFinishHide(...)0%110100%
ScreenSizeChanged(...)0%110100%
RefreshWorldPreviewSize()0%550100%

File(s)

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

#LineLine coverage
 1using DCL.Helpers;
 2using DCL.SettingsPanelHUD.Sections;
 3using System.Collections;
 4using UnityEngine;
 5using UnityEngine.UI;
 6
 7namespace DCL.SettingsPanelHUD
 8{
 9    /// <summary>
 10    /// MonoBehaviour that represents the main settings panel view and will act as a factory of SECTIONS.
 11    /// </summary>
 12    public class SettingsPanelHUDView : MonoBehaviour
 13    {
 14        [Header("General configuration")]
 15        [SerializeField] private GameObject mainWindow;
 16
 17        [Header("Sections configuration")]
 18        [SerializeField] private SettingsPanelModel settingsPanelConfig;
 19        [SerializeField] private Transform menuButtonsContainer;
 20        [SerializeField] private Transform sectionsContainer;
 21
 22        [Header("Reset All configuration")]
 23        [SerializeField] private Button resetAllButton;
 24        [SerializeField] private ShowHideAnimator resetAllConfirmation;
 25        [SerializeField] private Button resetAllOkButton;
 26        [SerializeField] private Button resetAllCancelButton;
 27        [SerializeField] private GameObject resetAllBlackOverlay;
 28        [SerializeField] internal InputAction_Trigger closeResetAllAction;
 29
 30        [Header("Open/Close Settings")]
 31        [SerializeField] private Button closeButton;
 32        [SerializeField] private Button backgroundButton;
 33        [SerializeField] private InputAction_Trigger openAction;
 34
 35        [Header("World Preview Window")]
 36        [SerializeField] private RectTransform worldPreviewWindowTransform;
 37        [SerializeField] private CanvasGroup worldPreviewCanvasGroup;
 38        [SerializeField] private RawImage worldPreviewRawImage;
 39
 40        [Header("Others")]
 41        [SerializeField] private Button tutorialButton;
 42        [SerializeField] private Button reportBugButton;
 43        [SerializeField] private Button helpAndSupportButton;
 44
 45        [Header("Animations")]
 46        [SerializeField] private ShowHideAnimator settingsAnimator;
 47
 048        public bool isOpen { get; private set; }
 49
 50        private const string PATH = "SettingsPanelHUD";
 51        private const float WORLD_PREVIEW_MIN_WIDTH_TO_BE_SHOWED = 200f;
 52        private const float WORLD_PREVIEW_ORIGINAL_WIDTH = 400f;
 53        private const float WORLD_PREVIEW_ORIGINAL_HEIGHT = 250f;
 54
 55        private IHUD hudController;
 56        private ISettingsPanelHUDController settingsPanelController;
 57
 58        public event System.Action OnRestartTutorial;
 59        public event System.Action OnHelpAndSupportClicked;
 60
 61        public static SettingsPanelHUDView Create()
 62        {
 263            SettingsPanelHUDView view = Instantiate(Resources.Load<GameObject>(PATH)).GetComponent<SettingsPanelHUDView>
 264            view.name = "_SettingsPanelHUD";
 265            return view;
 66        }
 67
 68        public void Initialize(IHUD hudController, ISettingsPanelHUDController settingsPanelController)
 69        {
 270            this.hudController = hudController;
 271            this.settingsPanelController = settingsPanelController;
 72
 273            openAction.OnTriggered += OpenAction_OnTriggered;
 274            closeResetAllAction.OnTriggered += CloseResetAllAction_OnTriggered;
 75
 276            resetAllButton.onClick.AddListener(ShowResetAllConfirmation);
 277            resetAllCancelButton.onClick.AddListener(HideResetAllConfirmation);
 278            resetAllOkButton.onClick.AddListener(ResetAllSettings);
 79
 280            closeButton.onClick.AddListener(CloseSettingsPanel);
 281            backgroundButton.onClick.AddListener(CloseSettingsPanel);
 282            settingsAnimator.OnWillFinishHide += OnFinishHide;
 83
 284            CreateSections();
 285            isOpen = !settingsAnimator.hideOnEnable;
 286            settingsAnimator.Hide(true);
 87
 288            tutorialButton.onClick.AddListener(() => OnRestartTutorial?.Invoke());
 89
 290            DataStore.i.screen.size.OnChange += ScreenSizeChanged;
 291            ScreenSizeChanged(DataStore.i.screen.size.Get(), Vector2Int.zero);
 292        }
 93
 94        public void Initialize(IHUD hudController, ISettingsPanelHUDController settingsPanelController, SettingsSectionL
 95        {
 196            settingsPanelConfig.sections = sections;
 197            Initialize(hudController, settingsPanelController);
 198        }
 99
 100        private void OnDestroy()
 101        {
 2102            openAction.OnTriggered -= OpenAction_OnTriggered;
 2103            closeResetAllAction.OnTriggered -= CloseResetAllAction_OnTriggered;
 104
 2105            if (settingsAnimator)
 2106                settingsAnimator.OnWillFinishHide -= OnFinishHide;
 107
 2108            tutorialButton.onClick.RemoveAllListeners();
 2109            helpAndSupportButton.onClick.RemoveAllListeners();
 110
 2111            DataStore.i.screen.size.OnChange -= ScreenSizeChanged;
 2112        }
 113
 114        private void CreateSections()
 115        {
 12116            foreach (SettingsSectionModel sectionConfig in settingsPanelConfig.sections)
 117            {
 4118                var newMenuButton = Instantiate(sectionConfig.menuButtonPrefab, menuButtonsContainer);
 4119                var newSection = Instantiate(sectionConfig.sectionPrefab, sectionsContainer);
 4120                newSection.gameObject.name = $"Section_{sectionConfig.text}";
 4121                var newSectionController = Instantiate(sectionConfig.sectionController);
 4122                settingsPanelController.AddSection(newMenuButton, newSection, newSectionController, sectionConfig);
 123            }
 124
 2125            settingsPanelController.OpenSection(0);
 2126            settingsPanelController.MarkMenuButtonAsSelected(0);
 2127        }
 128
 129        private void ShowResetAllConfirmation()
 130        {
 0131            DataStore.i.exploreV2.isSomeModalOpen.Set(true);
 0132            resetAllConfirmation.Show();
 0133            resetAllBlackOverlay.SetActive(true);
 0134        }
 135
 136        private void HideResetAllConfirmation()
 137        {
 1138            DataStore.i.exploreV2.isSomeModalOpen.Set(false);
 1139            resetAllConfirmation.Hide();
 1140            resetAllBlackOverlay.SetActive(false);
 1141        }
 142
 143        private void ResetAllSettings()
 144        {
 0145            DataStore.i.exploreV2.isSomeModalOpen.Set(false);
 0146            settingsPanelController.ResetAllSettings();
 0147            resetAllConfirmation.Hide();
 0148            resetAllBlackOverlay.SetActive(false);
 0149        }
 150
 0151        private void CloseSettingsPanel() { hudController.SetVisibility(false); }
 152
 153        public void SetVisibility(bool visible)
 154        {
 2155            if (visible)
 156            {
 1157                settingsAnimator.Show();
 1158                mainWindow.SetActive(true);
 1159                HideResetAllConfirmation();
 1160                settingsPanelController.OpenSection(0);
 1161                settingsPanelController.MarkMenuButtonAsSelected(0);
 1162            }
 163            else
 164            {
 1165                settingsAnimator.Hide();
 1166                settingsPanelController.SaveSettings();
 1167                SetWorldPreviewActive(false);
 168            }
 169
 2170            isOpen = visible;
 2171        }
 172
 173        public void SetAsFullScreenMenuMode(Transform parentTransform)
 174        {
 1175            if (parentTransform == null)
 1176                return;
 177
 0178            transform.SetParent(parentTransform);
 0179            transform.localScale = Vector3.one;
 180
 0181            RectTransform rectTransform = transform as RectTransform;
 0182            rectTransform.anchorMin = Vector2.zero;
 0183            rectTransform.anchorMax = Vector2.one;
 0184            rectTransform.pivot = new Vector2(0.5f, 0.5f);
 0185            rectTransform.localPosition = Vector2.zero;
 0186            rectTransform.offsetMax = Vector2.zero;
 0187            rectTransform.offsetMin = Vector2.zero;
 0188        }
 189
 0190        public void SetTutorialButtonEnabled(bool isEnabled) { tutorialButton.enabled = isEnabled; }
 191
 192        public void OnAddHelpAndSupportWindow()
 193        {
 1194            helpAndSupportButton.gameObject.SetActive(true);
 1195            helpAndSupportButton.onClick.AddListener(() => OnHelpAndSupportClicked?.Invoke());
 1196        }
 197
 198        public void SetWorldPreviewActive(bool isActive)
 199        {
 3200            isActive = false;
 201
 3202            worldPreviewWindowTransform.gameObject.SetActive(isActive);
 3203            DataStore.i.camera.outputTexture.Set(isActive ? worldPreviewRawImage.texture as RenderTexture : null);
 3204            CommonScriptableObjects.isFullscreenHUDOpen.Set(DataStore.i.exploreV2.isOpen.Get() && !isActive);
 3205        }
 206
 207        private void OpenAction_OnTriggered(DCLAction_Trigger action)
 208        {
 0209            Utils.UnlockCursor();
 0210            hudController.SetVisibility(!isOpen);
 0211        }
 212
 213        private void CloseResetAllAction_OnTriggered(DCLAction_Trigger action)
 214        {
 0215            HideResetAllConfirmation();
 0216        }
 217
 4218        private void OnFinishHide(ShowHideAnimator animator) { mainWindow.SetActive(false); }
 219
 220        private void ScreenSizeChanged(Vector2Int current, Vector2Int previous)
 221        {
 2222            StartCoroutine(RefreshWorldPreviewSize());
 2223        }
 224
 225        private IEnumerator RefreshWorldPreviewSize()
 226        {
 2227            yield return null;
 228
 2229            float newHeight = Mathf.Clamp(worldPreviewWindowTransform.rect.size.x * WORLD_PREVIEW_ORIGINAL_HEIGHT / WORL
 230
 2231            worldPreviewWindowTransform.SetSizeWithCurrentAnchors(
 232                RectTransform.Axis.Vertical,
 233                newHeight);
 234
 2235            worldPreviewCanvasGroup.alpha = worldPreviewWindowTransform.rect.size.x >= WORLD_PREVIEW_MIN_WIDTH_TO_BE_SHO
 236
 2237            Utils.ForceRebuildLayoutImmediate(sectionsContainer as RectTransform);
 2238        }
 239    }
 240}