private void UnserializeAndPasteImpl(string operation, string data) { editorView.DlogObject.RegisterCompleteObjectUndo(operation); var copyPasteData = CopyPasteData.FromJson(data); this.InsertCopyPasteData(copyPasteData); }
public static void InsertCopyPasteData(this DlogGraphView graphView, CopyPasteData copyPasteData) { if (copyPasteData == null) { return; } foreach (var property in copyPasteData.Properties) { var copy = property.Copy(); graphView.DlogGraph.SanitizePropertyName(copy); graphView.DlogGraph.SanitizePropertyReference(copy, property.OverrideReferenceName); graphView.DlogGraph.AddProperty(copy); var dependentNodes = copyPasteData.Nodes.Where(node => node.Type == typeof(PropertyNode).FullName); foreach (var node in dependentNodes) { var root = JObject.Parse(node.NodeData); root["propertyGuid"] = copy.GUID; node.NodeData = root.ToString(Formatting.None); } } var remappedNodes = new List <SerializedNode>(); var remappedEdges = new List <SerializedEdge>(); graphView.DlogGraph.Paste(copyPasteData, remappedNodes, remappedEdges); // Compute the mean of the copied nodes. var centroid = Vector2.zero; var count = 1; foreach (var node in remappedNodes) { var position = node.DrawState.Position.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 drawState = node.DrawState; var positionRect = drawState.Position; var position = positionRect.position; position += viewCenter - centroid; positionRect.position = position; drawState.Position = positionRect; node.DrawState = drawState; } graphView.ClearSelection(); graphView.DlogGraph.QueueSelection(remappedNodes, remappedEdges); }
private string SerializeGraphElementsImpl(IEnumerable <GraphElement> elements) { var elementsList = elements.ToList(); var nodes = elementsList.OfType <AbstractNode>().Select(x => x.Owner); var edges = elementsList.OfType <Edge>().Select(x => x.userData).OfType <SerializedEdge>(); var properties = selection.OfType <BlackboardField>().Select(x => x.userData as AbstractProperty); // Collect the property nodes and get the corresponding properties var propertyNodeGuids = nodes.OfType <PropertyNode>().Select(x => x.PropertyGuid); var metaProperties = editorView.DlogObject.DlogGraph.Properties.Where(x => propertyNodeGuids.Contains(x.GUID)); var copyPasteData = new CopyPasteData(editorView, nodes, edges, properties, metaProperties); return(JsonUtility.ToJson(copyPasteData, true)); }
public void Paste(CopyPasteData copyPasteData, List <SerializedNode> remappedNodes, List <SerializedEdge> remappedEdges) { var nodeGuidMap = new Dictionary <string, string>(); var portGuidMap = new Dictionary <string, string>(); foreach (var node in copyPasteData.Nodes) { var oldGuid = node.GUID; var newGuid = Guid.NewGuid().ToString(); node.GUID = newGuid; nodeGuidMap[oldGuid] = newGuid; for (var i = 0; i < node.PortData.Count; i++) { var newPortGuid = Guid.NewGuid().ToString(); var oldPortGuid = node.PortData[i]; portGuidMap[oldPortGuid] = newPortGuid; node.PortData[i] = newPortGuid; } // Ugly magic to change dynamic port guid data if (node.Type == typeof(SelfNode).FullName) { var data = JObject.Parse(node.NodeData); var lines = JsonConvert.DeserializeObject <List <LineDataSelf> >(data.Value <string>("lines")); foreach (var currLine in lines) { currLine.PortGuidA = portGuidMap[currLine.PortGuidA]; currLine.PortGuidB = portGuidMap[currLine.PortGuidB]; } data["lines"] = new JValue(JsonConvert.SerializeObject(lines)); node.NodeData = data.ToString(Formatting.None); } else if (node.Type == typeof(NpcNode).FullName) { var data = JObject.Parse(node.NodeData); var lines = JsonConvert.DeserializeObject <List <LineDataNpc> >(data.Value <string>("lines")); foreach (var currLine in lines) { currLine.PortGuidA = portGuidMap[currLine.PortGuidA]; currLine.PortGuidB = portGuidMap[currLine.PortGuidB]; currLine.PortGuidC = portGuidMap[currLine.PortGuidC]; } data["lines"] = new JValue(JsonConvert.SerializeObject(lines)); node.NodeData = data.ToString(Formatting.None); } // offset the pasted node slightly so it's not on top of the original one var drawState = node.DrawState; var position = drawState.Position; position.x += 30; position.y += 30; drawState.Position = position; node.DrawState = drawState; remappedNodes.Add(node); AddNode(node); // add the node to the pasted node list pastedNodes.Add(node); } foreach (var edge in copyPasteData.Edges) { if ((nodeGuidMap.ContainsKey(edge.Input) && nodeGuidMap.ContainsKey(edge.Output)) && (portGuidMap.ContainsKey(edge.InputPort) && portGuidMap.ContainsKey(edge.OutputPort))) { var remappedOutputGuid = nodeGuidMap.ContainsKey(edge.Output) ? nodeGuidMap[edge.Output] : edge.Output; var remappedInputGuid = nodeGuidMap.ContainsKey(edge.Input) ? nodeGuidMap[edge.Input] : edge.Input; var remappedOutputPortGuid = portGuidMap.ContainsKey(edge.OutputPort) ? portGuidMap[edge.OutputPort] : edge.OutputPort; var remappedInputPortGuid = portGuidMap.ContainsKey(edge.InputPort) ? portGuidMap[edge.InputPort] : edge.InputPort; var remappedEdge = new SerializedEdge { Input = remappedInputGuid, Output = remappedOutputGuid, InputPort = remappedInputPortGuid, OutputPort = remappedOutputPortGuid, InputCapacity = edge.InputCapacity, OutputCapacity = edge.OutputCapacity }; remappedEdges.Add(remappedEdge); AddEdge(remappedEdge); } } }