Inheritance: NodeTree_Element
        public override NodeTree_Element_Option OnGUI()
        {
            NodeTree_Element_Option option = null;

            foldout = EditorGUILayout.Foldout(foldout, Title);
            if (foldout)
            {
                GUILayout.BeginHorizontal();
                GUILayout.Space(25.0f);
                GUILayout.BeginVertical();

                foreach (NodeTree_Element el in SubItems)
                {
                    NodeTree_Element_Option temp = el.OnGUI();
                    if (temp != null)
                    {
                        option = temp;
                    }
                }

                GUILayout.EndVertical();
                GUILayout.EndHorizontal();
            }

            return(option);
        }
Beispiel #2
0
 public void SelectOption(NodeTree_Element_Option option)
 {
     CurrentlyPlacing = option;
 }
Beispiel #3
0
        private void GUIHandleEvents(Rect graphArea, float graphXOffset)
        {
            Event   evt       = Event.current;
            Vector2 mPos      = evt.mousePosition + new Vector2(-graphXOffset, 0.0f),
                    localMPos = mPos + CamOffset;

            switch (evt.type)
            {
            case EventType.MouseUp:
                draggingMouseDown = false;
                break;

            case EventType.MouseDrag:
                if (draggingMouseDown && draggingWindowID < -1)
                {
                    CamOffset -= evt.delta;
                    Repaint();
                }
                break;

            case EventType.MouseDown:
                if (evt.button == 0)
                {
                    if (CurrentlyPlacing != null)
                    {
                        Node n = CurrentlyPlacing.NodeFactory(graph, new Rect(localMPos, Vector2.one));

                        //Double-check that the node is serializable.
                        if ((n.GetType().Attributes & System.Reflection.TypeAttributes.Serializable) == 0)
                        {
                            EditorUtility.DisplayDialog("Not serializable!",
                                                        "This node, type '" + n.GetType().ToString() +
                                                        "', isn't marked with the 'Serializable' attribute! " +
                                                        "Fix this problem in code before using this node.",
                                                        "OK");
                        }
                        else
                        {
                            graph.AddNode(n);
                            if (!unsavedStr.Contains("added node"))
                            {
                                unsavedStr = "added node, ";
                            }
                            Repaint();
                        }

                        CurrentlyPlacing = null;
                    }
                    else
                    {
                        activeWindowID = -2;                                 //TODO: Should this be -1? Also check the conditional below.
                        foreach (Node n in graph.Nodes)
                        {
                            if (n.Pos.Contains(localMPos))
                            {
                                activeWindowID = n.UID;
                            }
                        }

                        if (activeWindowID == -2)
                        {
                            EditorGUIUtility.editingTextField = false;
                        }
                    }
                }
                else if (evt.button == 1)
                {
                    //If a node is currently being placed, cancel it.
                    if (CurrentlyPlacing != null)
                    {
                        CurrentlyPlacing = null;
                    }
                    //Otherwise, see if we can drag the view.
                    else
                    {
                        draggingMouseDown = true;
                        draggingWindowID  = -2;

                        foreach (Node n in graph.Nodes)
                        {
                            if (n.Pos.Contains(localMPos))
                            {
                                activeWindowID   = n.UID;
                                draggingWindowID = activeWindowID;
                            }
                        }
                        if (graph.OutputPos.Contains(mPos))
                        {
                            draggingWindowID = -1;
                        }
                    }
                }
                break;

            case EventType.ContextClick:
                //If a node is currently being placed, cancel it.
                if (CurrentlyPlacing != null)
                {
                    CurrentlyPlacing = null;
                }
                break;

            case EventType.ValidateCommand:
                //Keeping this here in case we want to react to special events later.
                if (!EditorGUIUtility.editingTextField)
                {
                    //switch (evt.commandName)
                    //{

                    //}
                }
                break;

            case EventType.KeyDown:
                //Add certain kinds of nodes for different keystrokes.

                /*
                 * Node nd = null;
                 * switch (evt.keyCode)
                 * {
                 *      case KeyCode.Plus:
                 *      case KeyCode.KeypadPlus:
                 *              if (!EditorGUIUtility.editingTextField)
                 *              {
                 *                      nd = new SimpleNode(new Rect(localMPos, Vector2.one),
                 *                                                              "'f1' + 'f2'", "Add",
                 *                                                              new SimpleNode.Param("f1", 0.0f),
                 *                                                              new SimpleNode.Param("f2", 0.0f));
                 *              }
                 *              break;
                 *      case KeyCode.Minus:
                 *      case KeyCode.KeypadMinus:
                 *              if (!EditorGUIUtility.editingTextField)
                 *              {
                 *                      nd = new SimpleNode(new Rect(localMPos, Vector2.one),
                 *                                                              "'f1' - 'f2'", "Subtract",
                 *                                                              new SimpleNode.Param("f1", 0.0f),
                 *                                                              new SimpleNode.Param("f2", 0.0f));
                 *              }
                 *              break;
                 *      case KeyCode.Asterisk:
                 *      case KeyCode.KeypadMultiply:
                 *              if (!EditorGUIUtility.editingTextField)
                 *              {
                 *                      nd = new SimpleNode(new Rect(localMPos, Vector2.one),
                 *                                                              "'f1' * 'f2'", "Multiply",
                 *                                                              new SimpleNode.Param("f1", 1.0f),
                 *                                                              new SimpleNode.Param("f2", 1.0f));
                 *              }
                 *              break;
                 *      case KeyCode.Slash:
                 *      case KeyCode.Backslash:
                 *      case KeyCode.KeypadDivide:
                 *              if (!EditorGUIUtility.editingTextField)
                 *              {
                 *                      nd = new SimpleNode(new Rect(localMPos, Vector2.one),
                 *                                                              "'f1' / 'f2'", "Divide",
                 *                                                              new SimpleNode.Param("f1", float.NaN),
                 *                                                              new SimpleNode.Param("f2", 1.0f));
                 *              }
                 *              break;
                 * }
                 * if (nd != null && !EditorGUIUtility.editingTextField)
                 * {
                 *      graph.AddNode(nd);
                 *      if (!unsavedStr.Contains("added node"))
                 *              unsavedStr += "added node, ";
                 *      Repaint();
                 * }
                 */
                break;
            }
        }
Beispiel #4
0
        void OnGUI()
        {
            //Draw the node creation choices.
            Rect nodeChoicesArea = new Rect(0.0f, 0.0f, NodeChoiceSpace, position.height);

            GUI.Box(nodeChoicesArea, GUIUtil.WhitePixel);
            GUILayout.BeginArea(nodeChoicesArea);
            if (CurrentlyPlacing == null)
            {
                if (selectedGraph < 0)
                {
                    GUILayout.Label("No graph is\nselected for editing.");
                }
                else
                {
                    GUILayout.Label("Click on an option, then\nclick in the graph to place it.");
                    GUILayout.Label("Mouse over an option to\nget more info about it.");
                    GUILayout.Space(25.0f);

                    foreach (NodeTree_Element el in NewNodeOptions)
                    {
                        NodeTree_Element_Option opt = el.OnGUI();
                        if (opt != null)
                        {
                            SelectOption(opt);
                            break;
                        }
                    }
                }
            }
            else
            {
                GUILayout.Label("Left-click in the graph\nto place " + CurrentlyPlacing.Name);
                GUILayout.Label("Right-click in the graph\nto cancel its placement");
            }
            GUILayout.EndArea();


            //Draw the side-bar.
            Rect sidebarArea = new Rect(NodeChoiceSpace, 0, OptionsSpace, position.height);

            GUI.Box(sidebarArea, GUIUtil.WhitePixel);
            GUILayout.BeginArea(sidebarArea);
            GUILeftArea();
            GUILayout.EndArea();
            GUIUtil.DrawLine(new Vector2(NodeChoiceSpace + OptionsSpace + 4.0f, 0.0f),
                             new Vector2(NodeChoiceSpace + OptionsSpace + 4.0f, position.height),
                             2.0f, Color.black);
            if (graph == null)
            {
                return;
            }

            //Respond to UI events.
            Rect graphArea = new Rect(NodeChoiceSpace + OptionsSpace, 0.0f,
                                      position.width - NodeChoiceSpace - OptionsSpace,
                                      position.height);

            GUIHandleEvents(graphArea, NodeChoiceSpace + OptionsSpace);

            //Draw the various windows.
            GUILayout.BeginArea(graphArea);
            BeginWindows();
            Rect oldPos, newPos;

            foreach (Node n in graph.Nodes)
            {
                oldPos = new Rect(n.Pos.position - CamOffset, n.Pos.size);
                newPos = GUINode(oldPos, n);

                if (Mathf.Abs(oldPos.x - newPos.x) >= 2.0f ||
                    Mathf.Abs(oldPos.y - newPos.y) >= 2.0f)
                {
                    if (!unsavedStr.Contains("moved node"))
                    {
                        unsavedStr += "moved node, ";
                    }
                }

                newPos.position += CamOffset;
                n.Pos            = newPos;
            }

            oldPos = new Rect(graph.OutputPos.position - CamOffset, graph.OutputPos.size);
            newPos = GUINode(oldPos, null);
            if (Mathf.Abs(oldPos.x - newPos.x) >= 2.0f ||
                Mathf.Abs(oldPos.y - newPos.y) >= 2.0f)
            {
                if (!unsavedStr.Contains("moved graph output node"))
                {
                    unsavedStr += "moved graph output node, ";
                }
            }
            newPos.position += CamOffset;
            graph.OutputPos  = newPos;

            EndWindows();
            GUILayout.EndArea();
        }
Beispiel #5
0
        private void GUIHandleEvents(Rect graphArea, float graphXOffset)
        {
            Event evt = Event.current;
            Vector2 mPos = evt.mousePosition + new Vector2(-graphXOffset, 0.0f),
                    localMPos = mPos + CamOffset;
            switch (evt.type)
            {
                case EventType.MouseUp:
                    draggingMouseDown = false;
                    break;
                case EventType.MouseDrag:
                    if (draggingMouseDown && draggingWindowID < -1)
                    {
                        CamOffset -= Event.current.delta;
                        Repaint();
                    }
                    break;
                case EventType.MouseDown:
                    if (Event.current.button == 0)
                    {
                        if (CurrentlyPlacing != null)
                        {
                            Node n = CurrentlyPlacing.NodeFactory(Grph, new Rect(localMPos, Vector2.one));

                            //Double-check that the node is serializable.
                            if ((n.GetType().Attributes & System.Reflection.TypeAttributes.Serializable) == 0)
                            {
                                EditorUtility.DisplayDialog("Not serializable!",
                                                            "This node, type '" + n.GetType().ToString() +
                                                                "', isn't marked with the 'Serializable' attribute! " +
                                                                "Fix this problem in code before using this node.",
                                                            "OK");
                            }
                            else
                            {
                                Grph.AddNode(n);
                                if (!unsavedStr.Contains("added node"))
                                    unsavedStr = "added node, ";
                                Repaint();
                            }

                            CurrentlyPlacing = null;
                        }
                        else
                        {
                            activeWindowID = -2;
                            foreach (Node n in Grph.Nodes)
                            {
                                if (n.Pos.Contains(mPos))
                                {
                                    activeWindowID = n.UID;
                                }
                            }

                            if (activeWindowID == -2)
                            {
                                EditorGUIUtility.editingTextField = false;
                            }
                        }
                    }
                    else if (Event.current.button == 1)
                    {
                        //If a node is currently being placed, cancel it.
                        if (CurrentlyPlacing != null)
                        {
                            CurrentlyPlacing = null;
                        }
                        //Otherwise, see if we can drag the view.
                        else
                        {
                            draggingMouseDown = true;
                            draggingWindowID = -2;

                            foreach (Node n in Grph.Nodes)
                            {
                                if (n.Pos.Contains(mPos))
                                {
                                    activeWindowID = n.UID;
                                    draggingWindowID = activeWindowID;
                                }
                            }
                            if (Grph.OutputPos.Contains(mPos))
                            {
                                draggingWindowID = -1;
                            }
                        }
                    }
                    break;

                case EventType.ContextClick:
                    //If a node is currently being placed, cancel it.
                    if (CurrentlyPlacing != null)
                        CurrentlyPlacing = null;
                    break;

                case EventType.ValidateCommand:
                    //Keeping this here in case we want to react to special events later.
                    if (!EditorGUIUtility.editingTextField)
                    {
                        //switch (evt.commandName)
                        //{

                        //}
                    }
                    break;

                case EventType.KeyDown:
                    //Add certain kinds of nodes for different keystrokes.
                    Node nd = null;
                    switch (evt.keyCode)
                    {
                        case KeyCode.Plus:
                        case KeyCode.KeypadPlus:
                            if (!EditorGUIUtility.editingTextField)
                            {
                                nd = new SimpleNode(new Rect(localMPos, Vector2.one),
                                                    "'f1' + 'f2'", "Add",
                                                    new SimpleNode.Param("f1", 0.0f),
                                                    new SimpleNode.Param("f2", 0.0f));
                            }
                            break;
                        case KeyCode.Minus:
                        case KeyCode.KeypadMinus:
                            if (!EditorGUIUtility.editingTextField)
                            {
                                nd = new SimpleNode(new Rect(localMPos, Vector2.one),
                                                    "'f1' - 'f2'", "Subtract",
                                                    new SimpleNode.Param("f1", 0.0f),
                                                    new SimpleNode.Param("f2", 0.0f));
                            }
                            break;
                        case KeyCode.Asterisk:
                        case KeyCode.KeypadMultiply:
                            if (!EditorGUIUtility.editingTextField)
                            {
                                nd = new SimpleNode(new Rect(localMPos, Vector2.one),
                                                    "'f1' * 'f2'", "Multiply",
                                                    new SimpleNode.Param("f1", 1.0f),
                                                    new SimpleNode.Param("f2", 1.0f));
                            }
                            break;
                        case KeyCode.Slash:
                        case KeyCode.Backslash:
                        case KeyCode.KeypadDivide:
                            if (!EditorGUIUtility.editingTextField)
                            {
                                nd = new SimpleNode(new Rect(localMPos, Vector2.one),
                                                    "'f1' / 'f2'", "Divide",
                                                    new SimpleNode.Param("f1", float.NaN),
                                                    new SimpleNode.Param("f2", 1.0f));
                            }
                            break;
                    }
                    if (nd != null && !EditorGUIUtility.editingTextField)
                    {
                        Grph.AddNode(nd);
                        if (!unsavedStr.Contains("added node"))
                            unsavedStr += "added node, ";
                        Repaint();
                    }
                    break;
            }
        }
Beispiel #6
0
 public void SelectOption(NodeTree_Element_Option option)
 {
     CurrentlyPlacing = option;
 }