Browse Source

Refactored SayDialog to use an ISayDialog interface

master
Christopher 8 years ago
parent
commit
fe3e1b854b
  1. 2
      Assets/Fungus/Narrative/Scripts/Character.cs
  2. 6
      Assets/Fungus/Narrative/Scripts/Commands/Say.cs
  3. 8
      Assets/Fungus/Narrative/Scripts/ConversationManager.cs
  4. 51
      Assets/Fungus/Narrative/Scripts/ISayDialog.cs
  5. 12
      Assets/Fungus/Narrative/Scripts/ISayDialog.cs.meta
  6. 4
      Assets/Fungus/Narrative/Scripts/MenuDialog.cs
  7. 133
      Assets/Fungus/Narrative/Scripts/SayDialog.cs
  8. 2
      Assets/Fungus/Thirdparty/FungusLua/Scripts/LuaUtils.cs

2
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)]

6
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;

8
Assets/Fungus/Narrative/Scripts/ConversationManager.cs

@ -32,9 +32,9 @@ namespace Fungus
characters = UnityEngine.Object.FindObjectsOfType<Character>();
}
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)

51
Assets/Fungus/Narrative/Scripts/ISayDialog.cs

@ -0,0 +1,51 @@
using UnityEngine;
using System.Collections;
namespace Fungus
{
/// <summary>
/// Display story text in a visual novel style dialog box.
/// </summary>
public interface ISayDialog
{
/// <summary>
/// Sets the active state of the Say Dialog gameobject.
/// </summary>
void SetActive(bool state);
/// <summary>
/// Sets the active speaking character.
/// </summary>
/// <param name="character">The active speaking character.</param>
/// <param name="flowchart">An optional Flowchart to use for variable substitution in the character name string.</param>
void SetCharacter(Character character, Flowchart flowchart = null);
/// <summary>
/// Sets the character image to display on the Say Dialog.
/// </summary>
/// <param name="image">Image.</param>
void SetCharacterImage(Sprite image);
/// <summary>
/// Write a line of story text to the Say Dialog.
/// </summary>
/// <param name="text">The text to display.</param>
/// <param name="clearPrevious">Clear any previous text in the Say Dialog.</param>
/// <param name="waitForInput">Wait for player input before continuing once text is written.</param>
/// <param name="fadeWhenDone">Fade out the Say Dialog when writing and player input has finished.</param>
/// <param name="stopVoiceover">Stop any existing voiceover audio before writing starts.</param>
/// <param name="voiceOverClip">Voice over audio clip to play.</param>
/// <param name="onComplete">Callback to execute when writing and player input have finished.</param>
void Say(string text, bool clearPrevious, bool waitForInput, bool fadeWhenDone, bool stopVoiceover, AudioClip voiceOverClip, System.Action onComplete);
/// <summary>
/// Tell the Say Dialog to fade out once writing and player input have finished.
/// </summary>
bool FadeWhenDone { set; }
/// <summary>
/// Stop the Say Dialog while its writing text.
/// </summary>
void Stop();
}
}

12
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:

4
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;
}
}

133
Assets/Fungus/Narrative/Scripts/SayDialog.cs

@ -11,10 +11,10 @@ namespace Fungus
/// <summary>
/// Presents story text to the player in a dialogue box.
/// </summary>
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,7 +75,7 @@ namespace Fungus
GameObject go = Instantiate(prefab) as GameObject;
go.SetActive(false);
go.name = "SayDialog";
activeSayDialog = go.GetComponent<SayDialog>();
activeSayDialog = go.GetComponent<ISayDialog>();
}
}
}
@ -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)
@ -368,54 +406,19 @@ namespace Fungus
}
}
public virtual void SetCharacterName(string name, Color color)
{
if (nameText != null)
public virtual void Say(string text, bool clearPrevious, bool waitForInput, bool fadeWhenDone, bool stopVoiceover, AudioClip voiceOverClip, Action onComplete)
{
nameText.text = name;
nameText.color = color;
}
StartCoroutine(SayInternal(text, clearPrevious, waitForInput, fadeWhenDone, stopVoiceover, voiceOverClip, onComplete));
}
public virtual void Clear()
{
ClearStoryText();
// Kill any active write coroutine
StopAllCoroutines();
}
public virtual bool FadeWhenDone { set { fadeWhenDone = value; } }
protected virtual void ClearStoryText()
{
if (storyText != null)
public virtual void Stop()
{
storyText.text = "";
}
fadeWhenDone = true;
GetWriter().Stop();
}
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;
}
}
}
}
}
#endregion
}
}

2
Assets/Fungus/Thirdparty/FungusLua/Scripts/LuaUtils.cs vendored

@ -482,7 +482,7 @@ namespace Fungus
/// Sync the active say dialog with what Lua thinks the SayDialog should be
/// </summary>
/// <param name="sayDialog"></param>
public void SetSayDialog(SayDialog sayDialog)
public void SetSayDialog(ISayDialog sayDialog)
{
SayDialog.activeSayDialog = sayDialog;
}

Loading…
Cancel
Save