更新Readme
和CPS验证场景
This commit is contained in:
53
Artists/Scripts/UX/UXMetaElement.cs
Normal file
53
Artists/Scripts/UX/UXMetaElement.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using System.Linq;
|
||||
using Godot;
|
||||
|
||||
namespace BITKit;
|
||||
|
||||
public partial class UXMetaElement : Node3D, IMetaDisplayElement
|
||||
{
|
||||
[Export] private bool isEnabled=true;
|
||||
[Export] private Node3D proxy;
|
||||
[Export] protected string[] MetaEntries;
|
||||
private bool _registered;
|
||||
|
||||
string IMetaDisplayElement.Text
|
||||
{
|
||||
get
|
||||
{
|
||||
var node = proxy ?? this;
|
||||
return string.Join(MetaEntries?.Length>2 ? "\n" : ":", MetaEntries!
|
||||
.Where(x => string.IsNullOrEmpty(x) is false)
|
||||
.Where(x => node.HasMeta(x))
|
||||
.Select(x => node.GetMeta(x).AsString())
|
||||
.InsertOf<string>(0, node.Name)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Vector3 IMetaDisplayElement.Position => (proxy ?? this).GlobalPosition;
|
||||
// public override void _Ready()
|
||||
// {
|
||||
// if (!isEnabled) return;
|
||||
// UXMetaService.Register(this);
|
||||
// _registered = true;
|
||||
// }
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
if (_registered == isEnabled) return;
|
||||
if (isEnabled)
|
||||
{
|
||||
UXMetaService.Register(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
UXMetaService.UnRegister(this);
|
||||
}
|
||||
_registered = isEnabled;
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (isEnabled && _registered)
|
||||
UXMetaService.UnRegister(this);
|
||||
}
|
||||
}
|
61
Artists/Scripts/UX/UXMetaService.cs
Normal file
61
Artists/Scripts/UX/UXMetaService.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using Godot;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace BITKit;
|
||||
|
||||
public interface IMetaDisplayElement
|
||||
{
|
||||
string Text { get; }
|
||||
Vector3 Position { get; }
|
||||
}
|
||||
public partial class UXMetaService : Control
|
||||
{
|
||||
#region 静态方法
|
||||
private static readonly Queue<IMetaDisplayElement> AddQueue = new();
|
||||
private static readonly Queue<IMetaDisplayElement> RemoveQueue = new();
|
||||
private static readonly List<IMetaDisplayElement> Elements = new();
|
||||
public static void Register(IMetaDisplayElement element) => AddQueue.Enqueue(element);
|
||||
public static void UnRegister(IMetaDisplayElement element) => RemoveQueue.Enqueue(element);
|
||||
#endregion
|
||||
#region 实例方法
|
||||
private readonly Dictionary<IMetaDisplayElement, Label> _dictionary = new();
|
||||
#endregion
|
||||
/// <summary>
|
||||
/// 标签预制体
|
||||
/// </summary>
|
||||
[Export]
|
||||
private PackedScene labelTemplate;
|
||||
/// <summary>
|
||||
/// 主要处理过程
|
||||
/// </summary>
|
||||
/// <param name="delta"></param>
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
//相机服务未初始化时返回
|
||||
if(CameraService.Singleton is null)return;
|
||||
//处理添加队列
|
||||
while (AddQueue.TryDequeue(out var newElement))
|
||||
{
|
||||
if (!Elements.TryAdd(newElement)) continue;
|
||||
var instance = labelTemplate.Instantiate<Label>();
|
||||
_dictionary.Add(newElement,instance);
|
||||
AddChild(instance);
|
||||
}
|
||||
//处理每个Element的数据
|
||||
foreach (var element in Elements)
|
||||
{
|
||||
if (_dictionary.TryGetValue(element, out var label) is false) continue;
|
||||
var pos = CameraService.Singleton.UnprojectPosition(element.Position);
|
||||
label.Position = pos;
|
||||
label.Text = element.Text;
|
||||
}
|
||||
//处理移除队列
|
||||
while (RemoveQueue.TryDequeue(out var removeElement))
|
||||
{
|
||||
if (!_dictionary.TryGetValue(removeElement, out var label)) continue;
|
||||
if (!_dictionary.TryRemove(removeElement)) continue;
|
||||
Elements.Remove(removeElement);
|
||||
label.QueueFree();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user