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. 6
      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. 180
      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.")]
public class Choose : FungusCommand
{
public Dialog dialog;
static public Dialog activeDialog;
public ChooseDialog dialog;
static public ChooseDialog activeDialog;
public class Option
{
@ -54,12 +54,12 @@ namespace Fungus.Script
}
else
{
List<Dialog.Option> dialogOptions = new List<Dialog.Option>();
foreach (Option sayOption in options)
List<ChooseDialog.Option> dialogOptions = new List<ChooseDialog.Option>();
foreach (Option option in options)
{
Dialog.Option dialogOption = new Dialog.Option();
dialogOption.text = sayOption.optionText;
Sequence onSelectSequence = sayOption.targetSequence;
ChooseDialog.Option dialogOption = new ChooseDialog.Option();
dialogOption.text = option.optionText;
Sequence onSelectSequence = option.targetSequence;
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.")]
public class Say : FungusCommand
{
public Dialog dialog;
public SayDialog dialog;
static public Dialog activeDialog;
static public SayDialog activeDialog;
public Character character;
public string storyText;

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

@ -42,7 +42,7 @@ namespace Fungus.Script
typeof(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);
EditorGUILayout.Separator();

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

@ -26,9 +26,9 @@ namespace Fungus.Script
new GUIContent("<None>"),
t.character);
Dialog dialog = FungusCommandEditor.ObjectField<Dialog>(new GUIContent("Dialog", "Dialog to use when displaying choices"),
new GUIContent("<Default>"),
t.dialog);
ChooseDialog dialog = FungusCommandEditor.ObjectField<ChooseDialog>(new GUIContent("Choose Dialog", "Dialog to use when displaying choices"),
new GUIContent("<Default>"),
t.dialog);
float timeoutDuration = EditorGUILayout.FloatField(new GUIContent("Timeout Duration", "Time limit for player to make a choice. Set to 0 for no limit."), t.timeoutDuration);

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

@ -27,7 +27,7 @@ namespace Fungus.Script
new GUIContent("<None>"),
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>"),
t.dialog);

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

@ -8,7 +8,7 @@ namespace Fungus.Script
{
public string characterName;
public Sprite characterImage;
public Dialog.DialogSide dialogSide;
public SayDialog.DialogSide dialogSide;
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:

180
Assets/Fungus/Dialog/Scripts/Dialog.cs

@ -7,7 +7,7 @@ using System.Collections.Generic;
namespace Fungus.Script
{
public class Dialog : MonoBehaviour
{
public enum DialogSide
@ -16,26 +16,16 @@ namespace Fungus.Script
Right
};
public class Option
{
public string text;
public Action onSelect;
}
public float writingSpeed;
public AudioClip writingSound;
public bool loopWritingSound = true;
public Canvas dialogCanvas;
public List<UnityEngine.UI.Button> optionButtons = new List<UnityEngine.UI.Button>();
public Text nameText;
public Text storyText;
public Image continueImage;
public Image leftImage;
public Image rightImage;
List<Action> optionActions = new List<Action>();
public void ShowDialog(bool visible)
{
if (dialogCanvas != null)
@ -43,7 +33,7 @@ namespace Fungus.Script
dialogCanvas.gameObject.SetActive(visible);
}
}
public void SetCharacter(Character character)
{
if (character == null)
@ -77,7 +67,7 @@ namespace Fungus.Script
leftImage.enabled = false;
}
}
if (rightImage != null)
{
if (image != null &&
@ -92,7 +82,7 @@ namespace Fungus.Script
}
}
}
public void SetCharacterName(string name, Color color)
{
if (nameText != null)
@ -102,50 +92,7 @@ namespace Fungus.Script
}
}
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();
}
}));
}));
}
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)
protected IEnumerator WriteText(string text, Action onWritingComplete)
{
// Zero CPS means write instantly
// Also write instantly if text contains markup tags
@ -177,7 +124,7 @@ namespace Fungus.Script
float writeDelay = (1f / (float)writingSpeed);
float timeAccumulator = 0f;
int i = 0;
while (true)
{
timeAccumulator += Time.deltaTime;
@ -201,61 +148,25 @@ namespace Fungus.Script
yield return null;
}
if (typingAudio != null)
{
Destroy(typingAudio);
}
if (onWritingComplete != null)
{
onWritingComplete();
}
yield break;
}
IEnumerator WaitForInput(Action onInput)
{
// 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;
}
yield break;
}
void Clear()
protected virtual void Clear()
{
ClearStoryText();
ClearOptions();
ShowContinueIcon(false);
}
void ClearStoryText()
{
if (storyText != null)
@ -263,68 +174,7 @@ namespace Fungus.Script
storyText.text = "";
}
}
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
guid: 3a0bbe22c246e4c78ad8e9816cbae9d5
guid: b7e8f397d6557484f91f9992f702cff5
MonoImporter:
serializedVersion: 2
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