| | 1 | | using DCL; |
| | 2 | | using System; |
| | 3 | | using System.Collections; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | public interface IBaseComponentView : IDisposable |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// It will be triggered after the UI component is fully initialized (PostInitialization included). |
| | 10 | | /// </summary> |
| | 11 | | event Action OnFullyInitialized; |
| | 12 | |
|
| | 13 | | /// <summary> |
| | 14 | | /// Returns true if the UI component is already fully initialized (PostInitialization included). |
| | 15 | | /// </summary> |
| | 16 | | bool isFullyInitialized { get; } |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// It is called just after the UI component has been initialized. |
| | 20 | | /// </summary> |
| | 21 | | void PostInitialization(); |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// Shows the UI component. |
| | 25 | | /// </summary> |
| | 26 | | /// <param name="instant">True for not apply progressive animation.</param> |
| | 27 | | void Show(bool instant = false); |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// Hides the UI component. |
| | 31 | | /// </summary> |
| | 32 | | /// <param name="instant">True for not apply progressive animation.</param> |
| | 33 | | void Hide(bool instant = false); |
| | 34 | |
|
| | 35 | | /// <summary> |
| | 36 | | /// Updates the UI component with the current model. |
| | 37 | | /// </summary> |
| | 38 | | void RefreshControl(); |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// It is called just after the screen size has changed. |
| | 42 | | /// </summary> |
| | 43 | | void PostScreenSizeChanged(); |
| | 44 | | } |
| | 45 | |
|
| | 46 | | [RequireComponent(typeof(ShowHideAnimator))] |
| | 47 | | [RequireComponent(typeof(CanvasGroup))] |
| | 48 | | public abstract class BaseComponentView : MonoBehaviour, IBaseComponentView |
| | 49 | | { |
| | 50 | | public event Action OnFullyInitialized; |
| 0 | 51 | | public bool isFullyInitialized { get; private set; } |
| | 52 | |
|
| | 53 | | internal ShowHideAnimator showHideAnimator; |
| | 54 | |
|
| | 55 | | internal void Initialize() |
| | 56 | | { |
| 418 | 57 | | isFullyInitialized = false; |
| 418 | 58 | | showHideAnimator = GetComponent<ShowHideAnimator>(); |
| | 59 | |
|
| 418 | 60 | | DataStore.i.screen.size.OnChange += OnScreenSizeChange; |
| 418 | 61 | | } |
| | 62 | |
|
| | 63 | | public abstract void PostInitialization(); |
| | 64 | |
|
| 2 | 65 | | public virtual void Show(bool instant = false) { showHideAnimator.Show(instant); } |
| | 66 | |
|
| 2 | 67 | | public virtual void Hide(bool instant = false) { showHideAnimator.Hide(instant); } |
| | 68 | |
|
| | 69 | | public abstract void RefreshControl(); |
| | 70 | |
|
| 12 | 71 | | public virtual void PostScreenSizeChanged() { } |
| | 72 | |
|
| 960 | 73 | | public virtual void Dispose() { DataStore.i.screen.size.OnChange -= OnScreenSizeChange; } |
| | 74 | |
|
| 846 | 75 | | private void OnEnable() { OnScreenSizeChange(Vector2Int.zero, Vector2Int.zero); } |
| | 76 | |
|
| 834 | 77 | | private void Awake() { Initialize(); } |
| | 78 | |
|
| | 79 | | private void Start() |
| | 80 | | { |
| 225 | 81 | | PostInitialization(); |
| | 82 | |
|
| 225 | 83 | | isFullyInitialized = true; |
| 225 | 84 | | OnFullyInitialized?.Invoke(); |
| 8 | 85 | | } |
| | 86 | |
|
| 834 | 87 | | private void OnDestroy() { Dispose(); } |
| | 88 | |
|
| | 89 | | internal void OnScreenSizeChange(Vector2Int current, Vector2Int previous) |
| | 90 | | { |
| 423 | 91 | | if (!gameObject.activeInHierarchy) |
| 0 | 92 | | return; |
| | 93 | |
|
| 423 | 94 | | StartCoroutine(RefreshControlAfterScreenSize()); |
| 423 | 95 | | } |
| | 96 | |
|
| | 97 | | internal IEnumerator RefreshControlAfterScreenSize() |
| | 98 | | { |
| 423 | 99 | | yield return null; |
| 13 | 100 | | PostScreenSizeChanged(); |
| 13 | 101 | | } |
| | 102 | |
|
| | 103 | | internal static T Create<T>(string resourceName) where T : BaseComponentView |
| | 104 | | { |
| 72 | 105 | | T buttonComponentView = Instantiate(Resources.Load<GameObject>(resourceName)).GetComponent<T>(); |
| 72 | 106 | | return buttonComponentView; |
| | 107 | | } |
| | 108 | | } |