From b5ea08a4d242a8b2c78862ff13b91fe967c19fff Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Wed, 21 Jan 2015 16:43:22 +0000 Subject: [PATCH] Choose command that acts like a Switch statement #80 Deprecated old Choose & AddOption commands. Replaced Choose command with ChooseOption (uses command name Choose though). Option command can be hidden once visited, or hidden by evaluating a variable condition. --- Assets/Fungus/Dialog/Deprecated.meta | 5 + .../Commands => Deprecated}/AddOption.cs | 4 +- .../Commands => Deprecated}/AddOption.cs.meta | 0 .../Commands => Deprecated}/Choose.cs | 5 +- .../Commands => Deprecated}/Choose.cs.meta | 0 .../Dialog/Editor/ChooseOptionEditor.cs | 120 +++++++++++ .../Dialog/Editor/ChooseOptionEditor.cs.meta | 8 + Assets/Fungus/Dialog/Editor/OptionEditor.cs | 45 ++++ .../Fungus/Dialog/Editor/OptionEditor.cs.meta | 8 + Assets/Fungus/Dialog/Editor/SayEditor.cs | 6 +- .../Dialog/Scripts/Commands/ChooseOption.cs | 143 +++++++++++++ .../Scripts/Commands/ChooseOption.cs.meta | 8 + .../Fungus/Dialog/Scripts/Commands/Option.cs | 98 +++++++++ .../Dialog/Scripts/Commands/Option.cs.meta | 8 + .../FungusScript/Scripts/Commands/ElseIf.cs | 2 +- .../FungusScript/Scripts/Commands/If.cs | 11 +- .../FungusExamples/TheHunter/TheHunter.unity | 196 ++++++++++++------ 17 files changed, 586 insertions(+), 81 deletions(-) create mode 100644 Assets/Fungus/Dialog/Deprecated.meta rename Assets/Fungus/Dialog/{Scripts/Commands => Deprecated}/AddOption.cs (87%) rename Assets/Fungus/Dialog/{Scripts/Commands => Deprecated}/AddOption.cs.meta (100%) rename Assets/Fungus/Dialog/{Scripts/Commands => Deprecated}/Choose.cs (93%) rename Assets/Fungus/Dialog/{Scripts/Commands => Deprecated}/Choose.cs.meta (100%) create mode 100644 Assets/Fungus/Dialog/Editor/ChooseOptionEditor.cs create mode 100644 Assets/Fungus/Dialog/Editor/ChooseOptionEditor.cs.meta create mode 100644 Assets/Fungus/Dialog/Editor/OptionEditor.cs create mode 100644 Assets/Fungus/Dialog/Editor/OptionEditor.cs.meta create mode 100644 Assets/Fungus/Dialog/Scripts/Commands/ChooseOption.cs create mode 100644 Assets/Fungus/Dialog/Scripts/Commands/ChooseOption.cs.meta create mode 100644 Assets/Fungus/Dialog/Scripts/Commands/Option.cs create mode 100644 Assets/Fungus/Dialog/Scripts/Commands/Option.cs.meta diff --git a/Assets/Fungus/Dialog/Deprecated.meta b/Assets/Fungus/Dialog/Deprecated.meta new file mode 100644 index 00000000..f3205afb --- /dev/null +++ b/Assets/Fungus/Dialog/Deprecated.meta @@ -0,0 +1,5 @@ +fileFormatVersion: 2 +guid: e28117fbc8861436296315b42526caac +folderAsset: yes +DefaultImporter: + userData: diff --git a/Assets/Fungus/Dialog/Scripts/Commands/AddOption.cs b/Assets/Fungus/Dialog/Deprecated/AddOption.cs similarity index 87% rename from Assets/Fungus/Dialog/Scripts/Commands/AddOption.cs rename to Assets/Fungus/Dialog/Deprecated/AddOption.cs index b7f3c470..78af0b45 100644 --- a/Assets/Fungus/Dialog/Scripts/Commands/AddOption.cs +++ b/Assets/Fungus/Dialog/Deprecated/AddOption.cs @@ -7,8 +7,8 @@ namespace Fungus { [CommandInfo("Dialog", "Add Option", - "Adds an option for the player to select. All previously added options are displayed by the next Choose command. " + - "You can specify another sequence to call and/or a variable to set when the options is selected.")] + "Obsolete: This command will be removed in a future update. Please use the Option command instead.", + -1)] [AddComponentMenu("")] public class AddOption : SetVariable { diff --git a/Assets/Fungus/Dialog/Scripts/Commands/AddOption.cs.meta b/Assets/Fungus/Dialog/Deprecated/AddOption.cs.meta similarity index 100% rename from Assets/Fungus/Dialog/Scripts/Commands/AddOption.cs.meta rename to Assets/Fungus/Dialog/Deprecated/AddOption.cs.meta diff --git a/Assets/Fungus/Dialog/Scripts/Commands/Choose.cs b/Assets/Fungus/Dialog/Deprecated/Choose.cs similarity index 93% rename from Assets/Fungus/Dialog/Scripts/Commands/Choose.cs rename to Assets/Fungus/Dialog/Deprecated/Choose.cs index 956fe816..7e55b94c 100644 --- a/Assets/Fungus/Dialog/Scripts/Commands/Choose.cs +++ b/Assets/Fungus/Dialog/Deprecated/Choose.cs @@ -7,9 +7,8 @@ namespace Fungus { [CommandInfo("Dialog", "Choose", - "Presents a list of options for the player to choose from using a Choose Dialog. " + - "Place Add Option commands before the Choose command to specify the player options. " + - "You can also set a timeout which will cause the following command to execute when the timer runs out.")] + "Obsolete: This command will be removed in a future update. Please use the Option command instead.", + -1)] [AddComponentMenu("")] public class Choose : Command { diff --git a/Assets/Fungus/Dialog/Scripts/Commands/Choose.cs.meta b/Assets/Fungus/Dialog/Deprecated/Choose.cs.meta similarity index 100% rename from Assets/Fungus/Dialog/Scripts/Commands/Choose.cs.meta rename to Assets/Fungus/Dialog/Deprecated/Choose.cs.meta diff --git a/Assets/Fungus/Dialog/Editor/ChooseOptionEditor.cs b/Assets/Fungus/Dialog/Editor/ChooseOptionEditor.cs new file mode 100644 index 00000000..10095078 --- /dev/null +++ b/Assets/Fungus/Dialog/Editor/ChooseOptionEditor.cs @@ -0,0 +1,120 @@ +using UnityEditor; +using UnityEditorInternal; +using UnityEngine; +using System.Collections; +using System.Collections.Generic; +using Rotorz.ReorderableList; + +namespace Fungus +{ + + [CustomEditor (typeof(ChooseOption))] + public class ChooseOptionEditor : CommandEditor + { + static public bool showTagHelp; + + protected SerializedProperty chooseTextProp; + protected SerializedProperty characterProp; + protected SerializedProperty chooseDialogProp; + protected SerializedProperty portraitProp; + protected SerializedProperty voiceOverClipProp; + protected SerializedProperty timeoutDurationProp; + + protected virtual void OnEnable() + { + chooseTextProp = serializedObject.FindProperty("chooseText"); + characterProp = serializedObject.FindProperty("character"); + portraitProp = serializedObject.FindProperty("portrait"); + chooseDialogProp = serializedObject.FindProperty("chooseDialog"); + voiceOverClipProp = serializedObject.FindProperty("voiceOverClip"); + timeoutDurationProp = serializedObject.FindProperty("timeoutDuration"); + } + + public override void DrawCommandGUI() + { + serializedObject.Update(); + + ChooseOption t = target as ChooseOption; + + CommandEditor.ObjectField(characterProp, + new GUIContent("Character", "Character to display in dialog"), + new GUIContent(""), + Character.activeCharacters); + + CommandEditor.ObjectField(chooseDialogProp, + new GUIContent("Choose Dialog", "Choose Dialog object to use to display the multiple player choices"), + new GUIContent(""), + ChooseDialog.activeDialogs); + + bool showPortraits = false; + // Only show portrait selection if... + if (t.character != null && // Character is selected + t.character.portraits != null && // Character has a portraits field + t.character.portraits.Count > 0 ) // Selected Character has at least 1 portrait + { + ChooseDialog cd = t.chooseDialog; + if (t.chooseDialog == null) // If default box selected + { + cd = t.character.chooseDialogBox; // Try to get character's default choose dialog box + if (t.chooseDialog == null) // If no default specified, Try to get any ChooseDialog in the scene + { + cd = GameObject.FindObjectOfType(); + } + } + if (cd != null && cd.characterImage != null) // Check that selected choose dialog has a character image + { + showPortraits = true; + } + } + if (showPortraits) + { + CommandEditor.ObjectField(portraitProp, + new GUIContent("Portrait", "Portrait representing speaking character"), + new GUIContent(""), + t.character.portraits); + } + else + { + t.portrait = null; + } + + EditorGUILayout.PropertyField(chooseTextProp); + + EditorGUILayout.BeginHorizontal(); + + GUILayout.FlexibleSpace(); + if (GUILayout.Button(new GUIContent("Tag Help", "Show help info for tags"), new GUIStyle(EditorStyles.miniButton))) + { + showTagHelp = !showTagHelp; + } + EditorGUILayout.EndHorizontal(); + + if (showTagHelp) + { + SayEditor.DrawTagHelpLabel(); + } + + EditorGUILayout.Separator(); + + EditorGUILayout.PropertyField(voiceOverClipProp, new GUIContent("Voice Over Clip", "Voice over audio to play when the choose text is displayed")); + + EditorGUILayout.PropertyField(timeoutDurationProp, new GUIContent("Timeout Duration", "Time limit for player to make a choice. Set to 0 for no limit.")); + + if (showPortraits && t.portrait != null) + { + Texture2D characterTexture = t.portrait.texture; + + float aspect = (float)characterTexture.width / (float)characterTexture.height; + + Rect previewRect = GUILayoutUtility.GetAspectRect(aspect, GUILayout.Width(100), GUILayout.ExpandWidth(true)); + + CharacterEditor characterEditor = Editor.CreateEditor(t.character) as CharacterEditor; + characterEditor.DrawPreview(previewRect, characterTexture); + DestroyImmediate(characterEditor); + } + + serializedObject.ApplyModifiedProperties(); + } + } + +} \ No newline at end of file diff --git a/Assets/Fungus/Dialog/Editor/ChooseOptionEditor.cs.meta b/Assets/Fungus/Dialog/Editor/ChooseOptionEditor.cs.meta new file mode 100644 index 00000000..d5d15e9d --- /dev/null +++ b/Assets/Fungus/Dialog/Editor/ChooseOptionEditor.cs.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 92f79083287204bfcbfe02069f0ec4d2 +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: diff --git a/Assets/Fungus/Dialog/Editor/OptionEditor.cs b/Assets/Fungus/Dialog/Editor/OptionEditor.cs new file mode 100644 index 00000000..da77dbf8 --- /dev/null +++ b/Assets/Fungus/Dialog/Editor/OptionEditor.cs @@ -0,0 +1,45 @@ +using UnityEditor; +using UnityEditorInternal; +using UnityEngine; +using System.Collections; +using System.Collections.Generic; +using Rotorz.ReorderableList; + +namespace Fungus +{ + + [CustomEditor (typeof(Option))] + public class OptionEditor : IfEditor + { + protected SerializedProperty optionTextProp; + protected SerializedProperty hideOnSelectedProp; + protected SerializedProperty hideOnConditionProp; + + protected override void OnEnable() + { + base.OnEnable(); + optionTextProp = serializedObject.FindProperty("optionText"); + hideOnSelectedProp = serializedObject.FindProperty("hideOnSelected"); + hideOnConditionProp = serializedObject.FindProperty("hideOnCondition"); + } + + public override void DrawCommandGUI() + { + serializedObject.Update(); + + EditorGUILayout.PropertyField(optionTextProp); + EditorGUILayout.PropertyField(hideOnSelectedProp); + EditorGUILayout.PropertyField(hideOnConditionProp); + + serializedObject.ApplyModifiedProperties(); + + if (hideOnConditionProp.boolValue) + { + EditorGUI.indentLevel++; + base.DrawCommandGUI(); + EditorGUI.indentLevel--; + } + } + } + +} \ No newline at end of file diff --git a/Assets/Fungus/Dialog/Editor/OptionEditor.cs.meta b/Assets/Fungus/Dialog/Editor/OptionEditor.cs.meta new file mode 100644 index 00000000..36d4d76c --- /dev/null +++ b/Assets/Fungus/Dialog/Editor/OptionEditor.cs.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 7d495a449ea3144118349a7c5d989c52 +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: diff --git a/Assets/Fungus/Dialog/Editor/SayEditor.cs b/Assets/Fungus/Dialog/Editor/SayEditor.cs index 4b4bef86..c811964c 100644 --- a/Assets/Fungus/Dialog/Editor/SayEditor.cs +++ b/Assets/Fungus/Dialog/Editor/SayEditor.cs @@ -90,9 +90,9 @@ namespace Fungus if (showPortraits) { CommandEditor.ObjectField(portraitProp, - new GUIContent("Portrait", "Portrait representing speaking character"), - new GUIContent(""), - t.character.portraits); + new GUIContent("Portrait", "Portrait representing speaking character"), + new GUIContent(""), + t.character.portraits); } else { diff --git a/Assets/Fungus/Dialog/Scripts/Commands/ChooseOption.cs b/Assets/Fungus/Dialog/Scripts/Commands/ChooseOption.cs new file mode 100644 index 00000000..b5132f1a --- /dev/null +++ b/Assets/Fungus/Dialog/Scripts/Commands/ChooseOption.cs @@ -0,0 +1,143 @@ +using UnityEngine; +using System; +using System.Collections; +using System.Collections.Generic; + +namespace Fungus +{ + [CommandInfo("Dialog", + "Choose", + "Presents a list of options for the player to choose from using a Choose Dialog. " + + "Place Option commands after the Choose Option command to specify the player options, and terminate with an End command.")] + [AddComponentMenu("")] + public class ChooseOption : Command + { + [Tooltip("Story text to display to prompt player to choose an option")] + [TextArea(5,10)] + public string chooseText; + + [Tooltip("Speaking character to use when prompting the player to choose an option")] + public Character character; + + [Tooltip("Choose Dialog object to use to display the player options")] + public ChooseDialog chooseDialog; + + [Tooltip("Portrait that represents speaking character")] + public Sprite portrait; + + [Tooltip("Voiceover audio to play when prompting the player to choose an option")] + public AudioClip voiceOverClip; + + [Tooltip("Time limit for player to choose an option. Set to 0 for no time limit.")] + public float timeoutDuration; + + public override void OnEnter() + { + if (chooseDialog == null) + { + if ( character != null ) { + // Try to get character's choose box + chooseDialog = character.chooseDialogBox; + } + + if (chooseDialog == null) + { + // Try to get any ChooseDialog in the scene + chooseDialog = GameObject.FindObjectOfType(); + } + } + + // Build list of Option commands + End endCommand = null; + List