riversong code showcase
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
public class RuntimeTooltipUIController : UIControllerSystem<RuntimeTooltipUIView>, IDisposable
|
||||
{
|
||||
private VisualElement _currentSource;
|
||||
|
||||
public RuntimeTooltipUIController(IServiceLocator serviceLocator) : base(serviceLocator)
|
||||
{
|
||||
}
|
||||
|
||||
protected override RuntimeTooltipUIView View => UIRoot.GetView<RuntimeTooltipUIView>();
|
||||
|
||||
public override async UniTask InitializeAsync()
|
||||
{
|
||||
await base.InitializeAsync();
|
||||
|
||||
View.Show(false);
|
||||
|
||||
UIRoot.RootVisualElement.RegisterCallback<PointerEnterEvent>(OnPointerEnter, TrickleDown.TrickleDown);
|
||||
UIRoot.RootVisualElement.RegisterCallback<PointerLeaveEvent>(OnPointerLeave, TrickleDown.TrickleDown);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
UIRoot.RootVisualElement.UnregisterCallback<PointerEnterEvent>(OnPointerEnter, TrickleDown.TrickleDown);
|
||||
UIRoot.RootVisualElement.UnregisterCallback<PointerLeaveEvent>(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user