Files
riversong-code-showcase/Source/Riversong/Game/UI/Framework/UIControllerSystem.cs
2026-05-21 16:04:49 +02:00

54 lines
1.5 KiB
C#

using Cysharp.Threading.Tasks;
using UnityEngine.UIElements;
namespace DanieleMarotta.RiversongCodeShowcase
{
[GameSystemGroup(typeof(UISystemGroup))]
[InitializeAfter(typeof(UIInitializationSystem))]
public abstract class UIControllerSystem : GameSystem, IInitializable
{
protected UIControllerSystem(IServiceLocator serviceLocator) : base(serviceLocator)
{
}
[field: InjectService] protected UIService UIService { get; }
protected IUIRoot UIRoot => UIService.UIRoot;
public virtual UniTask InitializeAsync()
{
return UniTask.CompletedTask;
}
}
[GameSystemGroup(typeof(UISystemGroup))]
[InitializeAfter(typeof(UIInitializationSystem))]
public abstract class UIControllerSystem<T> : UIControllerSystem where T : UIView
{
private Button _closeButton;
protected UIControllerSystem(IServiceLocator serviceLocator) : base(serviceLocator)
{
}
protected abstract T View { get; }
public override async UniTask InitializeAsync()
{
await base.InitializeAsync();
_closeButton = View.RootElement.Q<Button>(className: "close-button");
_closeButton?.RegisterCallback<ClickEvent>(OnCloseButtonClick);
}
private void OnCloseButtonClick(ClickEvent evt)
{
CloseView();
}
protected virtual void CloseView(bool animate = true)
{
View.Show(false, animate);
}
}
}