diff --git a/Assets/Fungus/Scripts/Components/Writer.cs b/Assets/Fungus/Scripts/Components/Writer.cs index 5b9cfd9b..710def21 100644 --- a/Assets/Fungus/Scripts/Components/Writer.cs +++ b/Assets/Fungus/Scripts/Components/Writer.cs @@ -12,6 +12,23 @@ using Fungus.Utils; namespace Fungus { + /// + /// Current state of the writing process. + /// + public enum WriterState + { + /// Invalid state. + Invalid, + /// Writer has started writing. + Start, + /// Writing has been paused. + Pause, + /// Writing has resumed after a pause. + Resume, + /// Writing has ended. + End + } + /// /// Writes text using a typewriter effect to a UI text object. /// @@ -677,6 +694,8 @@ namespace Fungus protected virtual void NotifyStart(AudioClip audioClip) { + WriterSignals.DoWriterState(this, WriterState.Start); + foreach (IWriterListener writerListener in writerListeners) { writerListener.OnStart(audioClip); @@ -685,6 +704,8 @@ namespace Fungus protected virtual void NotifyPause() { + WriterSignals.DoWriterState(this, WriterState.Pause); + foreach (IWriterListener writerListener in writerListeners) { writerListener.OnPause(); @@ -693,6 +714,8 @@ namespace Fungus protected virtual void NotifyResume() { + WriterSignals.DoWriterState(this, WriterState.Resume); + foreach (IWriterListener writerListener in writerListeners) { writerListener.OnResume(); @@ -701,6 +724,8 @@ namespace Fungus protected virtual void NotifyEnd(bool stopAudio) { + WriterSignals.DoWriterState(this, WriterState.End); + foreach (IWriterListener writerListener in writerListeners) { writerListener.OnEnd(stopAudio); diff --git a/Assets/Fungus/Scripts/Signals/WriterSignals.cs b/Assets/Fungus/Scripts/Signals/WriterSignals.cs index 50334920..b519af29 100644 --- a/Assets/Fungus/Scripts/Signals/WriterSignals.cs +++ b/Assets/Fungus/Scripts/Signals/WriterSignals.cs @@ -16,7 +16,14 @@ namespace Fungus /// public delegate void TextTagTokenHandler(Writer writer, TextTagToken token, int index, int maxIndex); 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); } + + /// + /// WriterState signal. Sent when the writer changes state. + /// + 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 } diff --git a/Assets/Tests/Signals/SignalsTests.unity b/Assets/Tests/Signals/SignalsTests.unity index 720ca45f..9e4a4d9d 100644 --- a/Assets/Tests/Signals/SignalsTests.unity +++ b/Assets/Tests/Signals/SignalsTests.unity @@ -306,7 +306,7 @@ MonoBehaviour: textObject: {fileID: 41796120} text: stringRef: {fileID: 0} - stringVal: '{b}hello{/b}' + stringVal: '{b}{w=0.5}hello{/b}' description: clearText: 1 waitUntilFinished: 1 @@ -378,8 +378,9 @@ MonoBehaviour: y: -340 width: 1114 height: 859 - selectedBlock: {fileID: 0} - selectedCommands: [] + selectedBlock: {fileID: 411939903} + selectedCommands: + - {fileID: 411939901} variables: [] description: Check that Writer signals are sending correctly stepPause: 0 diff --git a/Assets/Tests/Signals/WriterSignalsTester.cs b/Assets/Tests/Signals/WriterSignalsTester.cs index 7a508078..ee0512e5 100644 --- a/Assets/Tests/Signals/WriterSignalsTester.cs +++ b/Assets/Tests/Signals/WriterSignalsTester.cs @@ -9,9 +9,12 @@ namespace Fungus [AddComponentMenu("")] public class WriterSignalsTester : MonoBehaviour { + int correctTagCount = 0; + void OnEnable() { WriterSignals.OnTextTagToken += OnTextTagToken; + WriterSignals.OnWriterState += OnWriterState; } void OnDisable() @@ -21,15 +24,44 @@ namespace Fungus 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(); } - else if (index == 1 && token.type != TokenType.Words) + if (writerState == WriterState.Resume && correctTagCount != 2) { 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(); }