using System; using Cysharp.Threading.Tasks; using UnityEngine.UIElements; namespace DanieleMarotta.RiversongCodeShowcase { public class RuntimeTooltipUIController : UIControllerSystem, IDisposable { private VisualElement _currentSource; public RuntimeTooltipUIController(IServiceLocator serviceLocator) : base(serviceLocator) { } protected override RuntimeTooltipUIView View => UIRoot.GetView(); public override async UniTask InitializeAsync() { await base.InitializeAsync(); View.Show(false); UIRoot.RootVisualElement.RegisterCallback(OnPointerEnter, TrickleDown.TrickleDown); UIRoot.RootVisualElement.RegisterCallback(OnPointerLeave, TrickleDown.TrickleDown); } public void Dispose() { UIRoot.RootVisualElement.UnregisterCallback(OnPointerEnter, TrickleDown.TrickleDown); UIRoot.RootVisualElement.UnregisterCallback(OnPointerLeave, TrickleDown.TrickleDown); } private void OnPointerEnter(PointerEnterEvent evt) { if (evt.target is not VisualElement element) return; var source = element.GetFirstAncestorOrSelf(static candidate => !string.IsNullOrEmpty(candidate.tooltip)); if (source == null || ReferenceEquals(source, _currentSource)) return; _currentSource = source; View.Show(true); View.AnchorTooltip(source, source.tooltip); } private void OnPointerLeave(PointerLeaveEvent evt) { if (!ReferenceEquals(evt.target, _currentSource)) return; _currentSource = null; View.Show(false); } } }