| | 1 | | using DCL.SettingsCommon; |
| | 2 | | using DCL.SettingsCommon.SettingsControllers.BaseControllers; |
| | 3 | | using DCL.SettingsPanelHUD.Common; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Linq; |
| | 6 | | using TMPro; |
| | 7 | | using UIComponents.Scripts.Components; |
| | 8 | | using UIComponents.Scripts.Components.Tooltip; |
| | 9 | | using UnityEngine; |
| | 10 | | using UnityEngine.UI; |
| | 11 | | using QualitySettings = DCL.SettingsCommon.QualitySettings; |
| | 12 | |
|
| | 13 | | namespace DCL.SettingsPanelHUD.Controls |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// MonoBehaviour that represents the base of a CONTROL view. |
| | 17 | | /// </summary> |
| | 18 | | public class SettingsControlView : MonoBehaviour, ISettingsControlView |
| | 19 | | { |
| | 20 | | [SerializeField] private CanvasGroup canvasGroup; |
| | 21 | | [SerializeField] private CanvasGroup controlCanvasGroup; |
| | 22 | |
|
| | 23 | | [Space] |
| | 24 | | [SerializeField] private TextMeshProUGUI title; |
| | 25 | | [SerializeField] private Color titleDeactivationColor; |
| | 26 | |
|
| | 27 | | [Space] |
| | 28 | | [SerializeField] private GameObject betaIndicator; |
| | 29 | | [SerializeField] private ButtonComponentView infoButton; |
| | 30 | | [SerializeField] private TooltipComponentView tooltip; |
| | 31 | |
|
| | 32 | | [Space] |
| | 33 | | [SerializeField] private List<TextMeshProUGUI> valueLabels; |
| | 34 | | [SerializeField] private Color valueLabelDeactivationColor; |
| | 35 | |
|
| | 36 | | [Space] |
| | 37 | | [SerializeField] private List<Image> handleImages; |
| | 38 | | [SerializeField] private Color handlerDeactivationColor; |
| | 39 | |
|
| | 40 | | [Space] |
| | 41 | | [SerializeField] private List<CanvasGroup> controlBackgroundCanvasGroups; |
| 50 | 42 | | [SerializeField] private float controlBackgroundDeactivationAlpha = 0.5f; |
| | 43 | |
|
| | 44 | | private 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 model, SettingsControlController controller) |
| | 53 | | { |
| 37 | 54 | | controlConfig = model; |
| 37 | 55 | | settingsControlController = controller; |
| 37 | 56 | | settingsControlController.Initialize(); |
| | 57 | |
|
| 37 | 58 | | betaIndicator.SetActive(model.isBeta); |
| | 59 | |
|
| 37 | 60 | | string tooltipMessage = !string.IsNullOrEmpty(model.infoTooltipMessage) ? model.infoTooltipMessage : "This s |
| 37 | 61 | | tooltip.SetModel(new TooltipComponentModel(tooltipMessage)); |
| | 62 | |
|
| 37 | 63 | | infoButton.onClick.AddListener(OnInfoButtonClicked); |
| 37 | 64 | | infoButton.gameObject.SetActive(model.infoButtonEnabled); |
| | 65 | |
|
| 37 | 66 | | title.text = model.title; |
| 37 | 67 | | originalTitleColor = title.color; |
| 37 | 68 | | originalLabelColor = valueLabels.Count > 0 ? valueLabels[0].color : Color.white; |
| 37 | 69 | | originalHandlerColor = handleImages.Count > 0 ? handleImages[0].color : Color.white; |
| 37 | 70 | | originalControlBackgroundAlpha = controlBackgroundCanvasGroups.Count > 0 ? controlBackgroundCanvasGroups[0]. |
| | 71 | |
|
| 43 | 72 | | SwitchInteractibility(isInteractable: model.flagsThatDisableMe.All(flag => flag.Get() == false)); |
| 41 | 73 | | SwitchVisibility(isVisible: model.flagsThatDeactivateMe.All(flag => flag.Get() == false)); |
| | 74 | |
|
| 37 | 75 | | if(!model.infoButtonEnabled) |
| 38 | 76 | | SetOverriden(@override: model.flagsThatOverrideMe.Any(flag => flag.Get())); |
| | 77 | |
|
| 37 | 78 | | RefreshControl(); |
| | 79 | |
|
| 86 | 80 | | foreach (BooleanVariable flag in model.flagsThatDisableMe) |
| 6 | 81 | | flag.OnChange += OnAnyDisableFlagChange; |
| | 82 | |
|
| 82 | 83 | | foreach (BooleanVariable flag in model.flagsThatDeactivateMe) |
| 4 | 84 | | flag.OnChange += OnAnyDeactivationFlagChange; |
| | 85 | |
|
| 78 | 86 | | foreach (BooleanVariable flag in model.flagsThatOverrideMe) |
| 2 | 87 | | flag.OnChange += OnAnyOverrideFlagChange; |
| | 88 | |
|
| 37 | 89 | | Settings.i.generalSettings.OnChanged += OnGeneralSettingsChanged; |
| 37 | 90 | | Settings.i.qualitySettings.OnChanged += OnQualitySettingsChanged; |
| 37 | 91 | | Settings.i.OnResetAllSettings += RefreshControl; |
| 37 | 92 | | } |
| | 93 | |
|
| | 94 | | protected virtual void OnDestroy() |
| | 95 | | { |
| 43 | 96 | | if (controlConfig != null) |
| | 97 | | { |
| 86 | 98 | | foreach (BooleanVariable flag in controlConfig.flagsThatDisableMe) |
| 6 | 99 | | flag.OnChange -= OnAnyDisableFlagChange; |
| | 100 | |
|
| 82 | 101 | | foreach (BooleanVariable flag in controlConfig.flagsThatDeactivateMe) |
| 4 | 102 | | flag.OnChange -= OnAnyDeactivationFlagChange; |
| | 103 | |
|
| 78 | 104 | | foreach (BooleanVariable flag in controlConfig.flagsThatOverrideMe) |
| 2 | 105 | | flag.OnChange -= OnAnyOverrideFlagChange; |
| | 106 | | } |
| | 107 | |
|
| 43 | 108 | | infoButton.onClick.RemoveListener(OnInfoButtonClicked); |
| | 109 | |
|
| 43 | 110 | | Settings.i.generalSettings.OnChanged -= OnGeneralSettingsChanged; |
| 43 | 111 | | Settings.i.qualitySettings.OnChanged -= OnQualitySettingsChanged; |
| 43 | 112 | | Settings.i.OnResetAllSettings -= RefreshControl; |
| 43 | 113 | | } |
| | 114 | |
|
| 37 | 115 | | public virtual void RefreshControl() { } |
| | 116 | |
|
| | 117 | | /// <summary> |
| | 118 | | /// It will be triggered when the CONTROL state changes and will execute the main flow of the CONTROL controller |
| | 119 | | /// </summary> |
| | 120 | | /// <param name="newValue">Value of the new state. It can be a bool (for toggle controls), a float (for slider c |
| | 121 | | protected void ApplySetting(object newValue) |
| | 122 | | { |
| 0 | 123 | | settingsControlController.UpdateSetting(newValue); |
| 0 | 124 | | settingsControlController.ApplySettings(); |
| 0 | 125 | | } |
| | 126 | |
|
| | 127 | | private void OnInfoButtonClicked() |
| | 128 | | { |
| 0 | 129 | | if (!tooltip.gameObject.activeSelf) |
| 0 | 130 | | tooltip.Show(); |
| 0 | 131 | | } |
| | 132 | |
|
| | 133 | | private void OnGeneralSettingsChanged(GeneralSettings _) => |
| 0 | 134 | | RefreshControl(); |
| | 135 | |
|
| | 136 | | private void OnQualitySettingsChanged(QualitySettings _) => |
| 0 | 137 | | RefreshControl(); |
| | 138 | |
|
| | 139 | | private void OnAnyDeactivationFlagChange(bool deactivateFlag, bool _ = false) => |
| 0 | 140 | | SwitchVisibility(isVisible: AllFlagsAreDisabled(deactivateFlag, controlConfig.flagsThatDeactivateMe)); |
| | 141 | |
|
| | 142 | | private void OnAnyDisableFlagChange(bool disableFlag, bool _ = false) => |
| 0 | 143 | | SwitchInteractibility(isInteractable: AllFlagsAreDisabled(disableFlag, controlConfig.flagsThatDisableMe)); |
| | 144 | |
|
| | 145 | | private void OnAnyOverrideFlagChange(bool overrideFlag, bool _ = false) => |
| 0 | 146 | | SetOverriden(@override: AnyFlagIsEnabled(overrideFlag, controlConfig.flagsThatOverrideMe)); |
| | 147 | |
|
| | 148 | | private static bool AllFlagsAreDisabled(bool flagEnabled, List<BooleanVariable> flags) => |
| 0 | 149 | | !flagEnabled && flags.All(flag => flag.Get() == false); |
| | 150 | |
|
| | 151 | | private static bool AnyFlagIsEnabled(bool flagEnabled, List<BooleanVariable> flags) => |
| 0 | 152 | | flagEnabled || flags.Any(flag => flag.Get()); |
| | 153 | |
|
| | 154 | | private void SwitchVisibility(bool isVisible) |
| | 155 | | { |
| 37 | 156 | | gameObject.SetActive(isVisible); |
| 37 | 157 | | CommonSettingsPanelEvents.RaiseRefreshAllWidgetsSize(); |
| 37 | 158 | | } |
| | 159 | |
|
| | 160 | | private void SwitchInteractibility(bool isInteractable) |
| | 161 | | { |
| 37 | 162 | | title.color = isInteractable ? originalTitleColor : titleDeactivationColor; |
| 37 | 163 | | SwitchUIControlInteractibility(isInteractable, canvasGroup); |
| 37 | 164 | | } |
| | 165 | |
|
| | 166 | | private void SetOverriden(bool @override) |
| | 167 | | { |
| 36 | 168 | | infoButton.gameObject.SetActive(@override); |
| 36 | 169 | | SwitchUIControlInteractibility(isInteractable: !@override, controlCanvasGroup); |
| 36 | 170 | | } |
| | 171 | |
|
| | 172 | | private void SwitchUIControlInteractibility(bool isInteractable, CanvasGroup group) |
| | 173 | | { |
| 300 | 174 | | foreach (TextMeshProUGUI text in valueLabels) |
| 77 | 175 | | text.color = isInteractable ? originalLabelColor : valueLabelDeactivationColor; |
| | 176 | |
|
| 308 | 177 | | foreach (Image image in handleImages) |
| 81 | 178 | | image.color = isInteractable ? originalHandlerColor : handlerDeactivationColor; |
| | 179 | |
|
| 308 | 180 | | foreach (CanvasGroup bkgCanvasGroup in controlBackgroundCanvasGroups) |
| 81 | 181 | | bkgCanvasGroup.alpha = isInteractable ? originalControlBackgroundAlpha : controlBackgroundDeactivationAl |
| | 182 | |
|
| 73 | 183 | | group.interactable = isInteractable; |
| 73 | 184 | | group.blocksRaycasts = isInteractable; |
| 73 | 185 | | } |
| | 186 | | } |
| | 187 | | } |