< 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:40
Uncovered lines:0
Coverable lines:40
Total lines:126
Line coverage:100% (40 of 40)
Covered branches:0
Total branches:0
Covered methods:4
Total methods:4
Method coverage:100% (4 of 4)

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        {
 939            this.settingsWidgetController = settingsWidgetController;
 940            this.controlColumns = controlColumns;
 41
 942            CommonSettingsPanelEvents.OnRefreshAllWidgetsSize += AdjustWidgetHeight;
 43
 944            this.title.text = title;
 945            CreateControls();
 946        }
 47
 2048        private void OnDestroy() { CommonSettingsPanelEvents.OnRefreshAllWidgetsSize -= AdjustWidgetHeight; }
 49
 50        private void CreateControls()
 51        {
 952            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
 5455            for (int columnIndex = 0; columnIndex < controlColumns.Count; columnIndex++)
 56            {
 11657                foreach (SettingsControlModel controlConfig in controlColumns[columnIndex].controls)
 58                {
 4059                    var newControl = Instantiate(controlConfig.controlPrefab, controlsContainerColumns[columnIndex]);
 4060                    newControl.gameObject.name = $"Control_{controlConfig.title}";
 4061                    var newWidgetController = Instantiate(controlConfig.controlController);
 4062                    settingsWidgetController.AddControl(newControl, newWidgetController, controlConfig);
 63                }
 64            }
 65
 966            AdjustWidgetHeight();
 967        }
 68
 69        [ContextMenu("AdjustWidgetHeight")]
 70        private void AdjustWidgetHeight()
 71        {
 72            // Calculate the height of the widget title
 14273            float titleHeight = ((RectTransform)title.transform).sizeDelta.y;
 74
 75            // Calculate the height of the highest column
 14276            Transform highestColumn = null;
 14277            float highestColumnHeight = 0f;
 14278            int highestColumnChildCount = 0;
 85279            foreach (var columnTransform in controlsContainerColumns)
 80            {
 28481                float columnHeight = 0f;
 28482                int columnChildCount = 0;
 205483                for (int controlIndex = 0; controlIndex < columnTransform.childCount; controlIndex++)
 84                {
 74385                    var childControl = (RectTransform)columnTransform.GetChild(controlIndex);
 74386                    if (childControl.gameObject.activeSelf)
 87                    {
 70488                        columnHeight += childControl.sizeDelta.y;
 70489                        columnChildCount++;
 90                    }
 91                }
 92
 28493                if (columnHeight > highestColumnHeight)
 94                {
 17195                    highestColumn = columnTransform;
 17196                    highestColumnHeight = columnHeight;
 17197                    highestColumnChildCount = columnChildCount;
 98                }
 99            }
 100
 101            // Calculate the total height of the widget
 102            float totalHeight;
 142103            if (highestColumn != null)
 104            {
 141105                VerticalLayoutGroup columnVerticalLayoutHroup = highestColumn.GetComponent<VerticalLayoutGroup>();
 106
 141107                totalHeight =
 108                    titleHeight +
 109                    highestColumnHeight +
 110                    columnVerticalLayoutHroup.padding.top +
 111                    columnVerticalLayoutHroup.padding.bottom +
 112                    (highestColumnChildCount * columnVerticalLayoutHroup.spacing);
 113            }
 114            else
 115            {
 1116                totalHeight = titleHeight + highestColumnHeight;
 117            }
 118
 119            // Apply the new widget height
 142120            RectTransform widgetTransform = (RectTransform)this.transform;
 142121            widgetTransform.sizeDelta = new Vector2(widgetTransform.sizeDelta.x, totalHeight);
 122
 142123            Utils.ForceRebuildLayoutImmediate((RectTransform)this.transform.parent);
 142124        }
 125    }
 126}