| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.UI; |
| | 6 | |
|
| | 7 | | public interface IModalComponentView |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// It will be triggered when the modal is closed. |
| | 11 | | /// </summary> |
| | 12 | | event Action OnCloseAction; |
| | 13 | |
|
| | 14 | | /// <summary> |
| | 15 | | /// Fill the model and updates the modal with this data. |
| | 16 | | /// </summary> |
| | 17 | | /// <param name="model">Data to configure the image.</param> |
| | 18 | | void Configure(ModalComponentModel model); |
| | 19 | | } |
| | 20 | |
|
| | 21 | | public class ModalComponentView : BaseComponentView, IModalComponentView |
| | 22 | | { |
| | 23 | | public event Action OnCloseAction; |
| | 24 | |
|
| | 25 | | [Header("Prefab References")] |
| | 26 | | [SerializeField] internal ButtonComponentView closeButton; |
| | 27 | | [SerializeField] internal Button alphaBackground; |
| | 28 | | [SerializeField] internal GameObject container; |
| | 29 | |
|
| | 30 | | [Header("Configuration")] |
| | 31 | | [SerializeField] internal ModalComponentModel model; |
| | 32 | |
|
| | 33 | | internal GameObject content; |
| | 34 | |
|
| | 35 | | public override void Start() |
| | 36 | | { |
| 0 | 37 | | base.Start(); |
| | 38 | |
|
| 0 | 39 | | closeButton.onClick.AddListener(CloseButtonClicked); |
| 0 | 40 | | alphaBackground.onClick.AddListener(CloseButtonClicked); |
| 0 | 41 | | RefreshControl(); |
| 0 | 42 | | } |
| | 43 | |
|
| | 44 | | public override void RefreshControl() |
| | 45 | | { |
| 1 | 46 | | if (content != null) |
| 0 | 47 | | Destroy(content); |
| 1 | 48 | | if (model.content != null) |
| 1 | 49 | | content = Instantiate(model.content, container.transform); |
| 1 | 50 | | } |
| | 51 | |
|
| | 52 | | public void Configure(ModalComponentModel model) |
| | 53 | | { |
| 1 | 54 | | this.model = model; |
| 1 | 55 | | RefreshControl(); |
| 1 | 56 | | } |
| | 57 | |
|
| | 58 | | internal void CloseButtonClicked() |
| | 59 | | { |
| 1 | 60 | | OnCloseAction?.Invoke(); |
| 1 | 61 | | Hide(); |
| 1 | 62 | | } |
| | 63 | |
|
| | 64 | | public override void Show(bool instant = false) |
| | 65 | | { |
| 0 | 66 | | if (isVisible) |
| 0 | 67 | | return; |
| | 68 | |
|
| 0 | 69 | | base.Show(instant); |
| 0 | 70 | | } |
| | 71 | | public override void Hide(bool instant = false) |
| | 72 | | { |
| 1 | 73 | | if (!isVisible) |
| 1 | 74 | | return; |
| | 75 | |
|
| 0 | 76 | | base.Hide(instant); |
| 0 | 77 | | } |
| | 78 | | } |