Skip to content

Commit 4ead683

Browse files
committed
Relations - add/test RelationsDebugView
1 parent ba377de commit 4ead683

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/ECS/Relations/Relations.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33

44

55
using System;
6+
using System.Diagnostics;
67
using System.Diagnostics.CodeAnalysis;
78
using System.Reflection;
89
using System.Text;
10+
using static System.Diagnostics.DebuggerBrowsableState;
11+
using Browse = System.Diagnostics.DebuggerBrowsableAttribute;
912

1013
// ReSharper disable InconsistentNaming
1114
// ReSharper disable once CheckNamespace
@@ -14,6 +17,7 @@ namespace Friflo.Engine.ECS;
1417
/// <summary>
1518
/// Contains the relations of a specific entity returned by <see cref="RelationExtensions.GetRelations{TRelation}"/>.
1619
/// </summary>
20+
[DebuggerTypeProxy(typeof(RelationsDebugView<>))]
1721
public readonly struct Relations<TRelation>
1822
where TRelation : struct
1923
{
@@ -140,4 +144,29 @@ public void Reset() {
140144

141145
// --- IDisposable
142146
public void Dispose() { }
147+
}
148+
149+
internal class RelationsDebugView<TRelation>
150+
where TRelation : struct
151+
{
152+
[Browse(RootHidden)]
153+
public TRelation[] Relations => GetRelations();
154+
155+
[Browse(Never)]
156+
private readonly Relations<TRelation> relations;
157+
158+
internal RelationsDebugView(Relations<TRelation> relations)
159+
{
160+
this.relations = relations;
161+
}
162+
163+
private TRelation[] GetRelations()
164+
{
165+
var array = new TRelation[relations.Length];
166+
int n = 0;
167+
foreach (var relation in relations) {
168+
array[n++] = relation;
169+
}
170+
return array;
171+
}
143172
}

src/Tests-internal/ECS/Test_Relations.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,19 @@ public static void Test_Relations_LinkRelationUtils()
6969
LinkRelationUtils.RemoveComponentValue(42, 2, relations);
7070
AreEqual(1, relations.Count);
7171
}
72+
73+
[Test]
74+
public static void Test_Relations_DebugView()
75+
{
76+
var store = new EntityStore();
77+
var entity = store.CreateEntity();
78+
entity.AddRelation(new StringRelation { value = "test" });
79+
var relations = entity.GetRelations<StringRelation>();
80+
81+
var debugView = new RelationsDebugView<StringRelation>(relations);
82+
AreEqual(1, debugView.Relations.Length);
83+
AreEqual("test", debugView.Relations[0].value);
84+
}
7285
}
7386

7487
}

0 commit comments

Comments
 (0)