From 19edf578d784cd12f0f48c157c3a588595fcf7fe Mon Sep 17 00:00:00 2001 From: Christopher Date: Tue, 24 Jan 2017 15:08:08 +0000 Subject: [PATCH] Fixed centered text shifts position when written by Say command #569 --- Assets/Fungus/Scripts/Components/Writer.cs | 41 ++++++++++++++++++++-- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/Assets/Fungus/Scripts/Components/Writer.cs b/Assets/Fungus/Scripts/Components/Writer.cs index 956652b3..661e107b 100644 --- a/Assets/Fungus/Scripts/Components/Writer.cs +++ b/Assets/Fungus/Scripts/Components/Writer.cs @@ -87,10 +87,13 @@ namespace Fungus protected StringBuilder leftString = new StringBuilder(1024); protected StringBuilder rightString = new StringBuilder(1024); protected StringBuilder outputString = new StringBuilder(1024); + protected StringBuilder readAheadString = new StringBuilder(1024); protected string hiddenColorOpen = ""; protected string hiddenColorClose = ""; + protected int visibleCharacterCount = 0; + protected virtual void Awake() { GameObject go = targetTextObject; @@ -257,6 +260,8 @@ namespace Fungus TokenType previousTokenType = TokenType.Invalid; + visibleCharacterCount = 0; + for (int i = 0; i < tokens.Count; ++i) { // Pause between tokens if Paused is set @@ -269,6 +274,24 @@ namespace Fungus // Notify listeners about new token WriterSignals.DoTextTagToken(this, token, i, tokens.Count); + + // Update the read ahead string buffer. This contains the text for any + // Word tags which are further ahead in the list. + readAheadString.Length = 0; + for (int j = i + 1; j < tokens.Count; ++j) + { + var readAheadToken = tokens[j]; + + if (readAheadToken.type == TokenType.Words && + readAheadToken.paramList.Count == 1) + { + readAheadString.Append(readAheadToken.paramList[0]); + } + else if (readAheadToken.type == TokenType.WaitForInputAndClear) + { + break; + } + } switch (token.type) { @@ -488,8 +511,15 @@ namespace Fungus { param = param.TrimStart(' ', '\t', '\r', '\n'); } - - string startText = Text; + + // Start with the visible portion of any existing displayed text. + string startText = ""; + if (visibleCharacterCount > 0 && + visibleCharacterCount <= Text.Length) + { + startText = Text.Substring(0, visibleCharacterCount); + } + UpdateOpenMarkup(); UpdateCloseMarkup(); @@ -588,9 +618,13 @@ namespace Fungus outputString.Append(leftString); outputString.Append(closeString); + // Track how many visible characters are currently displayed so + // we can easily extract the visible portion again later. + visibleCharacterCount = outputString.Length; + // Make right hand side text hidden if (SupportsRichText() && - rightString.Length > 0) + rightString.Length + readAheadString.Length > 0) { // Ensure the hidden color strings are populated if (hiddenColorOpen.Length == 0) @@ -600,6 +634,7 @@ namespace Fungus outputString.Append(hiddenColorOpen); outputString.Append(rightString); + outputString.Append(readAheadString); outputString.Append(hiddenColorClose); } }