Beispiel #1
0
        void AddCommandCallback(object obj)
        {
            AddCommandOperation commandOperation = obj as AddCommandOperation;

            Sequence sequence = commandOperation.sequence;

            if (sequence == null)
            {
                return;
            }

            sequence.GetFungusScript().ClearSelectedCommands();

            Command newCommand = Undo.AddComponent(sequence.gameObject, commandOperation.commandType) as Command;

            sequence.GetFungusScript().AddSelectedCommand(newCommand);
            newCommand.parentSequence = sequence;

            // Let command know it has just been added to the sequence
            newCommand.OnCommandAdded(sequence);

            Undo.RecordObject(sequence, "Set command type");
            if (commandOperation.index < sequence.commandList.Count - 1)
            {
                sequence.commandList[commandOperation.index] = newCommand;
            }
            else
            {
                sequence.commandList.Add(newCommand);
            }
        }
Beispiel #2
0
        protected void Copy()
        {
            Sequence     sequence     = target as Sequence;
            FungusScript fungusScript = sequence.GetFungusScript();

            if (fungusScript == null ||
                fungusScript.selectedSequence == null)
            {
                return;
            }

            CommandCopyBuffer commandCopyBuffer = CommandCopyBuffer.GetInstance();

            commandCopyBuffer.Clear();

            // Scan through all commands in execution order to see if each needs to be copied
            foreach (Command command in fungusScript.selectedSequence.commandList)
            {
                if (fungusScript.selectedCommands.Contains(command))
                {
                    System.Type type       = command.GetType();
                    Command     newCommand = Undo.AddComponent(commandCopyBuffer.gameObject, type) as Command;
                    System.Reflection.FieldInfo[] fields = type.GetFields();
                    foreach (System.Reflection.FieldInfo field in fields)
                    {
                        field.SetValue(newCommand, field.GetValue(command));
                    }
                }
            }
        }
Beispiel #3
0
        protected void SelectNext()
        {
            Sequence     sequence     = target as Sequence;
            FungusScript fungusScript = sequence.GetFungusScript();

            int lastSelectedIndex = -1;

            if (fungusScript.selectedCommands.Count > 0)
            {
                for (int i = 0; i < fungusScript.selectedSequence.commandList.Count; i++)
                {
                    Command commandInSequence = fungusScript.selectedSequence.commandList[i];

                    foreach (Command selectedCommand in fungusScript.selectedCommands)
                    {
                        if (commandInSequence == selectedCommand)
                        {
                            lastSelectedIndex = i;
                        }
                    }
                }
            }
            if (lastSelectedIndex < fungusScript.selectedSequence.commandList.Count - 1)
            {
                fungusScript.ClearSelectedCommands();
                fungusScript.AddSelectedCommand(fungusScript.selectedSequence.commandList[lastSelectedIndex + 1]);
            }

            Repaint();
        }
Beispiel #4
0
        protected void Delete()
        {
            Sequence     sequence     = target as Sequence;
            FungusScript fungusScript = sequence.GetFungusScript();

            if (fungusScript == null ||
                fungusScript.selectedSequence == null)
            {
                return;
            }

            for (int i = fungusScript.selectedSequence.commandList.Count - 1; i >= 0; --i)
            {
                Command command = fungusScript.selectedSequence.commandList[i];
                foreach (Command selectedCommand in fungusScript.selectedCommands)
                {
                    if (command == selectedCommand)
                    {
                        command.OnCommandRemoved(sequence);

                        Undo.RecordObject(fungusScript.selectedSequence, "Delete");
                        fungusScript.selectedSequence.commandList.RemoveAt(i);
                        Undo.DestroyObjectImmediate(command);

                        break;
                    }
                }
            }

            Undo.RecordObject(fungusScript, "Delete");
            fungusScript.ClearSelectedCommands();

            Repaint();
        }
Beispiel #5
0
        protected void Paste()
        {
            Sequence     sequence     = target as Sequence;
            FungusScript fungusScript = sequence.GetFungusScript();

            if (fungusScript == null ||
                fungusScript.selectedSequence == null)
            {
                return;
            }

            CommandCopyBuffer commandCopyBuffer = CommandCopyBuffer.GetInstance();

            // Find where to paste commands in sequence (either at end or after last selected command)
            int pasteIndex = fungusScript.selectedSequence.commandList.Count;

            if (fungusScript.selectedCommands.Count > 0)
            {
                for (int i = 0; i < fungusScript.selectedSequence.commandList.Count; ++i)
                {
                    Command command = fungusScript.selectedSequence.commandList[i];

                    foreach (Command selectedCommand in fungusScript.selectedCommands)
                    {
                        if (command == selectedCommand)
                        {
                            pasteIndex = i + 1;
                        }
                    }
                }
            }

            foreach (Command command in commandCopyBuffer.GetCommands())
            {
                // Using the Editor copy / paste functionality instead instead of reflection
                // because this does a deep copy of the command properties.
                if (ComponentUtility.CopyComponent(command))
                {
                    if (ComponentUtility.PasteComponentAsNew(fungusScript.gameObject))
                    {
                        Command[] commands      = fungusScript.GetComponents <Command>();
                        Command   pastedCommand = commands.Last <Command>();
                        if (pastedCommand != null)
                        {
                            fungusScript.selectedSequence.commandList.Insert(pasteIndex++, pastedCommand);
                        }
                    }

                    // This stops the user pasting the command manually into another game object.
                    ComponentUtility.CopyComponent(fungusScript.transform);
                }
            }

            Repaint();
        }
Beispiel #6
0
        public FungusScript GetFungusScript()
        {
            Sequence s = GetSequence();

            if (s == null)
            {
                return(null);
            }

            return(s.GetFungusScript());
        }
Beispiel #7
0
        /**
         * The Event Handler should call this method when the event is detected.
         */
        public virtual bool ExecuteSequence()
        {
            if (parentSequence == null)
            {
                return(false);
            }

            FungusScript fungusScript = parentSequence.GetFungusScript();

            return(fungusScript.ExecuteSequence(parentSequence));
        }
Beispiel #8
0
        public virtual bool AddOption(string text, Sequence targetSequence)
        {
            gameObject.SetActive(true);

            bool addedOption = false;

            foreach (Button button in cachedButtons)
            {
                if (!button.gameObject.activeSelf)
                {
                    button.gameObject.SetActive(true);

                    Text textComponent = button.GetComponentInChildren <Text>();
                    if (textComponent != null)
                    {
                        textComponent.text = text;
                    }

                    Sequence sequence = targetSequence;

                    button.onClick.AddListener(delegate {
                        StopAllCoroutines();                         // Stop timeout
                        Clear();
                        gameObject.SetActive(false);

                        // Hide the active Say dialog in case it's still being displayed
                        SayDialog activeSayDialog = SetSayDialog.GetActiveSayDialog();
                        if (activeSayDialog != null)
                        {
                            activeSayDialog.ShowDialog(false);
                        }

                        if (sequence != null)
                        {
                                                        #if UNITY_EDITOR
                            // Select the new target sequence in the Fungus Script window
                            FungusScript fungusScript     = sequence.GetFungusScript();
                            fungusScript.selectedSequence = sequence;
                                                        #endif

                            sequence.ExecuteCommand(0);
                        }
                    });

                    addedOption = true;
                    break;
                }
            }

            return(addedOption);
        }
Beispiel #9
0
        public virtual void ExecuteSequence(Sequence s)
        {
            OnExit();
            Sequence sequence = GetSequence();

            if (sequence != null)
            {
                sequence.Stop();
                FungusScript fungusScript = sequence.GetFungusScript();
                if (fungusScript != null)
                {
                    fungusScript.ExecuteSequence(s);
                }
            }
        }
Beispiel #10
0
        protected void SelectNone()
        {
            Sequence     sequence     = target as Sequence;
            FungusScript fungusScript = sequence.GetFungusScript();

            if (fungusScript == null ||
                fungusScript.selectedSequence == null)
            {
                return;
            }

            Undo.RecordObject(fungusScript, "Select None");
            fungusScript.ClearSelectedCommands();

            Repaint();
        }
Beispiel #11
0
        protected void SelectAll()
        {
            Sequence     sequence     = target as Sequence;
            FungusScript fungusScript = sequence.GetFungusScript();

            if (fungusScript == null ||
                fungusScript.selectedSequence == null)
            {
                return;
            }

            fungusScript.ClearSelectedCommands();
            Undo.RecordObject(fungusScript, "Select All");
            foreach (Command command in fungusScript.selectedSequence.commandList)
            {
                fungusScript.AddSelectedCommand(command);
            }

            Repaint();
        }
        public override void OnInspectorGUI()
        {
            SequenceInspector sequenceInspector = target as SequenceInspector;
            Sequence          sequence          = sequenceInspector.sequence;

            if (sequence == null)
            {
                return;
            }

            FungusScript fungusScript = sequence.GetFungusScript();

            SequenceEditor sequenceEditor = Editor.CreateEditor(sequence) as SequenceEditor;

            sequenceEditor.DrawSequenceGUI(fungusScript);
            DestroyImmediate(sequenceEditor);

            Command inspectCommand = null;

            if (fungusScript.selectedCommands.Count == 1)
            {
                inspectCommand = fungusScript.selectedCommands[0];
            }

            if (Application.isPlaying &&
                inspectCommand != null &&
                inspectCommand.parentSequence != sequence)
            {
                Repaint();
                return;
            }

            if (inspectCommand != null)
            {
                CommandEditor commandEditor = Editor.CreateEditor(inspectCommand) as CommandEditor;
                commandEditor.DrawCommandInspectorGUI();
                DestroyImmediate(commandEditor);
            }

            Repaint();
        }
Beispiel #13
0
        protected void SelectPrevious()
        {
            Sequence     sequence     = target as Sequence;
            FungusScript fungusScript = sequence.GetFungusScript();

            int  firstSelectedIndex        = fungusScript.selectedSequence.commandList.Count;
            bool firstSelectedCommandFound = false;

            if (fungusScript.selectedCommands.Count > 0)
            {
                for (int i = 0; i < fungusScript.selectedSequence.commandList.Count; i++)
                {
                    Command commandInSequence = fungusScript.selectedSequence.commandList[i];

                    foreach (Command selectedCommand in fungusScript.selectedCommands)
                    {
                        if (commandInSequence == selectedCommand)
                        {
                            if (!firstSelectedCommandFound)
                            {
                                firstSelectedIndex        = i;
                                firstSelectedCommandFound = true;
                                break;
                            }
                        }
                    }
                    if (firstSelectedCommandFound)
                    {
                        break;
                    }
                }
            }
            if (firstSelectedIndex > 0)
            {
                fungusScript.ClearSelectedCommands();
                fungusScript.AddSelectedCommand(fungusScript.selectedSequence.commandList[firstSelectedIndex - 1]);
            }

            Repaint();
        }
        void Callback(object obj)
        {
            SetCommandOperation commandOperation = obj as SetCommandOperation;

            Sequence sequence = commandOperation.sequence;

            if (sequence == null)
            {
                return;
            }

            Command newCommand = Undo.AddComponent(sequence.gameObject, commandOperation.commandType)  as Command;

            sequence.GetFungusScript().selectedCommand = newCommand;

            Command oldCommand = sequence.commandList[commandOperation.index];

            Undo.DestroyObjectImmediate(oldCommand);

            Undo.RecordObject(sequence, "Set command type");
            sequence.commandList[commandOperation.index] = newCommand;
        }
        void DrawWindow(int windowId)
        {
            // Select sequence when node is clicked
            if (!Application.isPlaying &&
                Event.current.button == 0 &&
                Event.current.type == EventType.MouseDown)
            {
                if (windowId < windowSequenceMap.Count)
                {
                    Sequence s = windowSequenceMap[windowId];
                    if (s != null)
                    {
                        FungusScript fungusScript = s.GetFungusScript();
                        if (fungusScript != null)
                        {
                            fungusScript.selectedSequence = s;
                            fungusScript.selectedCommand  = null;
                            Selection.activeGameObject    = fungusScript.gameObject;
                            GUIUtility.keyboardControl    = 0;                          // Fix for textarea not refeshing (change focus)
                        }
                    }
                }
            }

            Sequence sequence = windowSequenceMap[windowId];

            GUIStyle labelStyle = new GUIStyle(GUI.skin.label);

            labelStyle.alignment = TextAnchor.MiddleCenter;

            GUILayout.BeginVertical();
            GUILayout.FlexibleSpace();
            GUILayout.Label(sequence.name, labelStyle);
            GUILayout.FlexibleSpace();
            GUILayout.EndVertical();

            GUI.DragWindow();
        }
Beispiel #16
0
        public virtual void ExecuteSequence(Sequence s)
        {
            OnExit();
            if (parentSequence != null)
            {
                FungusScript fungusScript = parentSequence.GetFungusScript();

                // Record the currently selected sequence because Stop() will clear it.
                Sequence selectedSequence = fungusScript.selectedSequence;

                parentSequence.Stop();
                if (fungusScript != null)
                {
                    // If the executing sequence is currently selected then follow the execution
                    // onto the next sequence in the inspector.
                    if (selectedSequence == parentSequence)
                    {
                        fungusScript.selectedSequence = s;
                    }

                    fungusScript.ExecuteSequence(s);
                }
            }
        }
Beispiel #17
0
        public void ShowContextMenu()
        {
            Sequence     sequence     = target as Sequence;
            FungusScript fungusScript = sequence.GetFungusScript();

            if (fungusScript == null)
            {
                return;
            }

            bool showCut    = false;
            bool showCopy   = false;
            bool showDelete = false;
            bool showPaste  = false;

            if (fungusScript.selectedCommands.Count > 0)
            {
                showCut    = true;
                showCopy   = true;
                showDelete = true;
            }

            CommandCopyBuffer commandCopyBuffer = CommandCopyBuffer.GetInstance();

            if (commandCopyBuffer.HasCommands())
            {
                showPaste = true;
            }

            GenericMenu commandMenu = new GenericMenu();

            if (showCut)
            {
                commandMenu.AddItem(new GUIContent("Cut"), false, Cut);
            }
            else
            {
                commandMenu.AddDisabledItem(new GUIContent("Cut"));
            }

            if (showCopy)
            {
                commandMenu.AddItem(new GUIContent("Copy"), false, Copy);
            }
            else
            {
                commandMenu.AddDisabledItem(new GUIContent("Copy"));
            }

            if (showPaste)
            {
                commandMenu.AddItem(new GUIContent("Paste"), false, Paste);
            }
            else
            {
                commandMenu.AddDisabledItem(new GUIContent("Paste"));
            }

            if (showDelete)
            {
                commandMenu.AddItem(new GUIContent("Delete"), false, Delete);
            }
            else
            {
                commandMenu.AddDisabledItem(new GUIContent("Delete"));
            }

            commandMenu.AddSeparator("");

            commandMenu.AddItem(new GUIContent("Select All"), false, SelectAll);
            commandMenu.AddItem(new GUIContent("Select None"), false, SelectNone);

            commandMenu.ShowAsContext();
        }
        protected virtual void DrawWindow(int windowId)
        {
            Sequence     sequence     = windowSequenceMap[windowId];
            FungusScript fungusScript = sequence.GetFungusScript();

            // Select sequence when node is clicked
            if (Event.current.button == 0 &&
                Event.current.type == EventType.MouseDown &&
                !mouseOverVariables)
            {
                // Check if might be start of a window drag
                if (Event.current.button == 0)
                {
                    dragWindowId        = windowId;
                    startDragPosition.x = sequence.nodeRect.x;
                    startDragPosition.y = sequence.nodeRect.y;
                }

                if (windowId < windowSequenceMap.Count)
                {
                    Undo.RecordObject(fungusScript, "Select");

                    SelectSequence(fungusScript, sequence);

                    GUIUtility.keyboardControl = 0;                     // Fix for textarea not refeshing (change focus)
                }
            }

            bool selected = (fungusScript.selectedSequence == sequence);

            GUIStyle nodeStyleCopy = new GUIStyle(nodeStyle);

            if (sequence.eventHandler != null)
            {
                nodeStyleCopy.normal.background = selected ? FungusEditorResources.texEventNodeOn : FungusEditorResources.texEventNodeOff;
            }
            else
            {
                // Count the number of unique connections (excluding self references)
                List <Sequence> uniqueList         = new List <Sequence>();
                List <Sequence> connectedSequences = sequence.GetConnectedSequences();
                foreach (Sequence connectedSequence in connectedSequences)
                {
                    if (connectedSequence == sequence ||
                        uniqueList.Contains(connectedSequence))
                    {
                        continue;
                    }
                    uniqueList.Add(connectedSequence);
                }

                if (uniqueList.Count > 1)
                {
                    nodeStyleCopy.normal.background = selected ? FungusEditorResources.texChoiceNodeOn : FungusEditorResources.texChoiceNodeOff;
                }
                else
                {
                    nodeStyleCopy.normal.background = selected ? FungusEditorResources.texProcessNodeOn : FungusEditorResources.texProcessNodeOff;
                }
            }

            // Show event handler name, or a custom summary if one is provided
            string nodeName = "";

            if (sequence.eventHandler != null)
            {
                string handlerSummary = sequence.eventHandler.GetSummary();
                if (handlerSummary == "")
                {
                    EventHandlerInfoAttribute info = EventHandlerEditor.GetEventHandlerInfo(sequence.eventHandler.GetType());
                    if (info != null)
                    {
                        handlerSummary = info.EventHandlerName;
                    }
                }
                nodeName = "(" + handlerSummary + ")\n";
            }
            nodeName += sequence.sequenceName;

            // Make sure node is wide enough to fit the node name text
            float width = nodeStyleCopy.CalcSize(new GUIContent(nodeName)).x;

            sequence.nodeRect.width = Mathf.Max(sequence.nodeRect.width, width);

            GUILayout.Box(nodeName, nodeStyleCopy, GUILayout.Width(sequence.nodeRect.width), GUILayout.Height(sequence.nodeRect.height));
            if (sequence.description.Length > 0)
            {
                GUILayout.Label(sequence.description, EditorStyles.whiteLabel);
            }

            if (Event.current.type == EventType.ContextClick)
            {
                GenericMenu menu = new GenericMenu();

                menu.AddItem(new GUIContent("Duplicate"), false, DuplicateSequence, sequence);
                menu.AddItem(new GUIContent("Delete"), false, DeleteSequence, sequence);

                menu.ShowAsContext();
            }
        }