// 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)
using UnityEngine;
namespace Fungus
{
///
/// Writes text in a dialog box.
///
[CommandInfo("Narrative",
"Say",
"Writes text in a dialog box.")]
[AddComponentMenu("")]
public class Say : Command, ILocalizable
{
// Removed this tooltip as users's reported it obscures the text box
[TextArea(5,10)]
[SerializeField] protected string storyText = "";
[Tooltip("Notes about this story text for other authors, localization, etc.")]
[SerializeField] protected string description = "";
[Tooltip("Character that is speaking")]
[SerializeField] protected Character character;
[Tooltip("Portrait that represents speaking character")]
[SerializeField] protected Sprite portrait;
[Tooltip("Voiceover audio to play when writing the text")]
[SerializeField] protected AudioClip voiceOverClip;
[Tooltip("Always show this Say text when the command is executed multiple times")]
[SerializeField] protected bool showAlways = true;
[Tooltip("Number of times to show this Say text when the command is executed multiple times")]
[SerializeField] protected int showCount = 1;
[Tooltip("Type this text in the previous dialog box.")]
[SerializeField] protected bool extendPrevious = false;
[Tooltip("Fade out the dialog box when writing has finished and not waiting for input.")]
[SerializeField] protected bool fadeWhenDone = true;
[Tooltip("Wait for player to click before continuing.")]
[SerializeField] protected bool waitForClick = true;
[Tooltip("Stop playing voiceover when text finishes writing.")]
[SerializeField] protected bool stopVoiceover = true;
[Tooltip("Sets the active Say dialog with a reference to a Say Dialog object in the scene. All story text will now display using this Say Dialog.")]
[SerializeField] protected SayDialog setSayDialog;
protected int executionCount;
#region Public members
///
/// Character that is speaking.
///
public virtual Character _Character { get { return character; } }
///
/// Portrait that represents speaking character.
///
public virtual Sprite Portrait { get { return portrait; } set { portrait = value; } }
///
/// Type this text in the previous dialog box.
///
public virtual bool ExtendPrevious { get { return extendPrevious; } }
public override void OnEnter()
{
if (!showAlways && executionCount >= showCount)
{
Continue();
return;
}
executionCount++;
// Override the active say dialog if needed
if (character != null && character.SetSayDialog != null)
{
SayDialog.ActiveSayDialog = character.SetSayDialog;
}
if (setSayDialog != null)
{
SayDialog.ActiveSayDialog = setSayDialog;
}
var sayDialog = SayDialog.GetSayDialog();
if (sayDialog == null)
{
Continue();
return;
}
var flowchart = GetFlowchart();
sayDialog.SetActive(true);
sayDialog.SetCharacter(character);
sayDialog.SetCharacterImage(portrait);
string displayText = storyText;
var activeCustomTags = CustomTag.activeCustomTags;
for (int i = 0; i < activeCustomTags.Count; i++)
{
var ct = activeCustomTags[i];
displayText = displayText.Replace(ct.TagStartSymbol, ct.ReplaceTagStartWith);
if (ct.TagEndSymbol != "" && ct.ReplaceTagEndWith != "")
{
displayText = displayText.Replace(ct.TagEndSymbol, ct.ReplaceTagEndWith);
}
}
string subbedText = flowchart.SubstituteVariables(displayText);
sayDialog.Say(subbedText, !extendPrevious, waitForClick, fadeWhenDone, stopVoiceover, voiceOverClip, delegate {
Continue();
});
}
public override string GetSummary()
{
string namePrefix = "";
if (character != null)
{
namePrefix = character.NameText + ": ";
}
if (extendPrevious)
{
namePrefix = "EXTEND" + ": ";
}
return namePrefix + "\"" + storyText + "\"";
}
public override Color GetButtonColor()
{
return new Color32(184, 210, 235, 255);
}
public override void OnReset()
{
executionCount = 0;
}
public override void OnStopExecuting()
{
var sayDialog = SayDialog.GetSayDialog();
if (sayDialog == null)
{
return;
}
sayDialog.Stop();
}
#endregion
#region ILocalizable implementation
public virtual string GetStandardText()
{
return storyText;
}
public virtual void SetStandardText(string standardText)
{
storyText = standardText;
}
public virtual string GetDescription()
{
return description;
}
public virtual string GetStringId()
{
// String id for Say commands is SAY...[Character Name]
string stringId = "SAY." + GetFlowchartLocalizationId() + "." + itemId + ".";
if (character != null)
{
stringId += character.NameText;
}
return stringId;
}
#endregion
}
}