コード例 #1
0
        void HandleEvents(Event e, Vector2 canvasMousePos)
        {
            //variable is set as well, so that  nodes know if they can be clicked
            var inspectorWithScrollbar = new Rect(inspectorRect.x, inspectorRect.y, inspectorRect.width + 14, inspectorRect.height);

            allowClick = !inspectorWithScrollbar.Contains(e.mousePosition) && !blackboardRect.Contains(e.mousePosition);
            if (!allowClick)
            {
                return;
            }


            //Tilt '`' opens up the compelte context menu browser
            if (e.type == EventType.KeyDown && e.keyCode == KeyCode.BackQuote)
            {
                CompleteContextMenu.Show(GetAddNodeMenu(canvasMousePos), e.mousePosition, string.Format("Add {0} Node", this.GetType().Name));
            }



            //right click canvas context menu. Basicaly for adding new nodes
            if (e.type == EventType.MouseDown && e.button == 1)
            {
                var menu = GetAddNodeMenu(canvasMousePos);

                if (Node.copiedNodes != null && Node.copiedNodes[0].GetType().IsSubclassOf(baseNodeType))
                {
                    if (Node.copiedNodes.Length == 1)
                    {
                        menu.AddItem(new GUIContent(string.Format("Paste Node ({0})", Node.copiedNodes[0].name)), false, () => { var newNode = Node.copiedNodes[0].Duplicate(this); newNode.nodePosition = canvasMousePos; });
                    }
                    else
                    if (Node.copiedNodes.Length > 1)
                    {
                        menu.AddItem(new GUIContent(string.Format("Paste Nodes ({0})", Node.copiedNodes.Length.ToString())), false, () =>
                        {
                            var newNodes             = Graph.CopyNodesToGraph(Node.copiedNodes.ToList(), this);
                            var diff                 = newNodes[0].nodeRect.center - canvasMousePos;
                            newNodes[0].nodePosition = canvasMousePos;
                            for (var i = 1; i < newNodes.Count; i++)
                            {
                                newNodes[i].nodePosition -= diff;
                            }
                        });
                    }
                }

                menu.ShowAsContext();
                e.Use();
            }
        }
コード例 #2
0
ファイル: EDITOR_Graph.cs プロジェクト: joceycesup/Cannibals
        void HandleEvents(Event e, Vector2 canvasMousePos)
        {
            //we also undo graph pans
            if (e.type == EventType.MouseDown && e.button == 2)
            {
                Undo.RegisterCompleteObjectUndo(this, "Graph Pan");
            }

            //variable is set as well, so that  nodes know if they can be clicked
            var inspectorWithScrollbar = new Rect(inspectorRect.x, inspectorRect.y, inspectorRect.width + 14, inspectorRect.height);

            allowClick = !inspectorWithScrollbar.Contains(e.mousePosition) && !blackboardRect.Contains(e.mousePosition);
            if (!allowClick)
            {
                return;
            }

            //Shortcuts
            if (e.type == EventType.KeyUp && GUIUtility.keyboardControl == 0)
            {
                //Delete
                if (e.keyCode == KeyCode.Delete || e.keyCode == KeyCode.Backspace)
                {
                    if (multiSelection != null && multiSelection.Count > 0)
                    {
                        foreach (var obj in multiSelection.ToArray())
                        {
                            if (obj is Node)
                            {
                                RemoveNode(obj as Node);
                            }
                            if (obj is Connection)
                            {
                                RemoveConnection(obj as Connection);
                            }
                        }
                        multiSelection = null;
                    }

                    if (selectedNode != null)
                    {
                        RemoveNode(selectedNode);
                        currentSelection = null;
                    }

                    if (selectedConnection != null)
                    {
                        RemoveConnection(selectedConnection);
                        currentSelection = null;
                    }
                    e.Use();
                }

                //Duplicate
                if (e.keyCode == KeyCode.D && e.control)
                {
                    if (multiSelection != null && multiSelection.Count > 0)
                    {
                        var newNodes = CopyNodesToGraph(multiSelection.OfType <Node>().ToList(), this);
                        multiSelection = newNodes.Cast <object>().ToList();
                    }
                    if (selectedNode != null)
                    {
                        currentSelection = selectedNode.Duplicate(this);
                    }
                    //Connections can't be duplicated by themselves. They do so as part of multiple node duplication though.
                    e.Use();
                }
            }


            //Tilt '`' or 'space' opens up the compelte context menu browser
            if (e.type == EventType.KeyDown && !e.shift && (e.keyCode == KeyCode.BackQuote || e.keyCode == KeyCode.Space))
            {
                CompleteContextMenu.Show(GetAddNodeMenu(canvasMousePos), e.mousePosition, string.Format("Add {0} Node", this.GetType().FriendlyName()), baseNodeType);
            }


            //Right click canvas context menu. Basicaly for adding new nodes.
            if (e.type == EventType.ContextClick)
            {
                var menu = GetAddNodeMenu(canvasMousePos);

                if (Node.copiedNodes != null && Node.copiedNodes[0].GetType().IsSubclassOf(baseNodeType))
                {
                    menu.AddSeparator("/");
                    if (Node.copiedNodes.Length == 1)
                    {
                        menu.AddItem(new GUIContent(string.Format("Paste Node ({0})", Node.copiedNodes[0].GetType().Name)), false, () =>
                        {
                            var newNode          = Node.copiedNodes[0].Duplicate(this);
                            newNode.nodePosition = canvasMousePos;
                            currentSelection     = newNode;
                        });
                    }
                    else if (Node.copiedNodes.Length > 1)
                    {
                        menu.AddItem(new GUIContent(string.Format("Paste Nodes ({0})", Node.copiedNodes.Length.ToString())), false, () =>
                        {
                            var newNodes             = Graph.CopyNodesToGraph(Node.copiedNodes.ToList(), this);
                            var diff                 = newNodes[0].nodeRect.center - canvasMousePos;
                            newNodes[0].nodePosition = canvasMousePos;
                            for (var i = 1; i < newNodes.Count; i++)
                            {
                                newNodes[i].nodePosition -= diff;
                            }
                            multiSelection = newNodes.Cast <object>().ToList();
                        });
                    }
                }

                if (NCPrefs.useBrowser)
                {
                    CompleteContextMenu.Show(menu, e.mousePosition, string.Format("Add {0} Node", this.GetType().FriendlyName()), baseNodeType);
                }
                else
                {
                    menu.ShowAsContext();
                }
                e.Use();
            }
        }
コード例 #3
0
        void HandleEvents(Event e, Vector2 canvasMousePos)
        {
            //we also undo graph pans?
            if (e.button == 2 && (e.type == EventType.MouseDown || e.type == EventType.MouseUp))
            {
                Undo.RegisterCompleteObjectUndo(this, "Graph Pan");
            }

            //variable is set as well, so that  nodes know if they can be clicked
            var inspectorWithMargins  = inspectorRect.ExpandBy(14);
            var blackboardWithMargins = blackboardRect.ExpandBy(14);

            allowClick = !inspectorWithMargins.Contains(e.mousePosition) && !blackboardWithMargins.Contains(e.mousePosition);
            if (!allowClick)
            {
                return;
            }

            //Shortcuts
            if (e.type == EventType.ValidateCommand || e.type == EventType.Used)
            {
                if (e.commandName == "Copy" || e.commandName == "Cut")
                {
                    List <Node> selection = null;;
                    if (Graph.selectedNode != null)
                    {
                        selection = new List <Node> {
                            Graph.selectedNode
                        };
                    }
                    if (Graph.multiSelection != null && Graph.multiSelection.Count > 0)
                    {
                        selection = Graph.multiSelection.Cast <Node>().ToList();
                    }
                    if (selection != null)
                    {
                        Node.copiedNodes = Graph.CloneNodes(selection).ToArray();
                        if (e.commandName == "Cut")
                        {
                            foreach (Node node in selection)
                            {
                                this.RemoveNode(node);
                            }
                        }
                    }
                    e.Use();
                }
                if (e.commandName == "Paste")
                {
                    if (Node.copiedNodes != null && Node.copiedNodes.Length > 0)
                    {
                        TryPasteNodesInGraph(Node.copiedNodes, canvasMousePos + new Vector2(500, 500) / zoomFactor);
                    }
                    e.Use();
                }
            }

            //Shortcuts
            if (e.type == EventType.KeyUp && GUIUtility.keyboardControl == 0)
            {
                //Delete
                if (e.keyCode == KeyCode.Delete || e.keyCode == KeyCode.Backspace)
                {
                    if (multiSelection != null && multiSelection.Count > 0)
                    {
                        foreach (var obj in multiSelection.ToArray())
                        {
                            if (obj is Node)
                            {
                                RemoveNode(obj as Node);
                            }
                            if (obj is Connection)
                            {
                                RemoveConnection(obj as Connection);
                            }
                        }
                        multiSelection = null;
                    }

                    if (selectedNode != null)
                    {
                        RemoveNode(selectedNode);
                        currentSelection = null;
                    }

                    if (selectedConnection != null)
                    {
                        RemoveConnection(selectedConnection);
                        currentSelection = null;
                    }
                    e.Use();
                }

                //Duplicate
                if (e.keyCode == KeyCode.D && e.control)
                {
                    if (multiSelection != null && multiSelection.Count > 0)
                    {
                        var newNodes = CloneNodes(multiSelection.OfType <Node>().ToList(), this);
                        multiSelection = newNodes.Cast <object>().ToList();
                    }
                    if (selectedNode != null)
                    {
                        currentSelection = selectedNode.Duplicate(this);
                    }
                    //Connections can't be duplicated by themselves. They do so as part of multiple node duplication.
                    e.Use();
                }
            }


            //Tilt '`' or 'space' opens up the complete context menu browser
            if (e.type == EventType.KeyDown && !e.shift && (e.keyCode == KeyCode.BackQuote || e.keyCode == KeyCode.Space))
            {
                CompleteContextMenu.Show(GetAddNodeMenu(canvasMousePos), e.mousePosition, string.Format("Add {0} Node", this.GetType().FriendlyName()), baseNodeType);
            }


            //Right click canvas context menu. Basicaly for adding new nodes.
            if (e.type == EventType.ContextClick)
            {
                var menu = GetAddNodeMenu(canvasMousePos);
                if (Node.copiedNodes != null && Node.copiedNodes[0].GetType().IsSubclassOf(baseNodeType))
                {
                    menu.AddSeparator("/");
                    if (Node.copiedNodes.Length == 1)
                    {
                        menu.AddItem(new GUIContent(string.Format("Paste Node ({0})", Node.copiedNodes[0].GetType().FriendlyName())), false, () => { TryPasteNodesInGraph(Node.copiedNodes, canvasMousePos); });
                    }
                    else if (Node.copiedNodes.Length > 1)
                    {
                        menu.AddItem(new GUIContent(string.Format("Paste Nodes ({0})", Node.copiedNodes.Length.ToString())), false, () => { TryPasteNodesInGraph(Node.copiedNodes, canvasMousePos); });
                    }
                }

                if (NCPrefs.useBrowser)
                {
                    menu.ShowAsBrowser(e.mousePosition, string.Format("Add {0} Node", this.GetType().FriendlyName()), baseNodeType);
                }
                else
                {
                    menu.ShowAsContext();
                }
                e.Use();
            }
        }