1
This commit is contained in:
54
Src/Unity/Scripts/UX/Service/UI Toolkit/UIToolkitOverlay.cs
Normal file
54
Src/Unity/Scripts/UX/Service/UI Toolkit/UIToolkitOverlay.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using BITKit.Mod;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine.UIElements;
|
||||
// ReSharper disable MemberCanBePrivate.Global
|
||||
|
||||
namespace BITKit.UX
|
||||
{
|
||||
public abstract class UIToolkitOverlay:IDisposable
|
||||
{
|
||||
protected readonly CancellationTokenSource CancellationToken;
|
||||
protected readonly IUXService UXService;
|
||||
protected VisualElement RootVisualElement { get; set; }
|
||||
private bool _initialized;
|
||||
protected UIToolkitOverlay(IUXService uxService, CancellationTokenSource cancellationToken)
|
||||
{
|
||||
UXService = uxService;
|
||||
CancellationToken = cancellationToken;
|
||||
CancellationToken.Token.Register(Dispose);
|
||||
}
|
||||
|
||||
protected abstract string DocumentPath { get; }
|
||||
// ReSharper disable once MemberCanBeProtected.Global
|
||||
public virtual async UniTask InitializeAsync()
|
||||
{
|
||||
if(_initialized)return;
|
||||
var asset =await ModService.LoadAsset<VisualTreeAsset>(DocumentPath);
|
||||
var root= RootVisualElement = UXService.Root.As<VisualElement>().Create(asset);
|
||||
|
||||
RootVisualElement.BringToFront();
|
||||
|
||||
RootVisualElement.name = GetType().Name;
|
||||
RootVisualElement.pickingMode = PickingMode.Ignore;
|
||||
|
||||
root.style.position = Position.Absolute;
|
||||
root.style.top = 0;
|
||||
root.style.bottom = 0;
|
||||
root.style.left = 0;
|
||||
root.style.right = 0;
|
||||
|
||||
UXUtils.Inject(this);
|
||||
_initialized = true;
|
||||
}
|
||||
public virtual void Dispose()
|
||||
{
|
||||
if(RootVisualElement is null)return;
|
||||
_initialized = false;
|
||||
RootVisualElement.RemoveFromHierarchy();
|
||||
RootVisualElement = null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user