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