mirror of
https://github.com/Siccity/xNode.git
synced 2025-12-21 17:56:06 +08:00
Undo support (#199)
Added undo support for move, create, duplicate and delete node, and connect port
This commit is contained in:
parent
a825af8192
commit
2d1a671b71
@ -81,6 +81,7 @@ namespace XNodeEditor {
|
|||||||
for (int i = 0; i < Selection.objects.Length; i++) {
|
for (int i = 0; i < Selection.objects.Length; i++) {
|
||||||
if (Selection.objects[i] is XNode.Node) {
|
if (Selection.objects[i] is XNode.Node) {
|
||||||
XNode.Node node = Selection.objects[i] as XNode.Node;
|
XNode.Node node = Selection.objects[i] as XNode.Node;
|
||||||
|
Undo.RecordObject(node, "Moved Node");
|
||||||
Vector2 initial = node.position;
|
Vector2 initial = node.position;
|
||||||
node.position = mousePos + dragOffset[i];
|
node.position = mousePos + dragOffset[i];
|
||||||
if (gridSnap) {
|
if (gridSnap) {
|
||||||
|
|||||||
@ -138,7 +138,9 @@ namespace XNodeEditor {
|
|||||||
|
|
||||||
/// <summary> Create a node and save it in the graph asset </summary>
|
/// <summary> Create a node and save it in the graph asset </summary>
|
||||||
public virtual XNode.Node CreateNode(Type type, Vector2 position) {
|
public virtual XNode.Node CreateNode(Type type, Vector2 position) {
|
||||||
|
Undo.RecordObject(target, "Create Node");
|
||||||
XNode.Node node = target.AddNode(type);
|
XNode.Node node = target.AddNode(type);
|
||||||
|
Undo.RegisterCreatedObjectUndo(node, "Create Node");
|
||||||
node.position = position;
|
node.position = position;
|
||||||
if (node.name == null || node.name.Trim() == "") node.name = NodeEditorUtilities.NodeDefaultName(type);
|
if (node.name == null || node.name.Trim() == "") node.name = NodeEditorUtilities.NodeDefaultName(type);
|
||||||
AssetDatabase.AddObjectToAsset(node, target);
|
AssetDatabase.AddObjectToAsset(node, target);
|
||||||
@ -149,7 +151,9 @@ namespace XNodeEditor {
|
|||||||
|
|
||||||
/// <summary> Creates a copy of the original node in the graph </summary>
|
/// <summary> Creates a copy of the original node in the graph </summary>
|
||||||
public XNode.Node CopyNode(XNode.Node original) {
|
public XNode.Node CopyNode(XNode.Node original) {
|
||||||
|
Undo.RecordObject(target, "Duplicate Node");
|
||||||
XNode.Node node = target.CopyNode(original);
|
XNode.Node node = target.CopyNode(original);
|
||||||
|
Undo.RegisterCreatedObjectUndo(node, "Duplicate Node");
|
||||||
node.name = original.name;
|
node.name = original.name;
|
||||||
AssetDatabase.AddObjectToAsset(node, target);
|
AssetDatabase.AddObjectToAsset(node, target);
|
||||||
if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
|
if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
|
||||||
@ -158,8 +162,13 @@ namespace XNodeEditor {
|
|||||||
|
|
||||||
/// <summary> Safely remove a node and all its connections. </summary>
|
/// <summary> Safely remove a node and all its connections. </summary>
|
||||||
public virtual void RemoveNode(XNode.Node node) {
|
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);
|
target.RemoveNode(node);
|
||||||
UnityEngine.Object.DestroyImmediate(node, true);
|
Undo.DestroyObjectImmediate(node);
|
||||||
if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
|
if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -199,6 +199,10 @@ namespace XNode {
|
|||||||
if (port == this) { Debug.LogWarning("Cannot connect port to self."); return; }
|
if (port == this) { Debug.LogWarning("Cannot connect port to self."); return; }
|
||||||
if (IsConnectedTo(port)) { Debug.LogWarning("Port already connected. "); 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 (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 (port.connectionType == Node.ConnectionType.Override && port.ConnectionCount != 0) { port.ClearConnections(); }
|
||||||
if (connectionType == Node.ConnectionType.Override && ConnectionCount != 0) { ClearConnections(); }
|
if (connectionType == Node.ConnectionType.Override && ConnectionCount != 0) { ClearConnections(); }
|
||||||
connections.Add(new PortConnection(port));
|
connections.Add(new PortConnection(port));
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user