Browse Source

Added WriterState signal

master
Christopher 8 years ago
parent
commit
73c93669b5
  1. 25
      Assets/Fungus/Scripts/Components/Writer.cs
  2. 9
      Assets/Fungus/Scripts/Signals/WriterSignals.cs
  3. 7
      Assets/Tests/Signals/SignalsTests.unity
  4. 38
      Assets/Tests/Signals/WriterSignalsTester.cs

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

@ -12,6 +12,23 @@ using Fungus.Utils;
namespace Fungus 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> /// <summary>
/// Writes text using a typewriter effect to a UI text object. /// Writes text using a typewriter effect to a UI text object.
/// </summary> /// </summary>
@ -677,6 +694,8 @@ namespace Fungus
protected virtual void NotifyStart(AudioClip audioClip) protected virtual void NotifyStart(AudioClip audioClip)
{ {
WriterSignals.DoWriterState(this, WriterState.Start);
foreach (IWriterListener writerListener in writerListeners) foreach (IWriterListener writerListener in writerListeners)
{ {
writerListener.OnStart(audioClip); writerListener.OnStart(audioClip);
@ -685,6 +704,8 @@ namespace Fungus
protected virtual void NotifyPause() protected virtual void NotifyPause()
{ {
WriterSignals.DoWriterState(this, WriterState.Pause);
foreach (IWriterListener writerListener in writerListeners) foreach (IWriterListener writerListener in writerListeners)
{ {
writerListener.OnPause(); writerListener.OnPause();
@ -693,6 +714,8 @@ namespace Fungus
protected virtual void NotifyResume() protected virtual void NotifyResume()
{ {
WriterSignals.DoWriterState(this, WriterState.Resume);
foreach (IWriterListener writerListener in writerListeners) foreach (IWriterListener writerListener in writerListeners)
{ {
writerListener.OnResume(); writerListener.OnResume();
@ -701,6 +724,8 @@ namespace Fungus
protected virtual void NotifyEnd(bool stopAudio) protected virtual void NotifyEnd(bool stopAudio)
{ {
WriterSignals.DoWriterState(this, WriterState.End);
foreach (IWriterListener writerListener in writerListeners) foreach (IWriterListener writerListener in writerListeners)
{ {
writerListener.OnEnd(stopAudio); writerListener.OnEnd(stopAudio);

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

@ -16,7 +16,14 @@ namespace Fungus
/// </summary> /// </summary>
public delegate void TextTagTokenHandler(Writer writer, TextTagToken token, int index, int maxIndex); public delegate void TextTagTokenHandler(Writer writer, TextTagToken token, int index, int maxIndex);
public static event TextTagTokenHandler OnTextTagToken; public static event TextTagTokenHandler OnTextTagToken;
public static void DoTextTagToken(Writer writer, TextTagToken token, int index, int maxIndex){ if(OnTextTagToken != null) OnTextTagToken(writer, token, index, 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 delegate void WriterStateHandler(Writer writer, WriterState writerState);
public static event WriterStateHandler OnWriterState;
public static void DoWriterState(Writer writer, WriterState writerState) { if (OnWriterState != null) OnWriterState(writer, writerState); }
#endregion #endregion
} }

7
Assets/Tests/Signals/SignalsTests.unity

@ -306,7 +306,7 @@ MonoBehaviour:
textObject: {fileID: 41796120} textObject: {fileID: 41796120}
text: text:
stringRef: {fileID: 0} stringRef: {fileID: 0}
stringVal: '{b}hello{/b}' stringVal: '{b}{w=0.5}hello{/b}'
description: description:
clearText: 1 clearText: 1
waitUntilFinished: 1 waitUntilFinished: 1
@ -378,8 +378,9 @@ MonoBehaviour:
y: -340 y: -340
width: 1114 width: 1114
height: 859 height: 859
selectedBlock: {fileID: 0} selectedBlock: {fileID: 411939903}
selectedCommands: [] selectedCommands:
- {fileID: 411939901}
variables: [] variables: []
description: Check that Writer signals are sending correctly description: Check that Writer signals are sending correctly
stepPause: 0 stepPause: 0

38
Assets/Tests/Signals/WriterSignalsTester.cs

@ -9,9 +9,12 @@ namespace Fungus
[AddComponentMenu("")] [AddComponentMenu("")]
public class WriterSignalsTester : MonoBehaviour public class WriterSignalsTester : MonoBehaviour
{ {
int correctTagCount = 0;
void OnEnable() void OnEnable()
{ {
WriterSignals.OnTextTagToken += OnTextTagToken; WriterSignals.OnTextTagToken += OnTextTagToken;
WriterSignals.OnWriterState += OnWriterState;
} }
void OnDisable() void OnDisable()
@ -21,15 +24,44 @@ namespace Fungus
void OnTextTagToken(Writer writer, TextTagToken token, int index, int maxIndex) void OnTextTagToken(Writer writer, TextTagToken token, int index, int maxIndex)
{ {
if (index == 0 && token.type != TokenType.BoldStart) 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(); IntegrationTest.Fail();
} }
else if (index == 1 && token.type != TokenType.Words) if (writerState == WriterState.Resume && correctTagCount != 2)
{ {
IntegrationTest.Fail(); IntegrationTest.Fail();
} }
else if (index == 2 && token.type == TokenType.BoldEnd) else if (writerState == WriterState.End && correctTagCount != 4)
{
IntegrationTest.Fail();
}
if (writerState == WriterState.End)
{ {
IntegrationTest.Pass(); IntegrationTest.Pass();
} }

Loading…
Cancel
Save