This commit is contained in:
CortexCore
2023-06-05 16:25:06 +08:00
parent 9027120bb8
commit 4565ff2e35
2947 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
{
"name": "BITKits.MarkSystem",
"rootNamespace": "",
"references": [
"GUID:a209c53514018594f9f482516f2a6781",
"GUID:49b49c76ee64f6b41bf28ef951cb0e50",
"GUID:66d2ae14764cc7d49aad4b16930747c0",
"GUID:6ef4ed8ff60a7aa4bb60a8030e6f4008",
"GUID:f51ebe6a0ceec4240a699833d6309b23",
"GUID:be17a8778dbfe454890ed8279279e153",
"GUID:14fe60d984bf9f84eac55c6ea033a8f4"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [
"ODIN_INSPECTOR"
],
"versionDefines": [],
"noEngineReferences": false
}

View File

@@ -0,0 +1,43 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using BITKit.SubSystems;
using UnityEngine;
namespace BITKit.Mark
{
public interface IMarkObject
{
string GetID();
string GetDisplay();
Vector3 GetPostion();
bool GetAcitve();
}
[SubSystemConfig(isMainThread = true)]
public class MarkSystem : SubBITSystem
{
public Action<IMarkObject> OnAdd;
public Action<IMarkObject> Update;
public Action<IMarkObject> OnRemove;
List<IMarkObject> markObjects = new();
public void Add(IMarkObject markObject)
{
markObjects.Add(markObject);
OnAdd?.Invoke(markObject);
}
public void Remove(IMarkObject markObject)
{
markObjects.TryRemove(markObject);
OnRemove?.Invoke(markObject);
}
public override void OnUpdate(float deltaTime)
{
foreach (var markObject in markObjects.ToArray())
{
Update?.Invoke(markObject);
}
}
}
}

View File

@@ -0,0 +1,129 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using BITKit.UX;
using BITKit.SubSystems;
using UnityEngine.UIElements;
using Cysharp.Threading.Tasks;
using System.Threading.Tasks;
using System.Threading;
namespace BITKit.Mark
{
public class MarkSystemUIToolkit : MonoBehaviour
{
public UXElement element;
[SerializeReference, SubclassSelector] public References className;
Dictionary<string, Label> dictionary = new();
MarkSystem markSystem;
IPanel panel;
CancellationToken cancellationToken;
void Awake()
{
cancellationToken = gameObject.GetCancellationTokenOnDestroy();
}
void Start()
{
panel = element.GetVisualElement().panel;
markSystem = BITSystems.GetOrCreate<MarkSystem>();
if (markSystem is not null)
{
markSystem.OnAdd += OnAdd;
markSystem.Update += OnUpdate;
markSystem.OnRemove += OnRemove;
}
}
void OnDestroy()
{
if (markSystem is not null)
{
markSystem.OnAdd -= OnAdd;
markSystem.Update -= OnUpdate;
markSystem.OnRemove -= OnRemove;
}
}
void OnAdd(IMarkObject markObject)
{
GetOrCreate(markObject.GetID());
}
async void OnRemove(IMarkObject markObject)
{
try
{
await UniTask.SwitchToMainThread(cancellationToken);
if (dictionary.TryGetValue(markObject.GetID(), out var label))
{
element.GetVisualElement().Remove(label);
dictionary.Remove(markObject.GetID());
}
}
catch (System.Exception e)
{
if (e is not OperationCanceledException)
throw;
}
}
async void OnUpdate(IMarkObject markObject)
{
var cameraTrans = Camera.main.transform;
try
{
await UniTask.SwitchToMainThread();
var active = markObject.GetAcitve();
var label = GetOrCreate(markObject.GetID());
if (active)
{
var pos = RuntimePanelUtils
.CameraTransformWorldToPanel(panel, markObject.GetPostion(), Camera.main);
pos.x = (pos.x - label.layout.width / 2);
Rect elementRect = new()
{
position = pos,
size = label.layout.size,
};
label.text = markObject.GetDisplay();
label.transform.position = pos;
if (Vector3.Dot(cameraTrans.forward, markObject.GetPostion() - cameraTrans.position) > 0)
{
label.SetActive(true);
}
else
{
label.SetActive(false);
}
}
else
{
label.SetActive(false);
}
}
catch (OperationCanceledException)
{
}
catch (System.Exception)
{
throw;
}
}
Label GetOrCreate(string id)
{
if (dictionary.TryGetValue(id, out var label))
{
}
else
{
label = new();
label.AddToClassList(className);
label.style.position = Position.Absolute;
dictionary.Add(id, label);
element.GetVisualElement().Add(label);
}
return label;
}
}
}