You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
890 lines
28 KiB
890 lines
28 KiB
8 years ago
|
// This code is part of the Fungus library (http://fungusgames.com) maintained by Chris Gregan (http://twitter.com/gofungus).
|
||
|
// It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE)
|
||
9 years ago
|
|
||
9 years ago
|
using UnityEngine;
|
||
|
using UnityEngine.UI;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using System;
|
||
9 years ago
|
using System.Reflection;
|
||
9 years ago
|
using System.Text;
|
||
9 years ago
|
|
||
|
namespace Fungus
|
||
|
{
|
||
8 years ago
|
/// <summary>
|
||
|
/// Writes text using a typewriter effect to a UI text object.
|
||
|
/// </summary>
|
||
8 years ago
|
public class Writer : MonoBehaviour, IWriter, IDialogInputListener
|
||
8 years ago
|
{
|
||
|
[Tooltip("Gameobject containing a Text, Inout Field or Text Mesh object to write to")]
|
||
8 years ago
|
[SerializeField] protected GameObject targetTextObject;
|
||
8 years ago
|
|
||
|
[Tooltip("Gameobject to punch when the punch tags are displayed. If none is set, the main camera will shake instead.")]
|
||
8 years ago
|
[SerializeField] protected GameObject punchObject;
|
||
8 years ago
|
|
||
|
[Tooltip("Writing characters per second")]
|
||
8 years ago
|
[SerializeField] protected float writingSpeed = 60;
|
||
8 years ago
|
|
||
|
[Tooltip("Pause duration for punctuation characters")]
|
||
8 years ago
|
[SerializeField] protected float punctuationPause = 0.25f;
|
||
8 years ago
|
|
||
|
[Tooltip("Color of text that has not been revealed yet")]
|
||
8 years ago
|
[SerializeField] protected Color hiddenTextColor = new Color(1,1,1,0);
|
||
8 years ago
|
|
||
|
[Tooltip("Write one word at a time rather one character at a time")]
|
||
8 years ago
|
[SerializeField] protected bool writeWholeWords = false;
|
||
8 years ago
|
|
||
|
[Tooltip("Force the target text object to use Rich Text mode so text color and alpha appears correctly")]
|
||
8 years ago
|
[SerializeField] protected bool forceRichText = true;
|
||
8 years ago
|
|
||
|
[Tooltip("Click while text is writing to finish writing immediately")]
|
||
8 years ago
|
[SerializeField] protected bool instantComplete = true;
|
||
8 years ago
|
|
||
|
// This property is true when the writer is waiting for user input to continue
|
||
8 years ago
|
protected bool isWaitingForInput;
|
||
8 years ago
|
|
||
|
// This property is true when the writer is writing text or waiting (i.e. still processing tokens)
|
||
8 years ago
|
protected bool isWriting;
|
||
8 years ago
|
|
||
|
protected float currentWritingSpeed;
|
||
|
protected float currentPunctuationPause;
|
||
|
protected Text textUI;
|
||
|
protected InputField inputField;
|
||
|
protected TextMesh textMesh;
|
||
|
protected Component textComponent;
|
||
|
protected PropertyInfo textProperty;
|
||
|
|
||
|
protected bool boldActive = false;
|
||
|
protected bool italicActive = false;
|
||
|
protected bool colorActive = false;
|
||
|
protected string colorText = "";
|
||
|
protected bool sizeActive = false;
|
||
|
protected float sizeValue = 16f;
|
||
|
protected bool inputFlag;
|
||
|
protected bool exitFlag;
|
||
|
|
||
|
protected List<IWriterListener> writerListeners = new List<IWriterListener>();
|
||
9 years ago
|
|
||
9 years ago
|
protected StringBuilder openString = new StringBuilder(256);
|
||
|
protected StringBuilder closeString = new StringBuilder(256);
|
||
|
protected StringBuilder leftString = new StringBuilder(1024);
|
||
|
protected StringBuilder rightString = new StringBuilder(1024);
|
||
|
protected StringBuilder outputString = new StringBuilder(1024);
|
||
|
|
||
|
protected string hiddenColorOpen = "";
|
||
|
protected string hiddenColorClose = "";
|
||
|
|
||
8 years ago
|
public virtual string text
|
||
8 years ago
|
{
|
||
|
get
|
||
|
{
|
||
|
if (textUI != null)
|
||
|
{
|
||
|
return textUI.text;
|
||
|
}
|
||
|
else if (inputField != null)
|
||
|
{
|
||
|
return inputField.text;
|
||
|
}
|
||
|
else if (textMesh != null)
|
||
|
{
|
||
|
return textMesh.text;
|
||
|
}
|
||
|
else if (textProperty != null)
|
||
|
{
|
||
|
return textProperty.GetValue(textComponent, null) as string;
|
||
|
}
|
||
|
|
||
|
return "";
|
||
|
}
|
||
|
|
||
|
set
|
||
|
{
|
||
|
if (textUI != null)
|
||
|
{
|
||
|
textUI.text = value;
|
||
|
}
|
||
|
else if (inputField != null)
|
||
|
{
|
||
|
inputField.text = value;
|
||
|
}
|
||
|
else if (textMesh != null)
|
||
|
{
|
||
|
textMesh.text = value;
|
||
|
}
|
||
|
else if (textProperty != null)
|
||
|
{
|
||
|
textProperty.SetValue(textComponent, value, null);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
protected virtual void Awake()
|
||
|
{
|
||
|
GameObject go = targetTextObject;
|
||
|
if (go == null)
|
||
|
{
|
||
|
go = gameObject;
|
||
|
}
|
||
|
|
||
|
textUI = go.GetComponent<Text>();
|
||
|
inputField = go.GetComponent<InputField>();
|
||
|
textMesh = go.GetComponent<TextMesh>();
|
||
|
|
||
|
// Try to find any component with a text property
|
||
|
if (textUI == null && inputField == null && textMesh == null)
|
||
|
{
|
||
|
foreach (Component c in go.GetComponents<Component>())
|
||
|
{
|
||
|
textProperty = c.GetType().GetProperty("text");
|
||
|
if (textProperty != null)
|
||
|
{
|
||
|
textComponent = c;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Cache the list of child writer listeners
|
||
|
foreach (Component component in GetComponentsInChildren<Component>())
|
||
|
{
|
||
|
IWriterListener writerListener = component as IWriterListener;
|
||
|
if (writerListener != null)
|
||
|
{
|
||
|
writerListeners.Add(writerListener);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
protected virtual void Start()
|
||
|
{
|
||
9 years ago
|
// Cache the hidden color string
|
||
|
Color32 c = hiddenTextColor;
|
||
|
hiddenColorOpen = String.Format("<color=#{0:X2}{1:X2}{2:X2}{3:X2}>", c.r, c.g, c.b, c.a);
|
||
|
hiddenColorClose = "</color>";
|
||
|
|
||
8 years ago
|
if (forceRichText)
|
||
|
{
|
||
|
if (textUI != null)
|
||
|
{
|
||
|
textUI.supportRichText = true;
|
||
|
}
|
||
|
|
||
|
// Input Field does not support rich text
|
||
|
|
||
|
if (textMesh != null)
|
||
|
{
|
||
|
textMesh.richText = true;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public virtual bool HasTextObject()
|
||
|
{
|
||
|
return (textUI != null || inputField != null || textMesh != null || textComponent != null);
|
||
|
}
|
||
|
|
||
|
public virtual bool SupportsRichText()
|
||
|
{
|
||
|
if (textUI != null)
|
||
|
{
|
||
|
return textUI.supportRichText;
|
||
|
}
|
||
|
if (inputField != null)
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
if (textMesh != null)
|
||
|
{
|
||
|
return textMesh.richText;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
protected virtual void UpdateOpenMarkup()
|
||
|
{
|
||
9 years ago
|
openString.Length = 0;
|
||
8 years ago
|
|
||
|
if (SupportsRichText())
|
||
|
{
|
||
|
if (sizeActive)
|
||
|
{
|
||
9 years ago
|
openString.Append("<size=");
|
||
|
openString.Append(sizeValue);
|
||
|
openString.Append(">");
|
||
8 years ago
|
}
|
||
|
if (colorActive)
|
||
|
{
|
||
9 years ago
|
openString.Append("<color=");
|
||
|
openString.Append(colorText);
|
||
|
openString.Append(">");
|
||
8 years ago
|
}
|
||
|
if (boldActive)
|
||
|
{
|
||
9 years ago
|
openString.Append("<b>");
|
||
8 years ago
|
}
|
||
|
if (italicActive)
|
||
|
{
|
||
9 years ago
|
openString.Append("<i>");
|
||
8 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
protected virtual void UpdateCloseMarkup()
|
||
|
{
|
||
9 years ago
|
closeString.Length = 0;
|
||
8 years ago
|
|
||
|
if (SupportsRichText())
|
||
|
{
|
||
|
if (italicActive)
|
||
|
{
|
||
9 years ago
|
closeString.Append("</i>");
|
||
8 years ago
|
}
|
||
|
if (boldActive)
|
||
|
{
|
||
9 years ago
|
closeString.Append("</b>");
|
||
8 years ago
|
}
|
||
|
if (colorActive)
|
||
|
{
|
||
9 years ago
|
closeString.Append("</color>");
|
||
8 years ago
|
}
|
||
|
if (sizeActive)
|
||
|
{
|
||
9 years ago
|
closeString.Append("</size>");
|
||
8 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
virtual protected bool CheckParamCount(List<string> paramList, int count)
|
||
9 years ago
|
{
|
||
8 years ago
|
if (paramList == null)
|
||
|
{
|
||
9 years ago
|
Debug.LogError("paramList is null");
|
||
8 years ago
|
return false;
|
||
|
}
|
||
9 years ago
|
if (paramList.Count != count)
|
||
|
{
|
||
|
Debug.LogError("There must be exactly " + paramList.Count + " parameters.");
|
||
|
return false;
|
||
|
}
|
||
8 years ago
|
return true;
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
protected virtual bool TryGetSingleParam(List<string> paramList, int index, float defaultValue, out float value)
|
||
9 years ago
|
{
|
||
8 years ago
|
value = defaultValue;
|
||
|
if (paramList.Count > index)
|
||
9 years ago
|
{
|
||
8 years ago
|
Single.TryParse(paramList[index], out value);
|
||
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
9 years ago
|
|
||
8 years ago
|
protected virtual IEnumerator ProcessTokens(List<TextTagToken> tokens, bool stopAudio, Action onComplete)
|
||
8 years ago
|
{
|
||
|
// Reset control members
|
||
|
boldActive = false;
|
||
|
italicActive = false;
|
||
|
colorActive = false;
|
||
|
sizeActive = false;
|
||
|
colorText = "";
|
||
|
sizeValue = 16f;
|
||
|
currentPunctuationPause = punctuationPause;
|
||
|
currentWritingSpeed = writingSpeed;
|
||
|
|
||
|
exitFlag = false;
|
||
|
isWriting = true;
|
||
|
|
||
8 years ago
|
TextTagToken.TokenType previousTokenType = TextTagToken.TokenType.Invalid;
|
||
8 years ago
|
|
||
8 years ago
|
foreach (TextTagToken token in tokens)
|
||
8 years ago
|
{
|
||
9 years ago
|
|
||
8 years ago
|
switch (token.type)
|
||
|
{
|
||
8 years ago
|
case TextTagToken.TokenType.Words:
|
||
8 years ago
|
yield return StartCoroutine(DoWords(token.paramList, previousTokenType));
|
||
|
break;
|
||
|
|
||
8 years ago
|
case TextTagToken.TokenType.BoldStart:
|
||
8 years ago
|
boldActive = true;
|
||
|
break;
|
||
|
|
||
8 years ago
|
case TextTagToken.TokenType.BoldEnd:
|
||
8 years ago
|
boldActive = false;
|
||
|
break;
|
||
|
|
||
8 years ago
|
case TextTagToken.TokenType.ItalicStart:
|
||
8 years ago
|
italicActive = true;
|
||
|
break;
|
||
|
|
||
8 years ago
|
case TextTagToken.TokenType.ItalicEnd:
|
||
8 years ago
|
italicActive = false;
|
||
|
break;
|
||
|
|
||
8 years ago
|
case TextTagToken.TokenType.ColorStart:
|
||
8 years ago
|
if (CheckParamCount(token.paramList, 1))
|
||
|
{
|
||
|
colorActive = true;
|
||
|
colorText = token.paramList[0];
|
||
|
}
|
||
|
break;
|
||
|
|
||
8 years ago
|
case TextTagToken.TokenType.ColorEnd:
|
||
8 years ago
|
colorActive = false;
|
||
|
break;
|
||
9 years ago
|
|
||
8 years ago
|
case TextTagToken.TokenType.SizeStart:
|
||
8 years ago
|
if (TryGetSingleParam(token.paramList, 0, 16f, out sizeValue))
|
||
|
{
|
||
|
sizeActive = true;
|
||
|
}
|
||
|
break;
|
||
9 years ago
|
|
||
8 years ago
|
case TextTagToken.TokenType.SizeEnd:
|
||
8 years ago
|
sizeActive = false;
|
||
|
break;
|
||
9 years ago
|
|
||
8 years ago
|
case TextTagToken.TokenType.Wait:
|
||
9 years ago
|
yield return StartCoroutine(DoWait(token.paramList));
|
||
8 years ago
|
break;
|
||
|
|
||
8 years ago
|
case TextTagToken.TokenType.WaitForInputNoClear:
|
||
8 years ago
|
yield return StartCoroutine(DoWaitForInput(false));
|
||
|
break;
|
||
|
|
||
8 years ago
|
case TextTagToken.TokenType.WaitForInputAndClear:
|
||
8 years ago
|
yield return StartCoroutine(DoWaitForInput(true));
|
||
|
break;
|
||
|
|
||
8 years ago
|
case TextTagToken.TokenType.WaitOnPunctuationStart:
|
||
9 years ago
|
TryGetSingleParam(token.paramList, 0, punctuationPause, out currentPunctuationPause);
|
||
8 years ago
|
break;
|
||
|
|
||
8 years ago
|
case TextTagToken.TokenType.WaitOnPunctuationEnd:
|
||
8 years ago
|
currentPunctuationPause = punctuationPause;
|
||
|
break;
|
||
|
|
||
8 years ago
|
case TextTagToken.TokenType.Clear:
|
||
8 years ago
|
text = "";
|
||
|
break;
|
||
|
|
||
8 years ago
|
case TextTagToken.TokenType.SpeedStart:
|
||
9 years ago
|
TryGetSingleParam(token.paramList, 0, writingSpeed, out currentWritingSpeed);
|
||
8 years ago
|
break;
|
||
|
|
||
8 years ago
|
case TextTagToken.TokenType.SpeedEnd:
|
||
8 years ago
|
currentWritingSpeed = writingSpeed;
|
||
|
break;
|
||
|
|
||
8 years ago
|
case TextTagToken.TokenType.Exit:
|
||
8 years ago
|
exitFlag = true;
|
||
|
break;
|
||
|
|
||
|
|
||
8 years ago
|
case TextTagToken.TokenType.Message:
|
||
9 years ago
|
if (CheckParamCount(token.paramList, 1))
|
||
8 years ago
|
{
|
||
9 years ago
|
Flowchart.BroadcastFungusMessage(token.paramList[0]);
|
||
|
}
|
||
8 years ago
|
break;
|
||
|
|
||
8 years ago
|
case TextTagToken.TokenType.VerticalPunch:
|
||
9 years ago
|
{
|
||
8 years ago
|
float vintensity;
|
||
|
float time;
|
||
|
TryGetSingleParam(token.paramList, 0, 10.0f, out vintensity);
|
||
|
TryGetSingleParam(token.paramList, 1, 0.5f, out time);
|
||
|
Punch(new Vector3(0, vintensity, 0), time);
|
||
|
}
|
||
9 years ago
|
break;
|
||
8 years ago
|
|
||
8 years ago
|
case TextTagToken.TokenType.HorizontalPunch:
|
||
9 years ago
|
{
|
||
8 years ago
|
float hintensity;
|
||
|
float time;
|
||
|
TryGetSingleParam(token.paramList, 0, 10.0f, out hintensity);
|
||
|
TryGetSingleParam(token.paramList, 1, 0.5f, out time);
|
||
|
Punch(new Vector3(hintensity, 0, 0), time);
|
||
9 years ago
|
}
|
||
|
break;
|
||
8 years ago
|
|
||
8 years ago
|
case TextTagToken.TokenType.Punch:
|
||
9 years ago
|
{
|
||
8 years ago
|
float intensity;
|
||
|
float time;
|
||
|
TryGetSingleParam(token.paramList, 0, 10.0f, out intensity);
|
||
|
TryGetSingleParam(token.paramList, 1, 0.5f, out time);
|
||
|
Punch(new Vector3(intensity, intensity, 0), time);
|
||
|
}
|
||
9 years ago
|
break;
|
||
8 years ago
|
|
||
8 years ago
|
case TextTagToken.TokenType.Flash:
|
||
8 years ago
|
float flashDuration;
|
||
9 years ago
|
TryGetSingleParam(token.paramList, 0, 0.2f, out flashDuration);
|
||
8 years ago
|
Flash(flashDuration);
|
||
|
break;
|
||
9 years ago
|
|
||
8 years ago
|
case TextTagToken.TokenType.Audio:
|
||
9 years ago
|
{
|
||
|
AudioSource audioSource = null;
|
||
|
if (CheckParamCount(token.paramList, 1))
|
||
|
{
|
||
|
audioSource = FindAudio(token.paramList[0]);
|
||
|
}
|
||
|
if (audioSource != null)
|
||
|
{
|
||
|
audioSource.PlayOneShot(audioSource.clip);
|
||
|
}
|
||
|
}
|
||
|
break;
|
||
8 years ago
|
|
||
8 years ago
|
case TextTagToken.TokenType.AudioLoop:
|
||
8 years ago
|
{
|
||
9 years ago
|
AudioSource audioSource = null;
|
||
8 years ago
|
if (CheckParamCount(token.paramList, 1))
|
||
|
{
|
||
|
audioSource = FindAudio(token.paramList[0]);
|
||
|
}
|
||
|
if (audioSource != null)
|
||
|
{
|
||
|
audioSource.Play();
|
||
|
audioSource.loop = true;
|
||
|
}
|
||
|
}
|
||
|
break;
|
||
|
|
||
8 years ago
|
case TextTagToken.TokenType.AudioPause:
|
||
8 years ago
|
{
|
||
9 years ago
|
AudioSource audioSource = null;
|
||
8 years ago
|
if (CheckParamCount(token.paramList, 1))
|
||
|
{
|
||
|
audioSource = FindAudio(token.paramList[0]);
|
||
|
}
|
||
|
if (audioSource != null)
|
||
|
{
|
||
|
audioSource.Pause();
|
||
|
}
|
||
|
}
|
||
|
break;
|
||
|
|
||
8 years ago
|
case TextTagToken.TokenType.AudioStop:
|
||
8 years ago
|
{
|
||
9 years ago
|
AudioSource audioSource = null;
|
||
8 years ago
|
if (CheckParamCount(token.paramList, 1))
|
||
|
{
|
||
|
audioSource = FindAudio(token.paramList[0]);
|
||
|
}
|
||
|
if (audioSource != null)
|
||
|
{
|
||
|
audioSource.Stop();
|
||
|
}
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
previousTokenType = token.type;
|
||
|
|
||
|
if (exitFlag)
|
||
|
{
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
inputFlag = false;
|
||
|
exitFlag = false;
|
||
|
isWaitingForInput = false;
|
||
|
isWriting = false;
|
||
|
|
||
|
NotifyEnd(stopAudio);
|
||
|
|
||
|
if (onComplete != null)
|
||
|
{
|
||
|
onComplete();
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
protected virtual IEnumerator DoWords(List<string> paramList, TextTagToken.TokenType previousTokenType)
|
||
8 years ago
|
{
|
||
|
if (!CheckParamCount(paramList, 1))
|
||
|
{
|
||
|
yield break;
|
||
|
}
|
||
|
|
||
|
string param = paramList[0];
|
||
|
|
||
|
// Trim whitespace after a {wc} or {c} tag
|
||
8 years ago
|
if (previousTokenType == TextTagToken.TokenType.WaitForInputAndClear ||
|
||
|
previousTokenType == TextTagToken.TokenType.Clear)
|
||
8 years ago
|
{
|
||
|
param = param.TrimStart(' ', '\t', '\r', '\n');
|
||
|
}
|
||
|
|
||
9 years ago
|
string startText = text;
|
||
9 years ago
|
UpdateOpenMarkup();
|
||
|
UpdateCloseMarkup();
|
||
9 years ago
|
|
||
|
float timeAccumulator = Time.deltaTime;
|
||
|
|
||
9 years ago
|
for (int i = 0; i < param.Length + 1; ++i)
|
||
9 years ago
|
{
|
||
|
// Exit immediately if the exit flag has been set
|
||
|
if (exitFlag)
|
||
|
{
|
||
|
break;
|
||
|
}
|
||
|
|
||
9 years ago
|
PartitionString(writeWholeWords, param, i);
|
||
|
ConcatenateString(startText);
|
||
|
text = outputString.ToString();
|
||
9 years ago
|
|
||
|
NotifyGlyph();
|
||
|
|
||
|
// No delay if user has clicked and Instant Complete is enabled
|
||
8 years ago
|
if (instantComplete && inputFlag)
|
||
9 years ago
|
{
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
// Punctuation pause
|
||
9 years ago
|
if (leftString.Length > 0 &&
|
||
8 years ago
|
rightString.Length > 0 &&
|
||
9 years ago
|
IsPunctuation(leftString.ToString(leftString.Length - 1, 1)[0]))
|
||
9 years ago
|
{
|
||
|
yield return StartCoroutine(DoWait(currentPunctuationPause));
|
||
|
}
|
||
|
|
||
|
// Delay between characters
|
||
|
if (currentWritingSpeed > 0f)
|
||
|
{
|
||
|
if (timeAccumulator > 0f)
|
||
|
{
|
||
|
timeAccumulator -= 1f / currentWritingSpeed;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
yield return new WaitForSeconds(1f / currentWritingSpeed);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
8 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
protected void PartitionString(bool wholeWords, string inputString, int i)
|
||
|
{
|
||
9 years ago
|
leftString.Length = 0;
|
||
9 years ago
|
rightString.Length = 0;
|
||
|
|
||
|
// Reached last character
|
||
9 years ago
|
leftString.Append(inputString);
|
||
9 years ago
|
if (i >= inputString.Length)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
9 years ago
|
|
||
|
rightString.Append(inputString);
|
||
9 years ago
|
|
||
8 years ago
|
if (wholeWords)
|
||
|
{
|
||
|
// Look ahead to find next whitespace or end of string
|
||
|
for (int j = i; j < inputString.Length + 1; ++j)
|
||
|
{
|
||
9 years ago
|
if (j == inputString.Length || Char.IsWhiteSpace(inputString[j]))
|
||
8 years ago
|
{
|
||
9 years ago
|
leftString.Length = j;
|
||
9 years ago
|
rightString.Remove(0, j);
|
||
8 years ago
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
9 years ago
|
leftString.Remove(i, inputString.Length - i);
|
||
|
rightString.Remove(0, i);
|
||
8 years ago
|
}
|
||
|
}
|
||
9 years ago
|
|
||
8 years ago
|
protected void ConcatenateString(string startText)
|
||
|
{
|
||
9 years ago
|
outputString.Length = 0;
|
||
|
|
||
|
// string tempText = startText + openText + leftText + closeText;
|
||
|
outputString.Append(startText);
|
||
|
outputString.Append(openString);
|
||
|
outputString.Append(leftString);
|
||
|
outputString.Append(closeString);
|
||
9 years ago
|
|
||
8 years ago
|
// Make right hand side text hidden
|
||
|
if (SupportsRichText() &&
|
||
|
rightString.Length > 0)
|
||
|
{
|
||
9 years ago
|
outputString.Append(hiddenColorOpen);
|
||
|
outputString.Append(rightString);
|
||
|
outputString.Append(hiddenColorClose);
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
|
public virtual string GetTagHelp()
|
||
|
{
|
||
|
return "";
|
||
|
}
|
||
|
|
||
|
protected virtual IEnumerator DoWait(List<string> paramList)
|
||
|
{
|
||
9 years ago
|
var param = "";
|
||
|
if (paramList.Count == 1)
|
||
|
{
|
||
|
param = paramList[0];
|
||
|
}
|
||
|
|
||
|
float duration = 1f;
|
||
|
if (!Single.TryParse(param, out duration))
|
||
|
{
|
||
|
duration = 1f;
|
||
|
}
|
||
|
|
||
8 years ago
|
yield return StartCoroutine( DoWait(duration) );
|
||
|
}
|
||
|
|
||
|
protected virtual IEnumerator DoWait(float duration)
|
||
|
{
|
||
|
NotifyPause();
|
||
|
|
||
|
float timeRemaining = duration;
|
||
|
while (timeRemaining > 0f && !exitFlag)
|
||
|
{
|
||
|
if (instantComplete && inputFlag)
|
||
|
{
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
timeRemaining -= Time.deltaTime;
|
||
|
yield return null;
|
||
|
}
|
||
|
|
||
|
NotifyResume();
|
||
|
}
|
||
|
|
||
|
protected virtual IEnumerator DoWaitForInput(bool clear)
|
||
|
{
|
||
|
NotifyPause();
|
||
|
|
||
|
inputFlag = false;
|
||
|
isWaitingForInput = true;
|
||
|
|
||
|
while (!inputFlag && !exitFlag)
|
||
|
{
|
||
|
yield return null;
|
||
|
}
|
||
|
|
||
|
isWaitingForInput = false;
|
||
|
inputFlag = false;
|
||
|
|
||
|
if (clear)
|
||
|
{
|
||
|
textUI.text = "";
|
||
|
}
|
||
|
|
||
|
NotifyResume();
|
||
|
}
|
||
|
|
||
|
protected virtual bool IsPunctuation(char character)
|
||
|
{
|
||
|
return character == '.' ||
|
||
|
character == '?' ||
|
||
|
character == '!' ||
|
||
|
character == ',' ||
|
||
|
character == ':' ||
|
||
|
character == ';' ||
|
||
|
character == ')';
|
||
|
}
|
||
|
|
||
|
protected virtual void Punch(Vector3 axis, float time)
|
||
|
{
|
||
|
GameObject go = punchObject;
|
||
|
if (go == null)
|
||
|
{
|
||
|
go = Camera.main.gameObject;
|
||
|
}
|
||
|
|
||
|
if (go != null)
|
||
|
{
|
||
9 years ago
|
iTween.ShakePosition(go, axis, time);
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
|
protected virtual void Flash(float duration)
|
||
|
{
|
||
8 years ago
|
ICameraController cameraController = CameraController.GetInstance();
|
||
8 years ago
|
cameraController.ScreenFadeTexture = CameraController.CreateColorTexture(new Color(1f,1f,1f,1f), 32, 32);
|
||
8 years ago
|
cameraController.Fade(1f, duration, delegate {
|
||
8 years ago
|
cameraController.ScreenFadeTexture = CameraController.CreateColorTexture(new Color(1f,1f,1f,1f), 32, 32);
|
||
8 years ago
|
cameraController.Fade(0f, duration, null);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
protected virtual AudioSource FindAudio(string audioObjectName)
|
||
|
{
|
||
|
GameObject go = GameObject.Find(audioObjectName);
|
||
|
if (go == null)
|
||
|
{
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
return go.GetComponent<AudioSource>();
|
||
|
}
|
||
|
|
||
|
protected virtual void NotifyInput()
|
||
|
{
|
||
|
foreach (IWriterListener writerListener in writerListeners)
|
||
|
{
|
||
|
writerListener.OnInput();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
protected virtual void NotifyStart(AudioClip audioClip)
|
||
|
{
|
||
|
foreach (IWriterListener writerListener in writerListeners)
|
||
|
{
|
||
|
writerListener.OnStart(audioClip);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
protected virtual void NotifyPause()
|
||
|
{
|
||
|
foreach (IWriterListener writerListener in writerListeners)
|
||
|
{
|
||
|
writerListener.OnPause();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
protected virtual void NotifyResume()
|
||
|
{
|
||
|
foreach (IWriterListener writerListener in writerListeners)
|
||
|
{
|
||
|
writerListener.OnResume();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
protected virtual void NotifyEnd(bool stopAudio)
|
||
|
{
|
||
|
foreach (IWriterListener writerListener in writerListeners)
|
||
|
{
|
||
|
writerListener.OnEnd(stopAudio);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
protected virtual void NotifyGlyph()
|
||
|
{
|
||
|
foreach (IWriterListener writerListener in writerListeners)
|
||
|
{
|
||
|
writerListener.OnGlyph();
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
#region IDialogInputListener implementation
|
||
|
|
||
8 years ago
|
public virtual void OnNextLineEvent()
|
||
|
{
|
||
|
inputFlag = true;
|
||
|
|
||
|
if (isWriting)
|
||
|
{
|
||
|
NotifyInput();
|
||
|
}
|
||
|
}
|
||
8 years ago
|
|
||
|
#endregion
|
||
8 years ago
|
|
||
|
#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}";
|
||
|
}
|
||
|
|
||
8 years ago
|
ITextTagParser tagParser = new TextTagParser();
|
||
|
List<TextTagToken> tokens = tagParser.Tokenize(tokenText);
|
||
8 years ago
|
|
||
|
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
|
||
8 years ago
|
}
|
||
9 years ago
|
}
|