Example #1
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;
            }
        }