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 OnPointerExit(PointerEventData eventData)
  30. {
  31. if(delay != null) LeanTween.cancel(delay.uniqueId);
  32. TooltipSystem.Hide();
  33. visible = false;
  34. }
  35. public void OnMouseEnter()
  36. {
  37. if(MouseInputUIBlocker.BlockedByUI) return;
  38. if(string.IsNullOrEmpty(header) && string.IsNullOrEmpty(content)) return;
  39. delay = LeanTween.delayedCall(TooltipSystem.GetDelay(), () =>
  40. {
  41. TooltipSystem.Show(content, header, tooltipType);
  42. visible = true;
  43. CheckTooltip();
  44. });
  45. }
  46. public void OnMouseExit()
  47. {
  48. if(delay != null) LeanTween.cancel(delay.uniqueId);
  49. TooltipSystem.Hide();
  50. visible = false;
  51. }
  52. private void CheckTooltip()
  53. {
  54. TooltipTrigger t = this;
  55. if(t.visible == false) return;
  56. Timer.ExecuteRealTime(500, () => {
  57. if(t == null)
  58. {
  59. TooltipSystem.Hide();
  60. return;
  61. }
  62. if(t.gameObject == null)
  63. {
  64. t.visible = false;
  65. TooltipSystem.Hide();
  66. return;
  67. }
  68. if(!t.gameObject.activeSelf)
  69. {
  70. t.visible = false;
  71. TooltipSystem.Hide();
  72. return;
  73. }
  74. if(t.gameObject.activeSelf && t.visible == true)
  75. {
  76. CheckTooltip();
  77. return;
  78. }
  79. });
  80. }
  81. }
  82. }