Skip to content

Commit a57215d

Browse files
author
Antoine Lelievre
committed
Added AddControlField property to add controls on nodes based on field names
1 parent c6b0f08 commit a57215d

4 files changed

Lines changed: 48 additions & 43 deletions

File tree

Assets/Examples/ExposedGetProperties.asset

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ MonoBehaviour:
2525
Culture=neutral, PublicKeyToken=null
2626
jsonDatas: '{"GUID":"fddbdd73-775e-4b42-b959-90c91f0994ea","computeOrder":1,"canProcess":true,"position":{"serializedVersion":"2","x":168.0,"y":81.0,"width":74.0,"height":97.0},"expanded":false,"debug":false,"nodeLock":false,"parameterGUID":"ae7b8636-b4ca-4259-afac-f09ff0dc0ad4","accessor":0}'
2727
- type: FloatNode, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
28-
jsonDatas: '{"GUID":"7a99c438-c7a4-4119-8a43-3c97f352dd7d","computeOrder":1,"canProcess":true,"position":{"serializedVersion":"2","x":164.0,"y":192.0,"width":73.0,"height":101.0},"expanded":false,"debug":false,"nodeLock":false,"output":21.0}'
28+
jsonDatas: '{"GUID":"7a99c438-c7a4-4119-8a43-3c97f352dd7d","computeOrder":1,"canProcess":true,"position":{"serializedVersion":"2","x":164.0,"y":192.0,"width":73.0,"height":101.0},"expanded":false,"debug":false,"nodeLock":false,"output":21.0,"input":0.0}'
29+
- type: MessageNode, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
30+
jsonDatas: '{"GUID":"fba0fb79-33b7-4750-a167-e5f8e7fdb269","computeOrder":1,"canProcess":true,"position":{"serializedVersion":"2","x":203.98825073242188,"y":309.4784240722656,"width":227.0,"height":98.0},"expanded":false,"debug":false,"nodeLock":false,"input":0.0}'
2931
edges:
3032
- GUID: 81ad2a95-ee5f-4b8d-9d14-73caf3602feb
3133
owner: {fileID: 11400000}
@@ -64,7 +66,7 @@ MonoBehaviour:
6466
- position:
6567
serializedVersion: 2
6668
x: 0
67-
y: 226
69+
y: 334
6870
width: 150
6971
height: 200
7072
opened: 1
@@ -98,5 +100,5 @@ MonoBehaviour:
98100
serializedName:
99101
serializedValue: 67
100102
input: 1
101-
position: {x: 0, y: 0, z: 0}
102-
scale: {x: 1, y: 1, z: 1}
103+
position: {x: 178, y: 119, z: 0}
104+
scale: {x: 1.520875, y: 1.520875, z: 1}

Assets/com.alelievr.NodeGraphProcessor/Editor/Utils/FieldFactory.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,23 @@ public static VisualElement CreateField(Type t, string label)
9191
}
9292
else
9393
{
94-
field = Activator.CreateInstance(drawerType,
95-
BindingFlags.CreateInstance |
96-
BindingFlags.Public |
97-
BindingFlags.NonPublic |
98-
BindingFlags.Instance |
99-
BindingFlags.OptionalParamBinding, null,
100-
new object[]{ label, Type.Missing }, CultureInfo.CurrentCulture);
101-
94+
try {
95+
field = Activator.CreateInstance(drawerType,
96+
BindingFlags.CreateInstance |
97+
BindingFlags.Public |
98+
BindingFlags.NonPublic |
99+
BindingFlags.Instance |
100+
BindingFlags.OptionalParamBinding, null,
101+
new object[]{ label, Type.Missing }, CultureInfo.CurrentCulture);
102+
} catch {
103+
field = Activator.CreateInstance(drawerType,
104+
BindingFlags.CreateInstance |
105+
BindingFlags.Public |
106+
BindingFlags.NonPublic |
107+
BindingFlags.Instance |
108+
BindingFlags.OptionalParamBinding, null,
109+
new object[]{ label }, CultureInfo.CurrentCulture);
110+
}
102111
}
103112

104113
// For mutiline

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ public virtual void Enable()
330330
DrawDefaultInspector();
331331
}
332332

333-
public virtual void DrawDefaultInspector()
333+
protected virtual void DrawDefaultInspector()
334334
{
335335
var fields = nodeTarget.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
336336

@@ -348,16 +348,27 @@ public virtual void DrawDefaultInspector()
348348
if (field.GetCustomAttribute(typeof(System.NonSerializedAttribute)) != null || field.GetCustomAttribute(typeof(HideInInspector)) != null)
349349
continue ;
350350

351-
var element = FieldFactory.CreateField(field.FieldType, field.GetValue(nodeTarget), (newValue) => {
352-
owner.RegisterCompleteObjectUndo("Updated " + newValue);
353-
field.SetValue(nodeTarget, newValue);
354-
}, field.Name);
355-
356-
if (element != null)
357-
controlsContainer.Add(element);
351+
AddControlField(field, field.Name);
358352
}
359353
}
360354

355+
protected void AddControlField(string fieldName, string label = null)
356+
=> AddControlField(nodeTarget.GetType().GetField(fieldName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance), label);
357+
358+
protected void AddControlField(FieldInfo field, string label = null)
359+
{
360+
if (field == null)
361+
return;
362+
363+
var element = FieldFactory.CreateField(field.FieldType, field.GetValue(nodeTarget), (newValue) => {
364+
owner.RegisterCompleteObjectUndo("Updated " + newValue);
365+
field.SetValue(nodeTarget, newValue);
366+
}, label);
367+
368+
if (element != null)
369+
controlsContainer.Add(element);
370+
}
371+
361372
internal void OnPortConnected(PortView port)
362373
{
363374
onPortConnected?.Invoke(port);

Assets/com.alelievr.NodeGraphProcessor/Runtime/Processing/TypeAdapter.cs

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,6 @@
55

66
namespace GraphProcessor
77
{
8-
// [AttributeUsage(AttributeTargets.Method)]
9-
// public class TypeAdapterAttribute : Attribute
10-
// {
11-
// public Type t1;
12-
// public Type t2;
13-
14-
// public TypeAdapterAttribute(Type t1, Type t2)
15-
// {
16-
// this.t1 = t1;
17-
// this.t2 = t2;
18-
// }
19-
// }
20-
218
/// <summary>
229
/// Implement this interface to use the inside your class to define type convertions to use inside the graph.
2310
/// Example:
@@ -34,7 +21,7 @@ public interface ITypeAdapter {}
3421
public static class TypeAdapter
3522
{
3623
static Dictionary< (Type from, Type to), Func<object, object> > adapters = new Dictionary< (Type, Type), Func<object, object> >();
37-
public static Dictionary< (Type from, Type to), MethodInfo > adapters2 = new Dictionary< (Type, Type), MethodInfo >();
24+
static Dictionary< (Type from, Type to), MethodInfo > adapterMethods = new Dictionary< (Type, Type), MethodInfo >();
3825

3926
[System.NonSerialized]
4027
static bool adaptersLoaded = false;
@@ -83,7 +70,7 @@ static void LoadAllAdapters()
8370
var r = (Func<object, object>) ret;
8471

8572
adapters.Add((method.GetParameters()[0].ParameterType, method.ReturnType), r);
86-
adapters2.Add((method.GetParameters()[0].ParameterType, method.ReturnType), method);
73+
adapterMethods.Add((method.GetParameters()[0].ParameterType, method.ReturnType), method);
8774
} catch (Exception e) {
8875
Debug.LogError($"Failed to load the type convertion method: {method}\n{e}");
8976
}
@@ -110,20 +97,16 @@ public static bool AreAssignable(Type from, Type to)
11097
return adapters.ContainsKey((from, to));
11198
}
11299

113-
public static MethodInfo GetConvertionMethod(Type from, Type to)
114-
{
115-
return adapters2[(from, to)];
116-
}
117-
100+
public static MethodInfo GetConvertionMethod(Type from, Type to) => adapterMethods[(from, to)];
118101

119102
public static object Convert(object from, Type targetType)
120103
{
121104
if (!adaptersLoaded)
122105
LoadAllAdapters();
123106

124-
// Delegate convertionFunction;
125-
// if (adapters.TryGetValue((from.GetType(), targetType), out convertionFunction))
126-
// return convertionFunction?.Invoke(from);
107+
Func<object, object> convertionFunction;
108+
if (adapters.TryGetValue((from.GetType(), targetType), out convertionFunction))
109+
return convertionFunction?.Invoke(from);
127110

128111
return null;
129112
}

0 commit comments

Comments
 (0)