| | 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 InputAction_Trigger closeAction; |
| | 31 | | [SerializeField] private InputAction_Trigger openAction; |
| | 32 | |
|
| | 33 | | [Header("Animations")] |
| | 34 | | [SerializeField] private ShowHideAnimator settingsAnimator; |
| | 35 | |
|
| 0 | 36 | | public bool isOpen { get; private set; } |
| | 37 | |
|
| | 38 | | private const string PATH = "SettingsPanelHUD"; |
| | 39 | |
|
| | 40 | | private IHUD hudController; |
| | 41 | | private ISettingsPanelHUDController settingsPanelController; |
| | 42 | |
|
| | 43 | | public static SettingsPanelHUDView Create() |
| | 44 | | { |
| 3 | 45 | | SettingsPanelHUDView view = Instantiate(Resources.Load<GameObject>(PATH)).GetComponent<SettingsPanelHUDView> |
| 3 | 46 | | view.name = "_SettingsPanelHUD"; |
| 3 | 47 | | return view; |
| | 48 | | } |
| | 49 | |
|
| | 50 | | public void Initialize(IHUD hudController, ISettingsPanelHUDController settingsPanelController) |
| | 51 | | { |
| 3 | 52 | | this.hudController = hudController; |
| 3 | 53 | | this.settingsPanelController = settingsPanelController; |
| | 54 | |
|
| 3 | 55 | | openAction.OnTriggered += OpenAction_OnTriggered; |
| | 56 | |
|
| 3 | 57 | | resetAllButton.onClick.AddListener(ShowResetAllConfirmation); |
| 3 | 58 | | resetAllCancelButton.onClick.AddListener(HideResetAllConfirmation); |
| 3 | 59 | | resetAllOkButton.onClick.AddListener(ResetAllSettings); |
| | 60 | |
|
| 3 | 61 | | closeButton.onClick.AddListener(CloseSettingsPanel); |
| 3 | 62 | | settingsAnimator.OnWillFinishHide += OnFinishHide; |
| | 63 | |
|
| 3 | 64 | | CreateSections(); |
| 3 | 65 | | isOpen = !settingsAnimator.hideOnEnable; |
| 3 | 66 | | settingsAnimator.Hide(true); |
| 3 | 67 | | } |
| | 68 | |
|
| | 69 | | public void Initialize(IHUD hudController, ISettingsPanelHUDController settingsPanelController, SettingsSectionL |
| | 70 | | { |
| 1 | 71 | | settingsPanelConfig.sections = sections; |
| 1 | 72 | | Initialize(hudController, settingsPanelController); |
| 1 | 73 | | } |
| | 74 | |
|
| | 75 | | private void OnDestroy() |
| | 76 | | { |
| 3 | 77 | | openAction.OnTriggered -= OpenAction_OnTriggered; |
| | 78 | |
|
| 3 | 79 | | if (settingsAnimator) |
| 3 | 80 | | settingsAnimator.OnWillFinishHide -= OnFinishHide; |
| 3 | 81 | | } |
| | 82 | |
|
| | 83 | | private void CreateSections() |
| | 84 | | { |
| 14 | 85 | | foreach (SettingsSectionModel sectionConfig in settingsPanelConfig.sections) |
| | 86 | | { |
| 4 | 87 | | var newMenuButton = Instantiate(sectionConfig.menuButtonPrefab, menuButtonsContainer); |
| 4 | 88 | | var newSection = Instantiate(sectionConfig.sectionPrefab, sectionsContainer); |
| 4 | 89 | | newSection.gameObject.name = $"Section_{sectionConfig.text}"; |
| 4 | 90 | | var newSectionController = Instantiate(sectionConfig.sectionController); |
| 4 | 91 | | settingsPanelController.AddSection(newMenuButton, newSection, newSectionController, sectionConfig); |
| | 92 | | } |
| | 93 | |
|
| 3 | 94 | | settingsPanelController.OpenSection(0); |
| 3 | 95 | | settingsPanelController.MarkMenuButtonAsSelected(0); |
| 3 | 96 | | } |
| | 97 | |
|
| | 98 | | private void ShowResetAllConfirmation() |
| | 99 | | { |
| 0 | 100 | | resetAllConfirmation.Show(); |
| 0 | 101 | | resetAllBlackOverlay.SetActive(true); |
| 0 | 102 | | } |
| | 103 | |
|
| | 104 | | private void HideResetAllConfirmation() |
| | 105 | | { |
| 1 | 106 | | resetAllConfirmation.Hide(); |
| 1 | 107 | | resetAllBlackOverlay.SetActive(false); |
| 1 | 108 | | } |
| | 109 | |
|
| | 110 | | private void ResetAllSettings() |
| | 111 | | { |
| 0 | 112 | | settingsPanelController.ResetAllSettings(); |
| 0 | 113 | | resetAllConfirmation.Hide(); |
| 0 | 114 | | resetAllBlackOverlay.SetActive(false); |
| 0 | 115 | | } |
| | 116 | |
|
| 0 | 117 | | private void CloseSettingsPanel() { hudController.SetVisibility(false); } |
| | 118 | |
|
| | 119 | | public void SetVisibility(bool visible) |
| | 120 | | { |
| 1 | 121 | | if (visible && !isOpen) |
| 1 | 122 | | AudioScriptableObjects.dialogOpen.Play(true); |
| 0 | 123 | | else if (isOpen) |
| 0 | 124 | | AudioScriptableObjects.dialogClose.Play(true); |
| | 125 | |
|
| 1 | 126 | | closeAction.OnTriggered -= CloseAction_OnTriggered; |
| 1 | 127 | | if (visible) |
| | 128 | | { |
| 1 | 129 | | closeAction.OnTriggered += CloseAction_OnTriggered; |
| 1 | 130 | | settingsAnimator.Show(); |
| 1 | 131 | | mainWindow.SetActive(true); |
| 1 | 132 | | HideResetAllConfirmation(); |
| 1 | 133 | | settingsPanelController.OpenSection(0); |
| 1 | 134 | | settingsPanelController.MarkMenuButtonAsSelected(0); |
| 1 | 135 | | } |
| | 136 | | else |
| | 137 | | { |
| 0 | 138 | | settingsAnimator.Hide(); |
| 0 | 139 | | settingsPanelController.SaveSettings(); |
| | 140 | | } |
| | 141 | |
|
| 1 | 142 | | isOpen = visible; |
| 1 | 143 | | } |
| | 144 | |
|
| | 145 | | private void OpenAction_OnTriggered(DCLAction_Trigger action) |
| | 146 | | { |
| 0 | 147 | | Utils.UnlockCursor(); |
| 0 | 148 | | hudController.SetVisibility(!isOpen); |
| 0 | 149 | | } |
| | 150 | |
|
| 0 | 151 | | private void CloseAction_OnTriggered(DCLAction_Trigger action) { CloseSettingsPanel(); } |
| | 152 | |
|
| 6 | 153 | | private void OnFinishHide(ShowHideAnimator animator) { mainWindow.SetActive(false); } |
| | 154 | | } |
| | 155 | | } |