Esempio n. 1
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. 3
0
        public static GenericMenu ExecutableContextMenu(ExecutableNode executable, Node node)
        {
            GenericMenu menu = new GenericMenu();

            if (executable == null)
            {
                return(menu);
            }
            menu.AddItem(new GUIContent("Enable"), executable.IsEnabled, delegate() {
                executable.IsEnabled = !executable.IsEnabled;
            });

            menu.AddSeparator("");

            menu.AddItem(new GUIContent("Find Script"), false, delegate() {
                MonoScript[] monoScriptArray = (MonoScript[])Resources.FindObjectsOfTypeAll(typeof(MonoScript));
                Selection.activeObject       = monoScriptArray.ToList().Find(x => x.GetClass() == executable.GetType());
            });

            menu.AddItem(new GUIContent("Edit Script"), false, delegate() {
                MonoScript[] monoScriptArray = (MonoScript[])Resources.FindObjectsOfTypeAll(typeof(MonoScript));
                Selection.activeObject       = monoScriptArray.ToList().Find(x => x.GetClass() == executable.GetType());
                AssetDatabase.OpenAsset(Selection.activeObject);
            });

            menu.AddSeparator("");

            bool moveDown     = false;
            int  currentIndex = -1;

            if (executable.GetType().IsSubclassOf(typeof(StateAction)))
            {
                State state = node as State;
                currentIndex = Array.IndexOf(state.Actions, executable);
                moveDown     = currentIndex + 1 < state.Actions.Length;
            }
            else
            {
                currentIndex = Array.IndexOf(FsmEditor.SelectedTransition.Conditions, executable);
                moveDown     = currentIndex + 1 < FsmEditor.SelectedTransition.Conditions.Length;
            }

            if (currentIndex - 1 >= 0)
            {
                menu.AddItem(new GUIContent("Move Up"), false, delegate() {
                    if (executable.GetType().IsSubclassOf(typeof(StateAction)))
                    {
                        State state   = node as State;
                        state.Actions = ArrayUtility.MoveItem(state.Actions, currentIndex, currentIndex - 1);
                    }
                    else
                    {
                        FsmEditor.SelectedTransition.Conditions = ArrayUtility.MoveItem(FsmEditor.SelectedTransition.Conditions, currentIndex, currentIndex - 1);
                    }
                    NodeInspector.Dirty();
                });
            }
            else
            {
                menu.AddDisabledItem(new GUIContent("Move Up"));
            }

            if (moveDown)
            {
                menu.AddItem(new GUIContent("Move Down"), false, delegate() {
                    if (executable.GetType().IsSubclassOf(typeof(StateAction)))
                    {
                        State state   = node as State;
                        state.Actions = ArrayUtility.MoveItem(state.Actions, currentIndex, currentIndex + 1);
                    }
                    else
                    {
                        FsmEditor.SelectedTransition.Conditions = ArrayUtility.MoveItem(FsmEditor.SelectedTransition.Conditions, currentIndex, currentIndex + 1);
                    }
                    NodeInspector.Dirty();
                });
            }
            else
            {
                menu.AddDisabledItem(new GUIContent("Move Down"));
            }

            menu.AddSeparator("");

            menu.AddItem(new GUIContent("Copy"), false, delegate() {
                executableCopy = executable;
            });

            if (executableCopy != null)
            {
                menu.AddItem(new GUIContent("Paste After"), false, delegate() {
                    ExecutableNode dest = FsmUtility.Copy(executableCopy);
                    if (dest.GetType().IsSubclassOf(typeof(StateAction)))
                    {
                        State state   = node as State;
                        state.Actions = ArrayUtility.Insert <StateAction>(state.Actions, (StateAction)dest, currentIndex + 1);
                    }
                    else
                    {
                        FsmEditor.SelectedTransition.Conditions = ArrayUtility.Insert <Condition>(FsmEditor.SelectedTransition.Conditions, (Condition)dest, currentIndex + 1);
                    }
                    FsmEditorUtility.ParentChilds(node);
                    NodeInspector.Dirty();
                });

                menu.AddItem(new GUIContent("Paste Before"), false, delegate() {
                    ExecutableNode dest = FsmUtility.Copy(executableCopy);
                    if (dest.GetType().IsSubclassOf(typeof(StateAction)))
                    {
                        State state   = node as State;
                        state.Actions = ArrayUtility.Insert <StateAction>(state.Actions, (StateAction)dest, currentIndex);
                    }
                    else
                    {
                        FsmEditor.SelectedTransition.Conditions = ArrayUtility.Insert <Condition>(FsmEditor.SelectedTransition.Conditions, (Condition)dest, currentIndex);
                    }
                    FsmEditorUtility.ParentChilds(node);
                    NodeInspector.Dirty();
                });


                menu.AddItem(new GUIContent("Replace"), false, delegate() {
                    ExecutableNode dest = FsmUtility.Copy(executableCopy);
                    if (dest.GetType().IsSubclassOf(typeof(StateAction)))
                    {
                        State state = node as State;
                        FsmEditorUtility.DestroyImmediate(state.Actions[currentIndex]);
                        state.Actions = ArrayUtility.RemoveAt <StateAction>(state.Actions, currentIndex);
                        state.Actions = ArrayUtility.Insert <StateAction>(state.Actions, (StateAction)dest, currentIndex);
                    }
                    else
                    {
                        FsmEditorUtility.DestroyImmediate(FsmEditor.SelectedTransition.Conditions[currentIndex]);
                        FsmEditor.SelectedTransition.Conditions = ArrayUtility.RemoveAt <Condition>(FsmEditor.SelectedTransition.Conditions, currentIndex);
                        FsmEditor.SelectedTransition.Conditions = ArrayUtility.Insert <Condition>(FsmEditor.SelectedTransition.Conditions, (Condition)dest, currentIndex);
                    }

                    FsmEditorUtility.ParentChilds(node);
                    NodeInspector.Dirty();
                });
            }
            else
            {
                menu.AddDisabledItem(new GUIContent("Paste After"));
                menu.AddDisabledItem(new GUIContent("Paste Before"));
                menu.AddDisabledItem(new GUIContent("Replace"));
            }
            menu.AddSeparator("");

            menu.AddItem(new GUIContent("Remove"), false, delegate() {
                if (executable.GetType().IsSubclassOf(typeof(StateAction)))
                {
                    State state   = node as State;
                    state.Actions = ArrayUtility.Remove <StateAction> (state.Actions, (StateAction)executable);
                }
                else
                {
                    FsmEditor.SelectedTransition.Conditions = ArrayUtility.Remove <Condition>(FsmEditor.SelectedTransition.Conditions, (Condition)executable);
                }

                FsmEditorUtility.DestroyImmediate(executable);
                NodeInspector.Dirty();
            });

            return(menu);
        }
Esempio n. 4
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();
            }
        }