From b14da8fcda2acfcf2caa7e7e0387eb0412ea06cb Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Wed, 16 Sep 2015 15:00:28 +0100 Subject: [PATCH] Added Flowchart.StopAllBlocks() and command callback --- Assets/Fungus/Flowchart/Scripts/Block.cs | 6 ++++++ Assets/Fungus/Flowchart/Scripts/Command.cs | 11 ++++++++++- Assets/Fungus/Flowchart/Scripts/Commands/Call.cs | 2 +- Assets/Fungus/Flowchart/Scripts/Commands/Else.cs | 4 ++-- .../Fungus/Flowchart/Scripts/Commands/ElseIf.cs | 4 ++-- Assets/Fungus/Flowchart/Scripts/Commands/If.cs | 6 +++--- .../Flowchart/Scripts/Commands/InvokeMethod.cs | 4 ++-- Assets/Fungus/Flowchart/Scripts/Commands/Stop.cs | 4 ++-- Assets/Fungus/Flowchart/Scripts/Flowchart.cs | 15 +++++++++++++++ 9 files changed, 43 insertions(+), 13 deletions(-) diff --git a/Assets/Fungus/Flowchart/Scripts/Block.cs b/Assets/Fungus/Flowchart/Scripts/Block.cs index b61f4f54..16391e3e 100644 --- a/Assets/Fungus/Flowchart/Scripts/Block.cs +++ b/Assets/Fungus/Flowchart/Scripts/Block.cs @@ -237,6 +237,12 @@ namespace Fungus public virtual void Stop() { + // Tell the executing command to stop immediately + if (activeCommand != null) + { + activeCommand.OnStopExecuting(); + } + // This will cause the execution loop to break on the next iteration jumpToCommandIndex = int.MaxValue; } diff --git a/Assets/Fungus/Flowchart/Scripts/Command.cs b/Assets/Fungus/Flowchart/Scripts/Command.cs index 12f910ed..9018ef63 100644 --- a/Assets/Fungus/Flowchart/Scripts/Command.cs +++ b/Assets/Fungus/Flowchart/Scripts/Command.cs @@ -95,7 +95,7 @@ namespace Fungus } } - public virtual void Stop() + public virtual void StopParentBlock() { OnExit(); if (parentBlock != null) @@ -104,6 +104,15 @@ namespace Fungus } } + /** + * Called when the parent block has been requested to stop executing, and + * this command is the currently executing command. + * Use this callback to terminate any asynchronous operations and + * cleanup state so that the command is ready to execute again later on. + */ + public virtual void OnStopExecuting() + {} + /** * 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 b23ad187..d766d3a3 100644 --- a/Assets/Fungus/Flowchart/Scripts/Commands/Call.cs +++ b/Assets/Fungus/Flowchart/Scripts/Commands/Call.cs @@ -75,7 +75,7 @@ namespace Fungus if (callMode == CallMode.Stop) { - Stop(); + StopParentBlock(); } else if (callMode == CallMode.Continue) { diff --git a/Assets/Fungus/Flowchart/Scripts/Commands/Else.cs b/Assets/Fungus/Flowchart/Scripts/Commands/Else.cs index c140ebf6..23d8da2d 100644 --- a/Assets/Fungus/Flowchart/Scripts/Commands/Else.cs +++ b/Assets/Fungus/Flowchart/Scripts/Commands/Else.cs @@ -20,7 +20,7 @@ namespace Fungus // Stop if this is the last command in the list if (commandIndex >= parentBlock.commandList.Count - 1) { - Stop(); + StopParentBlock(); return; } @@ -43,7 +43,7 @@ namespace Fungus } // No End command found - Stop(); + StopParentBlock(); } public override bool OpenBlock() diff --git a/Assets/Fungus/Flowchart/Scripts/Commands/ElseIf.cs b/Assets/Fungus/Flowchart/Scripts/Commands/ElseIf.cs index 12095328..ca76f72f 100644 --- a/Assets/Fungus/Flowchart/Scripts/Commands/ElseIf.cs +++ b/Assets/Fungus/Flowchart/Scripts/Commands/ElseIf.cs @@ -31,7 +31,7 @@ namespace Fungus // Stop if this is the last command in the list if (commandIndex >= parentBlock.commandList.Count - 1) { - Stop(); + StopParentBlock(); return; } @@ -54,7 +54,7 @@ namespace Fungus } // No End command found - Stop(); + StopParentBlock(); } } diff --git a/Assets/Fungus/Flowchart/Scripts/Commands/If.cs b/Assets/Fungus/Flowchart/Scripts/Commands/If.cs index 19b03009..21ac95ba 100644 --- a/Assets/Fungus/Flowchart/Scripts/Commands/If.cs +++ b/Assets/Fungus/Flowchart/Scripts/Commands/If.cs @@ -97,7 +97,7 @@ namespace Fungus // Last command in block if (commandIndex >= parentBlock.commandList.Count) { - Stop(); + StopParentBlock(); return; } @@ -128,7 +128,7 @@ namespace Fungus if (i >= parentBlock.commandList.Count - 1) { // Last command in Block, so stop - Stop(); + StopParentBlock(); } else { @@ -147,7 +147,7 @@ namespace Fungus } // No matching End command found, so just stop the block - Stop(); + StopParentBlock(); } public override string GetSummary() diff --git a/Assets/Fungus/Flowchart/Scripts/Commands/InvokeMethod.cs b/Assets/Fungus/Flowchart/Scripts/Commands/InvokeMethod.cs index 13a36a54..2645a75b 100755 --- a/Assets/Fungus/Flowchart/Scripts/Commands/InvokeMethod.cs +++ b/Assets/Fungus/Flowchart/Scripts/Commands/InvokeMethod.cs @@ -1,4 +1,4 @@ -using UnityEngine; +using UnityEngine; using System.Collections; using System.Reflection; using System.Linq; @@ -120,7 +120,7 @@ namespace Fungus } else if(callMode == Call.CallMode.Stop) { - Stop(); + StopParentBlock(); } } } diff --git a/Assets/Fungus/Flowchart/Scripts/Commands/Stop.cs b/Assets/Fungus/Flowchart/Scripts/Commands/Stop.cs index 2dd1a837..600d45cc 100644 --- a/Assets/Fungus/Flowchart/Scripts/Commands/Stop.cs +++ b/Assets/Fungus/Flowchart/Scripts/Commands/Stop.cs @@ -6,13 +6,13 @@ namespace Fungus { [CommandInfo("Flow", "Stop", - "Stop executing the current Flowchart.")] + "Stop executing the Block that contains this command.")] [AddComponentMenu("")] public class Stop : Command { public override void OnEnter() { - Stop(); + StopParentBlock(); } public override Color GetButtonColor() diff --git a/Assets/Fungus/Flowchart/Scripts/Flowchart.cs b/Assets/Fungus/Flowchart/Scripts/Flowchart.cs index c0c8fe49..102f51cb 100644 --- a/Assets/Fungus/Flowchart/Scripts/Flowchart.cs +++ b/Assets/Fungus/Flowchart/Scripts/Flowchart.cs @@ -425,6 +425,21 @@ namespace Fungus return true; } + /** + * Stop all executing Blocks in this Flowchart. + */ + public virtual void StopAllBlocks() + { + Block [] blocks = GetComponentsInChildren(); + foreach (Block block in blocks) + { + if (block.IsExecuting()) + { + block.Stop(); + } + } + } + /** * Returns a new variable key that is guaranteed not to clash with any existing variable in the list. */