Browse Source

New dialog option buttons

master
chrisgregan 10 years ago
parent
commit
05a5dc9268
  1. 110
      Assets/Fungus/Dialog/Commands/Say.cs
  2. 5
      Assets/Fungus/Dialog/Editor.meta
  3. 0
      Assets/Fungus/Dialog/Editor/OptionEditor.cs
  4. 0
      Assets/Fungus/Dialog/Editor/OptionEditor.cs.meta
  5. 86
      Assets/Fungus/Dialog/Editor/SayEditor.cs
  6. 0
      Assets/Fungus/Dialog/Editor/SayEditor.cs.meta
  7. 15
      Assets/Fungus/Dialog/Scripts/Character.cs
  8. 8
      Assets/Fungus/Dialog/Scripts/Character.cs.meta
  9. 263
      Assets/Fungus/Dialog/Scripts/DialogController.cs
  10. 51
      Assets/Fungus/FungusScript/Editor/SayEditor.cs
  11. 38
      Assets/Fungus/FungusScript/Editor/SequenceEditor.cs
  12. BIN
      Assets/Shuttle/NewDialog.prefab
  13. 4
      Assets/Shuttle/NewDialog.prefab.meta
  14. BIN
      Assets/Shuttle/NewDialog.unity
  15. 4
      Assets/Shuttle/NewDialog.unity.meta
  16. BIN
      Assets/Shuttle/ShuttleGame.unity

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

@ -9,65 +9,33 @@ namespace Fungus.Script
[HelpText("Writes a line of story text to the dialog. A condition can be specified for when the story text should be shown.")]
public class Say : FungusCommand
{
public enum ShowCondition
public DialogController dialogController;
public Character character;
public string storyText;
public bool displayOnce;
[Serializable]
public class SayOption
{
Always,
Once,
BooleanIsTrue,
BooleanIsFalse
public string optionText;
public Sequence targetSequence;
}
public string character;
public string text;
public ShowCondition showCondition;
public BooleanVariable booleanVariable;
public List<SayOption> options = new List<SayOption>();
public float timeoutDuration;
int executionCount;
public override void OnEnter()
{
Dialog dialog = Game.GetInstance().dialog;
if (dialog == null)
if (dialogController == null)
{
Continue();
return;
}
bool showSayText = true;
if (showCondition == ShowCondition.Always)
{
// Always show option
}
else if (showCondition == ShowCondition.Once)
{
if (executionCount > 0)
{
showSayText = false;
}
}
else
{
if (booleanVariable == null)
{
showSayText = false;
}
else
{
if (showCondition == ShowCondition.BooleanIsTrue &&
booleanVariable.Value != true)
{
showSayText = false;
}
else if (showCondition == ShowCondition.BooleanIsFalse &&
booleanVariable.Value != false)
{
showSayText = false;
}
}
}
if (!showSayText)
if (displayOnce && executionCount > 0)
{
Continue();
return;
@ -75,19 +43,55 @@ namespace Fungus.Script
executionCount++;
if (character.Length > 0)
if (character != null)
{
dialog.SetCharacter(character);
dialogController.SetCharacter(character);
}
dialog.Say (text, delegate {
Continue();
});
if (options.Count > 0)
{
List<DialogController.Option> dialogOptions = new List<DialogController.Option>();
foreach (SayOption sayOption in options)
{
DialogController.Option dialogOption = new DialogController.Option();
dialogOption.text = sayOption.optionText;
Sequence onSelectSequence = sayOption.targetSequence;
dialogOption.onSelect = delegate {
if (onSelectSequence == null)
{
Continue ();
}
else
{
ExecuteSequence(onSelectSequence);
}
};
dialogOptions.Add(dialogOption);
}
dialogController.Say(storyText, dialogOptions);
}
else
{
dialogController.Say(storyText, delegate {
Continue();
});
}
}
public override string GetSummary()
{
return "\"" + text + "\"";
return "\"" + storyText + "\"";
}
public override void GetConnectedSequences (ref List<Sequence> connectedSequences)
{
foreach (SayOption option in options)
{
connectedSequences.Add(option.targetSequence);
}
}
}

5
Assets/Fungus/Dialog/Editor.meta

@ -0,0 +1,5 @@
fileFormatVersion: 2
guid: 805471f08101f4ad8a705fc863435758
folderAsset: yes
DefaultImporter:
userData:

0
Assets/Fungus/FungusScript/Editor/OptionEditor.cs → Assets/Fungus/Dialog/Editor/OptionEditor.cs

0
Assets/Fungus/FungusScript/Editor/OptionEditor.cs.meta → Assets/Fungus/Dialog/Editor/OptionEditor.cs.meta

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

@ -0,0 +1,86 @@
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Rotorz.ReorderableList;
namespace Fungus.Script
{
[CustomEditor (typeof(Say))]
public class SayEditor : FungusCommandEditor
{
public override void DrawCommandGUI()
{
serializedObject.Update();
SerializedProperty optionListProperty = serializedObject.FindProperty("options");
Say t = target as Say;
EditorGUI.BeginChangeCheck();
EditorGUILayout.PrefixLabel(new GUIContent("Say Text", "Text to display in dialog"));
GUIStyle sayStyle = new GUIStyle(EditorStyles.textArea);
sayStyle.wordWrap = true;
string text = EditorGUILayout.TextArea(t.storyText, sayStyle, GUILayout.MinHeight(30));
DialogController dialogController = EditorGUILayout.ObjectField(new GUIContent("Dialog", "Dialog to display story text with"),
t.dialogController,
typeof(DialogController), true) as DialogController;
Character character = EditorGUILayout.ObjectField(new GUIContent("Character", "Character to display in dialog"),
t.character,
typeof(Character), true) as Character;
bool displayOnce = EditorGUILayout.Toggle(new GUIContent("Display Once", "Display this text once and never show it again."), t.displayOnce);
if (EditorGUI.EndChangeCheck())
{
Undo.RecordObject(t, "Set Say");
t.dialogController = dialogController;
t.character = character;
t.storyText = text;
t.displayOnce = displayOnce;
}
ReorderableListGUI.Title("Options");
ReorderableListGUI.ListField(optionListProperty);
serializedObject.ApplyModifiedProperties();
}
}
[CustomPropertyDrawer (typeof(Say.SayOption))]
public class SayOptionDrawer : PropertyDrawer
{
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);
SerializedProperty optionTextProp = property.FindPropertyRelative("optionText");
SerializedProperty targetSequenceProp = property.FindPropertyRelative("targetSequence");
Rect optionTextRect = position;
optionTextRect.width *= 0.5f;
Rect targetSequenceRect = position;
targetSequenceRect.width *= 0.5f;
targetSequenceRect.x += optionTextRect.width;
FungusScript fungusScript = FungusScriptWindow.GetFungusScript();
optionTextProp.stringValue = EditorGUI.TextField(optionTextRect, optionTextProp.stringValue);
if (fungusScript != null)
{
targetSequenceProp.objectReferenceValue = SequenceEditor.SequenceField(targetSequenceRect,
new GUIContent("<Continue>"),
fungusScript,
targetSequenceProp.objectReferenceValue as Sequence);
}
EditorGUI.EndProperty();
}
}
}

0
Assets/Fungus/FungusScript/Editor/SayEditor.cs.meta → Assets/Fungus/Dialog/Editor/SayEditor.cs.meta

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

@ -0,0 +1,15 @@
using UnityEngine;
using System.Collections;
namespace Fungus.Script
{
public class Character : MonoBehaviour
{
public string characterName;
public Sprite characterImage;
public DialogController.DialogSide dialogSide;
public Color characterColor;
}
}

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

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

263
Assets/Fungus/Dialog/Scripts/DialogController.cs

@ -5,145 +5,216 @@ using System;
using System.Collections;
using System.Collections.Generic;
public class DialogController : MonoBehaviour
namespace Fungus.Script
{
public Sprite testCharacter;
public Canvas dialogCanvas;
public List<Button> optionButtons = new List<Button>();
public Text nameText;
public Text storyText;
public Image continueImage;
public Image leftImage;
public Image rightImage;
public enum ImageSide
{
Left,
Right
};
public void SetCharacterImage(Sprite image, ImageSide side)
public class DialogController : MonoBehaviour
{
if (leftImage != null)
public enum DialogSide
{
Left,
Right
};
public class Option
{
public string text;
public Action onSelect;
}
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 SetCharacter(Character character)
{
if (character == null)
{
return;
}
SetCharacterImage(character.characterImage, character.dialogSide);
SetCharacterName(character.name, character.characterColor);
}
public void SetCharacterImage(Sprite image, DialogSide side)
{
if (image != null &&
side == ImageSide.Left)
if (leftImage != null)
{
leftImage.sprite = image;
leftImage.enabled = true;
if (image != null &&
side == DialogSide.Left)
{
leftImage.sprite = image;
leftImage.enabled = true;
}
else
{
leftImage.enabled = false;
}
}
else
if (rightImage != null)
{
leftImage.enabled = false;
rightImage.sprite = null;
if (image != null &&
side == DialogSide.Right)
{
rightImage.sprite = image;
rightImage.enabled = true;
}
else
{
rightImage.sprite = null;
rightImage.enabled = false;
}
}
}
if (rightImage != null)
public void SetCharacterName(string name, Color color)
{
rightImage.sprite = null;
if (image != null &&
side == ImageSide.Right)
if (nameText != null)
{
rightImage.sprite = image;
rightImage.enabled = true;
nameText.text = name;
nameText.color = color;
}
else
}
public void Say(string text, Action onComplete)
{
Clear();
if (storyText != null)
{
rightImage.sprite = null;
rightImage.enabled = false;
storyText.text = text;
}
// TODO: Wait for text to finish writing
ShowContinueIcon(true);
StartCoroutine(WaitForInput(onComplete));
}
}
public void SetCharacterName(string name, Color color)
{
if (nameText != null)
public void Say(string text, List<Option> options)
{
nameText.text = name;
nameText.color = color;
Clear();
if (storyText != null)
{
storyText.text = text;
}
ShowContinueIcon(false);
foreach (Option option in options)
{
AddOption(option.text, option.onSelect);
}
}
}
public void SetStoryText(string text)
{
if (storyText != null)
IEnumerator WaitForInput(Action onComplete)
{
storyText.text = text;
// TODO: Handle touch input
while (!Input.GetMouseButtonDown(0))
{
yield return null;
}
Clear();
if (onComplete != null)
{
onComplete();
}
}
}
public void ShowContinueIcon(bool visible)
{
if (continueImage != null)
void ShowContinueIcon(bool visible)
{
continueImage.enabled = visible;
if (continueImage != null)
{
continueImage.enabled = visible;
}
}
}
public void ClearOptions()
{
if (optionButtons == null)
void Clear()
{
return;
ClearStoryText();
ClearOptions();
}
foreach (Button button in optionButtons)
void ClearStoryText()
{
button.gameObject.SetActive(false);
if (storyText != null)
{
storyText.text = "";
}
}
}
public void AddOption(string text, Action action)
{
if (optionButtons == null)
void ClearOptions()
{
return;
if (optionButtons == null)
{
return;
}
optionActions.Clear();
foreach (UnityEngine.UI.Button button in optionButtons)
{
if (button != null)
{
button.gameObject.SetActive(false);
}
}
}
foreach (Button button in optionButtons)
bool AddOption(string text, Action action)
{
if (!button.gameObject.activeSelf)
if (optionButtons == null)
{
button.gameObject.SetActive(true);
return false;
}
Text textComponent = button.GetComponentInChildren<Text>();
if (textComponent != null)
bool addedOption = false;
foreach (UnityEngine.UI.Button button in optionButtons)
{
if (!button.gameObject.activeSelf)
{
textComponent.text = text;
}
button.gameObject.SetActive(true);
Text textComponent = button.GetComponentInChildren<Text>();
if (textComponent != null)
{
textComponent.text = text;
}
// TODO: Connect action
optionActions.Add(action);
break;
addedOption = true;
break;
}
}
}
}
public void Start()
{
SetCharacterImage(testCharacter, ImageSide.Left);
SetCharacterName("Podrick", Color.red);
SetStoryText("Simple story text");
ShowContinueIcon(false);
ClearOptions();
AddOption("Something 1", Callback );
AddOption("Something 2", Callback );
}
return addedOption;
}
void Callback()
{
Debug.Log ("Callback");
public void SelectOption(int index)
{
if (index < optionActions.Count)
{
Action optionAction = optionActions[index];
if (optionAction != null)
{
Clear();
optionAction();
}
}
}
}
//public UnityEvent testEvent;
// Write story text over time
// Show character image (with side, fade in?)
// Hide / Show canvas
// Show continue image
// Show one button
// Show button grid
}

51
Assets/Fungus/FungusScript/Editor/SayEditor.cs

@ -1,51 +0,0 @@
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace Fungus.Script
{
[CustomEditor (typeof(Say))]
public class SayEditor : FungusCommandEditor
{
public override void DrawCommandGUI()
{
Say t = target as Say;
EditorGUI.BeginChangeCheck();
string character = EditorGUILayout.TextField(new GUIContent("Character", "Character to display in dialog"), t.character);
EditorGUILayout.PrefixLabel(new GUIContent("Say Text", "Text to display in dialog"));
GUIStyle sayStyle = new GUIStyle(EditorStyles.textArea);
sayStyle.wordWrap = true;
string text = EditorGUILayout.TextArea(t.text, sayStyle, GUILayout.MinHeight(30));
Say.ShowCondition showCondition = (Say.ShowCondition)EditorGUILayout.EnumPopup(new GUIContent("Show Condition", "Condition when this say text should be visible."), t.showCondition);
BooleanVariable booleanVariable = t.booleanVariable;
if (showCondition == Say.ShowCondition.BooleanIsFalse ||
showCondition == Say.ShowCondition.BooleanIsTrue)
{
booleanVariable = FungusVariableEditor.VariableField (new GUIContent ("Boolean Variable", "Boolean variable to test for condition"),
t.GetFungusScript (),
t.booleanVariable,
v => { return v.GetType() == typeof(BooleanVariable); }) as BooleanVariable;
}
if (EditorGUI.EndChangeCheck())
{
Undo.RecordObject(t, "Set Say");
t.character = character;
t.text = text;
t.showCondition = showCondition;
t.booleanVariable = booleanVariable;
}
}
}
}

38
Assets/Fungus/FungusScript/Editor/SequenceEditor.cs

@ -47,6 +47,44 @@ namespace Fungus.Script
return result;
}
static public Sequence SequenceField(Rect position, GUIContent nullLabel, FungusScript fungusScript, Sequence sequence)
{
if (fungusScript == null)
{
return null;
}
Sequence result = sequence;
// Build dictionary of child sequences
List<GUIContent> sequenceNames = new List<GUIContent>();
int selectedIndex = 0;
sequenceNames.Add(nullLabel);
Sequence[] sequences = fungusScript.GetComponentsInChildren<Sequence>();
for (int i = 0; i < sequences.Length; ++i)
{
sequenceNames.Add(new GUIContent(sequences[i].name));
if (sequence == sequences[i])
{
selectedIndex = i + 1;
}
}
selectedIndex = EditorGUI.Popup(position, selectedIndex, sequenceNames.ToArray());
if (selectedIndex == 0)
{
result = null; // Option 'None'
}
else
{
result = sequences[selectedIndex - 1];
}
return result;
}
}
}

BIN
Assets/Shuttle/NewDialog.prefab

Binary file not shown.

4
Assets/Shuttle/NewDialog.prefab.meta

@ -0,0 +1,4 @@
fileFormatVersion: 2
guid: 4417bf892b8c448528ef7c527b82338a
NativeFormatImporter:
userData:

BIN
Assets/Shuttle/NewDialog.unity

Binary file not shown.

4
Assets/Shuttle/NewDialog.unity.meta

@ -0,0 +1,4 @@
fileFormatVersion: 2
guid: 9ffd6f62d04cb4c33aedde50a56aa90c
DefaultImporter:
userData:

BIN
Assets/Shuttle/ShuttleGame.unity

Binary file not shown.
Loading…
Cancel
Save