TooltipTrigger.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.EventSystems;
  6. using Sirenix.OdinInspector;
  7. using KairoEngine.Core;
  8. namespace KairoEngine.UI.Tooltips
  9. {
  10. [HideMonoScript]
  11. public class TooltipTrigger : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
  12. {
  13. private static LTDescr delay;
  14. public string header;
  15. public string content;
  16. public string tooltipType = "";
  17. private bool visible = false;
  18. public void OnPointerEnter(PointerEventData eventData)
  19. {
  20. if(MouseInputUIBlocker.BlockedByUI) return;
  21. if(string.IsNullOrEmpty(header) && string.IsNullOrEmpty(content)) return;
  22. delay = LeanTween.delayedCall(TooltipSystem.GetDelay(), () =>
  23. {
  24. TooltipSystem.Show(content, header, tooltipType);
  25. visible = true;
  26. CheckTooltip();
  27. });
  28. }
  29. public void OnMouseEnter()
  30. {
  31. if(MouseInputUIBlocker.BlockedByUI) return;
  32. if(string.IsNullOrEmpty(header) && string.IsNullOrEmpty(content)) return;
  33. delay = LeanTween.delayedCall(TooltipSystem.GetDelay(), () =>
  34. {
  35. TooltipSystem.Show(content, header, tooltipType);
  36. visible = true;
  37. CheckTooltip();
  38. });
  39. }
  40. public void OnPointerExit(PointerEventData eventData) => DisableTooltip();
  41. public void OnMouseExit() => DisableTooltip();
  42. private void OnDisable() => DisableTooltip();
  43. private void OnDestroy() => DisableTooltip();
  44. public void DisableTooltip()
  45. {
  46. if(delay != null) LeanTween.cancel(delay.uniqueId);
  47. TooltipSystem.Hide();
  48. visible = false;
  49. }
  50. private void CheckTooltip()
  51. {
  52. TooltipTrigger t = this;
  53. if(t.visible == false) return;
  54. Timer.ExecuteRealTime(500, () => {
  55. if(t == null)
  56. {
  57. TooltipSystem.Hide();
  58. return;
  59. }
  60. if(t.gameObject == null)
  61. {
  62. t.visible = false;
  63. TooltipSystem.Hide();
  64. return;
  65. }
  66. if(!t.gameObject.activeSelf)
  67. {
  68. t.visible = false;
  69. TooltipSystem.Hide();
  70. return;
  71. }
  72. if(t.gameObject.activeSelf && t.visible == true)
  73. {
  74. CheckTooltip();
  75. return;
  76. }
  77. });
  78. }
  79. }
  80. }