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