| | 1 | | using DCL.Helpers; |
| | 2 | | using DCL.SettingsPanelHUD.Sections; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.UI; |
| | 5 | |
|
| | 6 | | namespace DCL.SettingsPanelHUD |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// MonoBehaviour that represents the main settings panel view and will act as a factory of SECTIONS. |
| | 10 | | /// </summary> |
| | 11 | | public class SettingsPanelHUDView : MonoBehaviour |
| | 12 | | { |
| | 13 | | [Header("General configuration")] |
| | 14 | | [SerializeField] private GameObject mainWindow; |
| | 15 | |
|
| | 16 | | [Header("Sections configuration")] |
| | 17 | | [SerializeField] private SettingsPanelModel settingsPanelConfig; |
| | 18 | | [SerializeField] private Transform menuButtonsContainer; |
| | 19 | | [SerializeField] private Transform sectionsContainer; |
| | 20 | |
|
| | 21 | | [Header("Reset All configuration")] |
| | 22 | | [SerializeField] private Button resetAllButton; |
| | 23 | | [SerializeField] private ShowHideAnimator resetAllConfirmation; |
| | 24 | | [SerializeField] private Button resetAllOkButton; |
| | 25 | | [SerializeField] private Button resetAllCancelButton; |
| | 26 | | [SerializeField] private GameObject resetAllBlackOverlay; |
| | 27 | |
|
| | 28 | | [Header("Open/Close Settings")] |
| | 29 | | [SerializeField] private Button closeButton; |
| | 30 | | [SerializeField] private Button backgroundButton; |
| | 31 | | [SerializeField] private InputAction_Trigger closeAction; |
| | 32 | | [SerializeField] private InputAction_Trigger openAction; |
| | 33 | |
|
| | 34 | | [Header("Others")] |
| | 35 | | [SerializeField] private Button tutorialButton; |
| | 36 | | [SerializeField] private Button reportBugButton; |
| | 37 | | [SerializeField] private Button helpAndSupportButton; |
| | 38 | |
|
| | 39 | | [Header("Animations")] |
| | 40 | | [SerializeField] private ShowHideAnimator settingsAnimator; |
| | 41 | |
|
| 0 | 42 | | public bool isOpen { get; private set; } |
| | 43 | |
|
| | 44 | | private const string PATH = "SettingsPanelHUD"; |
| | 45 | |
|
| | 46 | | private IHUD hudController; |
| | 47 | | private ISettingsPanelHUDController settingsPanelController; |
| | 48 | |
|
| | 49 | | public event System.Action OnRestartTutorial; |
| | 50 | | public event System.Action OnHelpAndSupportClicked; |
| | 51 | |
|
| | 52 | | public static SettingsPanelHUDView Create() |
| | 53 | | { |
| 2 | 54 | | SettingsPanelHUDView view = Instantiate(Resources.Load<GameObject>(PATH)).GetComponent<SettingsPanelHUDView> |
| 2 | 55 | | view.name = "_SettingsPanelHUD"; |
| 2 | 56 | | return view; |
| | 57 | | } |
| | 58 | |
|
| | 59 | | public void Initialize(IHUD hudController, ISettingsPanelHUDController settingsPanelController) |
| | 60 | | { |
| 2 | 61 | | this.hudController = hudController; |
| 2 | 62 | | this.settingsPanelController = settingsPanelController; |
| | 63 | |
|
| 2 | 64 | | openAction.OnTriggered += OpenAction_OnTriggered; |
| | 65 | |
|
| 2 | 66 | | resetAllButton.onClick.AddListener(ShowResetAllConfirmation); |
| 2 | 67 | | resetAllCancelButton.onClick.AddListener(HideResetAllConfirmation); |
| 2 | 68 | | resetAllOkButton.onClick.AddListener(ResetAllSettings); |
| | 69 | |
|
| 2 | 70 | | closeButton.onClick.AddListener(CloseSettingsPanel); |
| 2 | 71 | | backgroundButton.onClick.AddListener(CloseSettingsPanel); |
| 2 | 72 | | settingsAnimator.OnWillFinishHide += OnFinishHide; |
| | 73 | |
|
| 2 | 74 | | CreateSections(); |
| 2 | 75 | | isOpen = !settingsAnimator.hideOnEnable; |
| 2 | 76 | | settingsAnimator.Hide(true); |
| | 77 | |
|
| 2 | 78 | | tutorialButton.onClick.AddListener(() => OnRestartTutorial?.Invoke()); |
| 2 | 79 | | } |
| | 80 | |
|
| | 81 | | public void Initialize(IHUD hudController, ISettingsPanelHUDController settingsPanelController, SettingsSectionL |
| | 82 | | { |
| 1 | 83 | | settingsPanelConfig.sections = sections; |
| 1 | 84 | | Initialize(hudController, settingsPanelController); |
| 1 | 85 | | } |
| | 86 | |
|
| | 87 | | private void OnDestroy() |
| | 88 | | { |
| 2 | 89 | | openAction.OnTriggered -= OpenAction_OnTriggered; |
| | 90 | |
|
| 2 | 91 | | if (settingsAnimator) |
| 2 | 92 | | settingsAnimator.OnWillFinishHide -= OnFinishHide; |
| | 93 | |
|
| 2 | 94 | | tutorialButton.onClick.RemoveAllListeners(); |
| 2 | 95 | | helpAndSupportButton.onClick.RemoveAllListeners(); |
| 2 | 96 | | } |
| | 97 | |
|
| | 98 | | private void CreateSections() |
| | 99 | | { |
| 12 | 100 | | foreach (SettingsSectionModel sectionConfig in settingsPanelConfig.sections) |
| | 101 | | { |
| 4 | 102 | | var newMenuButton = Instantiate(sectionConfig.menuButtonPrefab, menuButtonsContainer); |
| 4 | 103 | | var newSection = Instantiate(sectionConfig.sectionPrefab, sectionsContainer); |
| 4 | 104 | | newSection.gameObject.name = $"Section_{sectionConfig.text}"; |
| 4 | 105 | | var newSectionController = Instantiate(sectionConfig.sectionController); |
| 4 | 106 | | settingsPanelController.AddSection(newMenuButton, newSection, newSectionController, sectionConfig); |
| | 107 | | } |
| | 108 | |
|
| 2 | 109 | | settingsPanelController.OpenSection(0); |
| 2 | 110 | | settingsPanelController.MarkMenuButtonAsSelected(0); |
| 2 | 111 | | } |
| | 112 | |
|
| | 113 | | private void ShowResetAllConfirmation() |
| | 114 | | { |
| 0 | 115 | | resetAllConfirmation.Show(); |
| 0 | 116 | | resetAllBlackOverlay.SetActive(true); |
| 0 | 117 | | } |
| | 118 | |
|
| | 119 | | private void HideResetAllConfirmation() |
| | 120 | | { |
| 1 | 121 | | resetAllConfirmation.Hide(); |
| 1 | 122 | | resetAllBlackOverlay.SetActive(false); |
| 1 | 123 | | } |
| | 124 | |
|
| | 125 | | private void ResetAllSettings() |
| | 126 | | { |
| 0 | 127 | | settingsPanelController.ResetAllSettings(); |
| 0 | 128 | | resetAllConfirmation.Hide(); |
| 0 | 129 | | resetAllBlackOverlay.SetActive(false); |
| 0 | 130 | | } |
| | 131 | |
|
| 0 | 132 | | private void CloseSettingsPanel() { hudController.SetVisibility(false); } |
| | 133 | |
|
| | 134 | | public void SetVisibility(bool visible) |
| | 135 | | { |
| 2 | 136 | | closeAction.OnTriggered -= CloseAction_OnTriggered; |
| 2 | 137 | | if (visible) |
| | 138 | | { |
| 1 | 139 | | closeAction.OnTriggered += CloseAction_OnTriggered; |
| 1 | 140 | | settingsAnimator.Show(); |
| 1 | 141 | | mainWindow.SetActive(true); |
| 1 | 142 | | HideResetAllConfirmation(); |
| 1 | 143 | | settingsPanelController.OpenSection(0); |
| 1 | 144 | | settingsPanelController.MarkMenuButtonAsSelected(0); |
| 1 | 145 | | } |
| | 146 | | else |
| | 147 | | { |
| 1 | 148 | | settingsAnimator.Hide(); |
| 1 | 149 | | settingsPanelController.SaveSettings(); |
| | 150 | | } |
| | 151 | |
|
| 2 | 152 | | isOpen = visible; |
| 2 | 153 | | } |
| | 154 | |
|
| | 155 | | public void SetAsFullScreenMenuMode(Transform parentTransform) |
| | 156 | | { |
| 1 | 157 | | if (parentTransform == null) |
| 1 | 158 | | return; |
| | 159 | |
|
| 0 | 160 | | transform.SetParent(parentTransform); |
| 0 | 161 | | transform.localScale = Vector3.one; |
| | 162 | |
|
| 0 | 163 | | RectTransform rectTransform = transform as RectTransform; |
| 0 | 164 | | rectTransform.anchorMin = Vector2.zero; |
| 0 | 165 | | rectTransform.anchorMax = Vector2.one; |
| 0 | 166 | | rectTransform.pivot = new Vector2(0.5f, 0.5f); |
| 0 | 167 | | rectTransform.localPosition = Vector2.zero; |
| 0 | 168 | | rectTransform.offsetMax = Vector2.zero; |
| 0 | 169 | | rectTransform.offsetMin = Vector2.zero; |
| 0 | 170 | | } |
| | 171 | |
|
| 0 | 172 | | public void SetTutorialButtonEnabled(bool isEnabled) { tutorialButton.enabled = isEnabled; } |
| | 173 | |
|
| | 174 | | public void OnAddHelpAndSupportWindow() |
| | 175 | | { |
| 1 | 176 | | helpAndSupportButton.gameObject.SetActive(true); |
| 1 | 177 | | helpAndSupportButton.onClick.AddListener(() => OnHelpAndSupportClicked?.Invoke()); |
| 1 | 178 | | } |
| | 179 | |
|
| | 180 | | private void OpenAction_OnTriggered(DCLAction_Trigger action) |
| | 181 | | { |
| 0 | 182 | | Utils.UnlockCursor(); |
| 0 | 183 | | hudController.SetVisibility(!isOpen); |
| 0 | 184 | | } |
| | 185 | |
|
| 0 | 186 | | private void CloseAction_OnTriggered(DCLAction_Trigger action) { CloseSettingsPanel(); } |
| | 187 | |
|
| 4 | 188 | | private void OnFinishHide(ShowHideAnimator animator) { mainWindow.SetActive(false); } |
| | 189 | | } |
| | 190 | | } |