| | 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 | | /// <summary> |
| | 21 | | /// This will hide the close button of the pop up, forcing the user to take an action |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="canBe"></param> |
| | 24 | | void CanBeCancelled(bool canBe); |
| | 25 | | } |
| | 26 | |
|
| | 27 | | public class ModalComponentView : BaseComponentView, IModalComponentView |
| | 28 | | { |
| | 29 | | public event Action OnCloseAction; |
| | 30 | |
|
| | 31 | | [Header("Prefab References")] |
| | 32 | | [SerializeField] internal ButtonComponentView closeButton; |
| | 33 | | [SerializeField] internal Button alphaBackground; |
| | 34 | | [SerializeField] internal GameObject container; |
| | 35 | |
|
| | 36 | | [Header("Configuration")] |
| | 37 | | [SerializeField] internal ModalComponentModel model; |
| | 38 | |
|
| | 39 | | internal GameObject content; |
| 8 | 40 | | internal bool canBeCanceled = true; |
| | 41 | |
|
| | 42 | | public override void Start() |
| | 43 | | { |
| 0 | 44 | | base.Start(); |
| | 45 | |
|
| 0 | 46 | | closeButton.onClick.AddListener(CloseButtonClicked); |
| 0 | 47 | | alphaBackground.onClick.AddListener(CloseButtonClicked); |
| 0 | 48 | | RefreshControl(); |
| 0 | 49 | | } |
| | 50 | |
|
| | 51 | | public override void RefreshControl() |
| | 52 | | { |
| 1 | 53 | | if (content != null) |
| 0 | 54 | | Destroy(content); |
| 1 | 55 | | if (model.content != null) |
| 1 | 56 | | content = Instantiate(model.content, container.transform); |
| 1 | 57 | | } |
| | 58 | |
|
| | 59 | | public void Configure(ModalComponentModel model) |
| | 60 | | { |
| 1 | 61 | | this.model = model; |
| 1 | 62 | | RefreshControl(); |
| 1 | 63 | | } |
| | 64 | |
|
| | 65 | | public void CanBeCancelled(bool canBe) |
| | 66 | | { |
| 0 | 67 | | closeButton.gameObject.SetActive(canBe); |
| 0 | 68 | | canBeCanceled = canBe; |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | internal void CloseButtonClicked() |
| | 72 | | { |
| 1 | 73 | | if (!canBeCanceled) |
| 0 | 74 | | return; |
| 1 | 75 | | OnCloseAction?.Invoke(); |
| 1 | 76 | | Hide(); |
| 1 | 77 | | } |
| | 78 | |
|
| | 79 | | public override void Show(bool instant = false) |
| | 80 | | { |
| 0 | 81 | | if (isVisible) |
| 0 | 82 | | return; |
| | 83 | |
|
| 0 | 84 | | base.Show(instant); |
| 0 | 85 | | } |
| | 86 | | public override void Hide(bool instant = false) |
| | 87 | | { |
| 1 | 88 | | if (!isVisible) |
| 1 | 89 | | return; |
| | 90 | |
|
| 0 | 91 | | base.Hide(instant); |
| 0 | 92 | | } |
| | 93 | | } |