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