From 2d1a671b71d11cb55339b9c8845c03f54a5efa00 Mon Sep 17 00:00:00 2001 From: Wang Cong Date: Wed, 6 Nov 2019 18:42:22 +0800 Subject: [PATCH] Undo support (#199) Added undo support for move, create, duplicate and delete node, and connect port --- Scripts/Editor/NodeEditorAction.cs | 1 + Scripts/Editor/NodeGraphEditor.cs | 11 ++++++++++- Scripts/NodePort.cs | 4 ++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Scripts/Editor/NodeEditorAction.cs b/Scripts/Editor/NodeEditorAction.cs index 67c50a7..2581676 100644 --- a/Scripts/Editor/NodeEditorAction.cs +++ b/Scripts/Editor/NodeEditorAction.cs @@ -81,6 +81,7 @@ namespace XNodeEditor { for (int i = 0; i < Selection.objects.Length; i++) { if (Selection.objects[i] is XNode.Node) { XNode.Node node = Selection.objects[i] as XNode.Node; + Undo.RecordObject(node, "Moved Node"); Vector2 initial = node.position; node.position = mousePos + dragOffset[i]; if (gridSnap) { diff --git a/Scripts/Editor/NodeGraphEditor.cs b/Scripts/Editor/NodeGraphEditor.cs index bc6e14c..56f0d69 100644 --- a/Scripts/Editor/NodeGraphEditor.cs +++ b/Scripts/Editor/NodeGraphEditor.cs @@ -138,7 +138,9 @@ namespace XNodeEditor { /// Create a node and save it in the graph asset public virtual XNode.Node CreateNode(Type type, Vector2 position) { + Undo.RecordObject(target, "Create Node"); XNode.Node node = target.AddNode(type); + Undo.RegisterCreatedObjectUndo(node, "Create Node"); node.position = position; if (node.name == null || node.name.Trim() == "") node.name = NodeEditorUtilities.NodeDefaultName(type); AssetDatabase.AddObjectToAsset(node, target); @@ -149,7 +151,9 @@ namespace XNodeEditor { /// Creates a copy of the original node in the graph public XNode.Node CopyNode(XNode.Node original) { + Undo.RecordObject(target, "Duplicate Node"); XNode.Node node = target.CopyNode(original); + Undo.RegisterCreatedObjectUndo(node, "Duplicate Node"); node.name = original.name; AssetDatabase.AddObjectToAsset(node, target); if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets(); @@ -158,8 +162,13 @@ namespace XNodeEditor { /// Safely remove a node and all its connections. public virtual void RemoveNode(XNode.Node node) { + Undo.RecordObject(node, "Delete Node"); + Undo.RecordObject(target, "Delete Node"); + foreach (var port in node.Ports) + foreach (var conn in port.GetConnections()) + Undo.RecordObject(conn.node, "Delete Node"); target.RemoveNode(node); - UnityEngine.Object.DestroyImmediate(node, true); + Undo.DestroyObjectImmediate(node); if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets(); } diff --git a/Scripts/NodePort.cs b/Scripts/NodePort.cs index 1000b23..9db7aee 100644 --- a/Scripts/NodePort.cs +++ b/Scripts/NodePort.cs @@ -199,6 +199,10 @@ namespace XNode { if (port == this) { Debug.LogWarning("Cannot connect port to self."); return; } if (IsConnectedTo(port)) { Debug.LogWarning("Port already connected. "); return; } if (direction == port.direction) { Debug.LogWarning("Cannot connect two " + (direction == IO.Input ? "input" : "output") + " connections"); return; } +#if UNITY_EDITOR + UnityEditor.Undo.RecordObject(node, "Connect Port"); + UnityEditor.Undo.RecordObject(port.node, "Connect Port"); +#endif if (port.connectionType == Node.ConnectionType.Override && port.ConnectionCount != 0) { port.ClearConnections(); } if (connectionType == Node.ConnectionType.Override && ConnectionCount != 0) { ClearConnections(); } connections.Add(new PortConnection(port));