예제 #1
0
        public static bool OnOpen(int instanceID, int line)
        {
            // Convert the instance ID to the TerraGraph object.
            TerraGraph graph = EditorUtility.InstanceIDToObject(instanceID) as TerraGraph;

            // Valid asset
            if (graph != null)
            {
                graph.RebuildConnections();
                TerraGraphEditor.Open(graph);
                return(true);
            }

            // Invalid asset
            return(false);
        }
예제 #2
0
        public static void ProcessGraphEditorEvents(Event e, TerraGraphEditor window)
        {
            switch (e.type)
            {
            case EventType.KeyDown:

                if (e.keyCode == KeyCode.Delete)
                {
                    foreach (TerraNode node in TerraGraphEditor.NodeSelection)
                    {
                        window.RemoveNode(node);
                    }

                    foreach (GraphComment comment in TerraGraphEditor.CommentSelection)
                    {
                        TerraGraphEditor.Graph.RemoveComment(comment);
                    }

                    TerraGraphEditor.NodeSelection.Clear();
                    TerraGraphEditor.CommentSelection.Clear();
                    TerraGraphEditor.Selecting = false;

                    e.Use();
                }

                if (e.keyCode == KeyCode.F)
                {
                    if (TerraGraphEditor.NodeSelection.Count != 0 || TerraGraphEditor.CommentSelection.Count != 0)
                    {
                        Vector2 currentCenter = TerraGraphEditor.Window.position.size * 0.5f;
                        Vector2 targetCenter  = TerraGraphEditor.NodeSelection[0].Rect.center;
                        Vector2 offset        = currentCenter - targetCenter;

                        foreach (TerraNode node in TerraGraphEditor.Graph.Nodes)
                        {
                            node.SetPosition(node.Rect.position + offset);
                        }
                        foreach (GraphComment comment in TerraGraphEditor.Graph.Comments)
                        {
                            comment.SetPosition(comment.Rect.position + offset);
                        }
                        TerraGUI.ApplyGridOffset(offset);

                        e.Use();
                    }
                }

                break;

            case EventType.MouseDown:

                if (e.button == 0)
                {
                    TerraGraphEditor.NodeSelection.Clear();
                    TerraGraphEditor.CommentSelection.Clear();
                    TerraGraphEditor.SelectionStart = e.mousePosition;
                    TerraGraphEditor.Selecting      = true;
                    GUI.FocusControl(null);
                    e.Use();
                }

                if (e.button == 1)
                {
                    TerraGraphEditor.NodeSelection.Clear();
                    TerraGraphEditor.CommentSelection.Clear();
                    GUI.FocusControl(null);
                    GenericMenu contextMenu = TerraUtility.CreateNodeContextMenu((t) => window.CreateNode(t, e.mousePosition));
                    contextMenu.AddSeparator("");
                    contextMenu.AddItem(new GUIContent("Add Comment"), false, () => TerraGraphEditor.Window.CreateComment(e.mousePosition));
                    contextMenu.ShowAsContext();
                    e.Use();
                }

                break;

            case EventType.MouseUp:

                if (e.button == 0)
                {
                    TerraGraphEditor.HeldSocket      = null;
                    TerraGraphEditor.ResizingComment = null;
                    TerraGraphEditor.Selecting       = false;
                    e.Use();
                }

                break;

            case EventType.MouseDrag:

                if (e.button == 0)
                {
                    if (!new Rect(new Vector2(0, 0), window.position.size).Contains(e.mousePosition))
                    {
                        TerraGraphEditor.NodeSelection.Clear();
                        TerraGraphEditor.CommentSelection.Clear();
                        TerraGraphEditor.Selecting       = false;
                        TerraGraphEditor.ResizingComment = null;
                        TerraGraphEditor.HeldSocket      = null;
                        GUI.FocusControl(null);
                        e.Use();
                    }

                    if (TerraGraphEditor.ResizingComment != null)
                    {
                        TerraGraphEditor.ResizingComment.SetSize(TerraGraphEditor.ResizingComment.Rect.size + e.delta);
                        e.Use();
                    }

                    else if (!TerraGraphEditor.Selecting)
                    {
                        foreach (TerraNode node in TerraGraphEditor.NodeSelection)
                        {
                            node.SetPosition(node.Rect.position + e.delta);
                        }

                        foreach (GraphComment comment in TerraGraphEditor.CommentSelection)
                        {
                            comment.SetPosition(comment.Rect.position + e.delta);
                        }

                        e.Use();
                    }
                }

                if (e.button == 2)
                {
                    TerraGUI.ApplyGridOffset(e.delta);

                    foreach (TerraNode node in TerraGraphEditor.Graph.Nodes)
                    {
                        node.SetPosition(node.Rect.position + e.delta);
                    }

                    foreach (GraphComment comment in TerraGraphEditor.Graph.Comments)
                    {
                        comment.SetPosition(comment.Rect.position + e.delta);
                    }

                    e.Use();
                }

                break;
            }
        }