|
|
|
|
|
|
using System.Reflection; |
|
|
|
using System.Runtime.InteropServices; |
|
|
|
using NUnit.Framework; |
|
|
|
using UnityEngine; |
|
|
|
namespace UnityEngine.Graphing |
|
|
|
namespace UnityEditor.Graphing |
|
|
|
{ |
|
|
|
[Serializable] |
|
|
|
public class SerializableGraph : IGraph, ISerializationCallbackReceiver |
|
|
|
|
|
|
[SerializeField] |
|
|
|
List<SerializationHelper.JSONSerializedElement> m_SerializableEdges = new List<SerializationHelper.JSONSerializedElement>(); |
|
|
|
|
|
|
|
[NonSerialized] |
|
|
|
List<INode> m_AddedNodes = new List<INode>(); |
|
|
|
|
|
|
|
[NonSerialized] |
|
|
|
List<INode> m_RemovedNodes = new List<INode>(); |
|
|
|
|
|
|
|
[NonSerialized] |
|
|
|
List<IEdge> m_AddedEdges = new List<IEdge>(); |
|
|
|
|
|
|
|
[NonSerialized] |
|
|
|
List<IEdge> m_RemovedEdges = new List<IEdge>(); |
|
|
|
|
|
|
|
public IEnumerable<INode> addedNodes |
|
|
|
{ |
|
|
|
get { return m_AddedNodes; } |
|
|
|
} |
|
|
|
|
|
|
|
public IEnumerable<INode> removedNodes |
|
|
|
{ |
|
|
|
get { return m_RemovedNodes; } |
|
|
|
} |
|
|
|
|
|
|
|
public IEnumerable<IEdge> addedEdges |
|
|
|
{ |
|
|
|
get { return m_AddedEdges; } |
|
|
|
} |
|
|
|
|
|
|
|
public IEnumerable<IEdge> removedEdges |
|
|
|
{ |
|
|
|
get { return m_RemovedEdges; } |
|
|
|
} |
|
|
|
|
|
|
|
public IGraphObject owner { get; set; } |
|
|
|
|
|
|
|
public virtual void ClearChanges() |
|
|
|
{ |
|
|
|
m_AddedNodes.Clear(); |
|
|
|
m_RemovedNodes.Clear(); |
|
|
|
m_AddedEdges.Clear(); |
|
|
|
m_RemovedEdges.Clear(); |
|
|
|
} |
|
|
|
|
|
|
|
public IEnumerable<T> GetNodes<T>() where T : INode |
|
|
|
{ |
|
|
|
return m_Nodes.Values.OfType<T>(); |
|
|
|
|
|
|
{ |
|
|
|
m_Nodes.Add(node.guid, node); |
|
|
|
node.owner = this; |
|
|
|
NotifyChange(new NodeAddedGraphChange(node)); |
|
|
|
m_AddedNodes.Add(node); |
|
|
|
} |
|
|
|
|
|
|
|
public virtual void RemoveNode(INode node) |
|
|
|
|
|
|
|
|
|
|
m_Nodes.Remove(node.guid); |
|
|
|
NotifyChange(new NodeRemovedGraphChange(node)); |
|
|
|
m_RemovedNodes.Add(node); |
|
|
|
ValidateGraph(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
m_Nodes.Remove(node.guid); |
|
|
|
NotifyChange(new NodeRemovedGraphChange(node)); |
|
|
|
m_RemovedNodes.Add(node); |
|
|
|
} |
|
|
|
|
|
|
|
void AddEdgeToNodeEdges(IEdge edge) |
|
|
|
|
|
|
return new Dictionary<SerializationHelper.TypeSerializationInfo, SerializationHelper.TypeSerializationInfo>(); |
|
|
|
} |
|
|
|
|
|
|
|
public virtual IEdge Connect(SlotReference fromSlotRef, SlotReference toSlotRef) |
|
|
|
IEdge ConnectNoValidate(SlotReference fromSlotRef, SlotReference toSlotRef) |
|
|
|
{ |
|
|
|
if (fromSlotRef == null || toSlotRef == null) |
|
|
|
return null; |
|
|
|
|
|
|
|
|
|
|
var newEdge = new Edge(outputSlot, inputSlot); |
|
|
|
m_Edges.Add(newEdge); |
|
|
|
NotifyChange(new EdgeAddedGraphChange(newEdge)); |
|
|
|
m_AddedEdges.Add(newEdge); |
|
|
|
return newEdge; |
|
|
|
} |
|
|
|
|
|
|
|
public virtual IEdge Connect(SlotReference fromSlotRef, SlotReference toSlotRef) |
|
|
|
{ |
|
|
|
var newEdge = ConnectNoValidate(fromSlotRef, toSlotRef); |
|
|
|
ValidateGraph(); |
|
|
|
return newEdge; |
|
|
|
} |
|
|
|
|
|
|
if (m_NodeEdges.TryGetValue(e.outputSlot.nodeGuid, out outputNodeEdges)) |
|
|
|
outputNodeEdges.Remove(e); |
|
|
|
|
|
|
|
NotifyChange(new EdgeRemovedGraphChange(e)); |
|
|
|
m_RemovedEdges.Add(e); |
|
|
|
} |
|
|
|
|
|
|
|
public INode GetNodeFromGuid(Guid guid) |
|
|
|
|
|
|
AddEdgeToNodeEdges(edge); |
|
|
|
|
|
|
|
OnEnable(); |
|
|
|
ValidateGraph(); |
|
|
|
ValidateGraph(); |
|
|
|
} |
|
|
|
|
|
|
|
public virtual void ValidateGraph() |
|
|
|
|
|
|
//manually modifies serialized data
|
|
|
|
//of if they delete a node in the inspector
|
|
|
|
//debug view.
|
|
|
|
foreach (var edge in edges.ToArray()) |
|
|
|
//debug view.
|
|
|
|
foreach (var edge in edges.ToArray()) |
|
|
|
{ |
|
|
|
var outputNode = GetNodeFromGuid(edge.outputSlot.nodeGuid); |
|
|
|
var inputNode = GetNodeFromGuid(edge.inputSlot.nodeGuid); |
|
|
|
|
|
|
removedNodeEdges.Add(edge); |
|
|
|
} |
|
|
|
foreach (var edge in removedNodeEdges) |
|
|
|
RemoveEdge(edge); |
|
|
|
RemoveEdgeNoValidate(edge); |
|
|
|
} |
|
|
|
|
|
|
|
// Remove all nodes and re-add them.
|
|
|
|
|
|
|
foreach (var node in m_Nodes.Values) |
|
|
|
removedNodeGuids.Add(node.guid); |
|
|
|
foreach (var nodeGuid in removedNodeGuids) |
|
|
|
RemoveNode(m_Nodes[nodeGuid]); |
|
|
|
RemoveNodeNoValidate(m_Nodes[nodeGuid]); |
|
|
|
|
|
|
|
ValidateGraph(); |
|
|
|
AddNode(node); |
|
|
|
AddNodeNoValidate(node); |
|
|
|
} |
|
|
|
|
|
|
|
// Add edges from other graph which don't exist in this one.
|
|
|
|
|
|
|
Connect(edge.outputSlot, edge.inputSlot); |
|
|
|
ConnectNoValidate(edge.outputSlot, edge.inputSlot); |
|
|
|
|
|
|
|
ValidateGraph(); |
|
|
|
} |
|
|
|
|
|
|
|
public void OnEnable() |
|
|
|
|
|
|
node.OnEnable(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public OnGraphChange onChange { get; set; } |
|
|
|
|
|
|
|
public IGraphObject owner { get; set; } |
|
|
|
|
|
|
|
protected void NotifyChange(GraphChange change) |
|
|
|
{ |
|
|
|
if (onChange != null) |
|
|
|
onChange(change); |
|
|
|
} |
|
|
|
} |
|
|
|
} |