/* namespace UnityEditor.Graphing.Drawing { [Serializable] internal class CopyPasteGraph : ISerializationCallbackReceiver { [NonSerialized] private HashSet m_Edges = new HashSet(); [NonSerialized] private HashSet m_Nodes = new HashSet(); [SerializeField] List m_SerializableNodes = new List(); [SerializeField] List m_SerializableEdges = new List(); public virtual void AddNode(INode node) { m_Nodes.Add(node); } public void AddEdge(IEdge edge) { m_Edges.Add(edge); } public IEnumerable GetNodes() where T : INode { return m_Nodes.OfType(); } public IEnumerable edges { get { return m_Edges; } } public virtual void OnBeforeSerialize() { m_SerializableNodes = SerializationHelper.Serialize(m_Nodes); m_SerializableEdges = SerializationHelper.Serialize(m_Edges); } public virtual void OnAfterDeserialize() { var nodes = SerializationHelper.Deserialize(m_SerializableNodes); m_Nodes.Clear(); foreach (var node in nodes) m_Nodes.Add(node); m_SerializableNodes = null; var edges = SerializationHelper.Deserialize(m_SerializableEdges); m_Edges.Clear(); foreach (var edge in edges) m_Edges.Add(edge); m_SerializableEdges = null; } } internal class CopySelected : IManipulate { public delegate void CopyElements(List elements); public bool GetCaps(ManipulatorCapability cap) { return false; } public void AttachTo(CanvasElement element) { element.ValidateCommand += Validate; element.ExecuteCommand += CopyPaste; } private bool Validate(CanvasElement element, Event e, Canvas2D parent) { if (e.type == EventType.Used) return false; if (e.commandName != "Copy" && e.commandName != "Paste" && e.commandName != "Duplicate") return false; e.Use(); return true; } private bool CopyPaste(CanvasElement element, Event e, Canvas2D parent) { if (e.type == EventType.Used) return false; if (e.commandName != "Copy" && e.commandName != "Paste" && e.commandName != "Duplicate") return false; if (e.commandName == "Copy" || e.commandName == "Duplicate") DoCopy(parent); if (e.commandName == "Paste" || e.commandName == "Duplicate") DoPaste(parent); e.Use(); return true; } private static void DoCopy(Canvas2D parent) { EditorGUIUtility.systemCopyBuffer = SerializeSelectedElements(parent); } public static string SerializeSelectedElements(Canvas2D parent) { var selectedElements = parent.selection; // build a graph to serialize (will just contain the // nodes and edges we are interested in. var graph = new CopyPasteGraph(); foreach (var thing in selectedElements) { var dNode = thing as DrawableNode; if (dNode != null) { graph.AddNode(dNode.m_Node); foreach (var edge in NodeUtils.GetAllEdges(dNode.m_Node)) graph.AddEdge(edge); } var dEdge = thing as DrawableEdge; if (dEdge != null) { graph.AddEdge(dEdge.m_Edge); } } // serialize then break references var serialized = JsonUtility.ToJson(graph, true); return serialized; } public static CopyPasteGraph DeserializeSelectedElements(string toDeserialize) { try { return JsonUtility.FromJson(toDeserialize); } catch { // ignored. just means copy buffer was not a graph :( return null; } } private static void DoPaste(Canvas2D parent) { var copyText = EditorGUIUtility.systemCopyBuffer; if (string.IsNullOrEmpty(copyText)) return; var pastedGraph = DeserializeSelectedElements(copyText); if (pastedGraph == null) return; if (parent.presenter == null) return; var presenter = parent.presenter as GraphDataSource; if (presenter == null) return; var asset = presenter.graphAsset; if (asset == null) return; var graph = asset.graph; if (graph == null) return; var addedNodes = new List(); var nodeGuidMap = new Dictionary(); foreach (var node in pastedGraph.GetNodes()) { var oldGuid = node.guid; var newGuid = node.RewriteGuid(); nodeGuidMap[oldGuid] = newGuid; var drawState = node.drawState; var position = drawState.position; position.x += 30; position.y += 30; drawState.position = position; node.drawState = drawState; graph.AddNode(node); addedNodes.Add(node); } // only connect edges within pasted elements, discard // external edges. var addedEdges = new List(); foreach (var edge in pastedGraph.edges) { var outputSlot = edge.outputSlot; var inputSlot = edge.inputSlot; Guid remappedOutputNodeGuid; Guid remappedInputNodeGuid; if (nodeGuidMap.TryGetValue(outputSlot.nodeGuid, out remappedOutputNodeGuid) && nodeGuidMap.TryGetValue(inputSlot.nodeGuid, out remappedInputNodeGuid)) { var outputSlotRef = new SlotReference(remappedOutputNodeGuid, outputSlot.slotId); var inputSlotRef = new SlotReference(remappedInputNodeGuid, inputSlot.slotId); addedEdges.Add(graph.Connect(outputSlotRef, inputSlotRef)); } } graph.ValidateGraph(); parent.ReloadData(); parent.Invalidate(); parent.selection.Clear(); foreach (var element in parent.elements) { var drawableNode = element as DrawableNode; if (drawableNode != null && addedNodes.Any(x => x == drawableNode.m_Node)) { drawableNode.selected = true; parent.selection.Add(drawableNode); continue; } var drawableEdge = element as DrawableEdge; if (drawableEdge != null && addedEdges.Any(x => x == drawableEdge.m_Edge)) { drawableEdge.selected = true; parent.selection.Add(drawableEdge); } } parent.Repaint(); } } }*/