Skip to content

Commit e17153e

Browse files
committed
Added default editor gui for nodes
1 parent 6f85599 commit e17153e

9 files changed

Lines changed: 76 additions & 29 deletions

File tree

Assets/Examples/DefaultNodes/Editor/IntNodeView.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ public class IntNodeView : BaseNodeView
1414
public override void Enable()
1515
{
1616
var intNode = nodeTarget as IntNode;
17-
18-
IntegerField intField = new IntegerField();
1917

20-
intField.value = intNode.output;
18+
IntegerField intField = new IntegerField
19+
{
20+
value = intNode.output
21+
};
2122

22-
intField.OnValueChanged((v) => {
23+
intField.OnValueChanged((v) => {
2324
intNode.output = (int)v.newValue;
2425
owner.RegisterCompleteObjectUndo("Updated IntNode output");
2526
});

Assets/Examples/DefaultNodes/Nodes/AddNode.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public class AddNode : BaseNode
1414

1515
public override string name { get { return "Add"; } }
1616

17+
public int test;
18+
1719
public override void Process()
1820
{
1921
output = 0;

Assets/Examples/DefaultNodes/Nodes/IntNode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[System.Serializable, NodeMenuItem("Primitives/Int")]
77
public class IntNode : BaseNode
88
{
9-
[Output]
9+
[Output]
1010
public int output;
1111

1212
public override string name { get { return "IntNode"; } }

Assets/Examples/test.asset

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ MonoBehaviour:
1919
- type: IntNode
2020
jsonDatas: '{"GUID":"240d9400-836b-4fb7-a157-5b7a9ca9318b","position":{"serializedVersion":"2","x":-53.13677978515625,"y":456.65179443359377,"width":89.0,"height":97.0},"expanded":false,"output":0}'
2121
- type: AddNode
22-
jsonDatas: '{"GUID":"17f48d73-bf80-4f52-ae02-d61b2009878c","position":{"serializedVersion":"2","x":191.94322204589845,"y":230.09181213378907,"width":137.0,"height":77.0},"expanded":false,"output":0.0}'
22+
jsonDatas: '{"GUID":"17f48d73-bf80-4f52-ae02-d61b2009878c","position":{"serializedVersion":"2","x":196.94322204589845,"y":269.05181884765627,"width":137.0,"height":117.0},"expanded":false,"output":0.0,"test":0}'
2323
- type: IntNode
2424
jsonDatas: '{"GUID":"a1a91bf2-b474-4f89-8235-38a1e71e4c31","position":{"serializedVersion":"2","x":-110.14024353027344,"y":320.9788513183594,"width":89.0,"height":97.0},"expanded":false,"output":0}'
2525
edges:
@@ -41,5 +41,5 @@ MonoBehaviour:
4141
outputNodeGUID: 240d9400-836b-4fb7-a157-5b7a9ca9318b
4242
inputFieldName: inputs
4343
outputFieldName: output
44-
position: {x: 309.09912, y: -139.76471, z: 0}
44+
position: {x: 209.70853, y: -128.78745, z: 0}
4545
scale: {x: 1, y: 1, z: 1}

Assets/NodeGraphProcessor/Editor/Resources/Styles/BaseNodeView.uss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ IntNodeView {
55
background-color: rgba(63, 63, 63, 0.8);
66
}
77

8-
IntNodeView #controls {
8+
#controls {
99
background-color: rgba(63, 63, 63, 0.8);
1010
}

Assets/NodeGraphProcessor/Editor/Utils/FieldFactory.cs

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ namespace GraphProcessor
1111
{
1212
public static class FieldFactory
1313
{
14-
static Dictionary< Type, Type > fieldDrawers = new Dictionary< Type, Type >();
14+
static readonly Dictionary< Type, Type > fieldDrawers = new Dictionary< Type, Type >();
15+
16+
static readonly MethodInfo createFieldMethod = typeof(FieldFactory).GetMethod("CreateFieldSpecific", BindingFlags.Static | BindingFlags.Public);
1517

1618
static FieldFactory()
1719
{
@@ -26,8 +28,9 @@ static FieldFactory()
2628
}
2729

2830
// щ(ºДºщ) ...
29-
AddDrawer(typeof(int), typeof(IntegerField));
30-
AddDrawer(typeof(float), typeof(DoubleField));
31+
AddDrawer(typeof(int), typeof(IntegerField));
32+
AddDrawer(typeof(long), typeof(LongField));
33+
AddDrawer(typeof(float), typeof(FloatField));
3134
AddDrawer(typeof(double), typeof(DoubleField));
3235
AddDrawer(typeof(string), typeof(TextField));
3336
AddDrawer(typeof(Bounds), typeof(BoundsField));
@@ -42,34 +45,55 @@ static FieldFactory()
4245

4346
static void AddDrawer(Type fieldType, Type drawerType)
4447
{
45-
if (!drawerType.IsSubclassOf(typeof(INotifyValueChanged<>)))
48+
var iNotifyType = typeof(INotifyValueChanged<>).MakeGenericType(fieldType);
49+
50+
if (!iNotifyType.IsAssignableFrom(drawerType))
4651
{
47-
Debug.LogError("The custom field drawer " + drawerType + " does not implements INotifyValueChanged< T >");
52+
Debug.LogError("The custom field drawer " + drawerType + " does not implements INotifyValueChanged< " + fieldType + " >");
4853
return ;
4954
}
5055

5156
fieldDrawers[fieldType] = drawerType;
5257
}
5358

5459
public static INotifyValueChanged< T > CreateField< T >()
60+
{
61+
return CreateField(typeof(T)) as INotifyValueChanged< T >;
62+
}
63+
64+
public static VisualElement CreateField(Type t)
5565
{
5666
Type drawerType;
5767

58-
fieldDrawers.TryGetValue(typeof(T), out drawerType);
68+
fieldDrawers.TryGetValue(t, out drawerType);
5969

6070
if (drawerType == null)
61-
{
62-
throw new ArgumentException("Can't find field drawer for type" + typeof(T));
63-
}
71+
drawerType = fieldDrawers.FirstOrDefault(kp => kp.Key.IsReallyAssignableFrom(t)).Value;
72+
73+
if (drawerType == null)
74+
throw new ArgumentException("Can't find field drawer for type: " + t);
6475

6576
var field = Activator.CreateInstance(drawerType);
66-
return field as INotifyValueChanged< T >;
77+
78+
return field as VisualElement;
79+
}
80+
81+
public static INotifyValueChanged< T > CreateFieldSpecific< T >(FieldInfo field, Action< object > onValueChanged)
82+
{
83+
var fieldDrawer = CreateField< T >();
84+
85+
fieldDrawer.OnValueChanged((e) => {
86+
onValueChanged(e.newValue);
87+
});
88+
89+
return fieldDrawer as INotifyValueChanged< T >;
6790
}
6891

69-
public static VisualElement CreateField(FieldInfo field)
92+
public static VisualElement CreateField(FieldInfo field, Action< object > onValueChanged)
7093
{
71-
//TODO: Create new field drawer and attach chnage events to this field
72-
return null;
94+
var createFieldSpecificMethod = createFieldMethod.MakeGenericMethod(field.FieldType);
95+
96+
return createFieldSpecificMethod.Invoke(null, new object[]{field, onValueChanged}) as VisualElement;
7397
}
7498
}
7599
}

Assets/NodeGraphProcessor/Editor/Views/BaseNodeView.cs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public void Initialize(BaseGraphView owner, BaseNode node)
4242
InitializeView();
4343

4444
Enable();
45+
46+
this.RefreshPorts();
4547
}
4648

4749
public void AddPort(PortView p)
@@ -131,22 +133,38 @@ public virtual void DrawDefaultInspector()
131133

132134
foreach (var field in fields)
133135
{
134-
if (!field.IsPublic || field.GetCustomAttribute(typeof(SerializeField)) == null)
136+
//skip if the field is not serilizable
137+
if (!field.IsPublic && field.GetCustomAttribute(typeof(SerializeField)) == null)
138+
continue ;
139+
140+
//skip if the field is an input/output
141+
if (field.GetCustomAttribute(typeof(InputAttribute)) != null || field.GetCustomAttribute(typeof(OutputAttribute)) != null)
135142
continue ;
136143

137-
FieldFactory.CreateField(field);
138-
//TODO: implement field factor
144+
//skip if marked with NonSerialized
145+
if (field.GetCustomAttribute(typeof(System.NonSerializedAttribute)) != null)
146+
continue ;
147+
148+
var controlLabel = new Label(field.Name);
149+
controlsContainer.Add(controlLabel);
150+
151+
var element = FieldFactory.CreateField(field, (newValue) => {
152+
field.SetValue(nodeTarget, newValue);
153+
owner.RegisterCompleteObjectUndo("Updated " + newValue);
154+
});
155+
156+
controlsContainer.Add(element);
139157
}
140158
}
141159

142160
public virtual void OnPortConnected(PortView port) {}
143161
public virtual void OnPortDisconnected(PortView port) {}
144162

145-
public override void SetPosition(Rect newPosition)
163+
public override void SetPosition(Rect newPos)
146164
{
147-
base.SetPosition(newPosition);
165+
base.SetPosition(newPos);
148166

149-
nodeTarget.position = newPosition;
167+
nodeTarget.position = newPos;
150168
}
151169

152170
public override bool expanded

Assets/NodeGraphProcessor/Editor/Views/CommentBlockView.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
using UnityEngine;
44
using UnityEditor.Experimental.UIElements.GraphView;
55
using UnityEngine.Experimental.UIElements;
6+
using UnityEditor.Graphs;
67

78
namespace GraphProcessor
89
{
9-
public class CommentBlockView : GroupNode
10+
public class CommentBlockView : GraphElement
1011
{
1112
public BaseGraphView owner;
1213
public CommentBlock commentBlock;

Assets/NodeGraphProcessor/Editor/Views/PortView.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ public class PortView : Port
1616

1717
List< EdgeView > edges = new List< EdgeView >();
1818

19-
public PortView(Orientation portOrientation, Direction portDirection, FieldInfo field, EdgeConnectorListener edgeConnectorListener) : base(portOrientation, portDirection, field.FieldType)
19+
public PortView(Orientation portOrientation, Direction portDirection, FieldInfo field, EdgeConnectorListener edgeConnectorListener)
20+
: base(portOrientation, portDirection, Capacity.Multi, field.FieldType)
2021
{
2122
AddStyleSheetPath("Styles/PortView");
2223

0 commit comments

Comments
 (0)