| | 1 | | using DCL.Helpers; |
| | 2 | | using DCL.SettingsPanelHUD.Sections; |
| | 3 | | using System.Collections; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.UI; |
| | 6 | |
|
| | 7 | | namespace DCL.SettingsPanelHUD |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// MonoBehaviour that represents the main settings panel view and will act as a factory of SECTIONS. |
| | 11 | | /// </summary> |
| | 12 | | public class SettingsPanelHUDView : MonoBehaviour |
| | 13 | | { |
| | 14 | | [Header("General configuration")] |
| | 15 | | [SerializeField] private GameObject mainWindow; |
| | 16 | |
|
| | 17 | | [Header("Sections configuration")] |
| | 18 | | [SerializeField] private SettingsPanelModel settingsPanelConfig; |
| | 19 | | [SerializeField] private Transform menuButtonsContainer; |
| | 20 | | [SerializeField] private Transform sectionsContainer; |
| | 21 | |
|
| | 22 | | [Header("Reset All configuration")] |
| | 23 | | [SerializeField] private Button resetAllButton; |
| | 24 | | [SerializeField] private ShowHideAnimator resetAllConfirmation; |
| | 25 | | [SerializeField] private Button resetAllOkButton; |
| | 26 | | [SerializeField] private Button resetAllCancelButton; |
| | 27 | | [SerializeField] private GameObject resetAllBlackOverlay; |
| | 28 | | [SerializeField] internal InputAction_Trigger closeResetAllAction; |
| | 29 | |
|
| | 30 | | [Header("Open/Close Settings")] |
| | 31 | | [SerializeField] private Button closeButton; |
| | 32 | | [SerializeField] private Button backgroundButton; |
| | 33 | | [SerializeField] private InputAction_Trigger openAction; |
| | 34 | |
|
| | 35 | | [Header("World Preview Window")] |
| | 36 | | [SerializeField] private RectTransform worldPreviewWindowTransform; |
| | 37 | | [SerializeField] private CanvasGroup worldPreviewCanvasGroup; |
| | 38 | | [SerializeField] private RawImage worldPreviewRawImage; |
| | 39 | |
|
| | 40 | | [Header("Others")] |
| | 41 | | [SerializeField] private Button tutorialButton; |
| | 42 | | [SerializeField] private Button reportBugButton; |
| | 43 | | [SerializeField] private Button helpAndSupportButton; |
| | 44 | |
|
| | 45 | | [Header("Animations")] |
| | 46 | | [SerializeField] private ShowHideAnimator settingsAnimator; |
| | 47 | |
|
| 0 | 48 | | public bool isOpen { get; private set; } |
| | 49 | |
|
| | 50 | | private const string PATH = "SettingsPanelHUD"; |
| | 51 | | private const float WORLD_PREVIEW_MIN_WIDTH_TO_BE_SHOWED = 200f; |
| | 52 | | private const float WORLD_PREVIEW_ORIGINAL_WIDTH = 400f; |
| | 53 | | private const float WORLD_PREVIEW_ORIGINAL_HEIGHT = 250f; |
| | 54 | |
|
| | 55 | | private IHUD hudController; |
| | 56 | | private ISettingsPanelHUDController settingsPanelController; |
| | 57 | |
|
| | 58 | | public event System.Action OnRestartTutorial; |
| | 59 | | public event System.Action OnHelpAndSupportClicked; |
| | 60 | |
|
| | 61 | | public static SettingsPanelHUDView Create() |
| | 62 | | { |
| 2 | 63 | | SettingsPanelHUDView view = Instantiate(Resources.Load<GameObject>(PATH)).GetComponent<SettingsPanelHUDView> |
| 2 | 64 | | view.name = "_SettingsPanelHUD"; |
| 2 | 65 | | return view; |
| | 66 | | } |
| | 67 | |
|
| | 68 | | public void Initialize(IHUD hudController, ISettingsPanelHUDController settingsPanelController) |
| | 69 | | { |
| 2 | 70 | | this.hudController = hudController; |
| 2 | 71 | | this.settingsPanelController = settingsPanelController; |
| | 72 | |
|
| 2 | 73 | | openAction.OnTriggered += OpenAction_OnTriggered; |
| 2 | 74 | | closeResetAllAction.OnTriggered += CloseResetAllAction_OnTriggered; |
| | 75 | |
|
| 2 | 76 | | resetAllButton.onClick.AddListener(ShowResetAllConfirmation); |
| 2 | 77 | | resetAllCancelButton.onClick.AddListener(HideResetAllConfirmation); |
| 2 | 78 | | resetAllOkButton.onClick.AddListener(ResetAllSettings); |
| | 79 | |
|
| 2 | 80 | | closeButton.onClick.AddListener(CloseSettingsPanel); |
| 2 | 81 | | backgroundButton.onClick.AddListener(CloseSettingsPanel); |
| 2 | 82 | | settingsAnimator.OnWillFinishHide += OnFinishHide; |
| | 83 | |
|
| 2 | 84 | | CreateSections(); |
| 2 | 85 | | isOpen = !settingsAnimator.hideOnEnable; |
| 2 | 86 | | settingsAnimator.Hide(true); |
| | 87 | |
|
| 2 | 88 | | tutorialButton.onClick.AddListener(() => OnRestartTutorial?.Invoke()); |
| | 89 | |
|
| 2 | 90 | | DataStore.i.screen.size.OnChange += ScreenSizeChanged; |
| 2 | 91 | | ScreenSizeChanged(DataStore.i.screen.size.Get(), Vector2Int.zero); |
| | 92 | |
|
| | 93 | | //WorldPreviewWindow is not being used at the moment. If we decide to reuse it, remove this activation |
| 2 | 94 | | worldPreviewWindowTransform.gameObject.SetActive(false); |
| 2 | 95 | | } |
| | 96 | |
|
| | 97 | | public void Initialize(IHUD hudController, ISettingsPanelHUDController settingsPanelController, SettingsSectionL |
| | 98 | | { |
| 1 | 99 | | settingsPanelConfig.sections = sections; |
| 1 | 100 | | Initialize(hudController, settingsPanelController); |
| 1 | 101 | | } |
| | 102 | |
|
| | 103 | | private void OnDestroy() |
| | 104 | | { |
| 2 | 105 | | openAction.OnTriggered -= OpenAction_OnTriggered; |
| 2 | 106 | | closeResetAllAction.OnTriggered -= CloseResetAllAction_OnTriggered; |
| | 107 | |
|
| 2 | 108 | | if (settingsAnimator) |
| 2 | 109 | | settingsAnimator.OnWillFinishHide -= OnFinishHide; |
| | 110 | |
|
| 2 | 111 | | tutorialButton.onClick.RemoveAllListeners(); |
| 2 | 112 | | helpAndSupportButton.onClick.RemoveAllListeners(); |
| | 113 | |
|
| 2 | 114 | | DataStore.i.screen.size.OnChange -= ScreenSizeChanged; |
| 2 | 115 | | } |
| | 116 | |
|
| | 117 | | private void CreateSections() |
| | 118 | | { |
| 12 | 119 | | foreach (SettingsSectionModel sectionConfig in settingsPanelConfig.sections) |
| | 120 | | { |
| 4 | 121 | | var newMenuButton = Instantiate(sectionConfig.menuButtonPrefab, menuButtonsContainer); |
| 4 | 122 | | var newSection = Instantiate(sectionConfig.sectionPrefab, sectionsContainer); |
| 4 | 123 | | newSection.gameObject.name = $"Section_{sectionConfig.text}"; |
| 4 | 124 | | var newSectionController = Instantiate(sectionConfig.sectionController); |
| 4 | 125 | | settingsPanelController.AddSection(newMenuButton, newSection, newSectionController, sectionConfig); |
| | 126 | | } |
| | 127 | |
|
| 2 | 128 | | settingsPanelController.OpenSection(0); |
| 2 | 129 | | settingsPanelController.MarkMenuButtonAsSelected(0); |
| 2 | 130 | | } |
| | 131 | |
|
| | 132 | | private void ShowResetAllConfirmation() |
| | 133 | | { |
| 0 | 134 | | DataStore.i.exploreV2.isSomeModalOpen.Set(true); |
| 0 | 135 | | resetAllConfirmation.Show(); |
| 0 | 136 | | resetAllBlackOverlay.SetActive(true); |
| 0 | 137 | | } |
| | 138 | |
|
| | 139 | | private void HideResetAllConfirmation() |
| | 140 | | { |
| 1 | 141 | | DataStore.i.exploreV2.isSomeModalOpen.Set(false); |
| 1 | 142 | | resetAllConfirmation.Hide(); |
| 1 | 143 | | resetAllBlackOverlay.SetActive(false); |
| 1 | 144 | | } |
| | 145 | |
|
| | 146 | | private void ResetAllSettings() |
| | 147 | | { |
| 0 | 148 | | DataStore.i.exploreV2.isSomeModalOpen.Set(false); |
| 0 | 149 | | settingsPanelController.ResetAllSettings(); |
| 0 | 150 | | resetAllConfirmation.Hide(); |
| 0 | 151 | | resetAllBlackOverlay.SetActive(false); |
| 0 | 152 | | } |
| | 153 | |
|
| 0 | 154 | | private void CloseSettingsPanel() { hudController.SetVisibility(false); } |
| | 155 | |
|
| | 156 | | public void SetVisibility(bool visible) |
| | 157 | | { |
| 2 | 158 | | if (visible) |
| | 159 | | { |
| 1 | 160 | | settingsAnimator.Show(); |
| 1 | 161 | | mainWindow.SetActive(true); |
| 1 | 162 | | HideResetAllConfirmation(); |
| 1 | 163 | | settingsPanelController.OpenSection(0); |
| 1 | 164 | | settingsPanelController.MarkMenuButtonAsSelected(0); |
| 1 | 165 | | } |
| | 166 | | else |
| | 167 | | { |
| 1 | 168 | | settingsAnimator.Hide(); |
| 1 | 169 | | settingsPanelController.SaveSettings(); |
| 1 | 170 | | SetWorldPreviewActive(false); |
| | 171 | | } |
| | 172 | |
|
| 2 | 173 | | isOpen = visible; |
| 2 | 174 | | } |
| | 175 | |
|
| | 176 | | public void SetAsFullScreenMenuMode(Transform parentTransform) |
| | 177 | | { |
| 1 | 178 | | if (parentTransform == null) |
| 1 | 179 | | return; |
| | 180 | |
|
| 0 | 181 | | transform.SetParent(parentTransform); |
| 0 | 182 | | transform.localScale = Vector3.one; |
| | 183 | |
|
| 0 | 184 | | RectTransform rectTransform = transform as RectTransform; |
| 0 | 185 | | rectTransform.anchorMin = Vector2.zero; |
| 0 | 186 | | rectTransform.anchorMax = Vector2.one; |
| 0 | 187 | | rectTransform.pivot = new Vector2(0.5f, 0.5f); |
| 0 | 188 | | rectTransform.localPosition = Vector2.zero; |
| 0 | 189 | | rectTransform.offsetMax = Vector2.zero; |
| 0 | 190 | | rectTransform.offsetMin = Vector2.zero; |
| 0 | 191 | | } |
| | 192 | |
|
| 0 | 193 | | public void SetTutorialButtonEnabled(bool isEnabled) { tutorialButton.enabled = isEnabled; } |
| | 194 | |
|
| | 195 | | public void OnAddHelpAndSupportWindow() |
| | 196 | | { |
| 1 | 197 | | helpAndSupportButton.gameObject.SetActive(true); |
| 1 | 198 | | helpAndSupportButton.onClick.AddListener(() => OnHelpAndSupportClicked?.Invoke()); |
| 1 | 199 | | } |
| | 200 | |
|
| | 201 | | public void SetWorldPreviewActive(bool isActive) |
| | 202 | | { |
| | 203 | | //WorldPreviewWindow is not being used at the moment. Leaving this code here |
| | 204 | | //in case we decide to reuse it someday. |
| 0 | 205 | | return; |
| | 206 | | worldPreviewWindowTransform.gameObject.SetActive(isActive); |
| | 207 | | DataStore.i.camera.outputTexture.Set(isActive ? worldPreviewRawImage.texture as RenderTexture : null); |
| | 208 | | } |
| | 209 | |
|
| | 210 | | private void OpenAction_OnTriggered(DCLAction_Trigger action) |
| | 211 | | { |
| 0 | 212 | | Utils.UnlockCursor(); |
| 0 | 213 | | hudController.SetVisibility(!isOpen); |
| 0 | 214 | | } |
| | 215 | |
|
| | 216 | | private void CloseResetAllAction_OnTriggered(DCLAction_Trigger action) |
| | 217 | | { |
| 0 | 218 | | HideResetAllConfirmation(); |
| 0 | 219 | | } |
| | 220 | |
|
| 4 | 221 | | private void OnFinishHide(ShowHideAnimator animator) { mainWindow.SetActive(false); } |
| | 222 | |
|
| | 223 | | private void ScreenSizeChanged(Vector2Int current, Vector2Int previous) |
| | 224 | | { |
| 2 | 225 | | StartCoroutine(RefreshWorldPreviewSize()); |
| 2 | 226 | | } |
| | 227 | |
|
| | 228 | | private IEnumerator RefreshWorldPreviewSize() |
| | 229 | | { |
| 2 | 230 | | yield return null; |
| | 231 | |
|
| 2 | 232 | | float newHeight = Mathf.Clamp(worldPreviewWindowTransform.rect.size.x * WORLD_PREVIEW_ORIGINAL_HEIGHT / WORL |
| | 233 | |
|
| 2 | 234 | | worldPreviewWindowTransform.SetSizeWithCurrentAnchors( |
| | 235 | | RectTransform.Axis.Vertical, |
| | 236 | | newHeight); |
| | 237 | |
|
| 2 | 238 | | worldPreviewCanvasGroup.alpha = worldPreviewWindowTransform.rect.size.x >= WORLD_PREVIEW_MIN_WIDTH_TO_BE_SHO |
| | 239 | |
|
| 2 | 240 | | Utils.ForceRebuildLayoutImmediate(sectionsContainer as RectTransform); |
| 2 | 241 | | } |
| | 242 | | } |
| | 243 | | } |