| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using DCL; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.UI; |
| | 6 | |
|
| | 7 | | public class CursorController : MonoBehaviour |
| | 8 | | { |
| | 9 | | private const float ALPHA_INTERPOLATION_DURATION = 0.1f; |
| | 10 | |
|
| 0 | 11 | | public static CursorController i { get; private set; } |
| | 12 | | public Image cursorImage; |
| | 13 | | public Sprite normalCursor; |
| | 14 | | public Sprite hoverCursor; |
| | 15 | | public CanvasGroup canvasGroup; |
| | 16 | |
|
| | 17 | | private Coroutine alphaRoutine; |
| | 18 | | private bool isAllUIHidden; |
| | 19 | |
|
| | 20 | | void Awake() |
| | 21 | | { |
| 597 | 22 | | i = this; |
| 597 | 23 | | DataStore_Cursor data = DataStore.i.Get<DataStore_Cursor>(); |
| 597 | 24 | | data.cursorVisible.OnChange += ChangeCursorVisible; |
| 597 | 25 | | data.cursorType.OnChange += OnChangeType; |
| 597 | 26 | | CommonScriptableObjects.allUIHidden.OnChange += AllUIVisible_OnChange; |
| 597 | 27 | | } |
| | 28 | |
|
| | 29 | | private void OnDestroy() |
| | 30 | | { |
| 597 | 31 | | DataStore_Cursor data = DataStore.i.Get<DataStore_Cursor>(); |
| 597 | 32 | | data.cursorVisible.OnChange -= ChangeCursorVisible; |
| 597 | 33 | | data.cursorType.OnChange -= OnChangeType; |
| 597 | 34 | | CommonScriptableObjects.allUIHidden.OnChange -= AllUIVisible_OnChange; |
| 597 | 35 | | } |
| | 36 | |
|
| | 37 | | private void OnChangeType(DataStore_Cursor.CursorType current, DataStore_Cursor.CursorType previous) |
| | 38 | | { |
| | 39 | | switch (current) |
| | 40 | | { |
| | 41 | | case DataStore_Cursor.CursorType.NORMAL: |
| 3 | 42 | | SetNormalCursor(); |
| 3 | 43 | | break; |
| | 44 | | case DataStore_Cursor.CursorType.HOVER: |
| 5 | 45 | | SetHoverCursor(); |
| | 46 | | break; |
| | 47 | | } |
| 5 | 48 | | } |
| | 49 | |
|
| | 50 | | private void AllUIVisible_OnChange(bool current, bool previous) |
| | 51 | | { |
| 14 | 52 | | isAllUIHidden = current; |
| | 53 | |
|
| 14 | 54 | | DataStore_Cursor data = DataStore.i.Get<DataStore_Cursor>(); |
| 14 | 55 | | ChangeCursorVisible(data.cursorVisible.Get(), false); |
| 14 | 56 | | } |
| | 57 | |
|
| | 58 | | private void ChangeCursorVisible(bool current, bool previous) |
| | 59 | | { |
| 51 | 60 | | if (current && !isAllUIHidden) |
| 1 | 61 | | Show(); |
| | 62 | | else |
| 50 | 63 | | Hide(); |
| 50 | 64 | | } |
| | 65 | |
|
| | 66 | | public void Show() |
| | 67 | | { |
| 1 | 68 | | if (cursorImage == null) return; |
| 2 | 69 | | if (cursorImage.gameObject.activeSelf) return; |
| | 70 | |
|
| 0 | 71 | | cursorImage.gameObject.SetActive(true); |
| | 72 | |
|
| 0 | 73 | | if (gameObject.activeSelf) |
| | 74 | | { |
| 0 | 75 | | if (alphaRoutine != null) StopCoroutine(alphaRoutine); |
| 0 | 76 | | alphaRoutine = StartCoroutine(SetAlpha(0f, 1f, ALPHA_INTERPOLATION_DURATION)); |
| | 77 | | } |
| 0 | 78 | | } |
| | 79 | |
|
| | 80 | | public void Hide() |
| | 81 | | { |
| 50 | 82 | | if (cursorImage == null) return; |
| 50 | 83 | | if (!cursorImage.gameObject.activeSelf) return; |
| | 84 | |
|
| 50 | 85 | | if (gameObject.activeSelf) |
| | 86 | | { |
| 50 | 87 | | if (alphaRoutine != null) StopCoroutine(alphaRoutine); |
| 50 | 88 | | alphaRoutine = StartCoroutine(SetAlpha(1f, 0f, ALPHA_INTERPOLATION_DURATION, |
| 4 | 89 | | () => cursorImage.gameObject.SetActive(false))); |
| 50 | 90 | | } |
| | 91 | | else |
| 0 | 92 | | cursorImage.gameObject.SetActive(false); |
| 0 | 93 | | } |
| | 94 | |
|
| | 95 | | public void SetNormalCursor() |
| | 96 | | { |
| 8 | 97 | | cursorImage.sprite = normalCursor; |
| 8 | 98 | | cursorImage.SetNativeSize(); |
| 8 | 99 | | } |
| | 100 | |
|
| | 101 | | public void SetHoverCursor() |
| | 102 | | { |
| 5 | 103 | | cursorImage.sprite = hoverCursor; |
| 5 | 104 | | cursorImage.SetNativeSize(); |
| 5 | 105 | | } |
| | 106 | |
|
| | 107 | | private IEnumerator SetAlpha(float from, float to, float duration, Action callback = null) |
| | 108 | | { |
| 50 | 109 | | var time = 0f; |
| | 110 | |
|
| 346 | 111 | | while (time < duration) |
| | 112 | | { |
| 342 | 113 | | time += Time.deltaTime; |
| 342 | 114 | | canvasGroup.alpha = Mathf.Lerp(from, to, time / duration); |
| 342 | 115 | | yield return null; |
| | 116 | | } |
| | 117 | |
|
| 4 | 118 | | canvasGroup.alpha = to; |
| 4 | 119 | | callback?.Invoke(); |
| 4 | 120 | | } |
| | 121 | | } |