72 lines
1.9 KiB
C#
72 lines
1.9 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
|
|
namespace BITKit.UX
|
|
{
|
|
[Serializable]
|
|
public struct UXBarDictionary:IUXBarService
|
|
{
|
|
public static readonly ConcurrentDictionary<string, IUXBarService> BarDictionary =new();
|
|
[SerializeReference, SubclassSelector] private IReference path;
|
|
|
|
public void SetOrCreate(int id, string name, float value, object data = null)
|
|
{
|
|
BarDictionary[path.Value].SetOrCreate(id, name, value, data);
|
|
}
|
|
public void Remove(int id)
|
|
{
|
|
BarDictionary[path.Value].Remove(id);
|
|
}
|
|
}
|
|
public class UXBarService : MonoBehaviour, IUXBarService
|
|
{
|
|
[SerializeField] private UIDocument document;
|
|
[SerializeReference, SubclassSelector] private IReference path;
|
|
[SerializeReference, SubclassSelector] private IReference containerPath;
|
|
[SerializeField] private VisualTreeAsset template;
|
|
|
|
private VisualElement _container;
|
|
private readonly ConcurrentDictionary<int, VisualElement> _bars = new();
|
|
private void Start()
|
|
{
|
|
_container = document.rootVisualElement.Q<VisualElement>(containerPath.Value);
|
|
|
|
_container.Clear();
|
|
|
|
UXBarDictionary.BarDictionary.GetOrAdd(path.Value, this);
|
|
|
|
destroyCancellationToken.Register(() =>
|
|
{
|
|
UXBarDictionary.BarDictionary.TryRemove(path.Value, out _);
|
|
});
|
|
}
|
|
|
|
|
|
public void SetOrCreate(int id, string name, float value, object data = null)
|
|
{
|
|
var bar = _bars.GetOrAdd(id, Create);
|
|
var label = bar.Get<Label>(0);
|
|
if(label is not null)
|
|
label.text = name;
|
|
//bar.SetProcess(value);
|
|
bar.Get<ProgressBar>().value = value;
|
|
}
|
|
private VisualElement Create(int arg)
|
|
{
|
|
var element = _container.Create(template);
|
|
return element;
|
|
}
|
|
public void Remove(int id)
|
|
{
|
|
if (_bars.TryRemove(id, out var bar))
|
|
{
|
|
bar.RemoveFromHierarchy();
|
|
}
|
|
}
|
|
}
|
|
}
|