From 7a11cceaa330343b4a026e04c8f086022b7ae95e Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Tue, 14 Apr 2015 12:09:49 +0100 Subject: [PATCH] Call a block in another Flowchart. Stop, Continue or Wait Until Finished. --- Assets/Fungus/Flowchart/Editor/CallEditor.cs | 31 +++++--- Assets/Fungus/Flowchart/Scripts/Block.cs | 11 ++- Assets/Fungus/Flowchart/Scripts/Command.cs | 27 ------- .../Fungus/Flowchart/Scripts/Commands/Call.cs | 77 ++++++++++++++++--- Assets/Fungus/Flowchart/Scripts/Flowchart.cs | 6 +- Assets/Fungus/Narrative/Deprecated/Choose.cs | 6 +- 6 files changed, 104 insertions(+), 54 deletions(-) diff --git a/Assets/Fungus/Flowchart/Editor/CallEditor.cs b/Assets/Fungus/Flowchart/Editor/CallEditor.cs index 8c04ec9f..591c1f4e 100644 --- a/Assets/Fungus/Flowchart/Editor/CallEditor.cs +++ b/Assets/Fungus/Flowchart/Editor/CallEditor.cs @@ -8,13 +8,15 @@ namespace Fungus [CustomEditor (typeof(Call))] public class CallEditor : CommandEditor { + protected SerializedProperty targetFlowchartProp; protected SerializedProperty targetBlockProp; - protected SerializedProperty stopParentBlockProp; + protected SerializedProperty callModeProp; protected virtual void OnEnable() { + targetFlowchartProp = serializedObject.FindProperty("targetFlowchart"); targetBlockProp = serializedObject.FindProperty("targetBlock"); - stopParentBlockProp = serializedObject.FindProperty("stopParentBlock"); + callModeProp = serializedObject.FindProperty("callMode"); } public override void DrawCommandGUI() @@ -23,18 +25,27 @@ namespace Fungus Call t = target as Call; - Flowchart flowchart = t.GetFlowchart(); - if (flowchart == null) + Flowchart flowchart = null; + if (targetFlowchartProp.objectReferenceValue == null) { - return; + flowchart = t.GetFlowchart(); } + else + { + flowchart = targetFlowchartProp.objectReferenceValue as Flowchart; + } + + EditorGUILayout.PropertyField(targetFlowchartProp); - BlockEditor.BlockField(targetBlockProp, - new GUIContent("Target Block", "Block to call"), - new GUIContent(""), - flowchart); + if (flowchart != null) + { + BlockEditor.BlockField(targetBlockProp, + new GUIContent("Target Block", "Block to call"), + new GUIContent(""), + flowchart); + } - EditorGUILayout.PropertyField(stopParentBlockProp); + EditorGUILayout.PropertyField(callModeProp); serializedObject.ApplyModifiedProperties(); } diff --git a/Assets/Fungus/Flowchart/Scripts/Block.cs b/Assets/Fungus/Flowchart/Scripts/Block.cs index 481a740e..ece3adf2 100644 --- a/Assets/Fungus/Flowchart/Scripts/Block.cs +++ b/Assets/Fungus/Flowchart/Scripts/Block.cs @@ -142,7 +142,7 @@ namespace Fungus return executionCount; } - public virtual bool Execute() + public virtual bool Execute(Action onComplete = null) { if (executionState != ExecutionState.Idle) { @@ -150,12 +150,12 @@ namespace Fungus } executionCount++; - StartCoroutine(ExecuteBlock()); + StartCoroutine(ExecuteBlock(onComplete)); return true; } - protected virtual IEnumerator ExecuteBlock() + protected virtual IEnumerator ExecuteBlock(Action onComplete = null) { Flowchart flowchart = GetFlowchart(); executionState = ExecutionState.Executing; @@ -242,6 +242,11 @@ namespace Fungus executionState = ExecutionState.Idle; activeCommand = null; + + if (onComplete != null) + { + onComplete(); + } } public virtual void Stop() diff --git a/Assets/Fungus/Flowchart/Scripts/Command.cs b/Assets/Fungus/Flowchart/Scripts/Command.cs index c4dfd665..ce7b9171 100644 --- a/Assets/Fungus/Flowchart/Scripts/Command.cs +++ b/Assets/Fungus/Flowchart/Scripts/Command.cs @@ -104,33 +104,6 @@ namespace Fungus } } - public virtual void ExecuteBlock(Block s, bool stopParentBlock) - { - OnExit(); - if (parentBlock != null) - { - Flowchart flowchart = parentBlock.GetFlowchart(); - if (flowchart == null) - { - return; - } - - if (stopParentBlock) - { - parentBlock.Stop(); - - // If the executing block is currently selected then follow the execution - // onto the next block in the inspector. - if (flowchart.selectedBlock == parentBlock) - { - flowchart.selectedBlock = s; - } - } - - flowchart.ExecuteBlock(s); - } - } - /** * Called when the new command is added to a block in the editor. */ diff --git a/Assets/Fungus/Flowchart/Scripts/Commands/Call.cs b/Assets/Fungus/Flowchart/Scripts/Commands/Call.cs index 34bc63a3..1e39f000 100644 --- a/Assets/Fungus/Flowchart/Scripts/Commands/Call.cs +++ b/Assets/Fungus/Flowchart/Scripts/Commands/Call.cs @@ -2,6 +2,7 @@ using UnityEngine; using UnityEngine.Serialization; using System.Collections; using System.Collections.Generic; +using System; namespace Fungus { @@ -12,25 +13,64 @@ namespace Fungus [AddComponentMenu("")] public class Call : Command { + [Tooltip("Flowchart which contains the block to execute. If none is specified then the current Flowchart is used.")] + public Flowchart targetFlowchart; + [FormerlySerializedAs("targetSequence")] [Tooltip("Block to start executing")] public Block targetBlock; - [Tooltip("Stop executing the parent block that contains this command")] - public bool stopParentBlock = true; + public enum CallMode + { + Stop, + Continue, + WaitUntilFinished + } + + [Tooltip("Select if the calling block should stop or continue executing commands, or wait until the called block finishes.")] + public CallMode callMode; public override void OnEnter() { + Flowchart flowchart = GetFlowchart(); + if (targetBlock != null) { - ExecuteBlock(targetBlock, stopParentBlock); - if (!stopParentBlock) + // Callback action for Wait Until Finished mode + Action onComplete = null; + if (callMode == CallMode.WaitUntilFinished) + { + onComplete = delegate { + flowchart.selectedBlock = parentBlock; + Continue(); + }; + } + + if (targetFlowchart == null || + targetFlowchart == GetFlowchart()) + { + // If the executing block is currently selected then follow the execution + // onto the next block in the inspector. + if (flowchart.selectedBlock == parentBlock) + { + flowchart.selectedBlock = targetBlock; + } + + targetBlock.Execute(onComplete); + } + else { - Continue(); + // Execute block in another Flowchart + targetFlowchart.ExecuteBlock(targetBlock, onComplete); } } - else - { + + if (callMode == CallMode.Stop) + { + Stop(); + } + else if (callMode == CallMode.Continue) + { Continue(); } } @@ -45,12 +85,31 @@ namespace Fungus public override string GetSummary() { + string summary = ""; + if (targetBlock == null) { - return ""; + summary = ""; + } + else + { + summary = targetBlock.blockName; + } + + switch (callMode) + { + case CallMode.Stop: + summary += " : Stop"; + break; + case CallMode.Continue: + summary += " : Continue"; + break; + case CallMode.WaitUntilFinished: + summary += " : Wait"; + break; } - return targetBlock.blockName; + return summary; } public override Color GetButtonColor() diff --git a/Assets/Fungus/Flowchart/Scripts/Flowchart.cs b/Assets/Fungus/Flowchart/Scripts/Flowchart.cs index fab8e431..5dde9758 100644 --- a/Assets/Fungus/Flowchart/Scripts/Flowchart.cs +++ b/Assets/Fungus/Flowchart/Scripts/Flowchart.cs @@ -222,11 +222,11 @@ namespace Fungus } /** - * Start running another Flowchart by executing a specific child block. + * Start executing a specific child block in the flowchart. * The block must be in an idle state to be executed. * Returns true if the Block started execution. */ - public virtual bool ExecuteBlock(Block block) + public virtual bool ExecuteBlock(Block block, Action onComplete = null) { // Block must be a component of the Flowchart game object if (block == null || @@ -242,7 +242,7 @@ namespace Fungus } // Execute the first command in the command list - block.Execute(); + block.Execute(onComplete); return true; } diff --git a/Assets/Fungus/Narrative/Deprecated/Choose.cs b/Assets/Fungus/Narrative/Deprecated/Choose.cs index 53fc7b73..e88bacd5 100644 --- a/Assets/Fungus/Narrative/Deprecated/Choose.cs +++ b/Assets/Fungus/Narrative/Deprecated/Choose.cs @@ -100,7 +100,8 @@ namespace Fungus } else { - ExecuteBlock(onSelectBlock, true); + Stop(); + onSelectBlock.Execute(); } }; @@ -175,7 +176,8 @@ namespace Fungus { options.Clear(); showBasicGUI = false; - ExecuteBlock(option.targetBlock, true); + Stop(); + option.targetBlock.Execute(); } }