public void TryAndCopy() { if (SelectedNodes.Count > 0) { List <string> nodes = new List <string>(); foreach (UINode n in SelectedNodes) { nodes.Add(n.Node.GetJson()); } GraphCopyData cd = new GraphCopyData() { nodes = nodes, }; //copy to clipboard Clipboard.SetText(JsonConvert.SerializeObject(cd)); } }
public void TryAndPaste() { try { if (Graph.ReadOnly) { return; } Point mp = Mouse.GetPosition(ViewPort); mp.X = Math.Max(0, mp.X); mp.Y = Math.Max(0, mp.Y); mp.X = mp.X > ViewPort.ActualWidth ? 0 : mp.X; mp.Y = mp.Y > ViewPort.ActualHeight ? 0 : mp.Y; ToWorld(ref mp); string data = Clipboard.GetText(); if (string.IsNullOrEmpty(data)) { return; } GraphCopyData cd = JsonConvert.DeserializeObject <GraphCopyData>(data); if (cd.nodes == null || cd.nodes.Count == 0) { return; } List <UINode> added = new List <UINode>(); Dictionary <string, string> jsonContent = new Dictionary <string, string>(); Dictionary <string, Node> realLookup = new Dictionary <string, Node>(); for (int i = 0; i < cd.nodes.Count; i++) { string json = cd.nodes[i]; var unode = AddNodeFromJson(json, realLookup); if (unode != null) { added.Add(unode); jsonContent[unode.Node.Id] = json; } } double minX = float.MaxValue; double minY = float.MaxValue; //apply copied data to new nodes foreach (UINode n in added) { string json = null; if (jsonContent.TryGetValue(n.Node.Id, out json)) { n.Node.FromJson(realLookup, json); if (minX > n.Node.ViewOriginX) { minX = n.Node.ViewOriginX; } if (minY > n.Node.ViewOriginY) { minY = n.Node.ViewOriginY; } } } //offset nodes origin by mouse point position foreach (UINode n in added) { double dx = n.Node.ViewOriginX - minX; double dy = n.Node.ViewOriginY - minY; n.OffsetTo(mp.X + dx, mp.Y + dy); } Task.Delay(250).ContinueWith((Task t) => { App.Current.Dispatcher.Invoke(() => { foreach (UINode n in added) { //finally load visual connections n.LoadConnections(lookup); } if (added.Count > 0) { Modified = true; } }); }); } catch (Exception ex) { Console.WriteLine(ex.StackTrace); } }