| | 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 | | /// <summary> |
| | 10 | | /// It will inform if the UI Component is currently visible or not. |
| | 11 | | /// </summary> |
| | 12 | | bool isVisible { get; } |
| | 13 | |
|
| | 14 | | /// <summary> |
| | 15 | | /// It will be triggered when UI Component is focused. |
| | 16 | | /// </summary> |
| | 17 | | event Action<bool> onFocused; |
| | 18 | |
|
| | 19 | | /// <summary> |
| | 20 | | /// It will inform if the UI Component is focused or not. |
| | 21 | | /// </summary> |
| | 22 | | bool isFocused { get; } |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// It is called at the beginning of the UI component lifecycle. |
| | 26 | | /// </summary> |
| | 27 | | void Awake(); |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// It is called each time the component is enabled. |
| | 31 | | /// </summary> |
| | 32 | | void OnEnable(); |
| | 33 | |
|
| | 34 | | /// <summary> |
| | 35 | | /// It is called each time the component is disabled. |
| | 36 | | /// </summary> |
| | 37 | | void OnDisable(); |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// It is called just after the UI component has been initialized. |
| | 41 | | /// </summary> |
| | 42 | | void Start(); |
| | 43 | |
|
| | 44 | | /// <summary> |
| | 45 | | /// It is called once per frame. |
| | 46 | | /// </summary> |
| | 47 | | void Update(); |
| | 48 | |
|
| | 49 | | /// <summary> |
| | 50 | | /// Updates the UI component with the current model configuration. |
| | 51 | | /// </summary> |
| | 52 | | void RefreshControl(); |
| | 53 | |
|
| | 54 | | /// <summary> |
| | 55 | | /// Shows the UI component. |
| | 56 | | /// </summary> |
| | 57 | | /// <param name="instant">True for not apply progressive animation.</param> |
| | 58 | | void Show(bool instant = false); |
| | 59 | |
|
| | 60 | | /// <summary> |
| | 61 | | /// Hides the UI component. |
| | 62 | | /// </summary> |
| | 63 | | /// <param name="instant">True for not apply progressive animation.</param> |
| | 64 | | void Hide(bool instant = false); |
| | 65 | |
|
| | 66 | | /// <summary> |
| | 67 | | /// It is called when the focus is set into the component. |
| | 68 | | /// </summary> |
| | 69 | | void OnFocus(); |
| | 70 | |
|
| | 71 | | /// <summary> |
| | 72 | | /// It is called when the focus is lost from the component. |
| | 73 | | /// </summary> |
| | 74 | | void OnLoseFocus(); |
| | 75 | |
|
| | 76 | | /// <summary> |
| | 77 | | /// It is called just after the screen size has changed. |
| | 78 | | /// </summary> |
| | 79 | | void OnScreenSizeChanged(); |
| | 80 | | } |
| | 81 | |
|
| | 82 | | public interface IComponentModelConfig |
| | 83 | | { |
| | 84 | | /// <summary> |
| | 85 | | /// Fill the model and updates the component with this data. |
| | 86 | | /// </summary> |
| | 87 | | /// <param name="newModel">Data to configure the component.</param> |
| | 88 | | void Configure(BaseComponentModel newModel); |
| | 89 | | } |
| | 90 | |
|
| | 91 | | public abstract class BaseComponentView : MonoBehaviour, IBaseComponentView |
| | 92 | | { |
| | 93 | | internal BaseComponentModel baseModel; |
| | 94 | | internal ShowHideAnimator showHideAnimator; |
| | 95 | |
|
| 0 | 96 | | public bool isVisible { get; private set; } |
| | 97 | | private bool isDestroyed = false; |
| | 98 | |
|
| | 99 | | public event Action<bool> onFocused; |
| 0 | 100 | | public bool isFocused { get; private set; } |
| | 101 | |
|
| | 102 | | public virtual void Awake() |
| | 103 | | { |
| 3120 | 104 | | showHideAnimator = GetComponent<ShowHideAnimator>(); |
| 3120 | 105 | | DataStore.i.screen.size.OnChange += OnScreenSizeModified; |
| 3120 | 106 | | } |
| | 107 | |
|
| 7104 | 108 | | public virtual void OnEnable() { StartCoroutine(RaiseOnScreenSizeChangedAfterDelay()); } |
| | 109 | |
|
| 7236 | 110 | | public virtual void OnDisable() { OnLoseFocus(); } |
| | 111 | |
|
| 888 | 112 | | public virtual void Start() { } |
| | 113 | |
|
| 2551 | 114 | | public virtual void Update() { } |
| | 115 | |
|
| | 116 | | public abstract void RefreshControl(); |
| | 117 | |
|
| | 118 | | public virtual void Show(bool instant = false) |
| | 119 | | { |
| 61 | 120 | | if (showHideAnimator == null) |
| 20 | 121 | | return; |
| | 122 | |
|
| 41 | 123 | | showHideAnimator.Show(instant); |
| 41 | 124 | | isVisible = true; |
| 41 | 125 | | } |
| | 126 | |
|
| | 127 | | public virtual void Hide(bool instant = false) |
| | 128 | | { |
| 158 | 129 | | if (showHideAnimator == null) |
| 20 | 130 | | return; |
| | 131 | |
|
| 138 | 132 | | showHideAnimator.Hide(instant); |
| 138 | 133 | | isVisible = false; |
| 138 | 134 | | } |
| | 135 | |
|
| | 136 | | public virtual void OnFocus() |
| | 137 | | { |
| 34 | 138 | | isFocused = true; |
| 34 | 139 | | onFocused?.Invoke(true); |
| 0 | 140 | | } |
| | 141 | |
|
| | 142 | | public virtual void OnLoseFocus() |
| | 143 | | { |
| 3652 | 144 | | isFocused = false; |
| 3652 | 145 | | onFocused?.Invoke(false); |
| 2 | 146 | | } |
| | 147 | |
|
| 29 | 148 | | public virtual void OnScreenSizeChanged() { } |
| | 149 | |
|
| | 150 | | public virtual void Dispose() |
| | 151 | | { |
| 4918 | 152 | | DataStore.i.screen.size.OnChange -= OnScreenSizeModified; |
| 4918 | 153 | | if (!isDestroyed) |
| 1798 | 154 | | Destroy(gameObject); |
| 4918 | 155 | | } |
| | 156 | |
|
| 8 | 157 | | public void OnPointerEnter(PointerEventData eventData) { OnFocus(); } |
| | 158 | |
|
| 8 | 159 | | public void OnPointerExit(PointerEventData eventData) { OnLoseFocus(); } |
| | 160 | |
|
| | 161 | | private void OnDestroy() |
| | 162 | | { |
| 3120 | 163 | | isDestroyed = true; |
| 3120 | 164 | | Dispose(); |
| 3120 | 165 | | } |
| | 166 | |
|
| | 167 | | internal void OnScreenSizeModified(Vector2Int current, Vector2Int previous) |
| | 168 | | { |
| 1 | 169 | | if (!gameObject.activeInHierarchy) |
| 0 | 170 | | return; |
| | 171 | |
|
| 1 | 172 | | StartCoroutine(RaiseOnScreenSizeChangedAfterDelay()); |
| 1 | 173 | | } |
| | 174 | |
|
| | 175 | | internal IEnumerator RaiseOnScreenSizeChangedAfterDelay() |
| | 176 | | { |
| 3553 | 177 | | yield return null; |
| 33 | 178 | | OnScreenSizeChanged(); |
| 33 | 179 | | } |
| | 180 | |
|
| | 181 | | internal static T Create<T>(string resourceName) where T : BaseComponentView |
| | 182 | | { |
| 285 | 183 | | T buttonComponentView = Instantiate(Resources.Load<GameObject>(resourceName)).GetComponent<T>(); |
| 285 | 184 | | return buttonComponentView; |
| | 185 | | } |
| | 186 | | } |