120 lines
2.6 KiB
C#
120 lines
2.6 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Net.NetworkInformation;
|
|
using BITKit;
|
|
using BITKit.Entities.Variables;
|
|
using BITKit.Mod;
|
|
using BITKit.UX;
|
|
using Cysharp.Threading.Tasks;
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
|
|
namespace BITFactory.Cutting.UX
|
|
{
|
|
public class UXMaterialBoardVariable : MonoBehaviour
|
|
{
|
|
[SerializeReference, SubclassSelector] private IVariable _variable;
|
|
[SerializeReference, SubclassSelector] private IReference[] _initialVariables;
|
|
|
|
[SerializeField] private VisualTreeAsset materialBoardTemplate;
|
|
|
|
[UXBindPath("material-board-container")]
|
|
private VisualElement _materialBoardContainer;
|
|
|
|
private readonly ConcurrentDictionary<string, UXContainer> _containers = new();
|
|
private async void Start()
|
|
{
|
|
UXUtils.Inject(this);
|
|
|
|
_variable.OnVariableChanged += OnVariableChanged;
|
|
|
|
OnVariableChanged(null,_variable.Variable);
|
|
|
|
_materialBoardContainer.Clear();
|
|
|
|
foreach (var x in _initialVariables)
|
|
{
|
|
_containers.GetOrAdd(x.Value, Create);
|
|
}
|
|
|
|
await UniTask.NextFrame();
|
|
if (destroyCancellationToken.IsCancellationRequested) return;
|
|
|
|
OnModLoaded(null);
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
ModService.OnModLoaded += OnModLoaded;
|
|
ModService.OnModUnLoaded += OnModUnLoaded;
|
|
}
|
|
|
|
private void OnModUnLoaded(IMod obj)
|
|
{
|
|
Rebuild();
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
ModService.OnModLoaded -= OnModLoaded;
|
|
ModService.OnModUnLoaded -= OnModUnLoaded;
|
|
}
|
|
private void OnModLoaded(IMod obj)
|
|
{
|
|
Rebuild();
|
|
}
|
|
|
|
private async void Rebuild()
|
|
{
|
|
await UniTask.NextFrame();
|
|
await UniTask.SwitchToMainThread();
|
|
if (destroyCancellationToken.IsCancellationRequested) return;
|
|
foreach (var x in _containers.Values)
|
|
{
|
|
x.visualElement.RemoveFromHierarchy();
|
|
}
|
|
_containers.Clear();
|
|
foreach (var x in _variable.Variables)
|
|
{
|
|
_containers.GetOrAdd(x, Create);
|
|
}
|
|
}
|
|
|
|
private void OnVariableChanged(string arg1, string arg2)
|
|
{
|
|
_containers.GetOrAdd(arg2, Create);
|
|
|
|
foreach (var x in _materialBoardContainer.Children())
|
|
{
|
|
x.SetEnabled(x.userData!=arg2);
|
|
}
|
|
}
|
|
|
|
private UXContainer Create(string item)
|
|
{
|
|
BIT4Log.Log<UXMaterialBoardVariable>($"创建材质板:{item}");
|
|
var container = new UXContainer(_materialBoardContainer.Create(materialBoardTemplate))
|
|
{
|
|
contextLabel =
|
|
{
|
|
text = item
|
|
},
|
|
visualElement =
|
|
{
|
|
userData = item
|
|
}
|
|
};
|
|
container.button.clicked += Select;
|
|
return container;
|
|
|
|
void Select()
|
|
{
|
|
_variable.Variable = item;
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|