Exemple #1
0
    /// <summary> Show right-click context menu </summary>
    public void ShowContextMenu()
    {
        GenericMenu contextMenu = new GenericMenu();
        Vector2     pos         = WindowToGridPosition(Event.current.mousePosition);

        if (hoveredNode != null)
        {
            Node node = hoveredNode;
            contextMenu.AddItem(new GUIContent("Remove"), false, () => graph.RemoveNode(node));
        }
        else
        {
            for (int i = 0; i < nodeTypes.Length; i++)
            {
                Type type       = nodeTypes[i];
                Type editorType = GetNodeEditor(type).GetType();

                string name = nodeTypes[i].ToString().Replace('.', '/');
                CustomNodeEditorAttribute attrib;
                if (NodeEditorUtilities.GetAttrib(editorType, out attrib))
                {
                    name = attrib.contextMenuName;
                }
                contextMenu.AddItem(new GUIContent(name), false, () => {
                    CreateNode(type, pos);
                });
            }
        }
        contextMenu.DropDown(new Rect(Event.current.mousePosition, Vector2.zero));
    }
Exemple #2
0
    /// <summary> Add items for the context menu when right-clicking this node. Override to add custom menu items. </summary>
    public override void AddContextMenuItems(GenericMenu menu)
    {
        menu.AddItem(new GUIContent("Init State Machines"), false,
                     () => (target as StateMachineGraph).parentMachine.InitStateMachines(false));
        menu.AddSeparator("");
        menu.AddItem(new GUIContent("Expand All"), false, () => (target as StateMachineGraph).ToggleExpandAll(false));
        menu.AddItem(new GUIContent("Collapse All"), false, () => (target as StateMachineGraph).ToggleExpandAll(true));
        menu.AddSeparator("");
        Vector2 pos       = NodeEditorWindow.current.WindowToGridPosition(Event.current.mousePosition);
        var     nodeTypes = NodeEditorReflection.nodeTypes.OrderBy(type => GetNodeMenuOrder(type)).ToArray();

        for (int i = 0; i < nodeTypes.Length; i++)
        {
            Type type = nodeTypes[i];

            //Get node context menu path
            string path = GetNodeMenuName(type);
            if (string.IsNullOrEmpty(path))
            {
                continue;
            }

            // Check if user is allowed to add more of given node type
            XNode.Node.DisallowMultipleNodesAttribute disallowAttrib;
            bool disallowed = false;
            if (NodeEditorUtilities.GetAttrib(type, out disallowAttrib))
            {
                int typeCount = target.nodes.Count(x => x.GetType() == type);
                if (typeCount >= disallowAttrib.max)
                {
                    disallowed = true;
                }
            }

            // Add node entry to context menu
            if (disallowed)
            {
                menu.AddItem(new GUIContent(path), false, null);
            }
            else
            {
                menu.AddItem(new GUIContent(path), false, () => {
                    XNode.Node node = CreateNode(type, pos);
                    NodeEditorWindow.current.AutoConnect(node);
                });
            }
        }
        menu.AddSeparator("");
        if (NodeEditorWindow.copyBuffer != null && NodeEditorWindow.copyBuffer.Length > 0)
        {
            menu.AddItem(new GUIContent("Paste"), false, () => NodeEditorWindow.current.PasteNodes(pos));
        }
        else
        {
            menu.AddDisabledItem(new GUIContent("Paste"));
        }
        menu.AddItem(new GUIContent("Preferences"), false, () => NodeEditorReflection.OpenPreferences());
        menu.AddCustomContextMenuItems(target);
    }
Exemple #3
0
        public virtual bool Update(INodeEditorData editor, INode node)
        {
            var target = node;
            var title  = target.ItemName;

            if (string.IsNullOrEmpty(title))
            {
                CreateNodeMenuAttribute attrib;
                var type = node.GetType();
                title = NodeEditorUtilities.GetAttrib(type, out attrib) ? attrib.nodeName : type.Name;
                node.SetName(title);
            }
            var renaming = NodeEditor.Renaming;

            if (NodeEditor.Renaming != 0 && target.IsSelected())
            {
                var controlId = GUIUtility.GetControlID(FocusType.Keyboard) + 1;
                if (renaming == 1)
                {
                    GUIUtility.keyboardControl        = controlId;
                    EditorGUIUtility.editingTextField = true;
                    NodeEditor.Renaming = 2;
                }

                var nodeName = EditorGUILayout.TextField(target.ItemName, NodeEditorResources.styles.nodeHeader,
                                                         GUILayout.Height(30));
                target.SetName(nodeName);

                if (EditorGUIUtility.editingTextField)
                {
                    return(true);
                }

                node.SetName(target.ItemName);
                NodeEditor.Renaming = 0;
            }
            else
            {
                GUILayout.Label(title, NodeEditorResources.styles.nodeHeader, GUILayout.Height(30));
            }

            return(true);
        }
Exemple #4
0
        public override string GetNodeMenuName(Type type)
        {
            if (type == typeof(CompoundTask))
            {
                return("Compound Task");
            }

            var subMenu = "";

            if (type.IsSubclassOf(typeof(TaskBase)))
            {
                subMenu = "Tasks/";
            }
            else if (type.IsSubclassOf(typeof(ContextualScorerBase)))
            {
                subMenu = "Ctx Scorers/";
            }
            else if (type.IsSubclassOf(typeof(OptionScorerBase)))
            {
                subMenu = "Option Scorers/";
            }
            else if (type.IsSubclassOf(typeof(CheckBase)))
            {
                subMenu = "Checks/";
            }

            //Check if type has the CreateNodeMenuAttribute
            Node.CreateNodeMenuAttribute attrib;
            if (NodeEditorUtilities.GetAttrib(type, out attrib)) // Return custom path
            {
                return(subMenu + attrib.menuName);
            }
            else // Return generated path
            {
                return(subMenu + ObjectNames.NicifyVariableName(type.ToString().Replace('.', '/')));
            }
        }
Exemple #5
0
    private void DrawFieldInfoDrawerGUI(FieldInfo fieldInfo)
    {
        Type   fieldType       = fieldInfo.FieldType;
        string fieldName       = fieldInfo.Name;
        string fieldPrettyName = fieldName.PrettifyCamelCase();
        object fieldValue      = fieldInfo.GetValue(target);

        object[] fieldAttribs = fieldInfo.GetCustomAttributes(false);

        HeaderAttribute headerAttrib;

        if (NodeEditorUtilities.GetAttrib(fieldAttribs, out headerAttrib))
        {
            EditorGUILayout.LabelField(headerAttrib.header);
        }

        EditorGUI.BeginChangeCheck();
        if (fieldType == typeof(int))
        {
            fieldValue = EditorGUILayout.IntField(fieldPrettyName, (int)fieldValue);
        }
        else if (fieldType == typeof(bool))
        {
            fieldValue = EditorGUILayout.Toggle(fieldPrettyName, (bool)fieldValue);
        }
        else if (fieldType.IsEnum)
        {
            fieldValue = EditorGUILayout.EnumPopup(fieldPrettyName, (Enum)fieldValue);
        }
        else if (fieldType == typeof(string))
        {
            if (fieldName == "name")
            {
                return;                      //Ignore 'name'
            }
            TextAreaAttribute textAreaAttrib;
            if (NodeEditorUtilities.GetAttrib(fieldAttribs, out textAreaAttrib))
            {
                fieldValue = EditorGUILayout.TextArea(fieldValue != null ? (string)fieldValue : "");
            }
            else
            {
                fieldValue = EditorGUILayout.TextField(fieldPrettyName, fieldValue != null ? (string)fieldValue : "");
            }
        }
        else if (fieldType == typeof(Rect))
        {
            if (fieldName == "position")
            {
                return;                          //Ignore 'position'
            }
            fieldValue = EditorGUILayout.RectField(fieldPrettyName, (Rect)fieldValue);
        }
        else if (fieldType == typeof(float))
        {
            fieldValue = EditorGUILayout.FloatField(fieldPrettyName, (float)fieldValue);
        }
        else if (fieldType == typeof(Vector2))
        {
            fieldValue = EditorGUILayout.Vector2Field(fieldPrettyName, (Vector2)fieldValue);
        }
        else if (fieldType == typeof(Vector3))
        {
            fieldValue = EditorGUILayout.Vector3Field(new GUIContent(fieldPrettyName), (Vector3)fieldValue);
        }
        else if (fieldType == typeof(Vector4))
        {
            fieldValue = EditorGUILayout.Vector4Field(fieldPrettyName, (Vector4)fieldValue);
        }
        else if (fieldType == typeof(Color))
        {
            fieldValue = EditorGUILayout.ColorField(fieldPrettyName, (Color)fieldValue);
        }
        else if (fieldType == typeof(AnimationCurve))
        {
            AnimationCurve curve = fieldValue != null ? (AnimationCurve)fieldValue : new AnimationCurve();
            curve = EditorGUILayout.CurveField(fieldPrettyName, curve);
            if (fieldValue != curve)
            {
                fieldInfo.SetValue(target, curve);
            }
        }
        else if (fieldType.IsSubclassOf(typeof(UnityEngine.Object)) || fieldType == typeof(UnityEngine.Object))
        {
            if (fieldName == "graph")
            {
                return;                       //Ignore 'graph'
            }
            fieldValue = EditorGUILayout.ObjectField(fieldPrettyName, (UnityEngine.Object)fieldValue, fieldType, true);
        }

        if (EditorGUI.EndChangeCheck())
        {
            fieldInfo.SetValue(target, fieldValue);
        }
    }