Esempio n. 1
0
        protected override void OnAddNode(Node node, Type type)
        {
            if (!typeof(State).IsAssignableFrom(node.GetType()))
            {
                EditorUtility.DisplayDialog("Could not add node " + type.Name + "!", "You can only add actions to a state.", "Cancel");
                return;
            }

            State       state  = node as State;
            StateAction action = (StateAction)ScriptableObject.CreateInstance(type);

            action.hideFlags = HideFlags.HideInHierarchy;
            action.name      = type.GetCategory() + "." + type.Name;
            state.Actions    = ArrayUtility.Add <StateAction> (state.Actions, action);
            if (EditorUtility.IsPersistent(state))
            {
                AssetDatabase.AddObjectToAsset(action, state);
                AssetDatabase.SaveAssets();
            }

            if (PreferencesEditor.GetBool(Preference.CloseActionBrowserOnAdd, false))
            {
                base.Close();
            }
        }
Esempio n. 2
0
        protected override void OnAddNode(Node node, Type type)
        {
            if (FsmEditor.SelectedTransition == null)
            {
                EditorUtility.DisplayDialog("Could not add node " + type.Name + "!", "Select a transition before you add a condition.", "Cancel");
                return;
            }
            Condition condition = (Condition)ScriptableObject.CreateInstance(type);

            condition.hideFlags = HideFlags.HideInHierarchy;
            condition.name      = type.GetCategory() + "." + type.Name;

            FsmEditor.SelectedTransition.Conditions = ArrayUtility.Add <Condition> (FsmEditor.SelectedTransition.Conditions, condition);

            if (EditorUtility.IsPersistent(FsmEditor.SelectedTransition))
            {
                AssetDatabase.AddObjectToAsset(condition, FsmEditor.SelectedTransition);
                AssetDatabase.SaveAssets();
            }

            if (PreferencesEditor.GetBool(Preference.CloseConditionBrowserOnAdd, false))
            {
                base.Close();
            }
        }
 public static void AddAction(StateAction action, State state)
 {
     state.Actions = ArrayUtility.Add <StateAction> (state.Actions, action);
     if (EditorUtility.IsPersistent(state))
     {
         AssetDatabase.AddObjectToAsset(action, state);
         AssetDatabase.SaveAssets();
     }
 }
Esempio n. 4
0
		private void AddTransition(Node fromNode,Node toNode){
			if (fromNode == null || toNode == null || toNode.GetType () == typeof(AnyState)) {
				return;
			}
			Transition transition= ScriptableObject.CreateInstance<Transition>();
			transition.hideFlags = HideFlags.HideInHierarchy;
			transition.Init(toNode,fromNode);
			if(EditorUtility.IsPersistent(fromNode)){
				AssetDatabase.AddObjectToAsset(transition,fromNode);
				AssetDatabase.SaveAssets();
			}
			fromNode.Transitions=ArrayUtility.Add<Transition>(fromNode.Transitions,transition);
			NodeInspector.Dirty ();	
		}
Esempio n. 5
0
        private void CreateCondition(Type type)
        {
            Condition condition = (Condition)ScriptableObject.CreateInstance(type);

            condition.name        = type.GetCategory() + "." + type.Name;
            condition.hideFlags   = HideFlags.HideInHierarchy;
            transition.Conditions = ArrayUtility.Add <Condition> (transition.Conditions, condition);
            if (EditorUtility.IsPersistent(transition))
            {
                AssetDatabase.AddObjectToAsset(condition, transition);
                AssetDatabase.SaveAssets();
            }
            this.ResetConditionList();
        }
Esempio n. 6
0
        private void CreateAction(Type type)
        {
            if (!type.IsSubclassOf(typeof(StateAction)) || state == null)
            {
                return;
            }
            StateAction action = (StateAction)ScriptableObject.CreateInstance(type);

            if (EditorUtility.IsPersistent(state))
            {
                AssetDatabase.AddObjectToAsset(action, state);
                AssetDatabase.SaveAssets();
            }
            state.Actions = ArrayUtility.Add <StateAction> (state.Actions, action);
        }
Esempio n. 7
0
        private void CreateAction(Type type)
        {
            StateAction action = (StateAction)ScriptableObject.CreateInstance(type);

            action.name      = type.GetCategory() + "." + type.Name;
            action.hideFlags = HideFlags.HideInHierarchy;
            state.Actions    = ArrayUtility.Add <StateAction> (state.Actions, action);

            if (EditorUtility.IsPersistent(state))
            {
                AssetDatabase.AddObjectToAsset(action, state);
                AssetDatabase.SaveAssets();
            }
            this.ResetActionList();
        }
Esempio n. 8
0
 private void CreateVariable(Type type)
 {
     if (FsmEditor.Root != null)
     {
         FsmVariable variable = (FsmVariable)ScriptableObject.CreateInstance(type);
         variable.Name            = "New " + type.Name.Replace("Fsm", "");
         variable.IsShared        = true;
         variable.hideFlags       = HideFlags.HideInHierarchy;
         FsmEditor.Root.Variables = ArrayUtility.Add <FsmVariable> (FsmEditor.Root.Variables, variable);
         scrollPosition.y         = Mathf.Infinity;
         if (EditorUtility.IsPersistent(FsmEditor.Root))
         {
             AssetDatabase.AddObjectToAsset(variable, FsmEditor.Root);
             AssetDatabase.SaveAssets();
         }
     }
 }
Esempio n. 9
0
        public static void CreateStateMachineAsset()
        {
            StateMachine stateMachine = AssetCreator.CreateAsset <StateMachine> (false);

            stateMachine.color = (int)NodeColor.Blue;
            stateMachine.Name  = stateMachine.name;

            FsmGameObject gameObject = ScriptableObject.CreateInstance <FsmGameObject> ();

            gameObject.Name      = "Owner";
            gameObject.hideFlags = HideFlags.HideInHierarchy;
            gameObject.IsHidden  = true;
            gameObject.IsShared  = true;

            stateMachine.Variables = ArrayUtility.Add <FsmVariable> (stateMachine.Variables, gameObject);
            AssetDatabase.AddObjectToAsset(gameObject, stateMachine);
            AssetDatabase.SaveAssets();


            AnyState state = FsmEditorUtility.AddNode <AnyState> (FsmEditor.Center, stateMachine);

            state.color = (int)NodeColor.Aqua;
            state.Name  = "Any State";
        }
        public static T AddNode <T>(Vector2 position, StateMachine parent)
        {
            if (parent == null)
            {
                Debug.LogWarning("Can't add node to parent state machine, because the parent state machine is null!");
                return(default(T));
            }
            Node node = (Node)ScriptableObject.CreateInstance(typeof(T));

            node.hideFlags = HideFlags.HideInHierarchy;

            node.Name    = FsmEditorUtility.GenerateUniqueNodeName <T> (parent.Root);
            node.Parent  = parent;
            parent.Nodes = ArrayUtility.Add <Node> (parent.Nodes, node);

            node.position = new Rect(position.x, position.y, FsmEditorStyles.StateWidth, FsmEditorStyles.StateHeight);
            UpdateNodeColor(node);

            if (EditorUtility.IsPersistent(parent))
            {
                AssetDatabase.AddObjectToAsset(node, parent);
            }

            if (node.GetType() == typeof(StateMachine))
            {
                node.position.width  = FsmEditorStyles.StateMachineWidth;
                node.position.height = FsmEditorStyles.StateMachineHeight;

                AnyState state = FsmEditorUtility.AddNode <AnyState> (FsmEditor.Center, (StateMachine)node);
                UpdateNodeColor(state);
                state.Name = "Any State";
            }

            AssetDatabase.SaveAssets();
            return((T)(object)node);
        }
        public static void Paste(Vector2 position, StateMachine stateMachine)
        {
            List <Node> copiedNodes = new List <Node> ();
            Vector2     center      = GetCenter(nodes);

            for (int i = 0; i < nodes.Count; i++)
            {
                Node origNode = nodes[i];
                List <FsmVariable> sharedVariables = new List <FsmVariable>();
                GetSharedVariables(origNode, ref sharedVariables);
                if (sharedVariables.Count > 0)
                {
                    string variableNames = string.Empty;
                    sharedVariables.Select(x => x.Name).ToList().ForEach(y => variableNames = (variableNames + (string.IsNullOrEmpty(variableNames)?"":",") + y));
                    if (EditorUtility.DisplayDialog("Paste Variables", "Copied states have reference to shared variables, do you want to paste those variables? (" + variableNames + ")", "Yes", "No"))
                    {
                        for (int j = 0; j < sharedVariables.Count; j++)
                        {
                            FsmVariable variable = sharedVariables[j];
                            stateMachine.SetVariable(variable.Name, variable.GetValue());
                        }
                    }
                }

                Node mNode = (Node)FsmUtility.Copy(origNode);
                mNode.Parent    = stateMachine;
                mNode.hideFlags = HideFlags.HideInHierarchy;
                if (mNode.IsStartNode && stateMachine.GetStartNode() != null)
                {
                    mNode.IsStartNode = false;
                }
                //mNode.Name = FsmEditorUtility.GenerateUniqueNodeName(mNode.GetType(),stateMachine);
                stateMachine.Nodes = ArrayUtility.Add <Node> (stateMachine.Nodes, mNode);

                mNode.position = new Rect(-(center.x - origNode.position.x) + position.x, -(center.y - origNode.position.y) + position.y, FsmEditorStyles.StateWidth, FsmEditorStyles.StateHeight);

                if (mNode.GetType() == typeof(StateMachine))
                {
                    mNode.position.width  = FsmEditorStyles.StateMachineWidth;
                    mNode.position.height = FsmEditorStyles.StateMachineHeight;
                }
                FsmEditorUtility.UpdateNodeColor(mNode);
                copiedNodes.Add(mNode);
            }

            for (int i = 0; i < copiedNodes.Count; i++)
            {
                Node mNode = copiedNodes [i];
                if (mNode is AnyState)
                {
                    bool     mOverride = EditorUtility.DisplayDialog("Override AnyState", "AnyState can only exist once per state machine. Do you want to override it?", "Yes", "No");
                    AnyState anyState  = stateMachine.Nodes.ToList().Find(x => x.GetType() == typeof(AnyState) && (mOverride && x != mNode || !mOverride && x == mNode)) as AnyState;
                    stateMachine.Nodes = ArrayUtility.Remove(stateMachine.Nodes, anyState);
                    FsmEditorUtility.DestroyImmediate(anyState);
                    FsmEditor.SelectedNodes.Clear();
                }
            }

            for (int i = 0; i < copiedNodes.Count; i++)
            {
                Node mNode = copiedNodes[i];

                foreach (Transition transition in mNode.Transitions)
                {
                    Node toNode = copiedNodes.Find(x => x.Name == transition.ToNode.Name) ?? stateMachine.Nodes.ToList().Find(x => x.Name == transition.ToNode.Name);
                    if (toNode != null)
                    {
                        transition.ToNode = toNode;
                    }
                    else
                    {
                        FsmEditorUtility.DestroyImmediate(transition);
                        mNode.Transitions = ArrayUtility.Remove(mNode.Transitions, transition);
                    }
                }
            }

            for (int i = 0; i < copiedNodes.Count; i++)
            {
                Node mNode = stateMachine.Nodes.ToList().Find(x => x.Name == copiedNodes[i].Name && x != copiedNodes[i]);
                if (mNode != null)
                {
                    copiedNodes[i].Name = FsmEditorUtility.GenerateUniqueNodeName(copiedNodes[i].GetType(), stateMachine);
                }
            }

            FsmEditorUtility.ParentChilds(stateMachine);
            nodes.Clear();
            EditorUtility.SetDirty(stateMachine);
            ErrorChecker.CheckForErrors();
        }
Esempio n. 12
0
        private void ResetConditionList()
        {
            if (transition == null)
            {
                return;
            }
            this.conditions    = this.transition.Conditions;
            this.conditionList = new ReorderableList(this.conditions, "Condition", true, true)
            {
                drawElementCallback = new ReorderableList.ElementCallbackDelegate(this.OnConditionElement),
                onReorderCallback   = new ReorderableList.ReorderCallbackDelegate(this.OnReorderConditionList),
                onAddCallback       = new ReorderableList.AddCallbackDelegate(delegate(){
                    FsmGUIUtility.SubclassMenu <Condition> (CreateCondition);
                }),
                onContextClick = new ReorderableList.ContextCallbackDelegate(delegate(int index){
                    FsmGUIUtility.ExecutableContextMenu(conditions[index], node).ShowAsContext();
                }),
                onHeaderClick = new ReorderableList.OnHeaderClick(delegate(){
                    GenericMenu menu = new GenericMenu();

                    if (conditions.Length > 0)
                    {
                        menu.AddItem(new GUIContent("Copy"), false, delegate {
                            copy = transition;
                        });
                    }
                    else
                    {
                        menu.AddDisabledItem(new GUIContent("Copy"));
                    }
                    if (copy != null && copy.Conditions.Length > 0)
                    {
                        menu.AddItem(new GUIContent("Paste After"), false, delegate() {
                            for (int i = 0; i < copy.Conditions.Length; i++)
                            {
                                ExecutableNode dest   = FsmUtility.Copy(copy.Conditions[i]);
                                transition.Conditions = ArrayUtility.Add <Condition>(transition.Conditions, (Condition)dest);
                                FsmEditorUtility.ParentChilds(transition);
                                NodeInspector.Dirty();
                            }
                        });
                        menu.AddItem(new GUIContent("Paste Before"), false, delegate() {
                            for (int i = 0; i < copy.Conditions.Length; i++)
                            {
                                ExecutableNode dest   = FsmUtility.Copy(copy.Conditions[i]);
                                transition.Conditions = ArrayUtility.Insert <Condition>(transition.Conditions, (Condition)dest, 0);
                                FsmEditorUtility.ParentChilds(transition);
                                NodeInspector.Dirty();
                            }
                        });
                        if (copy != transition)
                        {
                            menu.AddItem(new GUIContent("Replace"), false, delegate() {
                                for (int i = 0; i < transition.Conditions.Length; i++)
                                {
                                    FsmEditorUtility.DestroyImmediate(transition.Conditions[i]);
                                }
                                transition.Conditions = new Condition[0];
                                ResetConditionList();

                                for (int i = 0; i < copy.Conditions.Length; i++)
                                {
                                    ExecutableNode dest   = FsmUtility.Copy(copy.Conditions[i]);
                                    transition.Conditions = ArrayUtility.Add <Condition>(transition.Conditions, (Condition)dest);
                                    FsmEditorUtility.ParentChilds(transition);
                                    NodeInspector.Dirty();
                                }
                            });
                        }
                        else
                        {
                            menu.AddDisabledItem(new GUIContent("Replace"));
                        }
                    }
                    else
                    {
                        menu.AddDisabledItem(new GUIContent("Paste After"));
                        menu.AddDisabledItem(new GUIContent("Paste Before"));
                        menu.AddDisabledItem(new GUIContent("Replace"));
                    }
                    menu.ShowAsContext();
                }),
            };
            this.host.Repaint();
            if (FsmEditor.instance != null)
            {
                FsmEditor.instance.Repaint();
            }
        }
        private void ResetActionList()
        {
            SerializedObject   obj      = new SerializedObject(state);
            SerializedProperty elements = obj.FindProperty("actions");

            actionList = new ReorderableObjectList(obj, elements);

            actionList.drawHeaderCallback = delegate(Rect rect) {
                EditorGUI.LabelField(rect, "Actions");
            };

            actionList.onAddCallback = delegate(ReorderableObjectList list) {
                FsmGUIUtility.SubclassMenu <StateAction> (delegate(Type type){
                    StateAction action = (StateAction)ScriptableObject.CreateInstance(type);
                    action.name        = type.GetCategory() + "." + type.Name;
                    action.hideFlags   = HideFlags.HideInHierarchy;
                    state.Actions      = ArrayUtility.Add <StateAction> (state.Actions, action);

                    if (EditorUtility.IsPersistent(state))
                    {
                        AssetDatabase.AddObjectToAsset(action, state);
                        AssetDatabase.SaveAssets();
                    }
                    list.index = list.count;
                    EditorUtility.SetDirty(state);
                });
            };

            actionList.drawElementCallback = delegate(int index, bool selected) {
                StateAction action  = state.Actions [index];
                bool        enabled = action.IsEnabled;
                if (selected)
                {
                    GUIStyle selectBackground = new GUIStyle("MeTransitionSelectHead")
                    {
                        stretchHeight = false,
                    };
                    selectBackground.overflow = new RectOffset(-1, -2, -2, 2);
                    GUILayout.BeginVertical(selectBackground);
                }
                action.IsOpen = GUIDrawer.ObjectTitlebar(action, action.IsOpen, ref enabled, FsmGUIUtility.ExecutableContextMenu(action, state));
                if (selected)
                {
                    GUILayout.EndVertical();
                }
                action.IsEnabled = enabled;
                if (action.IsOpen)
                {
                    GUIDrawer.OnGUI(action);
                }
            };

            actionList.onRemoveCallback = delegate(ReorderableObjectList list) {
                StateAction action = state.Actions[list.index];
                state.Actions = ArrayUtility.Remove <StateAction> (state.Actions, action);
                FsmEditorUtility.DestroyImmediate(action);
                list.index = list.index - 1;
                ErrorChecker.CheckForErrors();
                EditorUtility.SetDirty(state);
            };

            actionList.onContextClick = delegate(int index) {
                FsmGUIUtility.ExecutableContextMenu(state.Actions [index], state).ShowAsContext();
            };

            actionList.onHeaderContextClick = delegate() {
                GenericMenu menu = new GenericMenu();

                if (state.Actions.Length > 0)
                {
                    menu.AddItem(new GUIContent("Copy"), false, delegate {
                        copy      = new List <StateAction>(state.Actions);
                        copyState = state;
                    });
                }
                else
                {
                    menu.AddDisabledItem(new GUIContent("Copy"));
                }

                if (copy == null)
                {
                    copy = new List <StateAction>();
                }

                copy.RemoveAll(x => x == null);
                if (copy.Count > 0)
                {
                    menu.AddItem(new GUIContent("Paste After"), false, delegate() {
                        for (int i = 0; i < copy.Count; i++)
                        {
                            ExecutableNode dest = FsmUtility.Copy(copy[i]);
                            state.Actions       = ArrayUtility.Add <StateAction>(state.Actions, (StateAction)dest);
                            FsmEditorUtility.ParentChilds(state);
                            EditorUtility.SetDirty(state);
                            //	NodeInspector.Dirty();
                            ErrorChecker.CheckForErrors();
                        }
                    });
                    menu.AddItem(new GUIContent("Paste Before"), false, delegate() {
                        for (int i = 0; i < copy.Count; i++)
                        {
                            ExecutableNode dest = FsmUtility.Copy(copy[i]);
                            state.Actions       = ArrayUtility.Insert <StateAction>(state.Actions, (StateAction)dest, 0);
                            FsmEditorUtility.ParentChilds(state);
                            EditorUtility.SetDirty(state);
                            //	NodeInspector.Dirty();
                            ErrorChecker.CheckForErrors();
                        }
                    });
                    if (copyState != state)
                    {
                        menu.AddItem(new GUIContent("Replace"), false, delegate() {
                            for (int i = 0; i < state.Actions.Length; i++)
                            {
                                FsmEditorUtility.DestroyImmediate(state.Actions[i]);
                            }
                            state.Actions = new StateAction[0];
                            ResetActionList();

                            for (int i = 0; i < copy.Count; i++)
                            {
                                ExecutableNode dest = FsmUtility.Copy(copy[i]);
                                state.Actions       = ArrayUtility.Add <StateAction>(state.Actions, (StateAction)dest);
                                FsmEditorUtility.ParentChilds(state);
                                EditorUtility.SetDirty(state);
                                //	NodeInspector.Dirty();
                                ErrorChecker.CheckForErrors();
                            }
                        });
                    }
                    else
                    {
                        menu.AddDisabledItem(new GUIContent("Replace"));
                    }
                }
                else
                {
                    menu.AddDisabledItem(new GUIContent("Paste After"));
                    menu.AddDisabledItem(new GUIContent("Paste Before"));
                    menu.AddDisabledItem(new GUIContent("Replace"));
                }
                menu.ShowAsContext();
            };

            this.host.Repaint();
            if (FsmEditor.instance != null)
            {
                FsmEditor.instance.Repaint();
            }
        }
Esempio n. 14
0
        private void ResetActionList()
        {
            this.actions    = this.state.Actions;
            this.actionList = new ReorderableList(this.actions, "Action", true, true)
            {
                drawElementCallback = new ReorderableList.ElementCallbackDelegate(this.OnActionElement),
                onReorderCallback   = new ReorderableList.ReorderCallbackDelegate(this.OnReorderList),
                onAddCallback       = new ReorderableList.AddCallbackDelegate(delegate(){
                    FsmGUIUtility.SubclassMenu <StateAction> (CreateAction);
                }),
                onContextClick = new ReorderableList.ContextCallbackDelegate(delegate(int index){
                    FsmGUIUtility.ExecutableContextMenu(actions[index], state).ShowAsContext();
                }),
                onHeaderClick = new ReorderableList.OnHeaderClick(delegate(){
                    GenericMenu menu = new GenericMenu();

                    if (actions.Length > 0)
                    {
                        menu.AddItem(new GUIContent("Copy"), false, delegate {
                            copy      = new List <StateAction>(actions);
                            copyState = state;
                        });
                    }
                    else
                    {
                        menu.AddDisabledItem(new GUIContent("Copy"));
                    }

                    if (copy == null)
                    {
                        copy = new List <StateAction>();
                    }

                    copy.RemoveAll(x => x == null);
                    if (copy.Count > 0)
                    {
                        menu.AddItem(new GUIContent("Paste After"), false, delegate() {
                            for (int i = 0; i < copy.Count; i++)
                            {
                                ExecutableNode dest = FsmUtility.Copy(copy[i]);
                                state.Actions       = ArrayUtility.Add <StateAction>(state.Actions, (StateAction)dest);
                                FsmEditorUtility.ParentChilds(state);
                                NodeInspector.Dirty();
                            }
                        });
                        menu.AddItem(new GUIContent("Paste Before"), false, delegate() {
                            for (int i = 0; i < copy.Count; i++)
                            {
                                ExecutableNode dest = FsmUtility.Copy(copy[i]);
                                state.Actions       = ArrayUtility.Insert <StateAction>(state.Actions, (StateAction)dest, 0);
                                FsmEditorUtility.ParentChilds(state);
                                NodeInspector.Dirty();
                            }
                        });
                        if (copyState != state)
                        {
                            menu.AddItem(new GUIContent("Replace"), false, delegate() {
                                for (int i = 0; i < state.Actions.Length; i++)
                                {
                                    FsmEditorUtility.DestroyImmediate(state.Actions[i]);
                                }
                                state.Actions = new StateAction[0];
                                ResetActionList();

                                for (int i = 0; i < copy.Count; i++)
                                {
                                    ExecutableNode dest = FsmUtility.Copy(copy[i]);
                                    state.Actions       = ArrayUtility.Add <StateAction>(state.Actions, (StateAction)dest);
                                    FsmEditorUtility.ParentChilds(state);
                                    NodeInspector.Dirty();
                                }
                            });
                        }
                        else
                        {
                            menu.AddDisabledItem(new GUIContent("Replace"));
                        }
                    }
                    else
                    {
                        menu.AddDisabledItem(new GUIContent("Paste After"));
                        menu.AddDisabledItem(new GUIContent("Paste Before"));
                        menu.AddDisabledItem(new GUIContent("Replace"));
                    }
                    menu.ShowAsContext();
                }),
            };
            this.host.Repaint();
            if (FsmEditor.instance != null)
            {
                FsmEditor.instance.Repaint();
            }
        }