public NodeMenuItem[] AddNodeMenuItems(NodeMachineModel model, Vector2 mousePosition, NodeMachineEditor editor)
        {
            HashSet <Type>         types     = StateNodeMenuHandler.LoadStateTypes(model);
            HashSet <NodeMenuItem> menuItems = new HashSet <NodeMenuItem>();
            int eventCount = 0;

            foreach (Type type in types)
            {
                foreach (MethodInfo method in type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
                {
                    EventAttribute methodEventInfo = method.GetCustomAttribute <EventAttribute>();
                    if (methodEventInfo != null)
                    {
                        eventCount++;
                        menuItems.Add(new NodeMenuItem("Events/" + type.ToString() + "/" + method.Name, () => {
                            EventNode node = new EventNode(type, method.Name, model, mousePosition);
                            editor.AddNode(node);
                        }, false, false));
                    }
                }
            }
            if (eventCount == 0)
            {
                menuItems.Add(new NodeMenuItem("Events", null, false, true));
            }
            return(menuItems.ToArray());
        }
Esempio n. 2
0
 void OnEnable()
 {
     windowIcon         = EditorGUIUtility.Load("Assets/NodeMachine/Editor/Editor Resources/State Machine Icon.png") as Texture2D;
     titlePlain.image   = windowIcon;
     titleUnsaved.image = windowIcon;
     titleContent       = titlePlain;
     NodeMachineGUIUtils.Init();
     _propertyMenu           = new PropertyMenu(this);
     _errorPanel             = new ErrorPanel(this);
     Undo.undoRedoPerformed += OnUndoRedo;
     if (_model == null)
     {
         _model = AssetDatabase.LoadAssetAtPath(AssetDatabase.GetAssetPath(_modelInstanceID), typeof(NodeMachineModel)) as NodeMachineModel;
     }
     if (_model != null)
     {
         LoadModel(_model);
         if (_selectedLink != null)
         {
             _selectedLink = _model.GetLinkFromID(_selectedLink.ID);
         }
         if (_selectedNode != null)
         {
             _selectedNode = _model.GetNodeFromID(_selectedNode.ID);
         }
     }
     else
     {
         _selectedLink = null;
         _selectedNode = null;
     }
     FindNodeMenuHandlers();
 }
        public NodeMenuItem[] AddNodeMenuItems(NodeMachineModel model, Vector2 mousePosition, NodeMachineEditor editor)
        {
            NodeMenuItem menuItem;

            if (model.machinePropsSchema.Count > 0)
            {
                menuItem = new NodeMenuItem("Conditional/Condition", () =>
                {
                    // TODO : CONDITIONS AND PROPERTIES WITH NO STANDARD TYPES???
                    KeyValuePair <string, Type> kvp      = model.machinePropsSchema.First();
                    Condition.ConditionType?tryParseType = Condition.ParseConditionType(kvp.Value);
                    if (tryParseType == null)
                    {
                        EditorUtility.DisplayDialog("Error", "There was an error while creating the condition!", "OK");
                        return;
                    }
                    Condition.ConditionType fieldType = (Condition.ConditionType)tryParseType;
                    string fieldName    = kvp.Key;
                    Condition condition = new Condition(fieldName, fieldType, Condition.Comparison.EQUAL, Condition.GetDefaultValue(fieldType, kvp.Value));
                    ConditionNode node  = new ConditionNode(editor._model, condition, mousePosition);
                    editor.AddNode(node);
                }, false, false);
            }
            else
            {
                menuItem = new NodeMenuItem("Conditional/Condition", null, false, true);
            }

            NodeMenuItem[] menuItems = { menuItem };
            return(menuItems);
        }
        public NodeMenuItem[] AddNodeMenuItems(NodeMachineModel model, Vector2 mousePosition, NodeMachineEditor editor)
        {
            HashSet <Type>         types     = LoadStateTypes(model);
            HashSet <NodeMenuItem> menuItems = new HashSet <NodeMenuItem>();
            int stateCount = 0;

            foreach (Type type in types)
            {
                foreach (MethodInfo method in type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
                {
                    StateAttribute methodStateInfo = method.GetCustomAttribute <StateAttribute>();
                    if (methodStateInfo != null)
                    {
                        if (!methodStateInfo.Visible)
                        {
                            continue;
                        }
                        stateCount++;
                        bool runOnEncounter = methodStateInfo.RunOnEncounter;
                        menuItems.Add(new NodeMenuItem("States/" + type.ToString() + "/" + method.Name, () => {
                            StateNode node      = new StateNode(type, method.Name, model, mousePosition);
                            node.runOnEncounter = runOnEncounter;
                            editor.AddNode(node);
                        }, false, false));
                    }
                }
            }
            if (stateCount == 0)
            {
                menuItems.Add(new NodeMenuItem("States", null, false, true));
            }
            return(menuItems.ToArray());
        }
        public NodeMenuItem[] NodeContextMenuItems(Node node, NodeMachineModel model)
        {
            ConditionNode conNode = node as ConditionNode;

            return(new NodeMenuItem[] {
                new NodeMenuItem("Collapse", () => conNode.Collapse(!conNode.collapsed), conNode.collapsed, false)
            });
        }
Esempio n. 6
0
 public EventNode(Type state, string method, NodeMachineModel model, Vector2 position) : base(model)
 {
     stateType        = state;
     stateTypeName    = stateType.AssemblyQualifiedName;
     eventMethodName  = method;
     transform        = new Rect(position.x, position.y, 150, 75);
     background       = "builtin skins/darkskin/images/node1.png";
     normalBackground = background;
     //activeBackground = "builtin skins/darkskin/images/node5.png";
 }
Esempio n. 7
0
 public void LoadModel(NodeMachineModel model)
 {
     _modelInstanceID = model.GetInstanceID();
     model.OnCheckin -= Repaint;
     model.OnCheckin += Repaint;
     model.OnSave    -= MarkSaved;
     model.OnSave    += MarkSaved;
     _model           = model;
     Repaint();
 }
Esempio n. 8
0
 public static EventNode GetEventNodeFromMethod(NodeMachineModel model, Type type, string methodName)
 {
     foreach (EventNode stateNode in model.GetNodes <EventNode>())
     {
         if (stateNode.stateType == type && stateNode.eventMethodName == methodName)
         {
             return(stateNode);
         }
     }
     return(null);
 }
Esempio n. 9
0
 public NodeFollower(Machine machine, Node startNode, NodeFollower parent)
 {
     this._machine  = machine;
     this._model    = machine._model;
     this.name      = machine.name;
     this.startNode = startNode;
     this.Parent    = parent;
     if (Parent != null)
     {
         parent.Children.Add(this);
     }
 }
Esempio n. 10
0
 void ReloadMachineModel()
 {
     if (_selectedMachine._model != null)
     {
         LoadModel(_selectedMachine.GetModel());
     }
     else
     {
         _model           = null;
         _modelInstanceID = -1;
     }
     Repaint();
 }
Esempio n. 11
0
        public FunctionNode(NodeMachineModel model, Vector2 position) : base(model)
        {
            transform.position = position;
            transform.size     = new Vector2(150, 75);
            background         = "Assets/NodeMachine/Editor/Editor Resources/function-node.png";
            int addNameNum = 1;

            while (model.GetFunction(name) != null)
            {
                name = "function (" + addNameNum + ")";
                addNameNum++;
            }
        }
Esempio n. 12
0
    static NodeMachineModel[] GetAllInstances()
    {
        string[]           guids = AssetDatabase.FindAssets("t:" + typeof(NodeMachineModel).Name);
        NodeMachineModel[] a     = new NodeMachineModel[guids.Length];

        for (int i = 0; i < guids.Length; i++)
        {
            string path = AssetDatabase.GUIDToAssetPath(guids[i]);
            a[i] = AssetDatabase.LoadAssetAtPath <NodeMachineModel>(path);
        }

        return(a);
    }
Esempio n. 13
0
    static string CreateStateScript(string name, string filepath, NodeMachineModel model)
    {
        if (name == null || filepath == null)
        {
            return(null);
        }

        if (model == null)
        {
            if (!EditorUtility.DisplayDialog("No model", "No model was specified. You will need to target to the model yourself.", "OK", "Cancel"))
            {
                return(null);
            }
        }
        else if (model._propertyType != null)
        {
            if (!EditorUtility.DisplayDialog("State script exists", "This model already has an associated state script. Creating a new state script will cause conflicts. Continue?", "Yes", "No"))
            {
                return(null);
            }
        }

        string classname      = Regex.Replace(name, "[^a-zA-Z0-9_]", "");
        int    nameAddition   = 0;
        string chosenFilepath = filepath + "/" + name + ".cs";

        while (File.Exists(filepath))
        {
            chosenFilepath = filepath + "/" + name + " (" + nameAddition + ").cs";
            nameAddition++;
        }

        string codeBase = File.ReadAllText(Application.dataPath + "/NodeMachine/Editor/StateScriptBase.txt");

        codeBase = codeBase.Replace("<name>", classname);
        if (model != null)
        {
            codeBase = codeBase.Replace("<model_name>", model.name);
            codeBase = codeBase.Replace("<warning_comment>", "");
            codeBase = codeBase.Replace("<remove_no_model>", "").Replace("</remove_no_model>", "");
        }
        else
        {
            codeBase = codeBase.Replace("<warning_comment>", "// !!WARNING!! State script is not targeted to a model!");
            codeBase = codeBase.Replace("<model_name>", "NOT SET");
        }

        File.WriteAllText(chosenFilepath, codeBase);
        AssetDatabase.Refresh();
        return(classname);
    }
Esempio n. 14
0
        void OnGUI()
        {
            GUIStyle wordWrap = new GUIStyle();

            wordWrap.wordWrap = true;
            Vector2 padding = new Vector2(5, 5);

            GUILayout.BeginArea(new Rect(padding, position.size - padding - new Vector2(5, 5)));
            GUILayout.BeginVertical();
            GUILayout.FlexibleSpace();
            GUILayout.Label(message, wordWrap);

            GUI.SetNextControlName("state name text field");
            text = GUILayout.TextField(text);

            if (!focused)
            {
                EditorGUI.FocusTextInControl("state name text field");
                focused = true;
            }

            model = EditorGUILayout.ObjectField(model, typeof(NodeMachineModel), false) as NodeMachineModel;

            GUILayout.FlexibleSpace();
            GUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();

            EditorGUI.BeginDisabledGroup(submitting);

            if (GUILayout.Button(okBtn, GUILayout.ExpandWidth(false)))
            {
                Submit(text);
            }
            if (GUILayout.Button(cancelBtn, GUILayout.ExpandWidth(false)))
            {
                Submit(null);
            }

            EditorGUI.EndDisabledGroup();

            GUILayout.EndHorizontal();
            GUILayout.EndVertical();
            GUILayout.EndArea();

            if (submitting)
            {
                Close();
            }
        }
Esempio n. 15
0
    public static bool OnOpenAsset(int instanceID, int line)
    {
        string           assetPath = AssetDatabase.GetAssetPath(instanceID);
        NodeMachineModel model     = AssetDatabase.LoadAssetAtPath <NodeMachineModel>(assetPath);

        if (model != null)
        {
            model.ReloadModel();
            NodeMachineEditor window = (NodeMachineEditor)EditorWindow.GetWindow(typeof(NodeMachineEditor));
            window.LoadModel(model);
            NodeMachineEditor.ShowWindow(window);
            return(true);
        }
        return(false); //let unity open it.
    }
        public NodeMenuItem[] NodeContextMenuItems(Node node, NodeMachineModel model)
        {
            FunctionNode funcNode = node as FunctionNode;

            return(new NodeMenuItem[] {
                new NodeMenuItem("Edit name", () => {
                    if (!funcNode.editingName)
                    {
                        funcNode.editingName = true;
                    }
                    else
                    {
                        FunctionNode func = model.GetFunction(funcNode.name);
                        if (func != null && func != funcNode)
                        {
                            EditorUtility.DisplayDialog("Function exists!", "A function with that name already exists.", "OK");
                            return;
                        }
                        funcNode.editingName = false;
                        model.UpdateFunctionCache();
                    }
                }, funcNode.editingName, false),
                new NodeMenuItem("Hide function group", () => {
                    foreach (Node hide in funcNode.GetFunctionGroup())
                    {
                        hide.visible = false;
                    }
                }, false, false),
                new NodeMenuItem("Reveal function group", () => {
                    foreach (Node hide in funcNode.GetFunctionGroup())
                    {
                        hide.visible = true;
                    }
                }, false, false),
                new NodeMenuItem("Remove function group", () => {
                    if (EditorUtility.DisplayDialog("Remove function group", "Are you sure you want to remove all nodes in this function?", "Yes", "No"))
                    {
                        foreach (Node remove in funcNode.GetFunctionGroup())
                        {
                            model.RemoveNode(remove);
                        }
                    }
                }, false, false)
            });
        }
Esempio n. 17
0
        public static HashSet <Type> LoadStateTypes(NodeMachineModel model)
        {
            Assembly           assembly   = Assembly.Load("Assembly-CSharp");
            IEnumerable <Type> stateTypes = assembly.GetTypes().Where(t => typeof(State).IsAssignableFrom(t));
            HashSet <Type>     types      = new HashSet <Type>();

            foreach (Type type in stateTypes)
            {
                if (type == typeof(State))
                {
                    continue;
                }
                StateTargetAttribute stateAttribute = type.GetCustomAttribute <StateTargetAttribute>();
                if (stateAttribute == null)
                {
                    continue;
                }
                if (stateAttribute.Model == model.name)
                {
                    types.Add(type);
                }
            }
            return(types);
        }
Esempio n. 18
0
 public void SetModel(NodeMachineModel model)
 {
     this._model = model;
 }
 public NodeMenuItem[] AddNodeMenuItems(NodeMachineModel model, Vector2 mousePosition, NodeMachineEditor editor)
 {
     return(null);
 }
Esempio n. 20
0
 protected RunnableNode(NodeMachineModel model) : base(model)
 {
 }
Esempio n. 21
0
 protected RunnableNode(NodeMachineModel model, Vector2 position) : base(model, position)
 {
 }
Esempio n. 22
0
 public TriggerNode(NodeMachineModel model, Vector2 position) : base(model)
 {
     transform.position = position;
     transform.size     = new Vector2(100, 75);
     background         = "Assets/NodeMachine/Editor/Editor Resources/trigger-node.png";
 }
Esempio n. 23
0
 public Node(NodeMachineModel model) : base(model.GetFreeNodeID())
 {
     transform  = new Rect();
     this.model = model;
 }
Esempio n. 24
0
 public EntryNode(NodeMachineModel model) : base(model)
 {
     transform  = new Rect(0, 0, 150, 75);
     background = "builtin skins/darkskin/images/node4.png";
 }
Esempio n. 25
0
 public GotoFunctionNode(NodeMachineModel model, Vector2 position) : base(model)
 {
     transform.position = position;
     transform.size     = new Vector2(150, 75);
     background         = "Assets/NodeMachine/Editor/Editor Resources/goto-node.png";
 }
Esempio n. 26
0
 public NodeMenuItem[] NodeContextMenuItems(Node node, NodeMachineModel model)
 {
     return(null);
 }
Esempio n. 27
0
 public RunFunctionNode(NodeMachineModel model, Vector2 position) : base(model)
 {
     transform.position = position;
     transform.size     = new Vector2(150, 100);
     background         = "builtin skins/darkskin/images/node2.png";
 }
Esempio n. 28
0
 public ConditionNode(NodeMachineModel model, Condition condition, Vector2 position) : base(model)
 {
     background     = "Assets/NodeMachine/Editor/Editor Resources/condition-node.png";
     transform      = new Rect(position.x, position.y, 160, 130);
     this.condition = condition;
 }
Esempio n. 29
0
 public BlankRunnableNode(NodeMachineModel model, Vector2 position) : base(model)
 {
     transform.position = position;
     transform.size     = new Vector2(100, 75);
     background         = "builtin skins/darkskin/images/node3.png";
 }
Esempio n. 30
0
 public ElseNode(NodeMachineModel model, Vector2 position) : base(model)
 {
     background = "Assets/NodeMachine/Editor/Editor Resources/else-node.png";
     transform  = new Rect(position.x - 25, position.y - 25, 50, 50);
 }