< 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

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        {
 839            this.settingsWidgetController = settingsWidgetController;
 840            this.controlColumns = controlColumns;
 41
 842            CommonSettingsPanelEvents.OnRefreshAllWidgetsSize += AdjustWidgetHeight;
 43
 844            this.title.text = title;
 845            CreateControls();
 846        }
 47
 1848        private void OnDestroy() { CommonSettingsPanelEvents.OnRefreshAllWidgetsSize -= AdjustWidgetHeight; }
 49
 50        private void CreateControls()
 51        {
 852            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
 4855            for (int columnIndex = 0; columnIndex < controlColumns.Count; columnIndex++)
 56            {
 10857                foreach (SettingsControlModel controlConfig in controlColumns[columnIndex].controls)
 58                {
 3859                    var newControl = Instantiate(controlConfig.controlPrefab, controlsContainerColumns[columnIndex]);
 3860                    newControl.gameObject.name = $"Control_{controlConfig.title}";
 3861                    var newWidgetController = Instantiate(controlConfig.controlController);
 3862                    settingsWidgetController.AddControl(newControl, newWidgetController, controlConfig);
 63                }
 64            }
 65
 866            AdjustWidgetHeight();
 867        }
 68
 69        [ContextMenu("AdjustWidgetHeight")]
 70        private void AdjustWidgetHeight()
 71        {
 72            // Calculate the height of the widget title
 10773            float titleHeight = ((RectTransform)title.transform).sizeDelta.y;
 74
 75            // Calculate the height of the highest column
 10776            Transform highestColumn = null;
 10777            float highestColumnHeight = 0f;
 10778            int highestColumnChildCount = 0;
 64279            foreach (var columnTransform in controlsContainerColumns)
 80            {
 21481                float columnHeight = 0f;
 21482                int columnChildCount = 0;
 176483                for (int controlIndex = 0; controlIndex < columnTransform.childCount; controlIndex++)
 84                {
 66885                    var childControl = (RectTransform)columnTransform.GetChild(controlIndex);
 66886                    if (childControl.gameObject.activeSelf)
 87                    {
 65788                        columnHeight += childControl.sizeDelta.y;
 65789                        columnChildCount++;
 90                    }
 91                }
 92
 21493                if (columnHeight > highestColumnHeight)
 94                {
 13395                    highestColumn = columnTransform;
 13396                    highestColumnHeight = columnHeight;
 13397                    highestColumnChildCount = columnChildCount;
 98                }
 99            }
 100
 101            // Calculate the total height of the widget
 102            float totalHeight;
 107103            if (highestColumn != null)
 104            {
 106105                VerticalLayoutGroup columnVerticalLayoutHroup = highestColumn.GetComponent<VerticalLayoutGroup>();
 106
 106107                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
 107120            RectTransform widgetTransform = (RectTransform)this.transform;
 107121            widgetTransform.sizeDelta = new Vector2(widgetTransform.sizeDelta.x, totalHeight);
 122
 107123            Utils.ForceRebuildLayoutImmediate((RectTransform)this.transform.parent);
 107124        }
 125    }
 126}