| | 1 | | using DCL.SettingsPanelHUD.Common; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL.SettingsCommon; |
| | 4 | | using DCL.SettingsCommon.SettingsControllers.BaseControllers; |
| | 5 | | using TMPro; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.UI; |
| | 8 | | using QualitySettings = DCL.SettingsCommon.QualitySettings; |
| | 9 | |
|
| | 10 | | namespace DCL.SettingsPanelHUD.Controls |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// Base interface to implement a view for a CONTROL. |
| | 14 | | /// </summary> |
| | 15 | | public interface ISettingsControlView |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// All the needed base logic to initializes the CONTROL view. |
| | 19 | | /// </summary> |
| | 20 | | /// <param name="controlConfig">Model that will contain the configuration of the CONTROL.</param> |
| | 21 | | /// <param name="settingsControlController">Controller associated to the CONTROL view.</param> |
| | 22 | | void Initialize(SettingsControlModel controlConfig, SettingsControlController settingsControlController); |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// This logic should update the CONTROL view with the stored value. |
| | 26 | | /// </summary> |
| | 27 | | void RefreshControl(); |
| | 28 | | } |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// MonoBehaviour that represents the base of a CONTROL view. |
| | 32 | | /// </summary> |
| | 33 | | public class SettingsControlView : MonoBehaviour, ISettingsControlView |
| | 34 | | { |
| | 35 | | [SerializeField] private TextMeshProUGUI title; |
| | 36 | | [SerializeField] private Color titleDeactivationColor; |
| | 37 | | [SerializeField] private GameObject betaIndicator; |
| | 38 | | [SerializeField] private CanvasGroup canvasGroup; |
| | 39 | | [SerializeField] private List<TextMeshProUGUI> valueLabels; |
| | 40 | | [SerializeField] private Color valueLabelDeactivationColor; |
| | 41 | | [SerializeField] private List<Image> handleImages; |
| | 42 | | [SerializeField] private Color handlerDeactivationColor; |
| | 43 | | [SerializeField] private List<CanvasGroup> controlBackgroundCanvasGroups; |
| 41 | 44 | | [SerializeField] private float controlBackgroundDeactivationAlpha = 0.5f; |
| | 45 | |
|
| | 46 | | protected SettingsControlController settingsControlController; |
| | 47 | |
|
| | 48 | | private SettingsControlModel controlConfig; |
| | 49 | | private Color originalTitleColor; |
| | 50 | | private Color originalLabelColor; |
| | 51 | | private Color originalHandlerColor; |
| | 52 | | private float originalControlBackgroundAlpha; |
| | 53 | |
|
| | 54 | | public virtual void Initialize(SettingsControlModel controlConfig, SettingsControlController settingsControlCont |
| | 55 | | { |
| 34 | 56 | | this.controlConfig = controlConfig; |
| 34 | 57 | | this.settingsControlController = settingsControlController; |
| 34 | 58 | | this.settingsControlController.Initialize(); |
| 34 | 59 | | title.text = controlConfig.title; |
| 34 | 60 | | betaIndicator.SetActive(controlConfig.isBeta); |
| 34 | 61 | | originalTitleColor = title.color; |
| 34 | 62 | | originalLabelColor = valueLabels.Count > 0 ? valueLabels[0].color : Color.white; |
| 34 | 63 | | originalHandlerColor = handleImages.Count > 0 ? handleImages[0].color : Color.white; |
| 34 | 64 | | originalControlBackgroundAlpha = controlBackgroundCanvasGroups.Count > 0 ? controlBackgroundCanvasGroups[0]. |
| | 65 | |
|
| 78 | 66 | | foreach (BooleanVariable flag in controlConfig.flagsThatDisableMe) |
| | 67 | | { |
| 5 | 68 | | flag.OnChange += OnAnyDisableFlagChange; |
| 5 | 69 | | OnAnyDisableFlagChange(flag.Get(), false); |
| | 70 | | } |
| | 71 | |
|
| 72 | 72 | | foreach (BooleanVariable flag in controlConfig.flagsThatDeactivateMe) |
| | 73 | | { |
| 2 | 74 | | flag.OnChange += OnAnyDeactivationFlagChange; |
| 2 | 75 | | OnAnyDeactivationFlagChange(flag.Get(), false); |
| | 76 | | } |
| | 77 | |
|
| 34 | 78 | | RefreshControl(); |
| | 79 | |
|
| 34 | 80 | | Settings.i.generalSettings.OnChanged += OnGeneralSettingsChanged; |
| 34 | 81 | | Settings.i.qualitySettings.OnChanged += OnQualitySettingsChanged; |
| 34 | 82 | | Settings.i.OnResetAllSettings += OnResetSettingsControl; |
| 34 | 83 | | } |
| | 84 | |
|
| | 85 | | protected virtual void OnDestroy() |
| | 86 | | { |
| 37 | 87 | | if (controlConfig != null) |
| | 88 | | { |
| 78 | 89 | | foreach (BooleanVariable flag in controlConfig.flagsThatDisableMe) |
| | 90 | | { |
| 5 | 91 | | flag.OnChange -= OnAnyDisableFlagChange; |
| | 92 | | } |
| | 93 | |
|
| 72 | 94 | | foreach (BooleanVariable flag in controlConfig.flagsThatDeactivateMe) |
| | 95 | | { |
| 2 | 96 | | flag.OnChange -= OnAnyDeactivationFlagChange; |
| | 97 | | } |
| | 98 | | } |
| | 99 | |
|
| 37 | 100 | | Settings.i.generalSettings.OnChanged -= OnGeneralSettingsChanged; |
| 37 | 101 | | Settings.i.qualitySettings.OnChanged -= OnQualitySettingsChanged; |
| 37 | 102 | | Settings.i.OnResetAllSettings -= OnResetSettingsControl; |
| 37 | 103 | | } |
| | 104 | |
|
| 0 | 105 | | public virtual void RefreshControl() { } |
| | 106 | |
|
| | 107 | | /// <summary> |
| | 108 | | /// It will be triggered when the CONTROL state changes and will execute the main flow of the CONTROL controller |
| | 109 | | /// </summary> |
| | 110 | | /// <param name="newValue">Value of the new state. It can be a bool (for toggle controls), a float (for slider c |
| | 111 | | protected void ApplySetting(object newValue) |
| | 112 | | { |
| 0 | 113 | | settingsControlController.UpdateSetting(newValue); |
| 0 | 114 | | settingsControlController.ApplySettings(); |
| 0 | 115 | | } |
| | 116 | |
|
| | 117 | | private void OnAnyDisableFlagChange(bool current, bool previous) |
| | 118 | | { |
| 5 | 119 | | bool canApplychange = true; |
| 5 | 120 | | if (!current) |
| | 121 | | { |
| | 122 | | // Check if all the disable flags are false before enable the control |
| 16 | 123 | | foreach (var flag in controlConfig.flagsThatDisableMe) |
| | 124 | | { |
| 4 | 125 | | if (flag.Get() == true) |
| | 126 | | { |
| 0 | 127 | | canApplychange = false; |
| 0 | 128 | | break; |
| | 129 | | } |
| | 130 | | } |
| | 131 | | } |
| | 132 | |
|
| 5 | 133 | | if (canApplychange) |
| 5 | 134 | | SetEnabled(!current); |
| 5 | 135 | | } |
| | 136 | |
|
| | 137 | | private void OnAnyDeactivationFlagChange(bool current, bool previous) |
| | 138 | | { |
| 2 | 139 | | bool canApplychange = true; |
| 2 | 140 | | if (!current) |
| | 141 | | { |
| | 142 | | // Check if all the deactivation flags are false before enable the control |
| 0 | 143 | | foreach (var flag in controlConfig.flagsThatDeactivateMe) |
| | 144 | | { |
| 0 | 145 | | if (flag.Get() == true) |
| | 146 | | { |
| 0 | 147 | | canApplychange = false; |
| 0 | 148 | | break; |
| | 149 | | } |
| | 150 | | } |
| | 151 | | } |
| | 152 | |
|
| 2 | 153 | | if (canApplychange) |
| 2 | 154 | | SetControlActive(!current); |
| 2 | 155 | | } |
| | 156 | |
|
| 0 | 157 | | private void OnGeneralSettingsChanged(GeneralSettings obj) { RefreshControl(); } |
| | 158 | |
|
| 0 | 159 | | private void OnQualitySettingsChanged(QualitySettings obj) { RefreshControl(); } |
| | 160 | |
|
| 0 | 161 | | private void OnResetSettingsControl() { RefreshControl(); } |
| | 162 | |
|
| | 163 | | private void SetEnabled(bool enabled) |
| | 164 | | { |
| 5 | 165 | | title.color = enabled ? originalTitleColor : titleDeactivationColor; |
| 20 | 166 | | foreach (var text in valueLabels) |
| | 167 | | { |
| 5 | 168 | | text.color = enabled ? originalLabelColor : valueLabelDeactivationColor; |
| | 169 | | } |
| 20 | 170 | | foreach (var image in handleImages) |
| | 171 | | { |
| 5 | 172 | | image.color = enabled ? originalHandlerColor : handlerDeactivationColor; |
| | 173 | | } |
| 20 | 174 | | foreach (var canvasGroup in controlBackgroundCanvasGroups) |
| | 175 | | { |
| 5 | 176 | | canvasGroup.alpha = enabled ? originalControlBackgroundAlpha : controlBackgroundDeactivationAlpha; |
| | 177 | | } |
| 5 | 178 | | canvasGroup.interactable = enabled; |
| 5 | 179 | | canvasGroup.blocksRaycasts = enabled; |
| 5 | 180 | | } |
| | 181 | |
|
| | 182 | | private void SetControlActive(bool actived) |
| | 183 | | { |
| 2 | 184 | | gameObject.SetActive(actived); |
| 2 | 185 | | CommonSettingsPanelEvents.RaiseRefreshAllWidgetsSize(); |
| 2 | 186 | | } |
| | 187 | | } |
| | 188 | | } |