1
This commit is contained in:
70
Src/Unity/Scripts/UX/UXBarService/UXBarService.cs
Normal file
70
Src/Unity/Scripts/UX/UXBarService/UXBarService.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
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, UXContainer> _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);
|
||||
}
|
||||
private UXContainer Create(int arg)
|
||||
{
|
||||
var element = new UXContainer(_container.Create(template));
|
||||
return element;
|
||||
}
|
||||
public void Remove(int id)
|
||||
{
|
||||
if (_bars.TryRemove(id, out var bar))
|
||||
{
|
||||
bar.visualElement.RemoveFromHierarchy();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user