| | 1 | | using DCL; |
| | 2 | | using System; |
| | 3 | | using System.Collections; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.EventSystems; |
| | 6 | |
|
| | 7 | | namespace UIComponents.Scripts.Components |
| | 8 | | { |
| | 9 | | public abstract class BaseComponentView<TModel> : BaseComponentView, IBaseComponentView<TModel> where TModel : IEqua |
| | 10 | | { |
| | 11 | | [field: SerializeField] |
| | 12 | | protected TModel model { get; private set; } |
| | 13 | |
|
| | 14 | | public void SetModel(TModel newModel) |
| | 15 | | { |
| | 16 | | if (!Equals(model, newModel)) |
| | 17 | | { |
| | 18 | | model = newModel; |
| | 19 | | RefreshControl(); |
| | 20 | | } |
| | 21 | | } |
| | 22 | | } |
| | 23 | | } |
| | 24 | |
|
| | 25 | | public interface IBaseComponentView : IPointerEnterHandler, IPointerExitHandler, IDisposable |
| | 26 | | { |
| | 27 | | /// <summary> |
| | 28 | | /// It will inform if the UI Component is currently visible or not. |
| | 29 | | /// </summary> |
| | 30 | | bool isVisible { get; } |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// It will be triggered when UI Component is focused. |
| | 34 | | /// </summary> |
| | 35 | | event Action<bool> onFocused; |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// It will inform if the UI Component is focused or not. |
| | 39 | | /// </summary> |
| | 40 | | bool isFocused { get; } |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// Updates the UI component with the current model configuration. |
| | 44 | | /// </summary> |
| | 45 | | void RefreshControl(); |
| | 46 | |
|
| | 47 | | /// <summary> |
| | 48 | | /// Shows the UI component. |
| | 49 | | /// </summary> |
| | 50 | | /// <param name="instant">True for not apply progressive animation.</param> |
| | 51 | | void Show(bool instant = false); |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// Hides the UI component. |
| | 55 | | /// </summary> |
| | 56 | | /// <param name="instant">True for not apply progressive animation.</param> |
| | 57 | | void Hide(bool instant = false); |
| | 58 | |
|
| | 59 | | /// <summary> |
| | 60 | | /// It is called when the focus is set into the component. |
| | 61 | | /// </summary> |
| | 62 | | void OnFocus(); |
| | 63 | |
|
| | 64 | | /// <summary> |
| | 65 | | /// It is called when the focus is lost from the component. |
| | 66 | | /// </summary> |
| | 67 | | void OnLoseFocus(); |
| | 68 | | } |
| | 69 | |
|
| | 70 | | public interface IComponentModelConfig<T> where T : BaseComponentModel |
| | 71 | | { |
| | 72 | | /// <summary> |
| | 73 | | /// Fill the model and updates the component with this data. |
| | 74 | | /// </summary> |
| | 75 | | /// <param name="newModel">Data to configure the component.</param> |
| | 76 | | void Configure(T newModel); |
| | 77 | | } |
| | 78 | |
|
| | 79 | | public abstract class BaseComponentView : MonoBehaviour, IBaseComponentView |
| | 80 | | { |
| | 81 | | internal BaseComponentModel baseModel; |
| | 82 | | public ShowHideAnimator showHideAnimator; |
| | 83 | |
|
| 389 | 84 | | public virtual bool isVisible { get; private set; } |
| | 85 | | protected bool isDestroyed = false; |
| | 86 | |
|
| | 87 | | public event Action<bool> onFocused; |
| 21865 | 88 | | public bool isFocused { get; private set; } |
| | 89 | |
|
| | 90 | | public virtual void Awake() |
| | 91 | | { |
| 13888 | 92 | | showHideAnimator = GetComponent<ShowHideAnimator>(); |
| 13888 | 93 | | DataStore.i.screen.size.OnChange += OnScreenSizeModified; |
| 13888 | 94 | | } |
| | 95 | |
|
| 42848 | 96 | | public virtual void OnEnable() { StartCoroutine(RaiseOnScreenSizeChangedAfterDelay()); } |
| | 97 | |
|
| 43546 | 98 | | public virtual void OnDisable() { OnLoseFocus(); } |
| | 99 | |
|
| 3071 | 100 | | public virtual void Start() { } |
| | 101 | |
|
| 165777 | 102 | | public virtual void Update() { } |
| | 103 | |
|
| | 104 | | public abstract void RefreshControl(); |
| | 105 | |
|
| | 106 | | public virtual void Show(bool instant = false) |
| | 107 | | { |
| 123 | 108 | | if (showHideAnimator == null) |
| 37 | 109 | | return; |
| | 110 | |
|
| 86 | 111 | | showHideAnimator.Show(instant); |
| 86 | 112 | | isVisible = true; |
| 86 | 113 | | } |
| | 114 | |
|
| | 115 | | public virtual void Hide(bool instant = false) |
| | 116 | | { |
| 329 | 117 | | if (showHideAnimator == null) |
| 41 | 118 | | return; |
| | 119 | |
|
| 288 | 120 | | showHideAnimator.Hide(instant); |
| 288 | 121 | | isVisible = false; |
| 288 | 122 | | } |
| | 123 | |
|
| | 124 | | public virtual void OnFocus() |
| | 125 | | { |
| 43 | 126 | | isFocused = true; |
| 43 | 127 | | onFocused?.Invoke(true); |
| 2 | 128 | | } |
| | 129 | |
|
| | 130 | | public virtual void OnLoseFocus() |
| | 131 | | { |
| 21816 | 132 | | isFocused = false; |
| 21816 | 133 | | onFocused?.Invoke(false); |
| 110 | 134 | | } |
| | 135 | |
|
| 471 | 136 | | public virtual void OnScreenSizeChanged() { } |
| | 137 | |
|
| | 138 | | public virtual void Dispose() |
| | 139 | | { |
| 19633 | 140 | | DataStore.i.screen.size.OnChange -= OnScreenSizeModified; |
| | 141 | |
|
| 19633 | 142 | | if (!isDestroyed) |
| 5877 | 143 | | Destroy(gameObject); |
| 19633 | 144 | | } |
| | 145 | |
|
| 12 | 146 | | public virtual void OnPointerEnter(PointerEventData eventData) { OnFocus(); } |
| | 147 | |
|
| 12 | 148 | | public virtual void OnPointerExit(PointerEventData eventData) { OnLoseFocus(); } |
| | 149 | |
|
| | 150 | | private void OnDestroy() |
| | 151 | | { |
| 13753 | 152 | | isDestroyed = true; |
| 13753 | 153 | | Dispose(); |
| 13753 | 154 | | } |
| | 155 | |
|
| | 156 | | internal void OnScreenSizeModified(Vector2Int current, Vector2Int previous) |
| | 157 | | { |
| 0 | 158 | | if (!gameObject.activeInHierarchy) |
| 0 | 159 | | return; |
| | 160 | |
|
| 0 | 161 | | StartCoroutine(RaiseOnScreenSizeChangedAfterDelay()); |
| 0 | 162 | | } |
| | 163 | |
|
| | 164 | | internal IEnumerator RaiseOnScreenSizeChangedAfterDelay() |
| | 165 | | { |
| 21424 | 166 | | yield return null; |
| 444 | 167 | | OnScreenSizeChanged(); |
| 444 | 168 | | } |
| | 169 | |
|
| | 170 | | public static T Create<T>(string resourceName) where T : BaseComponentView |
| | 171 | | { |
| 571 | 172 | | T buttonComponentView = Instantiate(Resources.Load<GameObject>(resourceName)).GetComponent<T>(); |
| 571 | 173 | | return buttonComponentView; |
| | 174 | | } |
| | 175 | | } |