Skip to content

Commit

Permalink
Relations - add/test RelationsDebugView
Browse files Browse the repository at this point in the history
  • Loading branch information
friflo committed Feb 2, 2025
1 parent ba377de commit 4ead683
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/ECS/Relations/Relations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -14,6 +17,7 @@ namespace Friflo.Engine.ECS;
/// <summary>
/// Contains the relations of a specific entity returned by <see cref="RelationExtensions.GetRelations{TRelation}"/>.
/// </summary>
[DebuggerTypeProxy(typeof(RelationsDebugView<>))]
public readonly struct Relations<TRelation>
where TRelation : struct
{
Expand Down Expand Up @@ -140,4 +144,29 @@ public void Reset() {

// --- IDisposable
public void Dispose() { }
}

internal class RelationsDebugView<TRelation>
where TRelation : struct
{
[Browse(RootHidden)]
public TRelation[] Relations => GetRelations();

[Browse(Never)]
private readonly Relations<TRelation> relations;

internal RelationsDebugView(Relations<TRelation> 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;
}
}
13 changes: 13 additions & 0 deletions src/Tests-internal/ECS/Test_Relations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<StringRelation>();

var debugView = new RelationsDebugView<StringRelation>(relations);
AreEqual(1, debugView.Relations.Length);
AreEqual("test", debugView.Relations[0].value);
}
}

}

0 comments on commit 4ead683

Please sign in to comment.