Browse Source

Merge pull request #539 from snozbot/pub-sub

Added signals (pub-sub system) for Writer and Block events
master
Chris Gregan 9 years ago committed by GitHub
parent
commit
a49c424356
  1. 3
      Assets/Fungus/Scripts/Components/Block.cs
  2. 35
      Assets/Fungus/Scripts/Components/Writer.cs
  3. 9
      Assets/Fungus/Scripts/Signals.meta
  4. 36
      Assets/Fungus/Scripts/Signals/BlockSignals.cs
  5. 12
      Assets/Fungus/Scripts/Signals/BlockSignals.cs.meta
  6. 45
      Assets/Fungus/Scripts/Signals/WriterSignals.cs
  7. 12
      Assets/Fungus/Scripts/Signals/WriterSignals.cs.meta
  8. 3
      Assets/Tests/Commands/FailTest.cs
  9. 3
      Assets/Tests/Commands/PassTest.cs
  10. 9
      Assets/Tests/Signals.meta
  11. 53
      Assets/Tests/Signals/BlockSignalsTester.cs
  12. 12
      Assets/Tests/Signals/BlockSignalsTester.cs.meta
  13. 1034
      Assets/Tests/Signals/SignalsTests.unity
  14. 8
      Assets/Tests/Signals/SignalsTests.unity.meta
  15. 97
      Assets/Tests/Signals/WriterSignalsTester.cs
  16. 12
      Assets/Tests/Signals/WriterSignalsTester.cs.meta

3
Assets/Fungus/Scripts/Components/Block.cs

@ -213,6 +213,7 @@ namespace Fungus
var flowchart = GetFlowchart();
executionState = ExecutionState.Executing;
BlockSignals.DoBlockStart(this);
#if UNITY_EDITOR
// Select the executing block & the first command
@ -278,6 +279,7 @@ namespace Fungus
// This icon timer is managed by the FlowchartWindow class, but we also need to
// set it here in case a command starts and finishes execution before the next window update.
command.ExecutingIconTimer = Time.realtimeSinceStartup + FungusConstants.ExecutingIconFadeTime;
BlockSignals.DoCommandExecute(this, command, i, commandList.Count);
command.Execute();
// Wait until the executing command sets another command to jump to via Command.Continue()
@ -298,6 +300,7 @@ namespace Fungus
executionState = ExecutionState.Idle;
activeCommand = null;
BlockSignals.DoBlockEnd(this);
if (onComplete != null)
{

35
Assets/Fungus/Scripts/Components/Writer.cs

@ -12,6 +12,23 @@ using Fungus.Utils;
namespace Fungus
{
/// <summary>
/// Current state of the writing process.
/// </summary>
public enum WriterState
{
/// <summary> Invalid state. </summary>
Invalid,
/// <summary> Writer has started writing. </summary>
Start,
/// <summary> Writing has been paused. </summary>
Pause,
/// <summary> Writing has resumed after a pause. </summary>
Resume,
/// <summary> Writing has ended. </summary>
End
}
/// <summary>
/// Writes text using a typewriter effect to a UI text object.
/// </summary>
@ -232,8 +249,12 @@ namespace Fungus
TokenType previousTokenType = TokenType.Invalid;
foreach (TextTagToken token in tokens)
for (int i = 0; i < tokens.Count; ++i)
{
var token = tokens[i];
// Notify listeners about new token
WriterSignals.DoTextTagToken(this, token, i, tokens.Count);
switch (token.type)
{
@ -665,6 +686,8 @@ namespace Fungus
protected virtual void NotifyInput()
{
WriterSignals.DoWriterInput(this);
foreach (IWriterListener writerListener in writerListeners)
{
writerListener.OnInput();
@ -673,6 +696,8 @@ namespace Fungus
protected virtual void NotifyStart(AudioClip audioClip)
{
WriterSignals.DoWriterState(this, WriterState.Start);
foreach (IWriterListener writerListener in writerListeners)
{
writerListener.OnStart(audioClip);
@ -681,6 +706,8 @@ namespace Fungus
protected virtual void NotifyPause()
{
WriterSignals.DoWriterState(this, WriterState.Pause);
foreach (IWriterListener writerListener in writerListeners)
{
writerListener.OnPause();
@ -689,6 +716,8 @@ namespace Fungus
protected virtual void NotifyResume()
{
WriterSignals.DoWriterState(this, WriterState.Resume);
foreach (IWriterListener writerListener in writerListeners)
{
writerListener.OnResume();
@ -697,6 +726,8 @@ namespace Fungus
protected virtual void NotifyEnd(bool stopAudio)
{
WriterSignals.DoWriterState(this, WriterState.End);
foreach (IWriterListener writerListener in writerListeners)
{
writerListener.OnEnd(stopAudio);
@ -705,6 +736,8 @@ namespace Fungus
protected virtual void NotifyGlyph()
{
WriterSignals.DoWriterGlyph(this);
foreach (IWriterListener writerListener in writerListeners)
{
writerListener.OnGlyph();

9
Assets/Fungus/Scripts/Signals.meta

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: c5af9b1dcfc3a4963add98929d9386c0
folderAsset: yes
timeCreated: 1474968754
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

36
Assets/Fungus/Scripts/Signals/BlockSignals.cs

@ -0,0 +1,36 @@
using Fungus.Utils;
namespace Fungus
{
/// <summary>
/// Block event signalling system.
/// You can use this to be notified about various events in the Block execution process.
/// </summary>
public static class BlockSignals
{
#region Public members
/// <summary>
/// BlockStart signal. Sent when the Block starts execution.
/// </summary>
public static event BlockStartHandler OnBlockStart;
public delegate void BlockStartHandler(Block block);
public static void DoBlockStart(Block block) { if (OnBlockStart != null) OnBlockStart(block); }
/// <summary>
/// BlockEnd signal. Sent when the Block ends execution.
/// </summary>
public static event BlockEndHandler OnBlockEnd;
public delegate void BlockEndHandler(Block block);
public static void DoBlockEnd(Block block) { if (OnBlockEnd != null) OnBlockEnd(block); }
/// <summary>
/// CommandExecute signal. Sent just before a Command in a Block executes.
/// </summary>
public static event CommandExecuteHandler OnCommandExecute;
public delegate void CommandExecuteHandler(Block block, Command command, int commandIndex, int maxCommandIndex);
public static void DoCommandExecute(Block block, Command command, int commandIndex, int maxCommandIndex) { if (OnCommandExecute != null) OnCommandExecute(block, command, commandIndex, maxCommandIndex); }
#endregion
}
}

12
Assets/Fungus/Scripts/Signals/BlockSignals.cs.meta

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 12109e10b0f034bbab8a7a5d700bc070
timeCreated: 1474988491
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

45
Assets/Fungus/Scripts/Signals/WriterSignals.cs

@ -0,0 +1,45 @@
using UnityEngine;
using System.Collections;
using Fungus.Utils;
namespace Fungus
{
/// <summary>
/// Writer event signalling system.
/// You can use this to be notified about various events in the writing process.
/// </summary>
public static class WriterSignals
{
#region Public members
/// <summary>
/// TextTagToken signal. Sent for each unique token when writing text.
/// </summary>
public static event TextTagTokenHandler OnTextTagToken;
public delegate void TextTagTokenHandler(Writer writer, TextTagToken token, int index, int maxIndex);
public static void DoTextTagToken(Writer writer, TextTagToken token, int index, int maxIndex) { if(OnTextTagToken != null) OnTextTagToken(writer, token, index, maxIndex); }
/// <summary>
/// WriterState signal. Sent when the writer changes state.
/// </summary>
public static event WriterStateHandler OnWriterState;
public delegate void WriterStateHandler(Writer writer, WriterState writerState);
public static void DoWriterState(Writer writer, WriterState writerState) { if (OnWriterState != null) OnWriterState(writer, writerState); }
/// <summary>
/// WriterInput signal. Sent when the writer receives player input.
/// </summary>
public static event WriterInputHandler OnWriterInput;
public delegate void WriterInputHandler(Writer writer);
public static void DoWriterInput(Writer writer) { if (OnWriterInput != null) OnWriterInput(writer); }
/// <summary>
/// WriterGlyph signal. Sent when the writer writes out a glyph.
/// </summary>
public delegate void WriterGlyphHandler(Writer writer);
public static event WriterGlyphHandler OnWriterGlyph;
public static void DoWriterGlyph(Writer writer) { if (OnWriterGlyph != null) OnWriterGlyph(writer); }
#endregion
}
}

12
Assets/Fungus/Scripts/Signals/WriterSignals.cs.meta

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: d8d3d56bc3bd9488d8e6e30b9bf99fc4
timeCreated: 1474970135
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

3
Assets/Tests/Commands/FailTest.cs

@ -6,10 +6,10 @@ using System.Collections;
namespace Fungus
{
[CommandInfo("Tests",
"Fail",
"Fails the current integration test")]
[AddComponentMenu("")]
public class FailTest : Command
{
public string failMessage;
@ -21,5 +21,4 @@ namespace Fungus
Continue();
}
}
}

3
Assets/Tests/Commands/PassTest.cs

@ -6,10 +6,10 @@ using System.Collections;
namespace Fungus
{
[CommandInfo("Tests",
"Pass",
"Passes the current integration test")]
[AddComponentMenu("")]
public class PassTest : Command
{
public override void OnEnter()
@ -19,5 +19,4 @@ namespace Fungus
Continue();
}
}
}

9
Assets/Tests/Signals.meta

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 2e1206d3bce424ca8a9b82f05c69335d
folderAsset: yes
timeCreated: 1474982411
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

53
Assets/Tests/Signals/BlockSignalsTester.cs

@ -0,0 +1,53 @@
using UnityEngine;
using System.Collections;
using Fungus.Commands;
namespace Fungus
{
/// <summary>
/// Checks if Block signals are being sent correctly.
/// </summary>
[AddComponentMenu("")]
public class BlockSignalsTester : MonoBehaviour
{
bool started = false;
bool commandExecuted = false;
void OnEnable()
{
BlockSignals.OnBlockStart += OnBlockStart;
BlockSignals.OnBlockEnd += OnBlockEnd;
BlockSignals.OnCommandExecute += OnCommandExecute;
}
void OnDisable()
{
BlockSignals.OnBlockStart -= OnBlockStart;
BlockSignals.OnBlockEnd -= OnBlockEnd;
BlockSignals.OnCommandExecute -= OnCommandExecute;
}
void OnBlockStart(Block block)
{
IntegrationTest.Assert(block.BlockName == "Start");
started = true;
}
void OnBlockEnd(Block block)
{
IntegrationTest.Assert(started);
IntegrationTest.Assert(commandExecuted);
IntegrationTest.Pass();
}
void OnCommandExecute(Block block, Command command, int commandIndex, int maxCommandIndex)
{
IntegrationTest.Assert(commandIndex == 0);
IntegrationTest.Assert(maxCommandIndex == 1);
IntegrationTest.Assert(command.GetType() == typeof(Wait));
commandExecuted = true;
}
}
}

12
Assets/Tests/Signals/BlockSignalsTester.cs.meta

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 5d396ca39a0454e2ca06d4aad7c5706c
timeCreated: 1474988463
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

1034
Assets/Tests/Signals/SignalsTests.unity

File diff suppressed because it is too large Load Diff

8
Assets/Tests/Signals/SignalsTests.unity.meta

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 8acd008952a224b138b2b02baae1f7ff
timeCreated: 1474970336
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

97
Assets/Tests/Signals/WriterSignalsTester.cs

@ -0,0 +1,97 @@
using UnityEngine;
using Fungus.Utils;
namespace Fungus
{
/// <summary>
/// Checks if Writer signals are being sent correctly.
/// </summary>
[AddComponentMenu("")]
public class WriterSignalsTester : MonoBehaviour
{
int correctTagCount = 0;
bool receivedInput = false;
int glyphCount = 0;
void OnEnable()
{
WriterSignals.OnTextTagToken += OnTextTagToken;
WriterSignals.OnWriterState += OnWriterState;
WriterSignals.OnWriterInput += OnWriterInput;
WriterSignals.OnWriterGlyph += OnWriterGlyph;
}
void OnDisable()
{
WriterSignals.OnTextTagToken -= OnTextTagToken;
WriterSignals.OnWriterState -= OnWriterState;
WriterSignals.OnWriterInput -= OnWriterInput;
WriterSignals.OnWriterGlyph -= OnWriterGlyph;
}
void OnTextTagToken(Writer writer, TextTagToken token, int index, int maxIndex)
{
if (index == 0 && token.type == TokenType.BoldStart)
{
correctTagCount++;
}
else if (index == 1 && token.type == TokenType.Wait)
{
correctTagCount++;
}
else if (index == 2 && token.type == TokenType.Words)
{
correctTagCount++;
}
else if (index == 3 && token.type == TokenType.BoldEnd)
{
correctTagCount++;
}
}
void OnWriterState(Writer writer, WriterState writerState)
{
if (writerState == WriterState.Start && correctTagCount != 0)
{
IntegrationTest.Fail();
}
if (writerState == WriterState.Pause && correctTagCount != 2)
{
IntegrationTest.Fail();
}
if (writerState == WriterState.Resume && correctTagCount != 2)
{
IntegrationTest.Fail();
}
else if (writerState == WriterState.End && correctTagCount != 4)
{
IntegrationTest.Fail();
}
if (writerState == WriterState.End)
{
if (!receivedInput)
{
IntegrationTest.Fail();
}
if (glyphCount != 6)
{
IntegrationTest.Fail();
}
IntegrationTest.Pass();
}
}
void OnWriterInput(Writer writer)
{
receivedInput = true;
}
void OnWriterGlyph(Writer writer)
{
glyphCount++;
}
}
}

12
Assets/Tests/Signals/WriterSignalsTester.cs.meta

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: cb302fcb509f14caf93dea80411e90ac
timeCreated: 1474968744
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
Loading…
Cancel
Save