BITKit/Src/Unity/Scripts/UX/Core/UXToolTips.cs

68 lines
1.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem;
using UnityEngine.UIElements;
namespace BITKit.UX
{
public class UXToolTips : MonoBehaviour
{
[SerializeField]
[ReadOnly]
private string currentHoverName;
private VisualElement root;
private Label label;
private void Start()
{
root = GetComponent<UIDocument>().rootVisualElement;
label = root.Q<Label>();
}
private void Update()
{
var tooltip = CurrentToolTip(root.panel);
if (tooltip != "")
{
var mouse = Mouse.current;
if (mouse is null) return;
var mousePos = mouse.position.ReadValue();
mousePos.y = Screen.height - mousePos.y;
var pos =RuntimePanelUtils.ScreenToPanel(label.panel,mousePos);
pos.x += 24;
if (pos.x + label.layout.width > label.panel.visualTree.layout.width)
{
//pos.x = label.panel.visualTree.layout.width - label.layout.width - label.layout.width;
pos.x-=label.layout.width+48;
}
label.visible = true;
label.text = tooltip;
label.transform.position = pos;
}
else
{
label.visible = false;
}
}
private string CurrentToolTip(IPanel panel)
{
// https://docs.unity3d.com/2022.2/Documentation/Manual/UIE-faq-event-and-input-system.html
if (!EventSystem.current.IsPointerOverGameObject()) return "";
var screenPosition = Mouse.current.position.ReadValue();
screenPosition.y = Screen.height - screenPosition.y;
VisualElement ve = panel.Pick(RuntimePanelUtils.ScreenToPanel(panel, screenPosition));
currentHoverName = ve?.name;
return ve == null ? "" : ve.tooltip;
}
}
}