Browse Source

Refactored Writer to use IWriter interface

master
Christopher 9 years ago
parent
commit
e2f70fd83c
  1. 8
      Assets/Fungus/Narrative/Scripts/SayDialog.cs
  2. 8
      Assets/Fungus/UI/Scripts/Commands/Write.cs
  3. 75
      Assets/Fungus/UI/Scripts/IWriter.cs
  4. 12
      Assets/Fungus/UI/Scripts/IWriter.cs.meta
  5. 203
      Assets/Fungus/UI/Scripts/Writer.cs

8
Assets/Fungus/Narrative/Scripts/SayDialog.cs

@ -46,7 +46,7 @@ namespace Fungus
protected float startStoryTextInset; protected float startStoryTextInset;
protected WriterAudio writerAudio; protected WriterAudio writerAudio;
protected Writer writer; protected IWriter writer;
protected CanvasGroup canvasGroup; protected CanvasGroup canvasGroup;
protected bool fadeWhenDone = true; protected bool fadeWhenDone = true;
@ -83,14 +83,14 @@ namespace Fungus
return activeSayDialog; return activeSayDialog;
} }
protected Writer GetWriter() protected IWriter GetWriter()
{ {
if (writer != null) if (writer != null)
{ {
return writer; return writer;
} }
writer = GetComponent<Writer>(); writer = GetComponent<IWriter>();
if (writer == null) if (writer == null)
{ {
writer = gameObject.AddComponent<Writer>(); writer = gameObject.AddComponent<Writer>();
@ -160,7 +160,7 @@ namespace Fungus
public virtual IEnumerator SayInternal(string text, bool clearPrevious, bool waitForInput, bool fadeWhenDone, bool stopVoiceover, AudioClip voiceOverClip, Action onComplete) 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) if (writer.IsWriting || writer.IsWaitingForInput)
{ {

8
Assets/Fungus/UI/Scripts/Commands/Write.cs

@ -43,12 +43,12 @@ namespace Fungus
[SerializeField] protected ColorData setColor = new ColorData(Color.white); [SerializeField] protected ColorData setColor = new ColorData(Color.white);
protected Writer GetWriter() protected IWriter GetWriter()
{ {
Writer writer = textObject.GetComponent<Writer>(); IWriter writer = textObject.GetComponent<IWriter>();
if (writer == null) if (writer == null)
{ {
writer = textObject.AddComponent<Writer>() as Writer; writer = textObject.AddComponent<Writer>();
} }
return writer; return writer;
@ -62,7 +62,7 @@ namespace Fungus
return; return;
} }
Writer writer = GetWriter(); IWriter writer = GetWriter();
if (writer == null) if (writer == null)
{ {
Continue(); Continue();

75
Assets/Fungus/UI/Scripts/IWriter.cs

@ -0,0 +1,75 @@
using UnityEngine;
using System.Collections;
namespace Fungus
{
/// <summary>
/// Writes text using a typewriter effect to a UI text object.
/// </summary>
public interface IWriter
{
/// <summary>
/// This property is true when the writer is writing text or waiting (i.e. still processing tokens).
/// </summary>
bool IsWriting { get; }
/// <summary>
/// This property is true when the writer is waiting for user input to continue.
/// </summary>
bool IsWaitingForInput { get; }
/// <summary>
/// Stop writing text.
/// </summary>
void Stop();
/// <summary>
/// Writes text using a typewriter effect to a UI text object.
/// </summary>
/// <param name="content">Text to be written</param>
/// <param name="clear">If true clears the previous text.</param>
/// <param name="waitForInput">Writes the text and then waits for player input before calling onComplete.</param>
/// <param name="stopAudio">Stops any currently playing audioclip.</param>
/// <param name="audioClip">Audio clip to play when text starts writing.</param>
/// <param name="onComplete">Callback to call when writing is finished.</param>
IEnumerator Write(string content, bool clear, bool waitForInput, bool stopAudio, AudioClip audioClip, System.Action onComplete);
/// <summary>
/// Sets the color property of the text UI object.
/// </summary>
void SetTextColor(Color textColor);
/// <summary>
/// Sets the alpha component of the color property of the text UI object.
/// </summary>
void SetTextAlpha(float textAlpha);
}
/// <summary>
/// Implement this interface to be notified about Writer events.
/// </summary>
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
/// <param name="audioClip">An optional audioClip sound effect can be supplied (e.g. for voiceover)</param>
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.
/// <param name="stopAudio">Controls whether audio should be stopped when writing ends.</param>
void OnEnd(bool stopAudio);
/// Called every time the Writer writes a new character glyph.
void OnGlyph();
}
}

12
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:

203
Assets/Fungus/UI/Scripts/Writer.cs

@ -11,38 +11,10 @@ using System.Text;
namespace Fungus namespace Fungus
{ {
/// <summary>
/// Implement this interface to be notified about Writer events.
/// </summary>
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
/// <param name="audioClip">An optional audioClip sound effect can be supplied (e.g. for voiceover)</param>
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.
/// <param name="stopAudio">Controls whether audio should be stopped when writing ends.</param>
void OnEnd(bool stopAudio);
/// Called every time the Writer writes a new character glyph.
void OnGlyph();
}
/// <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>
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")] [Tooltip("Gameobject containing a Text, Inout Field or Text Mesh object to write to")]
[SerializeField] protected GameObject targetTextObject; [SerializeField] protected GameObject targetTextObject;
@ -70,11 +42,9 @@ namespace Fungus
// This property is true when the writer is waiting for user input to continue // This property is true when the writer is waiting for user input to continue
protected bool isWaitingForInput; 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) // This property is true when the writer is writing text or waiting (i.e. still processing tokens)
protected bool isWriting; protected bool isWriting;
public virtual bool IsWriting { get { return isWriting; } }
protected float currentWritingSpeed; protected float currentWritingSpeed;
protected float currentPunctuationPause; 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<TextTagParser.Token> tokens = tagParser.Tokenize(tokenText);
gameObject.SetActive(true);
yield return StartCoroutine(ProcessTokens(tokens, stopAudio, onComplete));
}
virtual protected bool CheckParamCount(List<string> paramList, int count) virtual protected bool CheckParamCount(List<string> paramList, int count)
{ {
if (paramList == null) if (paramList == null)
@ -381,7 +270,6 @@ namespace Fungus
return true; return true;
} }
protected virtual bool TryGetSingleParam(List<string> paramList, int index, float defaultValue, out float value) protected virtual bool TryGetSingleParam(List<string> paramList, int index, float defaultValue, out float value)
{ {
value = defaultValue; value = defaultValue;
@ -908,5 +796,94 @@ namespace Fungus
} }
#endregion #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<TextTagParser.Token> 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
} }
} }

Loading…
Cancel
Save