Skip to content

Commit 9ccc91e

Browse files
author
Daniel Burgess
committed
Added Undo Functionality
Added FieldInfoWithPath as it easier to work with
1 parent 017e652 commit 9ccc91e

File tree

1 file changed

+54
-20
lines changed

1 file changed

+54
-20
lines changed

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

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ public virtual void Disable() { }
664664

665665
Dictionary<string, List<(object value, VisualElement target)>> visibleConditions = new Dictionary<string, List<(object value, VisualElement target)>>();
666666
Dictionary<string, VisualElement> hideElementIfConnected = new Dictionary<string, VisualElement>();
667-
Dictionary<FieldInfo, List<VisualElement>> fieldControlsMap = new Dictionary<FieldInfo, List<VisualElement>>();
667+
Dictionary<FieldInfoWithPath, List<VisualElement>> fieldControlsMap = new Dictionary<FieldInfoWithPath, List<VisualElement>>();
668668

669669
protected void AddInputContainer()
670670
{
@@ -692,20 +692,20 @@ protected virtual void DrawDefaultInspector(bool fromInspector = false)
692692
foreach (var port in portsPerFieldName[field.Name])
693693
{
694694
string fieldPath = port.portData.IsProxied ? port.portData.proxiedFieldPath : port.fieldName;
695-
DrawField(GetFieldInfoPath(fieldPath), fromInspector, port.portData.IsProxied);
695+
DrawField(new FieldInfoWithPath(GetFieldInfoPath(fieldPath)), fromInspector, port.portData.IsProxied);
696696
}
697697
}
698698
else
699699
{
700-
DrawField(new List<FieldInfo> { field }, fromInspector);
700+
DrawField(new FieldInfoWithPath(field), fromInspector);
701701
}
702702
}
703703
}
704704

705-
protected virtual void DrawField(List<FieldInfo> fieldInfoList, bool fromInspector, bool isProxied = false)
705+
protected virtual void DrawField(FieldInfoWithPath fieldInfoWithPath, bool fromInspector, bool isProxied = false)
706706
{
707-
FieldInfo field = fieldInfoList.Last();
708-
string fieldPath = fieldInfoList.GetPath();
707+
FieldInfo field = fieldInfoWithPath.Field;
708+
string fieldPath = fieldInfoWithPath.Path;
709709

710710
//skip if the field is a node setting
711711
if (field.HasCustomAttribute<SettingAttribute>())
@@ -825,7 +825,7 @@ void UpdateFieldVisibility(string fieldName, object newValue)
825825
}
826826
}
827827

828-
void UpdateOtherFieldValueSpecific<T>(FieldInfo field, object newValue)
828+
void UpdateOtherFieldValueSpecific<T>(FieldInfoWithPath field, object newValue)
829829
{
830830
foreach (var inputField in fieldControlsMap[field])
831831
{
@@ -836,16 +836,16 @@ void UpdateOtherFieldValueSpecific<T>(FieldInfo field, object newValue)
836836
}
837837

838838
static MethodInfo specificUpdateOtherFieldValue = typeof(BaseNodeView).GetMethod(nameof(UpdateOtherFieldValueSpecific), BindingFlags.NonPublic | BindingFlags.Instance);
839-
void UpdateOtherFieldValue(FieldInfo info, object newValue)
839+
void UpdateOtherFieldValue(FieldInfoWithPath info, object newValue)
840840
{
841841
// Warning: Keep in sync with FieldFactory CreateField
842-
var fieldType = info.FieldType.IsSubclassOf(typeof(UnityEngine.Object)) ? typeof(UnityEngine.Object) : info.FieldType;
842+
var fieldType = info.Field.FieldType.IsSubclassOf(typeof(UnityEngine.Object)) ? typeof(UnityEngine.Object) : info.Field.FieldType;
843843
var genericUpdate = specificUpdateOtherFieldValue.MakeGenericMethod(fieldType);
844844

845845
genericUpdate.Invoke(this, new object[] { info, newValue });
846846
}
847847

848-
object GetInputFieldValueSpecific<T>(FieldInfo field)
848+
object GetInputFieldValueSpecific<T>(FieldInfoWithPath field)
849849
{
850850
if (fieldControlsMap.TryGetValue(field, out var list))
851851
{
@@ -871,7 +871,7 @@ object GetInputFieldValue(FieldInfo info)
871871
protected VisualElement AddControlField(string fieldPath, string label = null, bool showInputDrawer = false, Action valueChangedCallback = null)
872872
{
873873
List<FieldInfo> fieldInfoPath = GetFieldInfoPath(fieldPath);
874-
return AddControlField(fieldInfoPath, label, showInputDrawer, valueChangedCallback);
874+
return AddControlField(new FieldInfoWithPath(fieldInfoPath.Last(), fieldPath), label, showInputDrawer, valueChangedCallback);
875875
}
876876
Regex s_ReplaceNodeIndexPropertyPath = new Regex(@"(^nodes.Array.data\[)(\d+)(\])");
877877
internal void SyncSerializedPropertyPathes()
@@ -902,14 +902,15 @@ protected SerializedProperty FindSerializedProperty(string fieldName)
902902
return owner.serializedGraph.FindProperty("nodes").GetArrayElementAtIndex(i).FindPropertyRelative(fieldName);
903903
}
904904

905-
protected VisualElement AddControlField(List<FieldInfo> fieldInfoPath, string label = null, bool showInputDrawer = false, Action valueChangedCallback = null)
905+
protected VisualElement AddControlField(FieldInfoWithPath fieldInfoWithPath, string label = null, bool showInputDrawer = false, Action valueChangedCallback = null)
906906
{
907-
var field = fieldInfoPath.Last(); //
907+
var field = fieldInfoWithPath.Field;
908+
var fieldPath = fieldInfoWithPath.Path;
908909

909-
if (fieldInfoPath == null && fieldInfoPath.IsValid())
910+
if (field == null)
910911
return null;
911912

912-
var element = new PropertyField(FindSerializedProperty(fieldInfoPath.GetPath()), showInputDrawer ? "" : label);
913+
var element = new PropertyField(FindSerializedProperty(fieldPath), showInputDrawer ? "" : label);
913914
element.Bind(owner.serializedGraph);
914915

915916
#if UNITY_2020_3 // In Unity 2020.3 the empty label on property field doesn't hide it, so we do it manually
@@ -922,7 +923,7 @@ protected VisualElement AddControlField(List<FieldInfo> fieldInfoPath, string la
922923

923924
element.RegisterValueChangeCallback(e =>
924925
{
925-
UpdateFieldVisibility(field.Name, fieldInfoPath.GetFinalValue(nodeTarget));
926+
UpdateFieldVisibility(field.Name, GetFieldInfoPath(fieldPath).GetFinalValue(nodeTarget));
926927
valueChangedCallback?.Invoke();
927928
NotifyNodeChanged();
928929
});
@@ -935,8 +936,8 @@ protected VisualElement AddControlField(List<FieldInfo> fieldInfoPath, string la
935936
objectField.allowSceneObjects = false;
936937
}
937938

938-
if (!fieldControlsMap.TryGetValue(field, out var inputFieldList))
939-
inputFieldList = fieldControlsMap[field] = new List<VisualElement>();
939+
if (!fieldControlsMap.TryGetValue(fieldInfoWithPath, out var inputFieldList))
940+
inputFieldList = fieldControlsMap[fieldInfoWithPath] = new List<VisualElement>();
940941
inputFieldList.Add(element);
941942

942943
if (element != null)
@@ -983,7 +984,7 @@ protected VisualElement AddControlField(List<FieldInfo> fieldInfoPath, string la
983984
void UpdateFieldValues()
984985
{
985986
foreach (var kp in fieldControlsMap)
986-
UpdateOtherFieldValue(kp.Key, kp.Key.GetValue(nodeTarget));
987+
UpdateOtherFieldValue(kp.Key, GetFieldInfoPath(kp.Key.Path).GetFinalValue(nodeTarget));
987988
}
988989

989990
protected void AddSettingField(FieldInfo field)
@@ -1237,4 +1238,37 @@ void UpdatePortsForField(string fieldName)
12371238

12381239
#endregion
12391240
}
1240-
}
1241+
1242+
public class FieldInfoWithPath : IEquatable<FieldInfoWithPath>
1243+
{
1244+
private FieldInfo field;
1245+
public FieldInfo Field => field;
1246+
private string path;
1247+
public string Path => path;
1248+
1249+
public FieldInfoWithPath(FieldInfo field, string path)
1250+
{
1251+
this.field = field;
1252+
this.path = path;
1253+
}
1254+
1255+
public FieldInfoWithPath(List<FieldInfo> fieldInfos)
1256+
{
1257+
this.field = fieldInfos.Last();
1258+
this.path = fieldInfos.GetPath();
1259+
}
1260+
1261+
public FieldInfoWithPath(FieldInfo field)
1262+
{
1263+
this.field = field;
1264+
this.path = field.Name;
1265+
}
1266+
1267+
public bool Equals(FieldInfoWithPath other)
1268+
{
1269+
return field == other.field
1270+
&& path == other.path;
1271+
}
1272+
}
1273+
}
1274+

0 commit comments

Comments
 (0)