| | 1 | | using System.Collections; |
| | 2 | | using UnityEngine; |
| | 3 | | using UnityEngine.EventSystems; |
| | 4 | |
|
| | 5 | | public interface ITooltipController |
| | 6 | | { |
| | 7 | | void Initialize(ITooltipView view); |
| | 8 | | void Dispose(); |
| | 9 | | void SetTooltipText(string text); |
| | 10 | | void ShowTooltip(BaseEventData data); |
| | 11 | | void ShowTooltip(BaseEventData data, Vector3 offset); |
| | 12 | | void HideTooltip(); |
| | 13 | | } |
| | 14 | |
|
| | 15 | | public class TooltipController : ITooltipController |
| | 16 | | { |
| | 17 | | internal ITooltipView view; |
| | 18 | | internal Coroutine changeAlphaCoroutine; |
| | 19 | |
|
| | 20 | | public void Initialize(ITooltipView view) |
| | 21 | | { |
| 3 | 22 | | this.view = view; |
| | 23 | |
|
| 3 | 24 | | view.OnHideTooltip += HideTooltip; |
| 3 | 25 | | } |
| | 26 | |
|
| | 27 | | public void Dispose() |
| | 28 | | { |
| 3 | 29 | | KillTooltipCoroutine(); |
| | 30 | |
|
| 3 | 31 | | view.OnHideTooltip -= HideTooltip; |
| 3 | 32 | | } |
| | 33 | |
|
| 2 | 34 | | public void SetTooltipText(string text) { view.SetText(text); } |
| | 35 | |
|
| 2 | 36 | | public void ShowTooltip(BaseEventData data) { ShowTooltip(data, Vector3.zero); } |
| | 37 | |
|
| | 38 | | public void ShowTooltip(BaseEventData data, Vector3 offset) |
| | 39 | | { |
| 2 | 40 | | if (!(data is PointerEventData dataConverted)) |
| 0 | 41 | | return; |
| | 42 | |
|
| 2 | 43 | | RectTransform selectedRT = dataConverted.pointerEnter.GetComponent<RectTransform>(); |
| 2 | 44 | | view.SetTooltipPosition(offset + selectedRT.position); |
| | 45 | |
|
| 2 | 46 | | KillTooltipCoroutine(); |
| | 47 | |
|
| 2 | 48 | | changeAlphaCoroutine = CoroutineStarter.Start(ChangeAlpha(0, 1)); |
| 2 | 49 | | } |
| | 50 | |
|
| | 51 | | public void HideTooltip() |
| | 52 | | { |
| 0 | 53 | | KillTooltipCoroutine(); |
| 0 | 54 | | changeAlphaCoroutine = CoroutineStarter.Start(ChangeAlpha(1, 0)); |
| 0 | 55 | | } |
| | 56 | |
|
| | 57 | | internal IEnumerator ChangeAlpha(float from, float to) |
| | 58 | | { |
| 2 | 59 | | if (view.currentAlpha == to) |
| 0 | 60 | | yield break; |
| | 61 | |
|
| 2 | 62 | | view.SetTooltipAlpha(from); |
| | 63 | |
|
| 2 | 64 | | float currentAlpha = from; |
| 2 | 65 | | float destinationAlpha = to; |
| | 66 | |
|
| 2 | 67 | | float fractionOfJourney = 0; |
| 2 | 68 | | float speed = view.alphaTranstionSpeed; |
| 2 | 69 | | while (fractionOfJourney < 1) |
| | 70 | | { |
| 2 | 71 | | fractionOfJourney += Time.unscaledDeltaTime * speed; |
| 2 | 72 | | float lerpedAlpha = Mathf.Lerp(currentAlpha, destinationAlpha, fractionOfJourney); |
| 2 | 73 | | view.SetTooltipAlpha(lerpedAlpha); |
| 2 | 74 | | yield return null; |
| | 75 | | } |
| 0 | 76 | | changeAlphaCoroutine = null; |
| 0 | 77 | | } |
| | 78 | |
|
| | 79 | | internal void KillTooltipCoroutine() |
| | 80 | | { |
| 5 | 81 | | if (changeAlphaCoroutine != null) |
| 2 | 82 | | CoroutineStarter.Stop(changeAlphaCoroutine); |
| 5 | 83 | | } |
| | 84 | | } |