Browse Source

Separate Say and Choose dialog classes with base Dialog class

master
chrisgregan 10 years ago
parent
commit
fb5d502121
  1. 14
      Assets/Fungus/Dialog/Commands/Choose.cs
  2. 4
      Assets/Fungus/Dialog/Commands/Say.cs
  3. 2
      Assets/Fungus/Dialog/Editor/CharacterEditor.cs
  4. 2
      Assets/Fungus/Dialog/Editor/ChooseEditor.cs
  5. 2
      Assets/Fungus/Dialog/Editor/SayEditor.cs
  6. 2
      Assets/Fungus/Dialog/Scripts/Character.cs
  7. 120
      Assets/Fungus/Dialog/Scripts/ChooseDialog.cs
  8. 8
      Assets/Fungus/Dialog/Scripts/ChooseDialog.cs.meta
  9. 154
      Assets/Fungus/Dialog/Scripts/Dialog.cs
  10. 2
      Assets/Fungus/Dialog/Scripts/Dialog.cs.meta
  11. 66
      Assets/Fungus/Dialog/Scripts/SayDialog.cs
  12. 8
      Assets/Fungus/Dialog/Scripts/SayDialog.cs.meta

14
Assets/Fungus/Dialog/Commands/Choose.cs

@ -9,8 +9,8 @@ namespace Fungus.Script
[HelpText("Presents a list of options for the player to choose from, with an optional timeout. Add options using preceding AddOption commands.")] [HelpText("Presents a list of options for the player to choose from, with an optional timeout. Add options using preceding AddOption commands.")]
public class Choose : FungusCommand public class Choose : FungusCommand
{ {
public Dialog dialog; public ChooseDialog dialog;
static public Dialog activeDialog; static public ChooseDialog activeDialog;
public class Option public class Option
{ {
@ -54,12 +54,12 @@ namespace Fungus.Script
} }
else else
{ {
List<Dialog.Option> dialogOptions = new List<Dialog.Option>(); List<ChooseDialog.Option> dialogOptions = new List<ChooseDialog.Option>();
foreach (Option sayOption in options) foreach (Option option in options)
{ {
Dialog.Option dialogOption = new Dialog.Option(); ChooseDialog.Option dialogOption = new ChooseDialog.Option();
dialogOption.text = sayOption.optionText; dialogOption.text = option.optionText;
Sequence onSelectSequence = sayOption.targetSequence; Sequence onSelectSequence = option.targetSequence;
dialogOption.onSelect = delegate { dialogOption.onSelect = delegate {

4
Assets/Fungus/Dialog/Commands/Say.cs

@ -9,9 +9,9 @@ namespace Fungus.Script
[HelpText("Writes a line of story text to the dialog. A list of options can be specified for the player to choose from. Use a non-zero timeout to give the player a limited time to choose.")] [HelpText("Writes a line of story text to the dialog. A list of options can be specified for the player to choose from. Use a non-zero timeout to give the player a limited time to choose.")]
public class Say : FungusCommand public class Say : FungusCommand
{ {
public Dialog dialog; public SayDialog dialog;
static public Dialog activeDialog; static public SayDialog activeDialog;
public Character character; public Character character;
public string storyText; public string storyText;

2
Assets/Fungus/Dialog/Editor/CharacterEditor.cs

@ -42,7 +42,7 @@ namespace Fungus.Script
typeof(Sprite), typeof(Sprite),
true) as Sprite; true) as Sprite;
Dialog.DialogSide dialogSide = (Dialog.DialogSide)EditorGUILayout.EnumPopup(new GUIContent("Image Side", "Which side to display the image in the dialog"), SayDialog.DialogSide dialogSide = (SayDialog.DialogSide)EditorGUILayout.EnumPopup(new GUIContent("Image Side", "Which side to display the image in the dialog"),
t.dialogSide); t.dialogSide);
EditorGUILayout.Separator(); EditorGUILayout.Separator();

2
Assets/Fungus/Dialog/Editor/ChooseEditor.cs

@ -26,7 +26,7 @@ namespace Fungus.Script
new GUIContent("<None>"), new GUIContent("<None>"),
t.character); t.character);
Dialog dialog = FungusCommandEditor.ObjectField<Dialog>(new GUIContent("Dialog", "Dialog to use when displaying choices"), ChooseDialog dialog = FungusCommandEditor.ObjectField<ChooseDialog>(new GUIContent("Choose Dialog", "Dialog to use when displaying choices"),
new GUIContent("<Default>"), new GUIContent("<Default>"),
t.dialog); t.dialog);

2
Assets/Fungus/Dialog/Editor/SayEditor.cs

@ -27,7 +27,7 @@ namespace Fungus.Script
new GUIContent("<None>"), new GUIContent("<None>"),
t.character); t.character);
Dialog dialog = FungusCommandEditor.ObjectField<Dialog>(new GUIContent("Dialog", "Dialog to use when displaying Say command story text"), SayDialog dialog = FungusCommandEditor.ObjectField<SayDialog>(new GUIContent("Say Dialog", "Dialog to use when displaying Say command story text"),
new GUIContent("<Default>"), new GUIContent("<Default>"),
t.dialog); t.dialog);

2
Assets/Fungus/Dialog/Scripts/Character.cs

@ -8,7 +8,7 @@ namespace Fungus.Script
{ {
public string characterName; public string characterName;
public Sprite characterImage; public Sprite characterImage;
public Dialog.DialogSide dialogSide; public SayDialog.DialogSide dialogSide;
public Color characterColor; public Color characterColor;
} }

120
Assets/Fungus/Dialog/Scripts/ChooseDialog.cs

@ -0,0 +1,120 @@
using UnityEngine;
using UnityEngine.UI;
using System;
using System.Collections;
using System.Collections.Generic;
namespace Fungus.Script
{
public class ChooseDialog : Dialog
{
public class Option
{
public string text;
public Action onSelect;
}
public List<UnityEngine.UI.Button> optionButtons = new List<UnityEngine.UI.Button>();
List<Action> optionActions = new List<Action>();
public void Choose(string text, List<Option> options, float timeoutDuration, Action onTimeout)
{
Clear();
StartCoroutine(WriteText(text, delegate {
foreach (Option option in options)
{
AddOption(option.text, option.onSelect);
}
if (timeoutDuration > 0)
{
StartCoroutine(WaitForTimeout(timeoutDuration, onTimeout));
}
}));
}
IEnumerator WaitForTimeout(float timeoutDuration, Action onTimeout)
{
yield return new WaitForSeconds(timeoutDuration);
Clear();
if (onTimeout != null)
{
onTimeout();
}
}
protected override void Clear()
{
base.Clear();
ClearOptions();
}
void ClearOptions()
{
if (optionButtons == null)
{
return;
}
optionActions.Clear();
foreach (UnityEngine.UI.Button button in optionButtons)
{
if (button != null)
{
button.gameObject.SetActive(false);
}
}
}
bool AddOption(string text, Action action)
{
if (optionButtons == null)
{
return false;
}
bool addedOption = false;
foreach (UnityEngine.UI.Button button in optionButtons)
{
if (!button.gameObject.activeSelf)
{
button.gameObject.SetActive(true);
Text textComponent = button.GetComponentInChildren<Text>();
if (textComponent != null)
{
textComponent.text = text;
}
optionActions.Add(action);
addedOption = true;
break;
}
}
return addedOption;
}
public void SelectOption(int index)
{
if (index < optionActions.Count)
{
Action optionAction = optionActions[index];
if (optionAction != null)
{
StopCoroutine("WaitForTimeout");
Clear();
optionAction();
}
}
}
}
}

8
Assets/Fungus/Dialog/Scripts/ChooseDialog.cs.meta

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: f7a657dd101e84d9da0a5ee5f934317d
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:

154
Assets/Fungus/Dialog/Scripts/Dialog.cs

@ -16,26 +16,16 @@ namespace Fungus.Script
Right Right
}; };
public class Option
{
public string text;
public Action onSelect;
}
public float writingSpeed; public float writingSpeed;
public AudioClip writingSound; public AudioClip writingSound;
public bool loopWritingSound = true; public bool loopWritingSound = true;
public Canvas dialogCanvas; public Canvas dialogCanvas;
public List<UnityEngine.UI.Button> optionButtons = new List<UnityEngine.UI.Button>();
public Text nameText; public Text nameText;
public Text storyText; public Text storyText;
public Image continueImage;
public Image leftImage; public Image leftImage;
public Image rightImage; public Image rightImage;
List<Action> optionActions = new List<Action>();
public void ShowDialog(bool visible) public void ShowDialog(bool visible)
{ {
if (dialogCanvas != null) if (dialogCanvas != null)
@ -102,50 +92,7 @@ namespace Fungus.Script
} }
} }
public void Say(string text, Action onComplete) protected IEnumerator WriteText(string text, Action onWritingComplete)
{
Clear();
if (storyText != null)
{
storyText.text = text;
}
StartCoroutine(WriteText(text, delegate {
ShowContinueIcon(true);
StartCoroutine(WaitForInput(delegate {
Clear();
if (onComplete != null)
{
onComplete();
}
}));
}));
}
public void Choose(string text, List<Option> options, float timeoutDuration, Action onTimeout)
{
Clear();
ShowContinueIcon(false);
StartCoroutine(WriteText(text, delegate {
foreach (Option option in options)
{
AddOption(option.text, option.onSelect);
}
if (timeoutDuration > 0)
{
StartCoroutine(WaitForTimeout(timeoutDuration, onTimeout));
}
}));
}
IEnumerator WriteText(string text, Action onWritingComplete)
{ {
// Zero CPS means write instantly // Zero CPS means write instantly
// Also write instantly if text contains markup tags // Also write instantly if text contains markup tags
@ -215,45 +162,9 @@ namespace Fungus.Script
yield break; yield break;
} }
IEnumerator WaitForInput(Action onInput) protected virtual void Clear()
{
// TODO: Handle touch input
while (!Input.GetMouseButtonDown(0))
{
yield return null;
}
if (onInput != null)
{
onInput();
}
}
IEnumerator WaitForTimeout(float timeoutDuration, Action onTimeout)
{
yield return new WaitForSeconds(timeoutDuration);
Clear();
if (onTimeout != null)
{
onTimeout();
}
}
void ShowContinueIcon(bool visible)
{
if (continueImage != null)
{
continueImage.enabled = visible;
}
}
void Clear()
{ {
ClearStoryText(); ClearStoryText();
ClearOptions();
ShowContinueIcon(false);
} }
void ClearStoryText() void ClearStoryText()
@ -264,67 +175,6 @@ namespace Fungus.Script
} }
} }
void ClearOptions()
{
if (optionButtons == null)
{
return;
}
optionActions.Clear();
foreach (UnityEngine.UI.Button button in optionButtons)
{
if (button != null)
{
button.gameObject.SetActive(false);
}
}
}
bool AddOption(string text, Action action)
{
if (optionButtons == null)
{
return false;
}
bool addedOption = false;
foreach (UnityEngine.UI.Button button in optionButtons)
{
if (!button.gameObject.activeSelf)
{
button.gameObject.SetActive(true);
Text textComponent = button.GetComponentInChildren<Text>();
if (textComponent != null)
{
textComponent.text = text;
}
optionActions.Add(action);
addedOption = true;
break;
}
}
return addedOption;
}
public void SelectOption(int index)
{
if (index < optionActions.Count)
{
Action optionAction = optionActions[index];
if (optionAction != null)
{
StopCoroutine("WaitForTimeout");
Clear();
optionAction();
}
}
}
} }
} }

2
Assets/Fungus/Dialog/Scripts/Dialog.cs.meta

@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 3a0bbe22c246e4c78ad8e9816cbae9d5 guid: b7e8f397d6557484f91f9992f702cff5
MonoImporter: MonoImporter:
serializedVersion: 2 serializedVersion: 2
defaultReferences: [] defaultReferences: []

66
Assets/Fungus/Dialog/Scripts/SayDialog.cs

@ -0,0 +1,66 @@
using UnityEngine;
using UnityEngine.UI;
using System;
using System.Collections;
namespace Fungus.Script
{
public class SayDialog : Dialog
{
public Image continueImage;
public void Say(string text, Action onComplete)
{
Clear();
if (storyText != null)
{
storyText.text = text;
}
StartCoroutine(WriteText(text, delegate {
ShowContinueIcon(true);
StartCoroutine(WaitForInput(delegate {
Clear();
if (onComplete != null)
{
onComplete();
}
}));
}));
}
protected override void Clear()
{
base.Clear();
ShowContinueIcon(false);
}
void ShowContinueIcon(bool visible)
{
if (continueImage != null)
{
continueImage.enabled = visible;
}
}
IEnumerator WaitForInput(Action onInput)
{
// TODO: Handle touch input
while (!Input.GetMouseButtonDown(0))
{
yield return null;
}
if (onInput != null)
{
onInput();
}
}
}
}

8
Assets/Fungus/Dialog/Scripts/SayDialog.cs.meta

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 3a0bbe22c246e4c78ad8e9816cbae9d5
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
Loading…
Cancel
Save