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; } protected bool IsInitialized { get; private set; } protected readonly UniTaskCompletionSource WaitUntilInitialized = new(); 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(IsInitialized)return; var asset =await ModService.LoadAsset(DocumentPath); var root= RootVisualElement = UXService.Root.As().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); IsInitialized = true; WaitUntilInitialized.TrySetResult(); } public virtual void Dispose() { if(RootVisualElement is null)return; IsInitialized = false; RootVisualElement.RemoveFromHierarchy(); RootVisualElement = null; } } }