< 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        {
 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            {
 10657                foreach (SettingsControlModel controlConfig in controlColumns[columnIndex].controls)
 58                {
 3759                    var newControl = Instantiate(controlConfig.controlPrefab, controlsContainerColumns[columnIndex]);
 3760                    newControl.gameObject.name = $"Control_{controlConfig.title}";
 3761                    var newWidgetController = Instantiate(controlConfig.controlController);
 3762                    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
 1773            float titleHeight = ((RectTransform)title.transform).sizeDelta.y;
 74
 75            // Calculate the height of the highest column
 1776            Transform highestColumn = null;
 1777            float highestColumnHeight = 0f;
 1778            int highestColumnChildCount = 0;
 10279            foreach (var columnTransform in controlsContainerColumns)
 80            {
 3481                float columnHeight = 0f;
 3482                int columnChildCount = 0;
 26883                for (int controlIndex = 0; controlIndex < columnTransform.childCount; controlIndex++)
 84                {
 10085                    var childControl = (RectTransform)columnTransform.GetChild(controlIndex);
 10086                    if (childControl.gameObject.activeSelf)
 87                    {
 9588                        columnHeight += childControl.sizeDelta.y;
 9589                        columnChildCount++;
 90                    }
 91                }
 92
 3493                if (columnHeight > highestColumnHeight)
 94                {
 1595                    highestColumn = columnTransform;
 1596                    highestColumnHeight = columnHeight;
 1597                    highestColumnChildCount = columnChildCount;
 98                }
 99            }
 100
 101            // Calculate the total height of the widget
 102            float totalHeight;
 17103            if (highestColumn != null)
 104            {
 15105                VerticalLayoutGroup columnVerticalLayoutHroup = highestColumn.GetComponent<VerticalLayoutGroup>();
 106
 15107                totalHeight =
 108                    titleHeight +
 109                    highestColumnHeight +
 110                    columnVerticalLayoutHroup.padding.top +
 111                    columnVerticalLayoutHroup.padding.bottom +
 112                    (highestColumnChildCount * columnVerticalLayoutHroup.spacing);
 15113            }
 114            else
 115            {
 2116                totalHeight = titleHeight + highestColumnHeight;
 117            }
 118
 119            // Apply the new widget height
 17120            RectTransform widgetTransform = (RectTransform)this.transform;
 17121            widgetTransform.sizeDelta = new Vector2(widgetTransform.sizeDelta.x, totalHeight);
 122
 17123            Utils.ForceRebuildLayoutImmediate((RectTransform)this.transform.parent);
 17124        }
 125    }
 126}