1
This commit is contained in:
62
Src/Unity/Scripts/UX/Core/UXToolTips.cs
Normal file
62
Src/Unity/Scripts/UX/Core/UXToolTips.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
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
|
||||
{
|
||||
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 = Input.mousePosition;
|
||||
screenPosition.y = Screen.height - screenPosition.y;
|
||||
|
||||
VisualElement ve = panel.Pick(RuntimePanelUtils.ScreenToPanel(panel, screenPosition));
|
||||
return ve == null ? "" : ve.tooltip;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user