| | 1 | | using DCL; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using System; |
| | 4 | | using System.Collections; |
| | 5 | | using UnityEditor; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.EventSystems; |
| | 8 | |
|
| | 9 | | namespace UIComponents.Scripts.Components |
| | 10 | | { |
| | 11 | | public abstract class BaseComponentView<TModel> : BaseComponentView, IBaseComponentView<TModel> |
| | 12 | | where TModel: IEquatable<TModel>, new() |
| | 13 | | { |
| | 14 | | [field: SerializeField] |
| | 15 | | protected TModel model { get; private set; } = new (); |
| | 16 | |
|
| | 17 | | public void SetModel(TModel newModel) |
| | 18 | | { |
| | 19 | | if (!Equals(model, newModel)) |
| | 20 | | { |
| | 21 | | model = newModel; |
| | 22 | | RefreshControl(); |
| | 23 | | } |
| | 24 | | } |
| | 25 | | } |
| | 26 | | } |
| | 27 | |
|
| | 28 | | public interface IBaseComponentView : IPointerEnterHandler, IPointerExitHandler, IDisposable |
| | 29 | | { |
| | 30 | | /// <summary> |
| | 31 | | /// It will inform if the UI Component is currently visible or not. |
| | 32 | | /// </summary> |
| | 33 | | bool isVisible { get; } |
| | 34 | |
|
| | 35 | | /// <summary> |
| | 36 | | /// It will be triggered when UI Component is focused. |
| | 37 | | /// </summary> |
| | 38 | | event Action<bool> onFocused; |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// It will inform if the UI Component is focused or not. |
| | 42 | | /// </summary> |
| | 43 | | bool isFocused { get; } |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Updates the UI component with the current model configuration. |
| | 47 | | /// </summary> |
| | 48 | | void RefreshControl(); |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// Shows the UI component. |
| | 52 | | /// </summary> |
| | 53 | | /// <param name="instant">True for not apply progressive animation.</param> |
| | 54 | | void Show(bool instant = false); |
| | 55 | |
|
| | 56 | | /// <summary> |
| | 57 | | /// Hides the UI component. |
| | 58 | | /// </summary> |
| | 59 | | /// <param name="instant">True for not apply progressive animation.</param> |
| | 60 | | void Hide(bool instant = false); |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// It is called when the focus is set into the component. |
| | 64 | | /// </summary> |
| | 65 | | void OnFocus(); |
| | 66 | |
|
| | 67 | | /// <summary> |
| | 68 | | /// It is called when the focus is lost from the component. |
| | 69 | | /// </summary> |
| | 70 | | void OnLoseFocus(); |
| | 71 | | } |
| | 72 | |
|
| | 73 | | public interface IComponentModelConfig<T> where T: BaseComponentModel |
| | 74 | | { |
| | 75 | | /// <summary> |
| | 76 | | /// Fill the model and updates the component with this data. |
| | 77 | | /// </summary> |
| | 78 | | /// <param name="newModel">Data to configure the component.</param> |
| | 79 | | void Configure(T newModel); |
| | 80 | | } |
| | 81 | |
|
| | 82 | | public abstract class BaseComponentView : MonoBehaviour, IBaseComponentView |
| | 83 | | { |
| | 84 | | internal BaseComponentModel baseModel; |
| | 85 | | private bool isDestroyed; |
| | 86 | |
|
| | 87 | | public ShowHideAnimator showHideAnimator; |
| 584 | 88 | | public virtual bool isVisible { get; private set; } |
| 22862 | 89 | | public bool isFocused { get; private set; } |
| 107879 | 90 | | public bool isViewEnabled { get; set; } = true; |
| | 91 | | public event Action<bool> onFocused; |
| | 92 | |
|
| | 93 | | public virtual void Dispose() |
| | 94 | | { |
| 25712 | 95 | | DataStore.i.screen.size.OnChange -= OnScreenSizeModified; |
| | 96 | |
|
| 25712 | 97 | | if (!isDestroyed && gameObject) |
| 4863 | 98 | | Utils.SafeDestroy(gameObject); |
| 25712 | 99 | | } |
| | 100 | |
|
| | 101 | | public virtual void Awake() |
| | 102 | | { |
| 20986 | 103 | | showHideAnimator = GetComponent<ShowHideAnimator>(); |
| 20986 | 104 | | DataStore.i.screen.size.OnChange += OnScreenSizeModified; |
| 20986 | 105 | | } |
| | 106 | |
|
| | 107 | | public virtual void OnEnable() |
| | 108 | | { |
| 22448 | 109 | | if(!isViewEnabled) return; |
| | 110 | |
|
| 22448 | 111 | | StartCoroutine(RaiseOnScreenSizeChangedAfterDelay()); |
| 22448 | 112 | | } |
| | 113 | |
|
| | 114 | | public virtual void OnDisable() |
| | 115 | | { |
| 22818 | 116 | | if(!isViewEnabled) return; |
| | 117 | |
|
| 22818 | 118 | | OnLoseFocus(); |
| 22818 | 119 | | } |
| | 120 | |
|
| | 121 | | private void OnDestroy() |
| | 122 | | { |
| 21009 | 123 | | isDestroyed = true; |
| 21009 | 124 | | Dispose(); |
| 21006 | 125 | | } |
| | 126 | |
|
| | 127 | | public abstract void RefreshControl(); |
| | 128 | |
|
| | 129 | | public virtual void Show(bool instant = false) |
| | 130 | | { |
| 87 | 131 | | if (!isViewEnabled || !showHideAnimator) |
| 36 | 132 | | return; |
| | 133 | |
|
| 51 | 134 | | showHideAnimator.Show(instant); |
| 51 | 135 | | isVisible = true; |
| 51 | 136 | | } |
| | 137 | |
|
| | 138 | | public virtual void Hide(bool instant = false) |
| | 139 | | { |
| 553 | 140 | | if (!isViewEnabled || !showHideAnimator) |
| 45 | 141 | | return; |
| | 142 | |
|
| 508 | 143 | | showHideAnimator.Hide(instant); |
| 508 | 144 | | isVisible = false; |
| 508 | 145 | | } |
| | 146 | |
|
| | 147 | | public virtual void OnFocus() |
| | 148 | | { |
| 45 | 149 | | if(!isViewEnabled) return; |
| | 150 | |
|
| 45 | 151 | | isFocused = true; |
| 45 | 152 | | onFocused?.Invoke(true); |
| 2 | 153 | | } |
| | 154 | |
|
| | 155 | | public virtual void OnLoseFocus() |
| | 156 | | { |
| 22811 | 157 | | if(!isViewEnabled) return; |
| | 158 | |
|
| 22811 | 159 | | isFocused = false; |
| 22811 | 160 | | onFocused?.Invoke(false); |
| 184 | 161 | | } |
| | 162 | |
|
| 484 | 163 | | public virtual void OnScreenSizeChanged() { } |
| | 164 | |
|
| | 165 | | public virtual void OnPointerEnter(PointerEventData eventData) |
| | 166 | | { |
| 7 | 167 | | if(!isViewEnabled) return; |
| | 168 | |
|
| 7 | 169 | | OnFocus(); |
| 7 | 170 | | } |
| | 171 | |
|
| | 172 | | public virtual void OnPointerExit(PointerEventData eventData) |
| | 173 | | { |
| 7 | 174 | | if(!isViewEnabled) return; |
| | 175 | |
|
| 7 | 176 | | OnLoseFocus(); |
| 7 | 177 | | } |
| | 178 | |
|
| | 179 | | private void OnScreenSizeModified(Vector2Int current, Vector2Int previous) |
| | 180 | | { |
| 0 | 181 | | if (!isViewEnabled || !gameObject.activeInHierarchy) |
| 0 | 182 | | return; |
| | 183 | |
|
| 0 | 184 | | StartCoroutine(RaiseOnScreenSizeChangedAfterDelay()); |
| 0 | 185 | | } |
| | 186 | |
|
| | 187 | | private IEnumerator RaiseOnScreenSizeChangedAfterDelay() |
| | 188 | | { |
| 22448 | 189 | | yield return null; |
| | 190 | |
|
| 484 | 191 | | if(isViewEnabled) |
| 484 | 192 | | OnScreenSizeChanged(); |
| 484 | 193 | | } |
| | 194 | |
|
| | 195 | | public static T Create<T>(string resourceName) where T: BaseComponentView |
| | 196 | | { |
| 280 | 197 | | T buttonComponentView = Instantiate(Resources.Load<GameObject>(resourceName)).GetComponent<T>(); |
| 280 | 198 | | return buttonComponentView; |
| | 199 | | } |
| | 200 | |
|
| | 201 | | #if UNITY_EDITOR |
| | 202 | | public static T CreateUIComponentFromAssetDatabase<T>(string assetName) where T: BaseComponentView => |
| 193 | 203 | | Instantiate(AssetDatabase.LoadAssetAtPath<T>($"Assets/UIComponents/Prefabs/{assetName}.prefab")); |
| | 204 | | #endif |
| | 205 | | } |