diff --git a/Assets/Example/Scenes/Example.unity b/Assets/Example/Scenes/Example.unity index ea78949f..2945b66d 100644 Binary files a/Assets/Example/Scenes/Example.unity and b/Assets/Example/Scenes/Example.unity differ diff --git a/Assets/Fungus/FungusScript/Commands/Comment.cs b/Assets/Fungus/FungusScript/Commands/Note.cs similarity index 90% rename from Assets/Fungus/FungusScript/Commands/Comment.cs rename to Assets/Fungus/FungusScript/Commands/Note.cs index 88dbc9c8..4c8328a8 100644 --- a/Assets/Fungus/FungusScript/Commands/Comment.cs +++ b/Assets/Fungus/FungusScript/Commands/Note.cs @@ -1,4 +1,5 @@ using UnityEngine; +using UnityEditor; using System.Collections; using System.Collections.Generic; @@ -7,11 +8,11 @@ namespace Fungus [CommandInfo("Scripting", "Note", "Records design notes and reminders about your script.")] - public class Comment : Command + public class Note : Command { [TextArea(2,4)] public string commentText = ""; - + public override void OnEnter() { Continue(); diff --git a/Assets/Fungus/FungusScript/Commands/Comment.cs.meta b/Assets/Fungus/FungusScript/Commands/Note.cs.meta similarity index 100% rename from Assets/Fungus/FungusScript/Commands/Comment.cs.meta rename to Assets/Fungus/FungusScript/Commands/Note.cs.meta diff --git a/Assets/Fungus/FungusScript/Editor/CommandEditor.cs b/Assets/Fungus/FungusScript/Editor/CommandEditor.cs index 16964b64..42caeefb 100644 --- a/Assets/Fungus/FungusScript/Editor/CommandEditor.cs +++ b/Assets/Fungus/FungusScript/Editor/CommandEditor.cs @@ -12,15 +12,6 @@ namespace Fungus { public static Command selectedCommand; - void OnEnable() - { - Command t = target as Command; - if (t != null) - { - t.hideFlags = HideFlags.HideInInspector; - } - } - public static CommandInfoAttribute GetCommandInfo(System.Type commandType) { object[] attributes = commandType.GetCustomAttributes(typeof(CommandInfoAttribute), false); diff --git a/Assets/Fungus/FungusScript/Editor/CommandListAdaptor.cs b/Assets/Fungus/FungusScript/Editor/CommandListAdaptor.cs index b15c9a3d..b2069df5 100644 --- a/Assets/Fungus/FungusScript/Editor/CommandListAdaptor.cs +++ b/Assets/Fungus/FungusScript/Editor/CommandListAdaptor.cs @@ -5,12 +5,21 @@ using UnityEngine; using UnityEditor; using System; +using System.Collections.Generic; +using System.Linq; using Rotorz.ReorderableList; namespace Fungus { public class CommandListAdaptor : IReorderableListAdaptor { - + + private class SetCommandOperation + { + public Sequence sequence; + public Type commandType; + public int index; + } + private SerializedProperty _arrayProperty; public float fixedItemHeight; @@ -74,8 +83,7 @@ namespace Fungus Command AddNewCommand() { FungusScript fungusScript = FungusScriptWindow.GetFungusScript(); - if (fungusScript == null || - fungusScript.selectedAddCommandType == null) + if (fungusScript == null) { return null; } @@ -86,7 +94,7 @@ namespace Fungus return null; } - Command newCommand = sequence.gameObject.AddComponent(fungusScript.selectedAddCommandType) as Command; + Command newCommand = sequence.gameObject.AddComponent() as Command; fungusScript.selectedCommand = newCommand; return newCommand; @@ -174,8 +182,10 @@ namespace Fungus } string commandName = commandInfoAttr.CommandName; - GUIStyle commandStyle = new GUIStyle(EditorStyles.miniButtonLeft); - float buttonWidth = Mathf.Max(commandStyle.CalcSize(new GUIContent(commandName)).x, 100f); + + GUIStyle commandLabelStyle = new GUIStyle(EditorStyles.miniButtonLeft); + + float buttonWidth = Mathf.Max(commandLabelStyle.CalcSize(new GUIContent(commandName)).x, 100f); float indentWidth = command.indentLevel * indentSize; Rect buttonRect = position; @@ -186,7 +196,7 @@ namespace Fungus Rect summaryRect = buttonRect; summaryRect.x += buttonWidth - 1; - summaryRect.width = position.width - buttonWidth - indentWidth; + summaryRect.width = position.width - buttonWidth - indentWidth - 15; if (!Application.isPlaying && Event.current.type == EventType.MouseDown && @@ -220,18 +230,31 @@ namespace Fungus } GUI.backgroundColor = buttonBackgroundColor; - GUI.Label(buttonRect, commandName, commandStyle); + GUI.Label(buttonRect, commandName, commandLabelStyle); - GUIStyle labelStyle = new GUIStyle(EditorStyles.miniButtonRight); - labelStyle.alignment = TextAnchor.MiddleLeft; + GUIStyle summaryStyle = new GUIStyle(EditorStyles.miniButtonRight); + summaryStyle.alignment = TextAnchor.MiddleLeft; if (error && !selected) { - labelStyle.normal.textColor = Color.white; + summaryStyle.normal.textColor = Color.white; } GUI.backgroundColor = summaryBackgroundColor; - GUI.Box(summaryRect, summary, labelStyle); + GUI.Box(summaryRect, summary, summaryStyle); + GUI.backgroundColor = Color.white; + + Rect menuRect = summaryRect; + menuRect.x += menuRect.width + 2; + menuRect.y = position.y + 1; + menuRect.width = 18; + menuRect.height = position.height; + + GUIStyle menuButtonStyle = new GUIStyle(EditorStyles.popup); + if (GUI.Button(menuRect, new GUIContent("", "Select command type"), menuButtonStyle)) + { + ShowCommandMenu(index, fungusScript.selectedSequence); + } } public virtual float GetItemHeight(int index) { @@ -276,7 +299,56 @@ namespace Fungus element.objectReferenceValue = null; break; } - } + } + + void ShowCommandMenu(int index, Sequence sequence) + { + GenericMenu commandMenu = new GenericMenu(); + + // Build menu list + List menuTypes = EditorExtensions.FindDerivedTypes(typeof(Command)).ToList(); + foreach(System.Type type in menuTypes) + { + object[] attributes = type.GetCustomAttributes(false); + foreach (object obj in attributes) + { + CommandInfoAttribute infoAttr = obj as CommandInfoAttribute; + if (infoAttr != null) + { + SetCommandOperation commandOperation = new SetCommandOperation(); + + commandOperation.sequence = sequence; + commandOperation.commandType = type; + commandOperation.index = index; + + commandMenu.AddItem (new GUIContent (infoAttr.Category + "/" + infoAttr.CommandName), + false, Callback, commandOperation); + } + } + } + + commandMenu.ShowAsContext(); + } + + void Callback(object obj) + { + SetCommandOperation commandOperation = obj as SetCommandOperation; + + Sequence sequence = commandOperation.sequence; + if (sequence == null) + { + return; + } + + Command newCommand = Undo.AddComponent(sequence.gameObject, commandOperation.commandType) as Command; + sequence.GetFungusScript().selectedCommand = newCommand; + + Command oldCommand = sequence.commandList[commandOperation.index]; + Undo.DestroyObjectImmediate(oldCommand); + + Undo.RecordObject(sequence, "Set command type"); + sequence.commandList[commandOperation.index] = newCommand; + } } } diff --git a/Assets/Fungus/FungusScript/Editor/SequenceEditor.cs b/Assets/Fungus/FungusScript/Editor/SequenceEditor.cs index c3a0690b..096b0327 100644 --- a/Assets/Fungus/FungusScript/Editor/SequenceEditor.cs +++ b/Assets/Fungus/FungusScript/Editor/SequenceEditor.cs @@ -55,86 +55,6 @@ namespace Fungus serializedObject.ApplyModifiedProperties(); return; } - - EditorGUI.BeginChangeCheck(); - - EditorGUILayout.BeginHorizontal(); - - // Build list of categories - List categories = new List(); - List subTypes = EditorExtensions.FindDerivedTypes(typeof(Command)).ToList(); - foreach(System.Type type in subTypes) - { - object[] attributes = type.GetCustomAttributes(false); - foreach (object obj in attributes) - { - CommandInfoAttribute categoryAttr = obj as CommandInfoAttribute; - if (categoryAttr != null) - { - if (!categories.Contains(categoryAttr.Category)) - { - categories.Add(categoryAttr.Category); - } - } - } - } - categories.Sort(); - - GUILayout.Label("New Command"); - GUILayout.FlexibleSpace(); - - // We should probably use SerializedProperty for the category & command index but there's no real benefit to doing so - int selectedCategoryIndex = EditorGUILayout.Popup(fungusScript.selectedCommandCategoryIndex, categories.ToArray()); - - List commandNames = new List(); - List commandTypes = new List(); - - string categoryName = categories[selectedCategoryIndex]; - foreach (System.Type type in subTypes) - { - CommandInfoAttribute commandInfoAttr = CommandEditor.GetCommandInfo(type); - if (commandInfoAttr == null) - { - continue; - } - - if (categoryName == commandInfoAttr.Category) - { - commandNames.Add(commandInfoAttr.CommandName); - commandTypes.Add(type); - } - } - - int selectedCommandIndex = EditorGUILayout.Popup(fungusScript.selectedAddCommandIndex, commandNames.ToArray()); - if (selectedCategoryIndex != fungusScript.selectedCommandCategoryIndex) - { - // Default to first item in list if category has changed - selectedCommandIndex = 0; - } - - EditorGUILayout.EndHorizontal(); - - if (EditorGUI.EndChangeCheck()) - { - Undo.RecordObject(fungusScript, "Select Command"); - fungusScript.selectedCommandCategoryIndex = selectedCategoryIndex; - fungusScript.selectedAddCommandIndex = selectedCommandIndex; - } - - if (selectedCommandIndex >= commandTypes.Count) - { - return; - } - - System.Type selectedType = commandTypes[selectedCommandIndex]; - if (fungusScript.selectedSequence == null || - selectedType == null) - { - serializedObject.ApplyModifiedProperties(); - return; - } - - fungusScript.selectedAddCommandType = selectedType; EditorGUILayout.BeginHorizontal(); @@ -150,12 +70,13 @@ namespace Fungus EditorGUILayout.EndHorizontal(); - CommandInfoAttribute infoAttr = CommandEditor.GetCommandInfo(selectedType); - if (infoAttr != null) + if (fungusScript.selectedCommand != null) { - GUIStyle labelStyle = new GUIStyle(EditorStyles.miniLabel); - labelStyle.wordWrap = true; - EditorGUILayout.HelpBox(infoAttr.HelpText, MessageType.Info); + CommandInfoAttribute infoAttr = CommandEditor.GetCommandInfo(fungusScript.selectedCommand.GetType()); + if (infoAttr != null) + { + EditorGUILayout.HelpBox(infoAttr.HelpText, MessageType.Info); + } } serializedObject.ApplyModifiedProperties(); diff --git a/Assets/Fungus/FungusScript/Scripts/FungusScript.cs b/Assets/Fungus/FungusScript/Scripts/FungusScript.cs index 0246857b..14776f94 100644 --- a/Assets/Fungus/FungusScript/Scripts/FungusScript.cs +++ b/Assets/Fungus/FungusScript/Scripts/FungusScript.cs @@ -15,15 +15,6 @@ namespace Fungus [System.NonSerialized] public Command copyCommand; - [HideInInspector] - public int selectedAddCommandIndex; - - [HideInInspector] - public System.Type selectedAddCommandType; - - [HideInInspector] - public int selectedCommandCategoryIndex; - [HideInInspector] public Vector2 scriptScrollPos;