예제 #1
0
        /// <summary>
        /// Helper for Undeo Redo System. Does not trigger new undo added to stack.
        /// </summary>
        /// <param name="json"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        public UINode AddNodeFromJson(string json, Point p)
        {
            try
            {
                Node.NodeData nd = JsonConvert.DeserializeObject <Node.NodeData>(json);

                if (nd == null)
                {
                    return(null);
                }

                Node   n     = null;
                UINode unode = null;

                n = Graph.CreateNode(nd.type);

                if (n != null)
                {
                    if (n is GraphInstanceNode)
                    {
                        GraphInstanceNode gn = (GraphInstanceNode)n;
                        gn.Load(nd.type);
                    }

                    n.Id = nd.id;

                    n.Width  = nd.width;
                    n.Height = nd.height;

                    Graph.Add(n);

                    n.FromJson(Graph.NodeLookup, json);

                    unode = new UINode(n, this, p.X, p.Y, XShift, YShift, Scale);
                    unode.HorizontalAlignment = HorizontalAlignment.Left;
                    unode.VerticalAlignment   = VerticalAlignment.Top;
                    lookup[n.Id] = unode;
                    ViewPort.Children.Add(unode);
                    GraphNodes.Add(unode);

                    Modified = true;

                    Task.Delay(250).ContinueWith(t =>
                    {
                        unode.LoadConnections(lookup);
                    });
                }

                return(unode);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
            }

            return(null);
        }
예제 #2
0
    void Start()
    {
        //Get components
        navAgent = GetComponent <NavMeshAgent>();
        core     = GameObject.Find("Core").transform;

        //Initialize
        cargo = new Node.NodeData();
        SetNode();
    }
예제 #3
0
        /// <summary>
        /// real lookup is required as the nodes added from json
        /// from a paste command will have new ids
        /// but will need to lookup the old ids
        /// to reconnect any that may have been connected when pasted
        /// </summary>
        /// <param name="json"></param>
        /// <param name="realLookup"></param>
        /// <returns></returns>
        protected UINode AddNodeFromJson(string json, Dictionary <string, Node> realLookup)
        {
            try
            {
                Node.NodeData nd = JsonConvert.DeserializeObject <Node.NodeData>(json);

                if (nd == null)
                {
                    return(null);
                }

                Node   n     = null;
                UINode unode = null;

                n = Graph.CreateNode(nd.type);

                if (n != null)
                {
                    if (n is GraphInstanceNode)
                    {
                        GraphInstanceNode gn = (GraphInstanceNode)n;
                        gn.Load(nd.type);
                    }

                    realLookup[nd.id] = n;

                    Graph.Add(n);

                    unode = new UINode(n, this, 0, 0, XShift, YShift, Scale);
                    unode.HorizontalAlignment = HorizontalAlignment.Left;
                    unode.VerticalAlignment   = VerticalAlignment.Top;
                    lookup[n.Id] = unode;
                    ViewPort.Children.Add(unode);
                    GraphNodes.Add(unode);

                    UndoRedoManager.AddUndo(new UndoCreateNode(Id, unode.Id, this));

                    Modified = true;
                }

                return(unode);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
                return(null);
            }
        }
예제 #4
0
        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;

                //apparently deserialize will throw an exception
                //it the content isn't json at atll
                try
                {
                    cd = JsonConvert.DeserializeObject <GraphCopyData>(data);
                }
                catch
                {
                    return;
                }

                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;

                //find minx and miny
                foreach (UINode n in added)
                {
                    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;

                    //also set node connections as needed
                    string json = null;
                    if (jsonContent.TryGetValue(n.Node.Id, out json))
                    {
                        Node.NodeData nd = JsonConvert.DeserializeObject <Node.NodeData>(json);
                        n.Node.SetConnections(realLookup, nd.outputs);
                    }

                    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);
            }
        }