| | 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); |
| 2 | 92 | | } |
| | 93 | |
|
| | 94 | | public void Initialize(IHUD hudController, ISettingsPanelHUDController settingsPanelController, SettingsSectionL |
| | 95 | | { |
| 1 | 96 | | settingsPanelConfig.sections = sections; |
| 1 | 97 | | Initialize(hudController, settingsPanelController); |
| 1 | 98 | | } |
| | 99 | |
|
| | 100 | | private void OnDestroy() |
| | 101 | | { |
| 2 | 102 | | openAction.OnTriggered -= OpenAction_OnTriggered; |
| 2 | 103 | | closeResetAllAction.OnTriggered -= CloseResetAllAction_OnTriggered; |
| | 104 | |
|
| 2 | 105 | | if (settingsAnimator) |
| 2 | 106 | | settingsAnimator.OnWillFinishHide -= OnFinishHide; |
| | 107 | |
|
| 2 | 108 | | tutorialButton.onClick.RemoveAllListeners(); |
| 2 | 109 | | helpAndSupportButton.onClick.RemoveAllListeners(); |
| | 110 | |
|
| 2 | 111 | | DataStore.i.screen.size.OnChange -= ScreenSizeChanged; |
| 2 | 112 | | } |
| | 113 | |
|
| | 114 | | private void CreateSections() |
| | 115 | | { |
| 12 | 116 | | foreach (SettingsSectionModel sectionConfig in settingsPanelConfig.sections) |
| | 117 | | { |
| 4 | 118 | | var newMenuButton = Instantiate(sectionConfig.menuButtonPrefab, menuButtonsContainer); |
| 4 | 119 | | var newSection = Instantiate(sectionConfig.sectionPrefab, sectionsContainer); |
| 4 | 120 | | newSection.gameObject.name = $"Section_{sectionConfig.text}"; |
| 4 | 121 | | var newSectionController = Instantiate(sectionConfig.sectionController); |
| 4 | 122 | | settingsPanelController.AddSection(newMenuButton, newSection, newSectionController, sectionConfig); |
| | 123 | | } |
| | 124 | |
|
| 2 | 125 | | settingsPanelController.OpenSection(0); |
| 2 | 126 | | settingsPanelController.MarkMenuButtonAsSelected(0); |
| 2 | 127 | | } |
| | 128 | |
|
| | 129 | | private void ShowResetAllConfirmation() |
| | 130 | | { |
| 0 | 131 | | DataStore.i.exploreV2.isSomeModalOpen.Set(true); |
| 0 | 132 | | resetAllConfirmation.Show(); |
| 0 | 133 | | resetAllBlackOverlay.SetActive(true); |
| 0 | 134 | | } |
| | 135 | |
|
| | 136 | | private void HideResetAllConfirmation() |
| | 137 | | { |
| 1 | 138 | | DataStore.i.exploreV2.isSomeModalOpen.Set(false); |
| 1 | 139 | | resetAllConfirmation.Hide(); |
| 1 | 140 | | resetAllBlackOverlay.SetActive(false); |
| 1 | 141 | | } |
| | 142 | |
|
| | 143 | | private void ResetAllSettings() |
| | 144 | | { |
| 0 | 145 | | DataStore.i.exploreV2.isSomeModalOpen.Set(false); |
| 0 | 146 | | settingsPanelController.ResetAllSettings(); |
| 0 | 147 | | resetAllConfirmation.Hide(); |
| 0 | 148 | | resetAllBlackOverlay.SetActive(false); |
| 0 | 149 | | } |
| | 150 | |
|
| 0 | 151 | | private void CloseSettingsPanel() { hudController.SetVisibility(false); } |
| | 152 | |
|
| | 153 | | public void SetVisibility(bool visible) |
| | 154 | | { |
| 2 | 155 | | if (visible) |
| | 156 | | { |
| 1 | 157 | | settingsAnimator.Show(); |
| 1 | 158 | | mainWindow.SetActive(true); |
| 1 | 159 | | HideResetAllConfirmation(); |
| 1 | 160 | | settingsPanelController.OpenSection(0); |
| 1 | 161 | | settingsPanelController.MarkMenuButtonAsSelected(0); |
| 1 | 162 | | } |
| | 163 | | else |
| | 164 | | { |
| 1 | 165 | | settingsAnimator.Hide(); |
| 1 | 166 | | settingsPanelController.SaveSettings(); |
| 1 | 167 | | SetWorldPreviewActive(false); |
| | 168 | | } |
| | 169 | |
|
| 2 | 170 | | isOpen = visible; |
| 2 | 171 | | } |
| | 172 | |
|
| | 173 | | public void SetAsFullScreenMenuMode(Transform parentTransform) |
| | 174 | | { |
| 1 | 175 | | if (parentTransform == null) |
| 1 | 176 | | return; |
| | 177 | |
|
| 0 | 178 | | transform.SetParent(parentTransform); |
| 0 | 179 | | transform.localScale = Vector3.one; |
| | 180 | |
|
| 0 | 181 | | RectTransform rectTransform = transform as RectTransform; |
| 0 | 182 | | rectTransform.anchorMin = Vector2.zero; |
| 0 | 183 | | rectTransform.anchorMax = Vector2.one; |
| 0 | 184 | | rectTransform.pivot = new Vector2(0.5f, 0.5f); |
| 0 | 185 | | rectTransform.localPosition = Vector2.zero; |
| 0 | 186 | | rectTransform.offsetMax = Vector2.zero; |
| 0 | 187 | | rectTransform.offsetMin = Vector2.zero; |
| 0 | 188 | | } |
| | 189 | |
|
| 0 | 190 | | public void SetTutorialButtonEnabled(bool isEnabled) { tutorialButton.enabled = isEnabled; } |
| | 191 | |
|
| | 192 | | public void OnAddHelpAndSupportWindow() |
| | 193 | | { |
| 1 | 194 | | helpAndSupportButton.gameObject.SetActive(true); |
| 1 | 195 | | helpAndSupportButton.onClick.AddListener(() => OnHelpAndSupportClicked?.Invoke()); |
| 1 | 196 | | } |
| | 197 | |
|
| | 198 | | public void SetWorldPreviewActive(bool isActive) |
| | 199 | | { |
| 3 | 200 | | isActive = false; |
| | 201 | |
|
| 3 | 202 | | worldPreviewWindowTransform.gameObject.SetActive(isActive); |
| 3 | 203 | | DataStore.i.camera.outputTexture.Set(isActive ? worldPreviewRawImage.texture as RenderTexture : null); |
| 3 | 204 | | CommonScriptableObjects.isFullscreenHUDOpen.Set(DataStore.i.exploreV2.isOpen.Get() && !isActive); |
| 3 | 205 | | } |
| | 206 | |
|
| | 207 | | private void OpenAction_OnTriggered(DCLAction_Trigger action) |
| | 208 | | { |
| 0 | 209 | | Utils.UnlockCursor(); |
| 0 | 210 | | hudController.SetVisibility(!isOpen); |
| 0 | 211 | | } |
| | 212 | |
|
| | 213 | | private void CloseResetAllAction_OnTriggered(DCLAction_Trigger action) |
| | 214 | | { |
| 0 | 215 | | HideResetAllConfirmation(); |
| 0 | 216 | | } |
| | 217 | |
|
| 4 | 218 | | private void OnFinishHide(ShowHideAnimator animator) { mainWindow.SetActive(false); } |
| | 219 | |
|
| | 220 | | private void ScreenSizeChanged(Vector2Int current, Vector2Int previous) |
| | 221 | | { |
| 2 | 222 | | StartCoroutine(RefreshWorldPreviewSize()); |
| 2 | 223 | | } |
| | 224 | |
|
| | 225 | | private IEnumerator RefreshWorldPreviewSize() |
| | 226 | | { |
| 2 | 227 | | yield return null; |
| | 228 | |
|
| 2 | 229 | | float newHeight = Mathf.Clamp(worldPreviewWindowTransform.rect.size.x * WORLD_PREVIEW_ORIGINAL_HEIGHT / WORL |
| | 230 | |
|
| 2 | 231 | | worldPreviewWindowTransform.SetSizeWithCurrentAnchors( |
| | 232 | | RectTransform.Axis.Vertical, |
| | 233 | | newHeight); |
| | 234 | |
|
| 2 | 235 | | worldPreviewCanvasGroup.alpha = worldPreviewWindowTransform.rect.size.x >= WORLD_PREVIEW_MIN_WIDTH_TO_BE_SHO |
| | 236 | |
|
| 2 | 237 | | Utils.ForceRebuildLayoutImmediate(sectionsContainer as RectTransform); |
| 2 | 238 | | } |
| | 239 | | } |
| | 240 | | } |