diff --git a/Assets/Fungus/Narrative/Scripts/SayDialog.cs b/Assets/Fungus/Narrative/Scripts/SayDialog.cs index dae9243a..01bb550b 100644 --- a/Assets/Fungus/Narrative/Scripts/SayDialog.cs +++ b/Assets/Fungus/Narrative/Scripts/SayDialog.cs @@ -46,7 +46,7 @@ namespace Fungus protected float startStoryTextInset; protected WriterAudio writerAudio; - protected Writer writer; + protected IWriter writer; protected CanvasGroup canvasGroup; protected bool fadeWhenDone = true; @@ -83,14 +83,14 @@ namespace Fungus return activeSayDialog; } - protected Writer GetWriter() + protected IWriter GetWriter() { if (writer != null) { return writer; } - writer = GetComponent(); + writer = GetComponent(); if (writer == null) { writer = gameObject.AddComponent(); @@ -160,7 +160,7 @@ namespace Fungus public virtual IEnumerator SayInternal(string text, bool clearPrevious, bool waitForInput, bool fadeWhenDone, bool stopVoiceover, AudioClip voiceOverClip, Action onComplete) { - Writer writer = GetWriter(); + IWriter writer = GetWriter(); if (writer.IsWriting || writer.IsWaitingForInput) { diff --git a/Assets/Fungus/UI/Scripts/Commands/Write.cs b/Assets/Fungus/UI/Scripts/Commands/Write.cs index d4ecf3d2..c2c5f856 100644 --- a/Assets/Fungus/UI/Scripts/Commands/Write.cs +++ b/Assets/Fungus/UI/Scripts/Commands/Write.cs @@ -43,12 +43,12 @@ namespace Fungus [SerializeField] protected ColorData setColor = new ColorData(Color.white); - protected Writer GetWriter() + protected IWriter GetWriter() { - Writer writer = textObject.GetComponent(); + IWriter writer = textObject.GetComponent(); if (writer == null) { - writer = textObject.AddComponent() as Writer; + writer = textObject.AddComponent(); } return writer; @@ -62,7 +62,7 @@ namespace Fungus return; } - Writer writer = GetWriter(); + IWriter writer = GetWriter(); if (writer == null) { Continue(); diff --git a/Assets/Fungus/UI/Scripts/IWriter.cs b/Assets/Fungus/UI/Scripts/IWriter.cs new file mode 100644 index 00000000..98c5127d --- /dev/null +++ b/Assets/Fungus/UI/Scripts/IWriter.cs @@ -0,0 +1,75 @@ +using UnityEngine; +using System.Collections; + +namespace Fungus +{ + /// + /// Writes text using a typewriter effect to a UI text object. + /// + public interface IWriter + { + /// + /// This property is true when the writer is writing text or waiting (i.e. still processing tokens). + /// + bool IsWriting { get; } + + /// + /// This property is true when the writer is waiting for user input to continue. + /// + bool IsWaitingForInput { get; } + + /// + /// Stop writing text. + /// + void Stop(); + + /// + /// Writes text using a typewriter effect to a UI text object. + /// + /// Text to be written + /// If true clears the previous text. + /// Writes the text and then waits for player input before calling onComplete. + /// Stops any currently playing audioclip. + /// Audio clip to play when text starts writing. + /// Callback to call when writing is finished. + IEnumerator Write(string content, bool clear, bool waitForInput, bool stopAudio, AudioClip audioClip, System.Action onComplete); + + /// + /// Sets the color property of the text UI object. + /// + void SetTextColor(Color textColor); + + /// + /// Sets the alpha component of the color property of the text UI object. + /// + void SetTextAlpha(float textAlpha); + } + + /// + /// Implement this interface to be notified about Writer events. + /// + public interface IWriterListener + { + /// + /// Called when a user input event (e.g. a click) has been handled by the Writer. + /// + void OnInput(); + + /// Called when the Writer starts writing new text + /// An optional audioClip sound effect can be supplied (e.g. for voiceover) + void OnStart(AudioClip audioClip); + + /// Called when the Writer has paused writing text (e.g. on a {wi} tag). + void OnPause(); + + /// Called when the Writer has resumed writing text. + void OnResume(); + + /// Called when the Writer has finshed writing text. + /// Controls whether audio should be stopped when writing ends. + void OnEnd(bool stopAudio); + + /// Called every time the Writer writes a new character glyph. + void OnGlyph(); + } +} \ No newline at end of file diff --git a/Assets/Fungus/UI/Scripts/IWriter.cs.meta b/Assets/Fungus/UI/Scripts/IWriter.cs.meta new file mode 100644 index 00000000..e1c03443 --- /dev/null +++ b/Assets/Fungus/UI/Scripts/IWriter.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: fcf9b300dd82848cca0aec7c2e5cfee1 +timeCreated: 1473432327 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Fungus/UI/Scripts/Writer.cs b/Assets/Fungus/UI/Scripts/Writer.cs index e967bfa8..6fefff7a 100644 --- a/Assets/Fungus/UI/Scripts/Writer.cs +++ b/Assets/Fungus/UI/Scripts/Writer.cs @@ -11,38 +11,10 @@ using System.Text; namespace Fungus { - /// - /// Implement this interface to be notified about Writer events. - /// - public interface IWriterListener - { - /// - /// Called when a user input event (e.g. a click) has been handled by the Writer. - /// - void OnInput(); - - /// Called when the Writer starts writing new text - /// An optional audioClip sound effect can be supplied (e.g. for voiceover) - void OnStart(AudioClip audioClip); - - /// Called when the Writer has paused writing text (e.g. on a {wi} tag). - void OnPause(); - - /// Called when the Writer has resumed writing text. - void OnResume(); - - /// Called when the Writer has finshed writing text. - /// Controls whether audio should be stopped when writing ends. - void OnEnd(bool stopAudio); - - /// Called every time the Writer writes a new character glyph. - void OnGlyph(); - } - /// /// Writes text using a typewriter effect to a UI text object. /// - public class Writer : MonoBehaviour, IDialogInputListener + public class Writer : MonoBehaviour, IWriter, IDialogInputListener { [Tooltip("Gameobject containing a Text, Inout Field or Text Mesh object to write to")] [SerializeField] protected GameObject targetTextObject; @@ -70,11 +42,9 @@ namespace Fungus // This property is true when the writer is waiting for user input to continue protected bool isWaitingForInput; - public virtual bool IsWaitingForInput { get { return isWaitingForInput; } } // This property is true when the writer is writing text or waiting (i.e. still processing tokens) protected bool isWriting; - public virtual bool IsWriting { get { return isWriting; } } protected float currentWritingSpeed; protected float currentPunctuationPause; @@ -285,87 +255,6 @@ namespace Fungus } } - public virtual void SetTextColor(Color textColor) - { - if (textUI != null) - { - textUI.color = textColor; - } - else if (inputField != null) - { - if (inputField.textComponent != null) - { - inputField.textComponent.color = textColor; - } - } - else if (textMesh != null) - { - textMesh.color = textColor; - } - } - - public virtual void SetTextAlpha(float textAlpha) - { - if (textUI != null) - { - Color tempColor = textUI.color; - tempColor.a = textAlpha; - textUI.color = tempColor; - } - else if (inputField != null) - { - if (inputField.textComponent != null) - { - Color tempColor = inputField.textComponent.color; - tempColor.a = textAlpha; - inputField.textComponent.color = tempColor; - } - } - else if (textMesh != null) - { - Color tempColor = textMesh.color; - tempColor.a = textAlpha; - textMesh.color = tempColor; - } - } - - public virtual void Stop() - { - if (isWriting || isWaitingForInput) - { - exitFlag = true; - } - } - - public virtual IEnumerator Write(string content, bool clear, bool waitForInput, bool stopAudio, AudioClip audioClip, Action onComplete) - { - if (clear) - { - this.text = ""; - } - - if (!HasTextObject()) - { - yield break; - } - - // If this clip is null then WriterAudio will play the default sound effect (if any) - NotifyStart(audioClip); - - string tokenText = content; - if (waitForInput) - { - tokenText += "{wi}"; - } - - TextTagParser tagParser = new TextTagParser(); - List tokens = tagParser.Tokenize(tokenText); - - gameObject.SetActive(true); - - yield return StartCoroutine(ProcessTokens(tokens, stopAudio, onComplete)); - } - virtual protected bool CheckParamCount(List paramList, int count) { if (paramList == null) @@ -381,7 +270,6 @@ namespace Fungus return true; } - protected virtual bool TryGetSingleParam(List paramList, int index, float defaultValue, out float value) { value = defaultValue; @@ -908,5 +796,94 @@ namespace Fungus } #endregion + + #region IWriter implementation + + public virtual bool IsWriting { get { return isWriting; } } + + public virtual bool IsWaitingForInput { get { return isWaitingForInput; } } + + public virtual void Stop() + { + if (isWriting || isWaitingForInput) + { + exitFlag = true; + } + } + + public virtual IEnumerator Write(string content, bool clear, bool waitForInput, bool stopAudio, AudioClip audioClip, Action onComplete) + { + if (clear) + { + this.text = ""; + } + + if (!HasTextObject()) + { + yield break; + } + + // If this clip is null then WriterAudio will play the default sound effect (if any) + NotifyStart(audioClip); + + string tokenText = content; + if (waitForInput) + { + tokenText += "{wi}"; + } + + TextTagParser tagParser = new TextTagParser(); + List tokens = tagParser.Tokenize(tokenText); + + gameObject.SetActive(true); + + yield return StartCoroutine(ProcessTokens(tokens, stopAudio, onComplete)); + } + + public virtual void SetTextColor(Color textColor) + { + if (textUI != null) + { + textUI.color = textColor; + } + else if (inputField != null) + { + if (inputField.textComponent != null) + { + inputField.textComponent.color = textColor; + } + } + else if (textMesh != null) + { + textMesh.color = textColor; + } + } + + public virtual void SetTextAlpha(float textAlpha) + { + if (textUI != null) + { + Color tempColor = textUI.color; + tempColor.a = textAlpha; + textUI.color = tempColor; + } + else if (inputField != null) + { + if (inputField.textComponent != null) + { + Color tempColor = inputField.textComponent.color; + tempColor.a = textAlpha; + inputField.textComponent.color = tempColor; + } + } + else if (textMesh != null) + { + Color tempColor = textMesh.color; + tempColor.a = textAlpha; + textMesh.color = tempColor; + } + } + + #endregion } }