From 4ead6837350fd88cb911d5897da506ca3b9d5225 Mon Sep 17 00:00:00 2001 From: friflo Date: Sun, 2 Feb 2025 13:08:25 +0100 Subject: [PATCH] Relations - add/test RelationsDebugView --- src/ECS/Relations/Relations.cs | 29 ++++++++++++++++++++++++ src/Tests-internal/ECS/Test_Relations.cs | 13 +++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/ECS/Relations/Relations.cs b/src/ECS/Relations/Relations.cs index 291f6df2..ba370088 100644 --- a/src/ECS/Relations/Relations.cs +++ b/src/ECS/Relations/Relations.cs @@ -3,9 +3,12 @@ using System; +using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Reflection; using System.Text; +using static System.Diagnostics.DebuggerBrowsableState; +using Browse = System.Diagnostics.DebuggerBrowsableAttribute; // ReSharper disable InconsistentNaming // ReSharper disable once CheckNamespace @@ -14,6 +17,7 @@ namespace Friflo.Engine.ECS; /// /// Contains the relations of a specific entity returned by . /// +[DebuggerTypeProxy(typeof(RelationsDebugView<>))] public readonly struct Relations where TRelation : struct { @@ -140,4 +144,29 @@ public void Reset() { // --- IDisposable public void Dispose() { } +} + +internal class RelationsDebugView + where TRelation : struct +{ + [Browse(RootHidden)] + public TRelation[] Relations => GetRelations(); + + [Browse(Never)] + private readonly Relations relations; + + internal RelationsDebugView(Relations relations) + { + this.relations = relations; + } + + private TRelation[] GetRelations() + { + var array = new TRelation[relations.Length]; + int n = 0; + foreach (var relation in relations) { + array[n++] = relation; + } + return array; + } } \ No newline at end of file diff --git a/src/Tests-internal/ECS/Test_Relations.cs b/src/Tests-internal/ECS/Test_Relations.cs index 042c3b45..93e526d3 100644 --- a/src/Tests-internal/ECS/Test_Relations.cs +++ b/src/Tests-internal/ECS/Test_Relations.cs @@ -69,6 +69,19 @@ public static void Test_Relations_LinkRelationUtils() LinkRelationUtils.RemoveComponentValue(42, 2, relations); AreEqual(1, relations.Count); } + + [Test] + public static void Test_Relations_DebugView() + { + var store = new EntityStore(); + var entity = store.CreateEntity(); + entity.AddRelation(new StringRelation { value = "test" }); + var relations = entity.GetRelations(); + + var debugView = new RelationsDebugView(relations); + AreEqual(1, debugView.Relations.Length); + AreEqual("test", debugView.Relations[0].value); + } } }