TooltipSystem.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using UnityEngine;
  2. using UnityEngine.UIElements;
  3. public class TooltipSystem : MonoBehaviour
  4. {
  5. [Header("UI References")]
  6. public UIDocument uiDocument;
  7. [Header("Debug")]
  8. public bool debugMode = true;
  9. private VisualElement tooltipContainer;
  10. private Label tooltipLabel;
  11. private static TooltipSystem instance;
  12. private bool isSetup = false;
  13. public static TooltipSystem Instance => instance; void Awake()
  14. {
  15. if (instance == null)
  16. {
  17. instance = this;
  18. DontDestroyOnLoad(gameObject);
  19. }
  20. else
  21. {
  22. Destroy(gameObject);
  23. }
  24. }
  25. void Start()
  26. {
  27. if (debugMode) Debug.Log("TooltipSystem Start() called");
  28. SetupTooltip();
  29. }
  30. void SetupTooltip()
  31. {
  32. if (debugMode) Debug.Log("Setting up tooltip system...");
  33. if (uiDocument == null)
  34. uiDocument = GetComponent<UIDocument>();
  35. if (uiDocument == null)
  36. {
  37. Debug.LogError("UIDocument component not found on TooltipSystem!");
  38. return;
  39. }
  40. if (debugMode) Debug.Log("UIDocument found, creating tooltip UI...");
  41. // Create tooltip UI programmatically
  42. var root = uiDocument.rootVisualElement;
  43. tooltipContainer = new VisualElement();
  44. tooltipContainer.name = "tooltip-container";
  45. tooltipContainer.style.position = Position.Absolute;
  46. tooltipContainer.style.backgroundColor = new Color(0, 0, 0, 0.8f);
  47. tooltipContainer.style.borderTopWidth = 2;
  48. tooltipContainer.style.borderBottomWidth = 2;
  49. tooltipContainer.style.borderLeftWidth = 2;
  50. tooltipContainer.style.borderRightWidth = 2;
  51. tooltipContainer.style.borderTopColor = Color.white;
  52. tooltipContainer.style.borderBottomColor = Color.white;
  53. tooltipContainer.style.borderLeftColor = Color.white;
  54. tooltipContainer.style.borderRightColor = Color.white;
  55. tooltipContainer.style.borderTopLeftRadius = 5;
  56. tooltipContainer.style.borderTopRightRadius = 5;
  57. tooltipContainer.style.borderBottomLeftRadius = 5;
  58. tooltipContainer.style.borderBottomRightRadius = 5;
  59. tooltipContainer.style.paddingTop = 8;
  60. tooltipContainer.style.paddingBottom = 8;
  61. tooltipContainer.style.paddingLeft = 12;
  62. tooltipContainer.style.paddingRight = 12;
  63. tooltipContainer.style.display = DisplayStyle.None;
  64. tooltipLabel = new Label();
  65. tooltipLabel.style.color = Color.white;
  66. tooltipLabel.style.fontSize = 14;
  67. tooltipLabel.style.whiteSpace = WhiteSpace.NoWrap;
  68. tooltipContainer.Add(tooltipLabel);
  69. root.Add(tooltipContainer);
  70. isSetup = true;
  71. if (debugMode) Debug.Log("Tooltip system setup complete!");
  72. }
  73. public void ShowTooltip(string text, Vector2 screenPosition)
  74. {
  75. if (!isSetup || tooltipContainer == null || tooltipLabel == null)
  76. {
  77. if (debugMode) Debug.LogWarning("Tooltip system not setup properly!");
  78. return;
  79. }
  80. tooltipLabel.text = text;
  81. tooltipContainer.style.display = DisplayStyle.Flex;
  82. // Position tooltip near mouse but keep it on screen
  83. var containerRect = tooltipContainer.parent.contentRect;
  84. float tooltipWidth = 200; // Estimated width
  85. float tooltipHeight = 30; // Estimated height
  86. float x = screenPosition.x + 10;
  87. float y = screenPosition.y - 30;
  88. // Keep tooltip on screen
  89. if (x + tooltipWidth > containerRect.width)
  90. x = screenPosition.x - tooltipWidth - 10;
  91. if (y < 0)
  92. y = screenPosition.y + 10;
  93. if (y + tooltipHeight > containerRect.height)
  94. y = containerRect.height - tooltipHeight;
  95. tooltipContainer.style.left = x;
  96. tooltipContainer.style.top = y;
  97. }
  98. public void HideTooltip()
  99. {
  100. if (tooltipContainer != null)
  101. {
  102. tooltipContainer.style.display = DisplayStyle.None;
  103. }
  104. }
  105. void Update()
  106. {
  107. // Update tooltip position if it's visible
  108. if (tooltipContainer != null && tooltipContainer.style.display == DisplayStyle.Flex)
  109. {
  110. Vector2 mousePos = Input.mousePosition;
  111. mousePos.y = Screen.height - mousePos.y; // Flip Y coordinate for UI
  112. ShowTooltip(tooltipLabel.text, mousePos);
  113. }
  114. }
  115. }