| | 1 | | using DCL.Helpers; |
| | 2 | | using DCL.SettingsPanelHUD.Common; |
| | 3 | | using DCL.SettingsPanelHUD.Controls; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using TMPro; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.Assertions; |
| | 8 | | using UnityEngine.UI; |
| | 9 | |
|
| | 10 | | namespace 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 | | { |
| 8 | 39 | | this.settingsWidgetController = settingsWidgetController; |
| 8 | 40 | | this.controlColumns = controlColumns; |
| | 41 | |
|
| 8 | 42 | | CommonSettingsPanelEvents.OnRefreshAllWidgetsSize += AdjustWidgetHeight; |
| | 43 | |
|
| 8 | 44 | | this.title.text = title; |
| 8 | 45 | | CreateControls(); |
| 8 | 46 | | } |
| | 47 | |
|
| 18 | 48 | | private void OnDestroy() { CommonSettingsPanelEvents.OnRefreshAllWidgetsSize -= AdjustWidgetHeight; } |
| | 49 | |
|
| | 50 | | private void CreateControls() |
| | 51 | | { |
| 8 | 52 | | 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 | |
|
| 48 | 55 | | for (int columnIndex = 0; columnIndex < controlColumns.Count; columnIndex++) |
| | 56 | | { |
| 106 | 57 | | foreach (SettingsControlModel controlConfig in controlColumns[columnIndex].controls) |
| | 58 | | { |
| 37 | 59 | | var newControl = Instantiate(controlConfig.controlPrefab, controlsContainerColumns[columnIndex]); |
| 37 | 60 | | newControl.gameObject.name = $"Control_{controlConfig.title}"; |
| 37 | 61 | | var newWidgetController = Instantiate(controlConfig.controlController); |
| 37 | 62 | | settingsWidgetController.AddControl(newControl, newWidgetController, controlConfig); |
| | 63 | | } |
| | 64 | | } |
| | 65 | |
|
| 8 | 66 | | AdjustWidgetHeight(); |
| 8 | 67 | | } |
| | 68 | |
|
| | 69 | | [ContextMenu("AdjustWidgetHeight")] |
| | 70 | | private void AdjustWidgetHeight() |
| | 71 | | { |
| | 72 | | // Calculate the height of the widget title |
| 17 | 73 | | float titleHeight = ((RectTransform)title.transform).sizeDelta.y; |
| | 74 | |
|
| | 75 | | // Calculate the height of the highest column |
| 17 | 76 | | Transform highestColumn = null; |
| 17 | 77 | | float highestColumnHeight = 0f; |
| 17 | 78 | | int highestColumnChildCount = 0; |
| 102 | 79 | | foreach (var columnTransform in controlsContainerColumns) |
| | 80 | | { |
| 34 | 81 | | float columnHeight = 0f; |
| 34 | 82 | | int columnChildCount = 0; |
| 268 | 83 | | for (int controlIndex = 0; controlIndex < columnTransform.childCount; controlIndex++) |
| | 84 | | { |
| 100 | 85 | | var childControl = (RectTransform)columnTransform.GetChild(controlIndex); |
| 100 | 86 | | if (childControl.gameObject.activeSelf) |
| | 87 | | { |
| 95 | 88 | | columnHeight += childControl.sizeDelta.y; |
| 95 | 89 | | columnChildCount++; |
| | 90 | | } |
| | 91 | | } |
| | 92 | |
|
| 34 | 93 | | if (columnHeight > highestColumnHeight) |
| | 94 | | { |
| 15 | 95 | | highestColumn = columnTransform; |
| 15 | 96 | | highestColumnHeight = columnHeight; |
| 15 | 97 | | highestColumnChildCount = columnChildCount; |
| | 98 | | } |
| | 99 | | } |
| | 100 | |
|
| | 101 | | // Calculate the total height of the widget |
| | 102 | | float totalHeight; |
| 17 | 103 | | if (highestColumn != null) |
| | 104 | | { |
| 15 | 105 | | VerticalLayoutGroup columnVerticalLayoutHroup = highestColumn.GetComponent<VerticalLayoutGroup>(); |
| | 106 | |
|
| 15 | 107 | | totalHeight = |
| | 108 | | titleHeight + |
| | 109 | | highestColumnHeight + |
| | 110 | | columnVerticalLayoutHroup.padding.top + |
| | 111 | | columnVerticalLayoutHroup.padding.bottom + |
| | 112 | | (highestColumnChildCount * columnVerticalLayoutHroup.spacing); |
| 15 | 113 | | } |
| | 114 | | else |
| | 115 | | { |
| 2 | 116 | | totalHeight = titleHeight + highestColumnHeight; |
| | 117 | | } |
| | 118 | |
|
| | 119 | | // Apply the new widget height |
| 17 | 120 | | RectTransform widgetTransform = (RectTransform)this.transform; |
| 17 | 121 | | widgetTransform.sizeDelta = new Vector2(widgetTransform.sizeDelta.x, totalHeight); |
| | 122 | |
|
| 17 | 123 | | Utils.ForceRebuildLayoutImmediate((RectTransform)this.transform.parent); |
| 17 | 124 | | } |
| | 125 | | } |
| | 126 | | } |