Browse Source

Renamed Glyph to Token in Dialog class

master
chrisgregan 10 years ago
parent
commit
b06e3098e4
  1. 148
      Assets/Fungus/Dialog/Scripts/Dialog.cs

148
Assets/Fungus/Dialog/Scripts/Dialog.cs

@ -35,7 +35,7 @@ namespace Fungus
protected bool wasPointerClicked; protected bool wasPointerClicked;
protected enum GlyphType protected enum TokenType
{ {
Character, // Text character Character, // Text character
BoldStart, // b BoldStart, // b
@ -53,9 +53,9 @@ namespace Fungus
Exit // x Exit // x
} }
protected class Glyph protected class Token
{ {
public GlyphType type = GlyphType.Character; public TokenType type = TokenType.Character;
public string param = ""; public string param = "";
} }
@ -149,9 +149,9 @@ namespace Fungus
currentSpeed = writingSpeed; currentSpeed = writingSpeed;
currentPunctuationPause = punctuationPause; currentPunctuationPause = punctuationPause;
List<Glyph> glyphs = MakeGlyphList(text); List<Token> tokens = MakeTokenList(text);
if (glyphs.Count == 0) if (tokens.Count == 0)
{ {
if (onWritingComplete != null) if (onWritingComplete != null)
{ {
@ -188,7 +188,7 @@ namespace Fungus
float timeAccumulator = 0f; float timeAccumulator = 0f;
int i = 0; int i = 0;
while (i < glyphs.Count) while (i < tokens.Count)
{ {
timeAccumulator += Time.deltaTime; timeAccumulator += Time.deltaTime;
@ -202,13 +202,13 @@ namespace Fungus
{ {
timeAccumulator -= writeDelay; timeAccumulator -= writeDelay;
Glyph glyph = glyphs[i]; Token token = tokens[i];
switch (glyph.type) switch (token.type)
{ {
case GlyphType.Character: case TokenType.Character:
if (storyText.text.Length == 0 && glyph.param == "\n") if (storyText.text.Length == 0 && token.param == "\n")
{ {
// Ignore leading newlines // Ignore leading newlines
} }
@ -234,7 +234,7 @@ namespace Fungus
end += "</color>"; end += "</color>";
} }
storyText.text += start + glyph.param + end; storyText.text += start + token.param + end;
if (wasPointerClicked) if (wasPointerClicked)
{ {
@ -243,19 +243,19 @@ namespace Fungus
} }
} }
// Add a wait glyph on punctuation marks // Add a wait token on punctuation marks
bool doPause = punctuationPause > 0 && IsPunctuation(glyph.param); bool doPause = punctuationPause > 0 && IsPunctuation(token.param);
if (i == glyphs.Count - 1) if (i == tokens.Count - 1)
{ {
doPause = false; // No pause on last character doPause = false; // No pause on last character
} }
else else
{ {
// No pause if next glyph is a pause // No pause if next token is a pause
GlyphType nextType = glyphs[i + 1].type; TokenType nextType = tokens[i + 1].type;
if (nextType == GlyphType.Wait || if (nextType == TokenType.Wait ||
nextType == GlyphType.WaitForInputAndClear || nextType == TokenType.WaitForInputAndClear ||
nextType == GlyphType.WaitForInputNoClear) nextType == TokenType.WaitForInputNoClear)
{ {
doPause = false; doPause = false;
} }
@ -268,10 +268,10 @@ namespace Fungus
if (doPause) if (doPause)
{ {
// Ignore if next glyph is also punctuation, or if punctuation is the last character. // Ignore if next token is also punctuation, or if punctuation is the last character.
bool skipCharacter = (i < glyphs.Count - 1 && bool skipCharacter = (i < tokens.Count - 1 &&
glyphs[i + 1].type == GlyphType.Character && tokens[i + 1].type == TokenType.Character &&
IsPunctuation(glyphs[i + 1].param)); IsPunctuation(tokens[i + 1].param));
if (!skipCharacter) if (!skipCharacter)
{ {
@ -287,34 +287,34 @@ namespace Fungus
break; break;
case GlyphType.BoldStart: case TokenType.BoldStart:
boldActive = true; boldActive = true;
break; break;
case GlyphType.BoldEnd: case TokenType.BoldEnd:
boldActive = false; boldActive = false;
break; break;
case GlyphType.ItalicStart: case TokenType.ItalicStart:
italicActive = true; italicActive = true;
break; break;
case GlyphType.ItalicEnd: case TokenType.ItalicEnd:
italicActive = false; italicActive = false;
break; break;
case GlyphType.ColorStart: case TokenType.ColorStart:
colorActive = true; colorActive = true;
colorText = glyph.param; colorText = token.param;
break; break;
case GlyphType.ColorEnd: case TokenType.ColorEnd:
colorActive = false; colorActive = false;
break; break;
case GlyphType.Wait: case TokenType.Wait:
float duration = 1f; float duration = 1f;
if (!Single.TryParse(glyph.param, out duration)) if (!Single.TryParse(token.param, out duration))
{ {
duration = 1f; duration = 1f;
} }
@ -330,7 +330,7 @@ namespace Fungus
timeAccumulator = 0f; timeAccumulator = 0f;
break; break;
case GlyphType.WaitForInputNoClear: case TokenType.WaitForInputNoClear:
OnWaitForInputTag(true); OnWaitForInputTag(true);
if (typingAudio != null) if (typingAudio != null)
typingAudio.audio.Pause(); typingAudio.audio.Pause();
@ -345,7 +345,7 @@ namespace Fungus
OnWaitForInputTag(false); OnWaitForInputTag(false);
break; break;
case GlyphType.WaitForInputAndClear: case TokenType.WaitForInputAndClear:
OnWaitForInputTag(true); OnWaitForInputTag(true);
if (typingAudio != null) if (typingAudio != null)
@ -362,13 +362,13 @@ namespace Fungus
storyText.text = ""; storyText.text = "";
break; break;
case GlyphType.Clear: case TokenType.Clear:
storyText.text = ""; storyText.text = "";
timeAccumulator = 0f; timeAccumulator = 0f;
break; break;
case GlyphType.Speed: case TokenType.Speed:
if (!Single.TryParse(glyph.param, out currentSpeed)) if (!Single.TryParse(token.param, out currentSpeed))
{ {
currentSpeed = 0f; currentSpeed = 0f;
} }
@ -380,7 +380,7 @@ namespace Fungus
} }
break; break;
case GlyphType.Exit: case TokenType.Exit:
if (typingAudio != null) if (typingAudio != null)
{ {
@ -394,15 +394,15 @@ namespace Fungus
yield break; yield break;
case GlyphType.WaitOnPunctuation: case TokenType.WaitOnPunctuation:
if (!Single.TryParse(glyph.param, out currentPunctuationPause)) if (!Single.TryParse(token.param, out currentPunctuationPause))
{ {
currentPunctuationPause = 0f; currentPunctuationPause = 0f;
} }
break; break;
} }
if (++i >= glyphs.Count) if (++i >= tokens.Count)
{ {
break; break;
} }
@ -455,9 +455,9 @@ namespace Fungus
character == "!"; character == "!";
} }
protected virtual List<Glyph> MakeGlyphList(string storyText) protected virtual List<Token> MakeTokenList(string storyText)
{ {
List<Glyph> glyphList = new List<Glyph>(); List<Token> tokenList = new List<Token>();
string pattern = @"\{.*?\}"; string pattern = @"\{.*?\}";
Regex myRegex = new Regex(pattern); Regex myRegex = new Regex(pattern);
@ -473,10 +473,10 @@ namespace Fungus
foreach (char c in preText) foreach (char c in preText)
{ {
AddCharacterGlyph(glyphList, c.ToString()); AddCharacterToken(tokenList, c.ToString());
} }
AddTagGlyph(glyphList, tagText); AddTagToken(tokenList, tagText);
position = m.Index + tagText.Length; position = m.Index + tagText.Length;
m = m.NextMatch(); m = m.NextMatch();
@ -487,22 +487,22 @@ namespace Fungus
string postText = storyText.Substring(position, storyText.Length - position); string postText = storyText.Substring(position, storyText.Length - position);
foreach (char c in postText) foreach (char c in postText)
{ {
AddCharacterGlyph(glyphList, c.ToString()); AddCharacterToken(tokenList, c.ToString());
} }
} }
return glyphList; return tokenList;
} }
protected virtual void AddCharacterGlyph(List<Glyph> glyphList, string character) protected virtual void AddCharacterToken(List<Token> tokenList, string character)
{ {
Glyph glyph = new Glyph(); Token token = new Token();
glyph.type = GlyphType.Character; token.type = TokenType.Character;
glyph.param = character; token.param = character;
glyphList.Add(glyph); tokenList.Add(token);
} }
protected virtual void AddTagGlyph(List<Glyph> glyphList, string tagText) protected virtual void AddTagToken(List<Token> tokenList, string tagText)
{ {
if (tagText.Length < 3 || if (tagText.Length < 3 ||
tagText.Substring(0,1) != "{" || tagText.Substring(0,1) != "{" ||
@ -513,83 +513,83 @@ namespace Fungus
string tag = tagText.Substring(1, tagText.Length - 2); string tag = tagText.Substring(1, tagText.Length - 2);
GlyphType type = GlyphType.Character; TokenType type = TokenType.Character;
string paramText = ""; string paramText = "";
if (tag == "b") if (tag == "b")
{ {
type = GlyphType.BoldStart; type = TokenType.BoldStart;
} }
else if (tag == "/b") else if (tag == "/b")
{ {
type = GlyphType.BoldEnd; type = TokenType.BoldEnd;
} }
else if (tag == "i") else if (tag == "i")
{ {
type = GlyphType.ItalicStart; type = TokenType.ItalicStart;
} }
else if (tag == "/i") else if (tag == "/i")
{ {
type = GlyphType.ItalicEnd; type = TokenType.ItalicEnd;
} }
else if (tag.StartsWith("color=")) else if (tag.StartsWith("color="))
{ {
type = GlyphType.ColorStart; type = TokenType.ColorStart;
paramText = tag.Substring(6, tag.Length - 6); paramText = tag.Substring(6, tag.Length - 6);
} }
else if (tag == "/color") else if (tag == "/color")
{ {
type = GlyphType.ColorEnd; type = TokenType.ColorEnd;
} }
else if (tag == "wi") else if (tag == "wi")
{ {
type = GlyphType.WaitForInputNoClear; type = TokenType.WaitForInputNoClear;
} }
if (tag == "wc") if (tag == "wc")
{ {
type = GlyphType.WaitForInputAndClear; type = TokenType.WaitForInputAndClear;
} }
else if (tag.StartsWith("wp=")) else if (tag.StartsWith("wp="))
{ {
type = GlyphType.WaitOnPunctuation; type = TokenType.WaitOnPunctuation;
paramText = tag.Substring(3, tag.Length - 3); paramText = tag.Substring(3, tag.Length - 3);
} }
else if (tag == "wp") else if (tag == "wp")
{ {
type = GlyphType.WaitOnPunctuation; type = TokenType.WaitOnPunctuation;
} }
else if (tag.StartsWith("w=")) else if (tag.StartsWith("w="))
{ {
type = GlyphType.Wait; type = TokenType.Wait;
paramText = tag.Substring(2, tag.Length - 2); paramText = tag.Substring(2, tag.Length - 2);
} }
else if (tag == "w") else if (tag == "w")
{ {
type = GlyphType.Wait; type = TokenType.Wait;
} }
else if (tag == "c") else if (tag == "c")
{ {
type = GlyphType.Clear; type = TokenType.Clear;
} }
else if (tag.StartsWith("s=")) else if (tag.StartsWith("s="))
{ {
type = GlyphType.Speed; type = TokenType.Speed;
paramText = tag.Substring(2, tag.Length - 2); paramText = tag.Substring(2, tag.Length - 2);
} }
else if (tag == "s") else if (tag == "s")
{ {
type = GlyphType.Speed; type = TokenType.Speed;
} }
else if (tag == "x") else if (tag == "x")
{ {
type = GlyphType.Exit; type = TokenType.Exit;
} }
Glyph glyph = new Glyph(); Token token = new Token();
glyph.type = type; token.type = type;
glyph.param = paramText.Trim(); token.param = paramText.Trim();
glyphList.Add(glyph); tokenList.Add(token);
} }
protected virtual IEnumerator WaitForInput(Action onInput) protected virtual IEnumerator WaitForInput(Action onInput)

Loading…
Cancel
Save