110 lines
3.2 KiB
C#
110 lines
3.2 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.InputSystem;
|
|
using UnityEngine.UIElements;
|
|
|
|
namespace BITKit.UX
|
|
{
|
|
public class UXToolTips:IDisposable
|
|
{
|
|
public static VisualElement Hovering;
|
|
|
|
private readonly IUXService _uxService;
|
|
private readonly IMainTicker _ticker;
|
|
|
|
public UXToolTips(IUXService uxService, IMainTicker ticker)
|
|
{
|
|
_uxService = uxService;
|
|
_ticker = ticker;
|
|
_ticker.Add(OnTick);
|
|
}
|
|
private VisualElement _rootVisualElement;
|
|
private Label _label;
|
|
|
|
public void Dispose()
|
|
{
|
|
_ticker.Remove(OnTick);
|
|
}
|
|
private void OnTick(float obj)
|
|
{
|
|
if (_label is null && _uxService.Root is not null)
|
|
{
|
|
_rootVisualElement=_uxService.Root as VisualElement;
|
|
|
|
_label = _rootVisualElement.Create<Label>();
|
|
|
|
_label.AddToClassList("bitkit-tool-tips");
|
|
|
|
_label.style.position = Position.Absolute;
|
|
|
|
|
|
}
|
|
|
|
if (_label is null || _rootVisualElement is null)
|
|
{
|
|
Hovering = null;
|
|
return;
|
|
}
|
|
var tooltip = CurrentToolTip(_rootVisualElement.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;
|
|
_label.BringToFront();
|
|
}
|
|
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())
|
|
{
|
|
Hovering = null;
|
|
return "";
|
|
}
|
|
|
|
var screenPosition = Vector2.zero;
|
|
|
|
if (Mouse.current is not null)
|
|
{
|
|
screenPosition= Mouse.current.position.ReadValue();
|
|
}
|
|
|
|
if (Touchscreen.current is not null)
|
|
{
|
|
screenPosition = Touchscreen.current.position.ReadValue();
|
|
}
|
|
|
|
screenPosition.y = Screen.height - screenPosition.y;
|
|
|
|
VisualElement ve = panel.Pick(RuntimePanelUtils.ScreenToPanel(panel, screenPosition));
|
|
|
|
Hovering = ve;
|
|
|
|
return ve == null ? "" : ve.tooltip;
|
|
}
|
|
}
|
|
}
|
|
|