| | 1 | | using DCL.SettingsPanelHUD.Widgets; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.UI; |
| | 5 | |
|
| | 6 | | namespace DCL.SettingsPanelHUD.Sections |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Interface to implement a view for a SECTION. |
| | 10 | | /// </summary> |
| | 11 | | public interface ISettingsSectionView |
| | 12 | | { |
| | 13 | | string sectionName { get; } |
| | 14 | |
|
| | 15 | | /// <summary> |
| | 16 | | /// All the needed logic to initializes the SECTION view and put its WIDGETS factory into operation. |
| | 17 | | /// </summary> |
| | 18 | | /// <param name="settingsSectionController">Controller that will be associated to this view.</param> |
| | 19 | | /// <param name="widgets">List of WIDGETS associated to this SECTION.</param> |
| | 20 | | void Initialize(ISettingsSectionController settingsSectionController, List<SettingsWidgetModel> widgets, string |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// Activates/deactivates the SECTION. |
| | 24 | | /// </summary> |
| | 25 | | /// <param name="active">True for SECTION activation.</param> |
| | 26 | | void SetActive(bool active); |
| | 27 | | } |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// MonoBehaviour that represents a SECTION view and will act as a factory of WIDGETS. |
| | 31 | | /// </summary> |
| | 32 | | public class SettingsSectionView : MonoBehaviour, ISettingsSectionView |
| | 33 | | { |
| | 34 | | [SerializeField] private Transform widgetsContainer; |
| | 35 | | [SerializeField] private ScrollRect scrollRect; |
| | 36 | |
|
| | 37 | | private ISettingsSectionController settingsSectionController; |
| | 38 | | private List<SettingsWidgetModel> widgets; |
| | 39 | | private bool isOpen = false; |
| | 40 | |
|
| 2 | 41 | | public string sectionName { get; private set; } |
| | 42 | |
|
| | 43 | | public void Initialize(ISettingsSectionController settingsSectionController, List<SettingsWidgetModel> widgets, |
| | 44 | | { |
| 4 | 45 | | this.settingsSectionController = settingsSectionController; |
| 4 | 46 | | this.widgets = widgets; |
| 4 | 47 | | this.sectionName = sectionName; |
| | 48 | |
|
| 4 | 49 | | CreateWidgets(); |
| 4 | 50 | | } |
| | 51 | |
|
| | 52 | | public void SetActive(bool active) |
| | 53 | | { |
| 9 | 54 | | gameObject.SetActive(active); |
| | 55 | |
|
| 9 | 56 | | if (!isOpen && active) |
| 1 | 57 | | scrollRect.verticalNormalizedPosition = 1f; |
| | 58 | |
|
| 9 | 59 | | isOpen = active; |
| 9 | 60 | | } |
| | 61 | |
|
| | 62 | | private void CreateWidgets() |
| | 63 | | { |
| 20 | 64 | | foreach (SettingsWidgetModel widgetConfig in widgets) |
| | 65 | | { |
| 6 | 66 | | var newWidget = Instantiate(widgetConfig.widgetPrefab, widgetsContainer); |
| 6 | 67 | | newWidget.gameObject.name = $"Widget_{widgetConfig.title}"; |
| 6 | 68 | | var newWidgetController = Instantiate(widgetConfig.widgetController); |
| 6 | 69 | | settingsSectionController.AddWidget(newWidget, newWidgetController, widgetConfig); |
| | 70 | | } |
| 4 | 71 | | } |
| | 72 | | } |
| | 73 | | } |