Skip to content

Commit 65587d6

Browse files
committed
Fixed sorting of port by inheritance level
1 parent 8e93c04 commit 65587d6

File tree

8 files changed

+135
-7
lines changed

8 files changed

+135
-7
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using GraphProcessor;
5+
using System.Linq;
6+
7+
[System.Serializable, NodeMenuItem("Custom/Inheritance1")]
8+
public class Inheritance1 :InheritanceBase
9+
{
10+
[Input(name = "In 1")]
11+
public float input1;
12+
13+
[Output(name = "Out 1")]
14+
public float output1;
15+
16+
public float field1;
17+
18+
public override string name => "Inheritance1";
19+
20+
protected override void Process()
21+
{
22+
output = input * 42;
23+
}
24+
}

Assets/Examples/DefaultNodes/Nodes/Inheritance1.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using GraphProcessor;
5+
using System.Linq;
6+
7+
[System.Serializable, NodeMenuItem("Custom/Inheritance2")]
8+
public class Inheritance2 : Inheritance1
9+
{
10+
[Input(name = "In 2")]
11+
public float input2;
12+
13+
[Output(name = "Out 2")]
14+
public float output2;
15+
16+
public float field2;
17+
18+
public override string name => "Inheritance2";
19+
20+
protected override void Process()
21+
{
22+
output = input * 42;
23+
}
24+
}

Assets/Examples/DefaultNodes/Nodes/Inheritance2.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using GraphProcessor;
5+
using System.Linq;
6+
7+
[System.Serializable, NodeMenuItem("Custom/InheritanceBase")]
8+
public class InheritanceBase : BaseNode
9+
{
10+
[Input(name = "In Base")]
11+
public float input;
12+
13+
[Output(name = "Out Base")]
14+
public float output;
15+
16+
public float fieldBase;
17+
18+
public override string name => "InheritanceBase";
19+
20+
protected override void Process()
21+
{
22+
output = input * 42;
23+
}
24+
}

Assets/Examples/DefaultNodes/Nodes/InheritanceBase.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/com.alelievr.NodeGraphProcessor/Editor/Views/BaseNodeView.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -591,16 +591,15 @@ protected void AddInputContainer()
591591
inputContainerElement.SendToBack();
592592
inputContainerElement.pickingMode = PickingMode.Ignore;
593593
}
594-
594+
595595
protected virtual void DrawDefaultInspector(bool fromInspector = false)
596596
{
597597
var fields = nodeTarget.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
598598
// Filter fields from the BaseNode type since we are only interested in user-defined fields
599599
// (better than BindingFlags.DeclaredOnly because we keep any inherited user-defined fields)
600-
.Where(f => f.DeclaringType != typeof(BaseNode))
601-
// Order by MetadataToken to sync the order with the port order (make sure FieldDrawers are next to the correct port)
602-
//TODO: Also consider custom port order
603-
.OrderBy(f => f.MetadataToken);
600+
.Where(f => f.DeclaringType != typeof(BaseNode));
601+
602+
fields = nodeTarget.OverrideFieldOrder(fields).Reverse();
604603

605604
foreach (var field in fields)
606605
{

Assets/com.alelievr.NodeGraphProcessor/Runtime/Elements/BaseNode.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,9 @@ public virtual void InitializePorts()
249249
{
250250
InitializeCustomPortTypeMethods();
251251

252-
foreach (var nodeFieldKP in nodeFields.ToList().OrderByDescending(kp => kp.Value.info.MetadataToken))
252+
foreach (var key in OverrideFieldOrder(nodeFields.Values.Select(k => k.info)))
253253
{
254-
var nodeField = nodeFieldKP.Value;
254+
var nodeField = nodeFields[key.Name];
255255

256256
if (HasCustomBehavior(nodeField))
257257
{
@@ -265,6 +265,30 @@ public virtual void InitializePorts()
265265
}
266266
}
267267

268+
/// <summary>
269+
/// Override the field order inside the node. It allows to re-order all the ports and field in the UI.
270+
/// </summary>
271+
/// <param name="fields">List of fields to sort</param>
272+
/// <returns>Sorted list of fields</returns>
273+
public virtual IEnumerable<FieldInfo> OverrideFieldOrder(IEnumerable<FieldInfo> fields)
274+
{
275+
long GetFieldInheritanceLevel(FieldInfo f)
276+
{
277+
int level = 0;
278+
var t = f.DeclaringType;
279+
while (t != null)
280+
{
281+
t = t.BaseType;
282+
level++;
283+
}
284+
285+
return level;
286+
}
287+
288+
// Order by MetadataToken and inheritance level to sync the order with the port order (make sure FieldDrawers are next to the correct port)
289+
return fields.OrderByDescending(f => (long)(((GetFieldInheritanceLevel(f) << 32)) | (long)f.MetadataToken));
290+
}
291+
268292
protected BaseNode()
269293
{
270294
inputPorts = new NodeInputPortContainer(this);

0 commit comments

Comments
 (0)