| | 1 | | using DCL; |
| | 2 | | using System; |
| | 3 | | using System.Collections; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.EventSystems; |
| | 6 | |
|
| | 7 | | public interface IBaseComponentView : IPointerEnterHandler, IPointerExitHandler, IDisposable |
| | 8 | | { |
| | 9 | | bool isVisible { get; } |
| | 10 | |
|
| | 11 | | /// <summary> |
| | 12 | | /// It is called at the beginning of the UI component lifecycle. |
| | 13 | | /// </summary> |
| | 14 | | void Awake(); |
| | 15 | |
|
| | 16 | | /// <summary> |
| | 17 | | /// It is called each time the component is enabled. |
| | 18 | | /// </summary> |
| | 19 | | void OnEnable(); |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// It is called just after the UI component has been initialized. |
| | 23 | | /// </summary> |
| | 24 | | void Start(); |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// Updates the UI component with the current model configuration. |
| | 28 | | /// </summary> |
| | 29 | | void RefreshControl(); |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// Shows the UI component. |
| | 33 | | /// </summary> |
| | 34 | | /// <param name="instant">True for not apply progressive animation.</param> |
| | 35 | | void Show(bool instant = false); |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Hides the UI component. |
| | 39 | | /// </summary> |
| | 40 | | /// <param name="instant">True for not apply progressive animation.</param> |
| | 41 | | void Hide(bool instant = false); |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// It is called when the focus is set into the component. |
| | 45 | | /// </summary> |
| | 46 | | void OnFocus(); |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// It is called when the focus is lost from the component. |
| | 50 | | /// </summary> |
| | 51 | | void OnLoseFocus(); |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// It is called just after the screen size has changed. |
| | 55 | | /// </summary> |
| | 56 | | void OnScreenSizeChanged(); |
| | 57 | | } |
| | 58 | |
|
| | 59 | | public interface IComponentModelConfig |
| | 60 | | { |
| | 61 | | /// <summary> |
| | 62 | | /// Fill the model and updates the component with this data. |
| | 63 | | /// </summary> |
| | 64 | | /// <param name="newModel">Data to configure the component.</param> |
| | 65 | | void Configure(BaseComponentModel newModel); |
| | 66 | | } |
| | 67 | |
|
| | 68 | | public abstract class BaseComponentView : MonoBehaviour, IBaseComponentView |
| | 69 | | { |
| | 70 | | internal BaseComponentModel baseModel; |
| | 71 | | internal ShowHideAnimator showHideAnimator; |
| | 72 | |
|
| 0 | 73 | | public bool isVisible { get; private set; } |
| | 74 | |
|
| | 75 | | public virtual void Awake() |
| | 76 | | { |
| 1816 | 77 | | showHideAnimator = GetComponent<ShowHideAnimator>(); |
| 1816 | 78 | | DataStore.i.screen.size.OnChange += OnScreenSizeModified; |
| 1816 | 79 | | } |
| | 80 | |
|
| 3852 | 81 | | public virtual void OnEnable() { OnScreenSizeChanged(); } |
| | 82 | |
|
| 792 | 83 | | public virtual void Start() { } |
| | 84 | |
|
| | 85 | | public abstract void RefreshControl(); |
| | 86 | |
|
| | 87 | | public virtual void Show(bool instant = false) |
| | 88 | | { |
| 8 | 89 | | if (showHideAnimator == null) |
| 0 | 90 | | return; |
| | 91 | |
|
| 8 | 92 | | showHideAnimator.Show(instant); |
| 8 | 93 | | isVisible = true; |
| 8 | 94 | | } |
| | 95 | |
|
| | 96 | | public virtual void Hide(bool instant = false) |
| | 97 | | { |
| 47 | 98 | | if (showHideAnimator == null) |
| 0 | 99 | | return; |
| | 100 | |
|
| 47 | 101 | | showHideAnimator.Hide(instant); |
| 47 | 102 | | isVisible = false; |
| 47 | 103 | | } |
| | 104 | |
|
| 2 | 105 | | public virtual void OnFocus() { } |
| | 106 | |
|
| 2 | 107 | | public virtual void OnLoseFocus() { } |
| | 108 | |
|
| 1720 | 109 | | public virtual void OnScreenSizeChanged() { } |
| | 110 | |
|
| 5744 | 111 | | public virtual void Dispose() { DataStore.i.screen.size.OnChange -= OnScreenSizeModified; } |
| | 112 | |
|
| 4 | 113 | | public void OnPointerEnter(PointerEventData eventData) { OnFocus(); } |
| | 114 | |
|
| 4 | 115 | | public void OnPointerExit(PointerEventData eventData) { OnLoseFocus(); } |
| | 116 | |
|
| 3632 | 117 | | private void OnDestroy() { Dispose(); } |
| | 118 | |
|
| | 119 | | internal void OnScreenSizeModified(Vector2Int current, Vector2Int previous) |
| | 120 | | { |
| 1 | 121 | | if (!gameObject.activeInHierarchy) |
| 0 | 122 | | return; |
| | 123 | |
|
| 1 | 124 | | StartCoroutine(RaiseOnScreenSizeChangedAfterDelay()); |
| 1 | 125 | | } |
| | 126 | |
|
| | 127 | | internal IEnumerator RaiseOnScreenSizeChangedAfterDelay() |
| | 128 | | { |
| 1 | 129 | | yield return null; |
| 0 | 130 | | OnScreenSizeChanged(); |
| 0 | 131 | | } |
| | 132 | |
|
| | 133 | | internal static T Create<T>(string resourceName) where T : BaseComponentView |
| | 134 | | { |
| 236 | 135 | | T buttonComponentView = Instantiate(Resources.Load<GameObject>(resourceName)).GetComponent<T>(); |
| 236 | 136 | | return buttonComponentView; |
| | 137 | | } |
| | 138 | | } |