@ -3,6 +3,7 @@ using UnityEngine.UI;
using System.Collections ;
using System.Collections.Generic ;
using System ;
using System.Reflection ;
namespace Fungus
{
@ -52,15 +53,14 @@ namespace Fungus
[Tooltip("Force the target text object to use Rich Text mode so text color and alpha appears correctly")]
public bool forceRichText = true ;
/ * *
* This property is true when the writer is waiting for user input to continue
* /
[Tooltip("Click while text is writing to finish writing immediately")]
public bool instantComplete = true ;
// This property is true when the writer is waiting for user input to continue
[System.NonSerialized]
public bool 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)
[System.NonSerialized]
public bool isWriting ;
@ -69,6 +69,9 @@ namespace Fungus
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 ;
@ -94,6 +97,11 @@ namespace Fungus
{
return textMesh . text ;
}
else if ( textProperty ! = null )
{
return textProperty . GetValue ( textComponent , null ) as string ;
}
return "" ;
}
@ -111,6 +119,10 @@ namespace Fungus
{
textMesh . text = value ;
}
else if ( textProperty ! = null )
{
textProperty . SetValue ( textComponent , value , null ) ;
}
}
}
@ -126,6 +138,20 @@ namespace Fungus
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 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 > ( ) )
{
@ -157,7 +183,7 @@ namespace Fungus
public virtual bool HasTextObject ( )
{
return ( textUI ! = null | | inputField ! = null | | textMesh ! = null ) ;
return ( textUI ! = null | | inputField ! = null | | textMesh ! = null | | textComponent ! = null ) ;
}
public virtual bool SupportsRichText ( )
@ -302,6 +328,33 @@ namespace Fungus
StartCoroutine ( ProcessTokens ( tokens , onComplete ) ) ;
}
virtual protected bool CheckParamCount ( List < string > paramList , int count )
{
if ( paramList = = null )
{
Debug . LogError ( "paramList is null" ) ;
return false ;
}
if ( paramList . Count ! = count )
{
Debug . LogError ( "There must be exactly " + paramList . Count + " parameters." ) ;
return false ;
}
return true ;
}
protected virtual bool TryGetSingleParam ( List < string > paramList , int index , float defaultValue , out float value )
{
value = defaultValue ;
if ( paramList . Count > index )
{
Single . TryParse ( paramList [ index ] , out value ) ;
return true ;
}
return false ;
}
protected virtual IEnumerator ProcessTokens ( List < TextTagParser . Token > tokens , Action onComplete )
{
// Reset control members
@ -321,7 +374,7 @@ namespace Fungus
switch ( token . type )
{
case TextTagParser . TokenType . Words :
yield return StartCoroutine ( DoWords ( token . param ) ) ;
yield return StartCoroutine ( DoWords ( token . paramList ) ) ;
break ;
case TextTagParser . TokenType . BoldStart :
@ -341,8 +394,11 @@ namespace Fungus
break ;
case TextTagParser . TokenType . ColorStart :
if ( CheckParamCount ( token . paramList , 1 ) )
{
colorActive = true ;
colorText = token . param ;
colorText = token . paramList [ 0 ] ;
}
break ;
case TextTagParser . TokenType . ColorEnd :
@ -350,7 +406,7 @@ namespace Fungus
break ;
case TextTagParser . TokenType . Wait :
yield return StartCoroutine ( DoWait ( token . param ) ) ;
yield return StartCoroutine ( DoWait ( token . paramList ) ) ;
break ;
case TextTagParser . TokenType . WaitForInputNoClear :
@ -362,10 +418,7 @@ namespace Fungus
break ;
case TextTagParser . TokenType . WaitOnPunctuationStart :
if ( ! Single . TryParse ( token . param , out currentPunctuationPause ) )
{
currentPunctuationPause = punctuationPause ;
}
TryGetSingleParam ( token . paramList , 0 , punctuationPause , out currentPunctuationPause ) ;
break ;
case TextTagParser . TokenType . WaitOnPunctuationEnd :
@ -377,10 +430,7 @@ namespace Fungus
break ;
case TextTagParser . TokenType . SpeedStart :
if ( ! Single . TryParse ( token . param , out currentWritingSpeed ) )
{
currentWritingSpeed = writingSpeed ;
}
TryGetSingleParam ( token . paramList , 0 , writingSpeed , out currentWritingSpeed ) ;
break ;
case TextTagParser . TokenType . SpeedEnd :
@ -392,48 +442,55 @@ namespace Fungus
break ;
case TextTagParser . TokenType . Message :
Flowchart . BroadcastFungusMessage ( token . param ) ;
if ( CheckParamCount ( token . paramList , 1 ) )
{
Flowchart . BroadcastFungusMessage ( token . paramList [ 0 ] ) ;
}
break ;
case TextTagParser . TokenType . VerticalPunch :
float vintensity ;
if ( ! Single . TryParse ( token . param , out vintensity ) )
{
vintensity = 1 0f ;
float vintensity ;
float time ;
TryGetSingleParam ( token . paramList , 0 , 1 0.0f , out vintensity ) ;
TryGetSingleParam ( token . paramList , 1 , 0.5f , out time ) ;
Punch ( new Vector3 ( 0 , vintensity , 0 ) , time ) ;
}
Punch ( new Vector3 ( 0 , vintensity , 0 ) , 0.5f ) ;
break ;
case TextTagParser . TokenType . HorizontalPunch :
float hintensity ;
if ( ! Single . TryParse ( token . param , out hintensity ) )
{
hintensity = 1 0f ;
float hintensity ;
float time ;
TryGetSingleParam ( token . paramList , 0 , 1 0.0f , out hintensity ) ;
TryGetSingleParam ( token . paramList , 1 , 0.5f , out time ) ;
Punch ( new Vector3 ( hintensity , 0 , 0 ) , time ) ;
}
Punch ( new Vector3 ( hintensity , 0 , 0 ) , 0.5f ) ;
break ;
case TextTagParser . TokenType . Punch :
float intensity ;
if ( ! Single . TryParse ( token . param , out intensity ) )
{
intensity = 1 0f ;
float intensity ;
float time ;
TryGetSingleParam ( token . paramList , 0 , 1 0.0f , out intensity ) ;
TryGetSingleParam ( token . paramList , 1 , 0.5f , out time ) ;
Punch ( new Vector3 ( intensity , intensity , 0 ) , time ) ;
}
Punch ( new Vector3 ( intensity , intensity , 0 ) , 0.5f ) ;
break ;
case TextTagParser . TokenType . Flash :
float flashDuration ;
if ( ! Single . TryParse ( token . param , out flashDuration ) )
{
flashDuration = 0.2f ;
}
TryGetSingleParam ( token . paramList , 0 , 0.2f , out flashDuration ) ;
Flash ( flashDuration ) ;
break ;
case TextTagParser . TokenType . Audio :
{
AudioSource audioSource = FindAudio ( token . param ) ;
AudioSource audioSource = null ;
if ( CheckParamCount ( token . paramList , 1 ) )
{
audioSource = FindAudio ( token . paramList [ 0 ] ) ;
}
if ( audioSource ! = null )
{
audioSource . PlayOneShot ( audioSource . clip ) ;
@ -443,7 +500,11 @@ namespace Fungus
case TextTagParser . TokenType . AudioLoop :
{
AudioSource audioSource = FindAudio ( token . param ) ;
AudioSource audioSource = null ;
if ( CheckParamCount ( token . paramList , 1 ) )
{
audioSource = FindAudio ( token . paramList [ 0 ] ) ;
}
if ( audioSource ! = null )
{
audioSource . Play ( ) ;
@ -454,7 +515,11 @@ namespace Fungus
case TextTagParser . TokenType . AudioPause :
{
AudioSource audioSource = FindAudio ( token . param ) ;
AudioSource audioSource = null ;
if ( CheckParamCount ( token . paramList , 1 ) )
{
audioSource = FindAudio ( token . paramList [ 0 ] ) ;
}
if ( audioSource ! = null )
{
audioSource . Pause ( ) ;
@ -464,7 +529,11 @@ namespace Fungus
case TextTagParser . TokenType . AudioStop :
{
AudioSource audioSource = FindAudio ( token . param ) ;
AudioSource audioSource = null ;
if ( CheckParamCount ( token . paramList , 1 ) )
{
audioSource = FindAudio ( token . paramList [ 0 ] ) ;
}
if ( audioSource ! = null )
{
audioSource . Stop ( ) ;
@ -492,8 +561,14 @@ namespace Fungus
}
}
protected virtual IEnumerator DoWords ( string param )
protected virtual IEnumerator DoWords ( List < string > paramList )
{
if ( ! CheckParamCount ( paramList , 1 ) )
{
yield break ;
}
string param = paramList [ 0 ] ;
string startText = text ;
string openText = OpenMarkup ( ) ;
string closeText = CloseMarkup ( ) ;
@ -516,8 +591,8 @@ namespace Fungus
NotifyGlyph ( ) ;
// No delay if user has click ed
if ( inputFlag )
// No delay if user has clicked and Instant Complete is enabl ed
if ( instantComplete & & in putFlag )
{
continue ;
}
@ -593,24 +668,21 @@ namespace Fungus
return "" ;
}
protected virtual IEnumerator DoWait ( string param )
protected virtual IEnumerator DoWait ( List < string > paramList )
{
float duration = 1f ;
if ( ! Single . TryParse ( param , out duration ) )
var param = "" ;
if ( paramList . Count = = 1 )
{
duration = 1f ;
param = paramList [ 0 ] ;
}
NotifyPause ( ) ;
float timeRemaining = duration ;
while ( timeRemaining > 0f & & ! inputFlag )
float duration = 1f ;
if ( ! Single . TryParse ( param , out duration ) )
{
timeRemaining - = Time . deltaTime ;
yield return null ;
duration = 1f ;
}
NotifyResume ( ) ;
yield return StartCoroutine ( DoWait ( duration ) ) ;
}
protected virtual IEnumerator DoWait ( float duration )
@ -618,8 +690,13 @@ namespace Fungus
NotifyPause ( ) ;
float timeRemaining = duration ;
while ( timeRemaining > 0f & & ! inputFlag & & ! exitFlag )
while ( timeRemaining > 0f & & ! exitFlag )
{
if ( instantComplete & & inputFlag )
{
break ;
}
timeRemaining - = Time . deltaTime ;
yield return null ;
}
@ -663,7 +740,11 @@ namespace Fungus
protected virtual void Punch ( Vector3 axis , float time )
{
iTween . ShakePosition ( this . gameObject , axis , time ) ;
if ( Camera . main = = null )
{
return ;
}
iTween . ShakePosition ( Camera . main . gameObject , axis , time ) ;
}
protected virtual void Flash ( float duration )