1
This commit is contained in:
@@ -15,10 +15,21 @@ using UnityEngine.UIElements;
|
||||
|
||||
namespace BITKit.UX
|
||||
{
|
||||
public class UXModService : MonoBehaviour
|
||||
public class UXModService:UIToolKitPanel,IDisposable
|
||||
{
|
||||
[SerializeField] private UIDocument document;
|
||||
[SerializeField] private VisualTreeAsset modTemplate;
|
||||
protected override string DocumentPath => "ux_mod_Service";
|
||||
private const string TemplatePath = "ux_mod_service_template";
|
||||
public override bool AllowCursor => true;
|
||||
|
||||
public UXModService(IUXService uxService) : base(uxService)
|
||||
{
|
||||
ModService.OnModInstalled+=OnModInstalled;
|
||||
ModService.OnModUnInstalled+=OnModUnInstalled;
|
||||
ModService.OnModLoaded+=OnModLoaded;
|
||||
ModService.OnModUnLoaded+=OnModUnLoaded;
|
||||
ModService.OnLocked+=OnLocked;
|
||||
}
|
||||
private VisualTreeAsset _modTemplate;
|
||||
|
||||
[UXBindPath("open-mod-button")]
|
||||
private Button _openModButton;
|
||||
@@ -31,33 +42,18 @@ namespace BITKit.UX
|
||||
[UXBindPath("mod-description-label")]
|
||||
private Label _modDescriptionLabel;
|
||||
[UXBindPath("reload-mod-button",true)]
|
||||
private Button reloadModButton;
|
||||
private Button _reloadModButton;
|
||||
|
||||
private readonly ConcurrentDictionary<string,VisualElement> _modContainers=new();
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
ModService.OnModInstalled+=OnModInstalled;
|
||||
ModService.OnModUnInstalled+=OnModUnInstalled;
|
||||
ModService.OnModLoaded+=OnModLoaded;
|
||||
ModService.OnModUnLoaded+=OnModUnLoaded;
|
||||
ModService.OnLocked+=OnLocked;
|
||||
}
|
||||
private void OnDisable()
|
||||
{
|
||||
ModService.OnModInstalled-=OnModInstalled;
|
||||
ModService.OnModUnInstalled-=OnModUnInstalled;
|
||||
ModService.OnModLoaded-=OnModLoaded;
|
||||
ModService.OnModUnLoaded-=OnModUnLoaded;
|
||||
ModService.OnLocked-=OnLocked;
|
||||
}
|
||||
|
||||
private void OnLocked(bool obj)
|
||||
{
|
||||
document.rootVisualElement.SetEnabled(!obj);
|
||||
RootVisualElement?.SetEnabled(!obj);
|
||||
}
|
||||
|
||||
private void Start()
|
||||
public override async UniTask EntryAsync()
|
||||
{
|
||||
await base.EntryAsync();
|
||||
_modTemplate =await ModService.LoadAsset<VisualTreeAsset>(TemplatePath);
|
||||
UXUtils.Inject(this);
|
||||
if (_openModButton is not null)
|
||||
{
|
||||
@@ -66,7 +62,7 @@ namespace BITKit.UX
|
||||
|
||||
if (_returnButton is not null)
|
||||
{
|
||||
_returnButton.clicked += UxService.Return;
|
||||
_returnButton.clicked += UXService.Return;
|
||||
}
|
||||
|
||||
_modsContainer.Clear();
|
||||
@@ -75,24 +71,18 @@ namespace BITKit.UX
|
||||
OnModInstalled(x);
|
||||
}
|
||||
|
||||
if (reloadModButton is not null)
|
||||
reloadModButton.clicked += async () =>
|
||||
if (_reloadModButton is not null)
|
||||
_reloadModButton.clicked += async () =>
|
||||
{
|
||||
reloadModButton.SetEnabled(false);
|
||||
_reloadModButton.SetEnabled(false);
|
||||
await ModService.Reload();
|
||||
if (destroyCancellationToken.IsCancellationRequested)
|
||||
return;
|
||||
await UniTask.SwitchToMainThread();
|
||||
if (destroyCancellationToken.IsCancellationRequested)
|
||||
return;
|
||||
reloadModButton.SetEnabled(true);
|
||||
_reloadModButton.SetEnabled(true);
|
||||
};
|
||||
}
|
||||
|
||||
private async void OnModUnInstalled(IMod obj)
|
||||
{
|
||||
await UniTask.SwitchToMainThread();
|
||||
if (destroyCancellationToken.IsCancellationRequested) return;
|
||||
_modContainers.TryRemove(obj.Name, out var container);
|
||||
container.RemoveFromHierarchy();
|
||||
}
|
||||
@@ -100,7 +90,6 @@ namespace BITKit.UX
|
||||
private async void OnModInstalled(IMod obj)
|
||||
{
|
||||
await UniTask.SwitchToMainThread();
|
||||
if (destroyCancellationToken.IsCancellationRequested) return;
|
||||
var container = _modContainers.GetOrAdd(obj.Name,_=> Create(obj));
|
||||
container.RegisterCallback<MouseDownEvent>(x =>
|
||||
{
|
||||
@@ -115,7 +104,6 @@ namespace BITKit.UX
|
||||
private async void OnModUnLoaded(IMod obj)
|
||||
{
|
||||
await UniTask.SwitchToMainThread();
|
||||
if (destroyCancellationToken.IsCancellationRequested) return;
|
||||
//var container = _modContainers.GetOrAdd(obj.Name,_=> Create(obj));
|
||||
if(_modContainers.TryGetValue(obj.Name,out var container))
|
||||
{
|
||||
@@ -126,7 +114,6 @@ namespace BITKit.UX
|
||||
private async void OnModLoaded(IMod obj)
|
||||
{
|
||||
await UniTask.SwitchToMainThread();
|
||||
if (destroyCancellationToken.IsCancellationRequested) return;
|
||||
var container = _modContainers.GetOrAdd(obj.Name,_=> Create(obj));
|
||||
container.Get<Toggle>().SetValueWithoutNotify(true);
|
||||
}
|
||||
@@ -204,7 +191,7 @@ namespace BITKit.UX
|
||||
}
|
||||
private VisualElement Create(IMod mod)
|
||||
{
|
||||
var container =_modsContainer.Create(modTemplate);
|
||||
var container =_modsContainer.Create(_modTemplate);
|
||||
container.Get<Toggle>().RegisterValueChangedCallback(evt =>
|
||||
{
|
||||
if (evt.newValue)
|
||||
@@ -225,6 +212,15 @@ namespace BITKit.UX
|
||||
};
|
||||
return container;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
ModService.OnModInstalled-=OnModInstalled;
|
||||
ModService.OnModUnInstalled-=OnModUnInstalled;
|
||||
ModService.OnModLoaded-=OnModLoaded;
|
||||
ModService.OnModUnLoaded-=OnModUnLoaded;
|
||||
ModService.OnLocked-=OnLocked;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user