diff --git a/Assets/Fungus/Scripts/Components/Writer.cs b/Assets/Fungus/Scripts/Components/Writer.cs index 55b75870..b7583119 100644 --- a/Assets/Fungus/Scripts/Components/Writer.cs +++ b/Assets/Fungus/Scripts/Components/Writer.cs @@ -5,7 +5,7 @@ using UnityEngine.UI; using System.Collections; using System.Collections.Generic; -using System; +using System.Linq; using System.Reflection; using System.Text; @@ -26,8 +26,6 @@ namespace Fungus Resume, /// Writing has ended. End, - /// No text remaining to be written. - AllTextWritten, } /// @@ -81,7 +79,30 @@ namespace Fungus protected float sizeValue = 16f; protected bool inputFlag; protected bool exitFlag; - protected bool hasTextRemaining; + + //holds number of Word tokens in the currently running Write + public int WordTokensFound { get; protected set; } + //holds count of number of Word tokens completed + protected int wordTokensProcessed; + + /// + /// Updated during writing of Word tokens, when processed tips over found, fires NotifyAllWordsWritten + /// + public virtual int WordTokensProcessed + { + get { return wordTokensProcessed; } + protected set + { + if(wordTokensProcessed < WordTokensFound && value >= WordTokensFound) + { + NotifyAllWordsWritten(); + } + wordTokensProcessed = value; + } + } + + + public bool HasWordsRemaining { get { return WordTokensProcessed > WordTokensFound; } } protected List writerListeners = new List(); @@ -128,7 +149,7 @@ namespace Fungus { // Cache the hidden color string Color32 c = hiddenTextColor; - hiddenColorOpen = String.Format("", c.r, c.g, c.b, c.a); + hiddenColorOpen = string.Format("", c.r, c.g, c.b, c.a); hiddenColorClose = ""; } @@ -224,30 +245,21 @@ namespace Fungus value = defaultValue; if (paramList.Count > index) { - Single.TryParse(paramList[index], out value); + float.TryParse(paramList[index], out value); return true; } return false; } - protected virtual bool WordTokensRemaining(List tokens, int startingIndex) - { - for (int i = startingIndex; i < tokens.Count; i++) - { - if (tokens[i].type == TokenType.Words) - return true; - } - return false; - } - - protected virtual IEnumerator ProcessTokens(List tokens, bool stopAudio, Action onComplete) + protected virtual IEnumerator ProcessTokens(List tokens, bool stopAudio, System.Action onComplete) { // Reset control members boldActive = false; italicActive = false; colorActive = false; sizeActive = false; - hasTextRemaining = WordTokensRemaining(tokens, 0); + WordTokensFound = tokens.Count(x => x.type == TokenType.Words); + WordTokensProcessed = 0; colorText = ""; sizeValue = 16f; currentPunctuationPause = punctuationPause; @@ -270,13 +282,7 @@ namespace Fungus // Notify listeners about new token WriterSignals.DoTextTagToken(this, token, i, tokens.Count); - - if(hasTextRemaining && !WordTokensRemaining(tokens, i)) - { - hasTextRemaining = false; - NotifyAllTextWritten(); - } - + // Update the read ahead string buffer. This contains the text for any // Word tags which are further ahead in the list. if (doReadAheadText) @@ -302,6 +308,7 @@ namespace Fungus { case TokenType.Words: yield return StartCoroutine(DoWords(token.paramList, previousTokenType)); + WordTokensProcessed++; break; case TokenType.BoldStart: @@ -656,7 +663,7 @@ namespace Fungus // Look ahead to find next whitespace or end of string for (int j = i; j < inputString.Length + 1; ++j) { - if (j == inputString.Length || Char.IsWhiteSpace(inputString[j])) + if (j == inputString.Length || char.IsWhiteSpace(inputString[j])) { leftString.Length = j; rightString.Remove(0, j); @@ -714,7 +721,7 @@ namespace Fungus } float duration = 1f; - if (!Single.TryParse(param, out duration)) + if (!float.TryParse(param, out duration)) { duration = 1f; } @@ -867,14 +874,12 @@ namespace Fungus } } - protected virtual void NotifyAllTextWritten() + protected virtual void NotifyAllWordsWritten() { - WriterSignals.DoWriterState(this, WriterState.AllTextWritten); - for (int i = 0; i < writerListeners.Count; i++) { var writerListener = writerListeners[i]; - writerListener.OnAllTextWritten(); + writerListener.OnAllWordsWritten(); } } @@ -938,7 +943,7 @@ namespace Fungus /// Wait for the Voice over to complete before proceeding /// Audio clip to play when text starts writing. /// Callback to call when writing is finished. - public virtual IEnumerator Write(string content, bool clear, bool waitForInput, bool stopAudio, bool waitForVO, AudioClip audioClip, Action onComplete) + public virtual IEnumerator Write(string content, bool clear, bool waitForInput, bool stopAudio, bool waitForVO, AudioClip audioClip, System.Action onComplete) { if (clear) { diff --git a/Assets/Fungus/Scripts/Components/WriterAudio.cs b/Assets/Fungus/Scripts/Components/WriterAudio.cs index d1c145e9..b5162ca6 100644 --- a/Assets/Fungus/Scripts/Components/WriterAudio.cs +++ b/Assets/Fungus/Scripts/Components/WriterAudio.cs @@ -260,7 +260,7 @@ namespace Fungus targetAudioSource.Play(); } - public void OnAllTextWritten() + public void OnAllWordsWritten() { } diff --git a/Assets/Fungus/Scripts/Interfaces/IWriterListener.cs b/Assets/Fungus/Scripts/Interfaces/IWriterListener.cs index 7a4803c3..3c83bfff 100644 --- a/Assets/Fungus/Scripts/Interfaces/IWriterListener.cs +++ b/Assets/Fungus/Scripts/Interfaces/IWriterListener.cs @@ -2,7 +2,6 @@ // It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE) using UnityEngine; -using System.Collections; namespace Fungus { @@ -32,8 +31,15 @@ namespace Fungus /// Controls whether audio should be stopped when writing ends. void OnEnd(bool stopAudio); - /// Called when the Writer has no more text remaining, but may have waits or other tokens still pending. - void OnAllTextWritten(); + /// + /// Called when the Writer has no more Words remaining, but may have waits or other tokens still pending. + /// Will not be called if there is NO Words for the writer to process in the first place. e.g. Audio only says + /// do not trigger this. + /// + /// Note that the writer does not know what may happen after it's job is done. If a following Say does + /// not clear the existing, you'll get what looks like AllWordsWritten and then more words written. + /// + void OnAllWordsWritten(); /// Called every time the Writer writes a new character glyph. void OnGlyph();