private void UnserializeAndPasteImplementation(string operationName, string data) { Undo.RegisterCompleteObjectUndo(CodeGraph.Instance.GraphObject, operationName); var pastedGraph = CopyPasteGraphData.FromJson(data); this.InsertCopyPasteGraph(pastedGraph); }
private string SerializeGraphElementsImplementation(IEnumerable <GraphElement> elements) { // var groups = elements.OfType<ShaderGroup>().Select(x => x.userData); var nodes = elements.OfType <AbstractNode>().ToList(); var edges = elements.OfType <Edge>().ToList(); // var inputs = selection.OfType<BlackboardField>().Select(x => x.userData as ShaderInput); // var notes = enumerable.OfType<StickyNote>().Select(x => x.userData); // Collect the property nodes and get the corresponding properties // var propertyNodeGuids = nodes.OfType<PropertyNode>().Select(x => x.propertyGuid); // var metaProperties = this.graph.properties.Where(x => propertyNodeGuids.Contains(x.guid)); // Collect the keyword nodes and get the corresponding keywords // var keywordNodeGuids = nodes.OfType<KeywordNode>().Select(x => x.keywordGuid); // var metaKeywords = this.graph.keywords.Where(x => keywordNodeGuids.Contains(x.guid)); var assetGUID = AssetDatabase.AssetPathToGUID(CodeGraph.Instance.GraphObject.CodeGraphData.AssetPath); var nodePositions = new Dictionary <string, Rect>(); foreach (var node in nodes) { nodePositions.Add(node.GUID, node.GetPosition()); } var graph = new CopyPasteGraphData(assetGUID, nodes, nodePositions, edges); return(JsonUtility.ToJson(graph, true)); }
public static void InsertCopyPasteGraph(this CodeGraphView graphView, CopyPasteGraphData copyGraph) { if (copyGraph == null) { return; } var nodeGuidMap = new Dictionary <string, string>(); var nodeGuidMapReverse = new Dictionary <string, string>(); var remappedNodes = new List <AbstractNode>(); foreach (var node in copyGraph.Nodes) { var oldGuid = node.GUID; var newGuid = Guid.NewGuid().ToString(); nodeGuidMap[newGuid] = oldGuid; nodeGuidMapReverse[oldGuid] = newGuid; node.GUID = newGuid; remappedNodes.Add(node); } // Compute the mean of the copied nodes. var centroid = Vector2.zero; var count = 1; foreach (var node in remappedNodes) { var position = copyGraph.NodePositions[nodeGuidMap[node.GUID]].position; centroid += (position - centroid) / count; ++count; } // Get the center of the current view var viewCenter = graphView.contentViewContainer.WorldToLocal(graphView.layout.center); foreach (var node in remappedNodes) { var positionRect = copyGraph.NodePositions[nodeGuidMap[node.GUID]]; var position = positionRect.position; position += viewCenter - centroid; positionRect.position = position; node.SetPosition(positionRect); } remappedNodes.ForEach(graphView.AddElement); copyGraph.Edges.ToList().ForEach(graphView.Add); // Add new elements to selection graphView.ClearSelection(); copyGraph.Edges.ToList().ForEach(graphView.AddToSelection); remappedNodes.ForEach(graphView.AddToSelection); }