< Summary

Class:DCL.SettingsPanelHUD.Widgets.SettingsWidgetView
Assembly:SettingsPanelHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/SettingsPanelHUD/Scripts/WidgetsModule/SettingsWidgetView.cs
Covered lines:41
Uncovered lines:0
Coverable lines:41
Total lines:126
Line coverage:100% (41 of 41)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Initialize(...)0%110100%
OnDestroy()0%110100%
CreateControls()0%550100%
AdjustWidgetHeight()0%660100%

File(s)

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

#LineLine coverage
 1using DCL.Helpers;
 2using DCL.SettingsPanelHUD.Common;
 3using DCL.SettingsPanelHUD.Controls;
 4using System.Collections.Generic;
 5using TMPro;
 6using UnityEngine;
 7using UnityEngine.Assertions;
 8using UnityEngine.UI;
 9
 10namespace DCL.SettingsPanelHUD.Widgets
 11{
 12    /// <summary>
 13    /// Interface to implement a view for a WIDGET.
 14    /// </summary>
 15    public interface ISettingsWidgetView
 16    {
 17        /// <summary>
 18        /// All the needed logic to initializes the WIDGET view and put its CONTROLS factory into operation.
 19        /// </summary>
 20        /// <param name="title">Title of the WIDGET.</param>
 21        /// <param name="settingsWidgetController">Controller that will be associated to this view.</param>
 22        /// <param name="controlColumns">List of CONTROLS (grouped in columns) associated to this WIDGET.</param>
 23        void Initialize(string title, ISettingsWidgetController settingsWidgetController, List<SettingsControlGroup> con
 24    }
 25
 26    /// <summary>
 27    /// MonoBehaviour that represents a WIDGET view and will act as a factory of CONTROLS.
 28    /// </summary>
 29    public class SettingsWidgetView : MonoBehaviour, ISettingsWidgetView
 30    {
 31        [SerializeField] private TextMeshProUGUI title;
 32        [SerializeField] private List<Transform> controlsContainerColumns;
 33
 34        private ISettingsWidgetController settingsWidgetController;
 35        private List<SettingsControlGroup> controlColumns;
 36
 37        public void Initialize(string title, ISettingsWidgetController settingsWidgetController, List<SettingsControlGro
 38        {
 739            this.settingsWidgetController = settingsWidgetController;
 740            this.controlColumns = controlColumns;
 41
 742            CommonSettingsPanelEvents.OnRefreshAllWidgetsSize += AdjustWidgetHeight;
 43
 744            this.title.text = title;
 745            CreateControls();
 746        }
 47
 1648        private void OnDestroy() { CommonSettingsPanelEvents.OnRefreshAllWidgetsSize -= AdjustWidgetHeight; }
 49
 50        private void CreateControls()
 51        {
 752            Assert.IsTrue(controlColumns.Count == 0 || controlColumns.Count == controlsContainerColumns.Count,
 53                $"Settings Configuration exception: The number of columns set in the '{this.name}' view does not match w
 54
 4255            for (int columnIndex = 0; columnIndex < controlColumns.Count; columnIndex++)
 56            {
 9057                foreach (SettingsControlModel controlConfig in controlColumns[columnIndex].controls)
 58                {
 3159                    var newControl = Instantiate(controlConfig.controlPrefab, controlsContainerColumns[columnIndex]);
 3160                    newControl.gameObject.name = $"Control_{controlConfig.title}";
 3161                    var newWidgetController = Instantiate(controlConfig.controlController);
 3162                    settingsWidgetController.AddControl(newControl, newWidgetController, controlConfig);
 63                }
 64            }
 65
 766            AdjustWidgetHeight();
 767        }
 68
 69        [ContextMenu("AdjustWidgetHeight")]
 70        private void AdjustWidgetHeight()
 71        {
 72            // Calculate the height of the widget title
 1673            float titleHeight = ((RectTransform)title.transform).sizeDelta.y;
 74
 75            // Calculate the height of the highest column
 1676            Transform highestColumn = null;
 1677            float highestColumnHeight = 0f;
 1678            int highestColumnChildCount = 0;
 9679            foreach (var columnTransform in controlsContainerColumns)
 80            {
 3281                float columnHeight = 0f;
 3282                int columnChildCount = 0;
 25283                for (int controlIndex = 0; controlIndex < columnTransform.childCount; controlIndex++)
 84                {
 9485                    var childControl = (RectTransform)columnTransform.GetChild(controlIndex);
 9486                    if (childControl.gameObject.activeSelf)
 87                    {
 8588                        columnHeight += childControl.sizeDelta.y;
 8589                        columnChildCount++;
 90                    }
 91                }
 92
 3293                if (columnHeight > highestColumnHeight)
 94                {
 1495                    highestColumn = columnTransform;
 1496                    highestColumnHeight = columnHeight;
 1497                    highestColumnChildCount = columnChildCount;
 98                }
 99            }
 100
 101            // Calculate the total height of the widget
 102            float totalHeight;
 16103            if (highestColumn != null)
 104            {
 14105                VerticalLayoutGroup columnVerticalLayoutHroup = highestColumn.GetComponent<VerticalLayoutGroup>();
 106
 14107                totalHeight =
 108                    titleHeight +
 109                    highestColumnHeight +
 110                    columnVerticalLayoutHroup.padding.top +
 111                    columnVerticalLayoutHroup.padding.bottom +
 112                    (highestColumnChildCount * columnVerticalLayoutHroup.spacing);
 14113            }
 114            else
 115            {
 2116                totalHeight = titleHeight + highestColumnHeight;
 117            }
 118
 119            // Apply the new widget height
 16120            RectTransform widgetTransform = (RectTransform)this.transform;
 16121            widgetTransform.sizeDelta = new Vector2(widgetTransform.sizeDelta.x, totalHeight);
 122
 16123            Utils.ForceRebuildLayoutImmediate((RectTransform)this.transform.parent);
 16124        }
 125    }
 126}