diff --git a/Assets/Fungus/Narrative/Scripts/Character.cs b/Assets/Fungus/Narrative/Scripts/Character.cs index 2212c9b1..8fb3bbd5 100644 --- a/Assets/Fungus/Narrative/Scripts/Character.cs +++ b/Assets/Fungus/Narrative/Scripts/Character.cs @@ -38,7 +38,7 @@ namespace Fungus [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; - public virtual SayDialog SetSayDialog { get { return setSayDialog; } } + public virtual ISayDialog SetSayDialog { get { return setSayDialog; } } [FormerlySerializedAs("notes")] [TextArea(5,10)] diff --git a/Assets/Fungus/Narrative/Scripts/Commands/Say.cs b/Assets/Fungus/Narrative/Scripts/Commands/Say.cs index 17d073d0..7d594719 100644 --- a/Assets/Fungus/Narrative/Scripts/Commands/Say.cs +++ b/Assets/Fungus/Narrative/Scripts/Commands/Say.cs @@ -77,7 +77,7 @@ namespace Fungus SayDialog.activeSayDialog = setSayDialog; } - SayDialog sayDialog = SayDialog.GetSayDialog(); + ISayDialog sayDialog = SayDialog.GetSayDialog(); if (sayDialog == null) { @@ -87,7 +87,7 @@ namespace Fungus Flowchart flowchart = GetFlowchart(); - sayDialog.gameObject.SetActive(true); + sayDialog.SetActive(true); sayDialog.SetCharacter(character, flowchart); sayDialog.SetCharacterImage(portrait); @@ -136,7 +136,7 @@ namespace Fungus public override void OnStopExecuting() { - SayDialog sayDialog = SayDialog.GetSayDialog(); + ISayDialog sayDialog = SayDialog.GetSayDialog(); if (sayDialog == null) { return; diff --git a/Assets/Fungus/Narrative/Scripts/ConversationManager.cs b/Assets/Fungus/Narrative/Scripts/ConversationManager.cs index 07c41200..bbd6fce5 100644 --- a/Assets/Fungus/Narrative/Scripts/ConversationManager.cs +++ b/Assets/Fungus/Narrative/Scripts/ConversationManager.cs @@ -32,9 +32,9 @@ namespace Fungus characters = UnityEngine.Object.FindObjectsOfType(); } - protected SayDialog GetSayDialog(Character character) + protected ISayDialog GetSayDialog(Character character) { - SayDialog sayDialog = null; + ISayDialog sayDialog = null; if (character != null) { if (character.SetSayDialog != null) @@ -88,7 +88,7 @@ namespace Fungus currentPortrait = item.Portrait; currentPosition = item.Position; - SayDialog sayDialog = GetSayDialog(currentCharacter); + ISayDialog sayDialog = GetSayDialog(currentCharacter); if (sayDialog == null) { @@ -96,7 +96,7 @@ namespace Fungus yield break; } - sayDialog.gameObject.SetActive(true); + sayDialog.SetActive(true); if (currentCharacter != null && currentCharacter != previousCharacter) diff --git a/Assets/Fungus/Narrative/Scripts/ISayDialog.cs b/Assets/Fungus/Narrative/Scripts/ISayDialog.cs new file mode 100644 index 00000000..71c65b8d --- /dev/null +++ b/Assets/Fungus/Narrative/Scripts/ISayDialog.cs @@ -0,0 +1,51 @@ +using UnityEngine; +using System.Collections; + +namespace Fungus +{ + /// + /// Display story text in a visual novel style dialog box. + /// + public interface ISayDialog + { + /// + /// Sets the active state of the Say Dialog gameobject. + /// + void SetActive(bool state); + + /// + /// Sets the active speaking character. + /// + /// The active speaking character. + /// An optional Flowchart to use for variable substitution in the character name string. + void SetCharacter(Character character, Flowchart flowchart = null); + + /// + /// Sets the character image to display on the Say Dialog. + /// + /// Image. + void SetCharacterImage(Sprite image); + + /// + /// Write a line of story text to the Say Dialog. + /// + /// The text to display. + /// Clear any previous text in the Say Dialog. + /// Wait for player input before continuing once text is written. + /// Fade out the Say Dialog when writing and player input has finished. + /// Stop any existing voiceover audio before writing starts. + /// Voice over audio clip to play. + /// Callback to execute when writing and player input have finished. + void Say(string text, bool clearPrevious, bool waitForInput, bool fadeWhenDone, bool stopVoiceover, AudioClip voiceOverClip, System.Action onComplete); + + /// + /// Tell the Say Dialog to fade out once writing and player input have finished. + /// + bool FadeWhenDone { set; } + + /// + /// Stop the Say Dialog while its writing text. + /// + void Stop(); + } +} \ No newline at end of file diff --git a/Assets/Fungus/Narrative/Scripts/ISayDialog.cs.meta b/Assets/Fungus/Narrative/Scripts/ISayDialog.cs.meta new file mode 100644 index 00000000..ce474358 --- /dev/null +++ b/Assets/Fungus/Narrative/Scripts/ISayDialog.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: a1a9184c86a4048d5973b766e9fd6803 +timeCreated: 1473421269 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Fungus/Narrative/Scripts/MenuDialog.cs b/Assets/Fungus/Narrative/Scripts/MenuDialog.cs index 6d6e5b78..853acde6 100644 --- a/Assets/Fungus/Narrative/Scripts/MenuDialog.cs +++ b/Assets/Fungus/Narrative/Scripts/MenuDialog.cs @@ -173,10 +173,10 @@ namespace Fungus public virtual void HideSayDialog() { - SayDialog sayDialog = SayDialog.GetSayDialog(); + ISayDialog sayDialog = SayDialog.GetSayDialog(); if (sayDialog != null) { - sayDialog.FadeOut(); + sayDialog.FadeWhenDone = true; } } diff --git a/Assets/Fungus/Narrative/Scripts/SayDialog.cs b/Assets/Fungus/Narrative/Scripts/SayDialog.cs index 125fdecb..dae9243a 100644 --- a/Assets/Fungus/Narrative/Scripts/SayDialog.cs +++ b/Assets/Fungus/Narrative/Scripts/SayDialog.cs @@ -11,10 +11,10 @@ namespace Fungus /// /// Presents story text to the player in a dialogue box. /// - public class SayDialog : MonoBehaviour + public class SayDialog : MonoBehaviour, ISayDialog { // Currently active Say Dialog used to display Say text - public static SayDialog activeSayDialog; + public static ISayDialog activeSayDialog; // Most recent speaking character public static Character speakingCharacter; @@ -55,7 +55,7 @@ namespace Fungus protected Sprite currentCharacterImage; - public static SayDialog GetSayDialog() + public static ISayDialog GetSayDialog() { if (activeSayDialog == null) { @@ -75,14 +75,14 @@ namespace Fungus GameObject go = Instantiate(prefab) as GameObject; go.SetActive(false); go.name = "SayDialog"; - activeSayDialog = go.GetComponent(); + activeSayDialog = go.GetComponent(); } } } return activeSayDialog; } - + protected Writer GetWriter() { if (writer != null) @@ -158,11 +158,6 @@ namespace Fungus } } - public virtual void Say(string text, bool clearPrevious, bool waitForInput, bool fadeWhenDone, bool stopVoiceover, AudioClip voiceOverClip, Action onComplete) - { - StartCoroutine(SayInternal(text, clearPrevious, waitForInput, fadeWhenDone, stopVoiceover, voiceOverClip, onComplete)); - } - public virtual IEnumerator SayInternal(string text, bool clearPrevious, bool waitForInput, bool fadeWhenDone, bool stopVoiceover, AudioClip voiceOverClip, Action onComplete) { Writer writer = GetWriter(); @@ -206,19 +201,6 @@ namespace Fungus } } - // Tell dialog to fade out if it's finished writing. - public virtual void FadeOut() - { - fadeWhenDone = true; - } - - // Stop a Say Dialog while its writing text. - public virtual void Stop() - { - fadeWhenDone = true; - GetWriter().Stop(); - } - protected virtual void UpdateAlpha() { if (GetWriter().IsWriting) @@ -238,7 +220,6 @@ namespace Fungus } CanvasGroup canvasGroup = GetCanvasGroup(); - float fadeDuration = GetSayDialog().fadeDuration; if (fadeDuration <= 0f) { canvasGroup.alpha = targetAlpha; @@ -257,6 +238,63 @@ namespace Fungus } } + public virtual void SetCharacterName(string name, Color color) + { + if (nameText != null) + { + nameText.text = name; + nameText.color = color; + } + } + + public virtual void Clear() + { + ClearStoryText(); + + // Kill any active write coroutine + StopAllCoroutines(); + } + + protected virtual void ClearStoryText() + { + if (storyText != null) + { + storyText.text = ""; + } + } + + public static void StopPortraitTweens() + { + // Stop all tweening portraits + foreach( Character c in Character.activeCharacters ) + { + if (c.State.portraitImage != null) + { + if (LeanTween.isTweening(c.State.portraitImage.gameObject)) + { + LeanTween.cancel(c.State.portraitImage.gameObject, true); + + PortraitController.SetRectTransform(c.State.portraitImage.rectTransform, c.State.position); + if (c.State.dimmed == true) + { + c.State.portraitImage.color = new Color(0.5f, 0.5f, 0.5f, 1f); + } + else + { + c.State.portraitImage.color = Color.white; + } + } + } + } + } + + #region ISayDialog implementation + + public virtual void SetActive(bool state) + { + gameObject.SetActive(state); + } + public virtual void SetCharacter(Character character, Flowchart flowchart = null) { if (character == null) @@ -275,7 +313,7 @@ namespace Fungus { Character prevSpeakingCharacter = speakingCharacter; speakingCharacter = character; - + // Dim portraits of non-speaking characters foreach (Stage stage in Stage.activeStages) { @@ -298,24 +336,24 @@ namespace Fungus } } } - + string characterName = character.NameText; - + if (characterName == "") { // Use game object name as default characterName = character.name; } - + if (flowchart != null) { characterName = flowchart.SubstituteVariables(characterName); } - + SetCharacterName(characterName, character.NameColor); } } - + public virtual void SetCharacterImage(Sprite image) { if (characterImage == null) @@ -336,8 +374,8 @@ namespace Fungus if (startStoryTextWidth != 0) { storyText.rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, - startStoryTextInset, - startStoryTextWidth); + startStoryTextInset, + startStoryTextWidth); } } @@ -356,66 +394,31 @@ namespace Fungus if (storyText.rectTransform.position.x < characterImage.rectTransform.position.x) { storyText.rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, - startStoryTextInset, - startStoryTextWidth - characterImage.rectTransform.rect.width); + startStoryTextInset, + startStoryTextWidth - characterImage.rectTransform.rect.width); } else { storyText.rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Right, - startStoryTextInset, - startStoryTextWidth - characterImage.rectTransform.rect.width); + startStoryTextInset, + startStoryTextWidth - characterImage.rectTransform.rect.width); } } } - - public virtual void SetCharacterName(string name, Color color) - { - if (nameText != null) - { - nameText.text = name; - nameText.color = color; - } - } - - public virtual void Clear() - { - ClearStoryText(); - - // Kill any active write coroutine - StopAllCoroutines(); - } - - protected virtual void ClearStoryText() + + public virtual void Say(string text, bool clearPrevious, bool waitForInput, bool fadeWhenDone, bool stopVoiceover, AudioClip voiceOverClip, Action onComplete) { - if (storyText != null) - { - storyText.text = ""; - } + StartCoroutine(SayInternal(text, clearPrevious, waitForInput, fadeWhenDone, stopVoiceover, voiceOverClip, onComplete)); } - - public static void StopPortraitTweens() + + public virtual bool FadeWhenDone { set { fadeWhenDone = value; } } + + public virtual void Stop() { - // Stop all tweening portraits - foreach( Character c in Character.activeCharacters ) - { - if (c.State.portraitImage != null) - { - if (LeanTween.isTweening(c.State.portraitImage.gameObject)) - { - LeanTween.cancel(c.State.portraitImage.gameObject, true); - - PortraitController.SetRectTransform(c.State.portraitImage.rectTransform, c.State.position); - if (c.State.dimmed == true) - { - c.State.portraitImage.color = new Color(0.5f, 0.5f, 0.5f, 1f); - } - else - { - c.State.portraitImage.color = Color.white; - } - } - } - } + fadeWhenDone = true; + GetWriter().Stop(); } + + #endregion } } diff --git a/Assets/Fungus/Thirdparty/FungusLua/Scripts/LuaUtils.cs b/Assets/Fungus/Thirdparty/FungusLua/Scripts/LuaUtils.cs index a3c20d09..49348aad 100644 --- a/Assets/Fungus/Thirdparty/FungusLua/Scripts/LuaUtils.cs +++ b/Assets/Fungus/Thirdparty/FungusLua/Scripts/LuaUtils.cs @@ -482,7 +482,7 @@ namespace Fungus /// Sync the active say dialog with what Lua thinks the SayDialog should be /// /// - public void SetSayDialog(SayDialog sayDialog) + public void SetSayDialog(ISayDialog sayDialog) { SayDialog.activeSayDialog = sayDialog; }