From 9820dc403cebabe660faac0495a9d96f2772bb40 Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Wed, 18 Mar 2015 15:19:50 +0000 Subject: [PATCH] Command editor now scrolls independently of command list #84 Also changed add command to insert new commands after last selected command in the list. --- .../FungusScript/Editor/SequenceEditor.cs | 191 ++++++++++-------- .../FungusScript/Editor/SequenceInspector.cs | 65 +++++- .../FungusScript/Scripts/FungusScript.cs | 9 + 3 files changed, 184 insertions(+), 81 deletions(-) diff --git a/Assets/Fungus/FungusScript/Editor/SequenceEditor.cs b/Assets/Fungus/FungusScript/Editor/SequenceEditor.cs index 4ac5c4b2..5c291819 100644 --- a/Assets/Fungus/FungusScript/Editor/SequenceEditor.cs +++ b/Assets/Fungus/FungusScript/Editor/SequenceEditor.cs @@ -26,6 +26,27 @@ namespace Fungus public int index; } + public virtual void DrawSequenceName(FungusScript fungusScript) + { + serializedObject.Update(); + + SerializedProperty sequenceNameProperty = serializedObject.FindProperty("sequenceName"); + Rect sequenceLabelRect = new Rect(45, 5, 120, 16); + EditorGUI.LabelField(sequenceLabelRect, new GUIContent("Sequence Name")); + Rect sequenceNameRect = new Rect(45, 21, 180, 16); + EditorGUI.PropertyField(sequenceNameRect, sequenceNameProperty, new GUIContent("")); + + // Ensure sequence name is unique for this Fungus Script + Sequence sequence = target as Sequence; + string uniqueName = fungusScript.GetUniqueSequenceKey(sequenceNameProperty.stringValue, sequence); + if (uniqueName != sequence.sequenceName) + { + sequenceNameProperty.stringValue = uniqueName; + } + + serializedObject.ApplyModifiedProperties(); + } + public virtual void DrawSequenceGUI(FungusScript fungusScript) { serializedObject.Update(); @@ -45,20 +66,7 @@ namespace Fungus DrawEventHandlerGUI(fungusScript); UpdateIndentLevels(sequence); - - SerializedProperty sequenceNameProperty = serializedObject.FindProperty("sequenceName"); - Rect sequenceLabelRect = new Rect(45, 5, 120, 16); - EditorGUI.LabelField(sequenceLabelRect, new GUIContent("Sequence Name")); - Rect sequenceNameRect = new Rect(45, 21, 180, 16); - EditorGUI.PropertyField(sequenceNameRect, sequenceNameProperty, new GUIContent("")); - - // Ensure sequence name is unique for this Fungus Script - string uniqueName = fungusScript.GetUniqueSequenceKey(sequenceNameProperty.stringValue, sequence); - if (uniqueName != sequence.sequenceName) - { - sequenceNameProperty.stringValue = uniqueName; - } - + // Make sure each command has a reference to its parent sequence foreach (Command command in sequence.commandList) { @@ -82,27 +90,10 @@ namespace Fungus ShowContextMenu(); } - GUILayout.BeginHorizontal(); - - // Previous Command - if ((Event.current.type == EventType.keyDown) && (Event.current.keyCode == KeyCode.PageUp)) - { - SelectPrevious(); - GUI.FocusControl("dummycontrol"); - Event.current.Use(); - } - // Next Command - if ((Event.current.type == EventType.keyDown) && (Event.current.keyCode == KeyCode.PageDown)) - { - SelectNext(); - GUI.FocusControl("dummycontrol"); - Event.current.Use(); - } - if (GUIUtility.keyboardControl == 0) //Only call keyboard shortcuts when not typing in a text field { Event e = Event.current; - + // Copy keyboard shortcut if (e.type == EventType.ValidateCommand && e.commandName == "Copy") { @@ -111,12 +102,13 @@ namespace Fungus e.Use(); } } + if (e.type == EventType.ExecuteCommand && e.commandName == "Copy") { Copy(); e.Use(); } - + // Cut keyboard shortcut if (e.type == EventType.ValidateCommand && e.commandName == "Cut") { @@ -125,12 +117,13 @@ namespace Fungus e.Use(); } } + if (e.type == EventType.ExecuteCommand && e.commandName == "Cut") { Cut(); e.Use(); } - + // Paste keyboard shortcut if (e.type == EventType.ValidateCommand && e.commandName == "Paste") { @@ -140,12 +133,13 @@ namespace Fungus e.Use(); } } + if (e.type == EventType.ExecuteCommand && e.commandName == "Paste") { Paste(); e.Use(); } - + // Duplicate keyboard shortcut if (e.type == EventType.ValidateCommand && e.commandName == "Duplicate") { @@ -154,13 +148,14 @@ namespace Fungus e.Use(); } } + if (e.type == EventType.ExecuteCommand && e.commandName == "Duplicate") { Copy(); Paste(); e.Use(); } - + // Delete keyboard shortcut if (e.type == EventType.ValidateCommand && e.commandName == "Delete") { @@ -169,17 +164,19 @@ namespace Fungus e.Use(); } } + if (e.type == EventType.ExecuteCommand && e.commandName == "Delete") { Delete(); e.Use(); } - + // SelectAll keyboard shortcut if (e.type == EventType.ValidateCommand && e.commandName == "SelectAll") { e.Use(); } + if (e.type == EventType.ExecuteCommand && e.commandName == "SelectAll") { SelectAll(); @@ -187,47 +184,6 @@ namespace Fungus } } - // Up Button - Texture2D upIcon = Resources.Load("Icons/up") as Texture2D; - if (GUILayout.Button(upIcon)) - { - SelectPrevious(); - } - - // Down Button - Texture2D downIcon = Resources.Load("Icons/down") as Texture2D; - if (GUILayout.Button(downIcon)) - { - SelectNext(); - } - - GUILayout.FlexibleSpace(); - - GUILayout.FlexibleSpace(); - - // Add Button - Texture2D addIcon = Resources.Load("Icons/add") as Texture2D; - if (GUILayout.Button(addIcon)) - { - ShowCommandMenu(); - } - - // Duplicate Button - Texture2D duplicateIcon = Resources.Load("Icons/duplicate") as Texture2D; - if (GUILayout.Button(duplicateIcon)) - { - Copy(); - Paste(); - } - - // Delete Button - Texture2D deleteIcon = Resources.Load("Icons/delete") as Texture2D; - if (GUILayout.Button(deleteIcon)) - { - Delete(); - } - - GUILayout.EndHorizontal(); } // Remove any null entries in the command list. @@ -244,6 +200,66 @@ namespace Fungus serializedObject.ApplyModifiedProperties(); } + public virtual void DrawButtonToolbar() + { + GUILayout.BeginHorizontal(); + + // Previous Command + if ((Event.current.type == EventType.keyDown) && (Event.current.keyCode == KeyCode.PageUp)) + { + SelectPrevious(); + GUI.FocusControl("dummycontrol"); + Event.current.Use(); + } + // Next Command + if ((Event.current.type == EventType.keyDown) && (Event.current.keyCode == KeyCode.PageDown)) + { + SelectNext(); + GUI.FocusControl("dummycontrol"); + Event.current.Use(); + } + + // Up Button + Texture2D upIcon = Resources.Load("Icons/up") as Texture2D; + if (GUILayout.Button(upIcon)) + { + SelectPrevious(); + } + + // Down Button + Texture2D downIcon = Resources.Load("Icons/down") as Texture2D; + if (GUILayout.Button(downIcon)) + { + SelectNext(); + } + + GUILayout.FlexibleSpace(); + + // Add Button + Texture2D addIcon = Resources.Load("Icons/add") as Texture2D; + if (GUILayout.Button(addIcon)) + { + ShowCommandMenu(); + } + + // Duplicate Button + Texture2D duplicateIcon = Resources.Load("Icons/duplicate") as Texture2D; + if (GUILayout.Button(duplicateIcon)) + { + Copy(); + Paste(); + } + + // Delete Button + Texture2D deleteIcon = Resources.Load("Icons/delete") as Texture2D; + if (GUILayout.Button(deleteIcon)) + { + Delete(); + } + + GUILayout.EndHorizontal(); + } + protected void DrawEventHandlerGUI(FungusScript fungusScript) { // Show available Event Handlers in a drop down list with type of current @@ -454,7 +470,22 @@ namespace Fungus void ShowCommandMenu() { Sequence sequence = target as Sequence; - int index = sequence.commandList.Count; + + FungusScript fungusScript = sequence.GetFungusScript(); + + // Use index of last selected command in list, or end of list if nothing selected. + int index = -1; + foreach (Command command in fungusScript.selectedCommands) + { + if (command.commandIndex + 1 > index) + { + index = command.commandIndex + 1; + } + } + if (index == -1) + { + index = sequence.commandList.Count; + } GenericMenu commandMenu = new GenericMenu(); @@ -544,7 +575,7 @@ namespace Fungus Undo.RecordObject(sequence, "Set command type"); if (commandOperation.index < sequence.commandList.Count - 1) { - sequence.commandList[commandOperation.index] = newCommand; + sequence.commandList.Insert(commandOperation.index, newCommand); } else { diff --git a/Assets/Fungus/FungusScript/Editor/SequenceInspector.cs b/Assets/Fungus/FungusScript/Editor/SequenceInspector.cs index 9daad062..d3d3c223 100644 --- a/Assets/Fungus/FungusScript/Editor/SequenceInspector.cs +++ b/Assets/Fungus/FungusScript/Editor/SequenceInspector.cs @@ -19,6 +19,11 @@ namespace Fungus [CustomEditor (typeof(SequenceInspector), true)] public class SequenceInspectorEditor : Editor { + protected Vector2 sequenceScrollPos; + protected Vector2 commandScrollPos; + protected bool resize = false; + protected float topPanelHeight = 50; + public override void OnInspectorGUI () { SequenceInspector sequenceInspector = target as SequenceInspector; @@ -32,8 +37,11 @@ namespace Fungus FungusScript fungusScript = sequence.GetFungusScript(); SequenceEditor sequenceEditor = Editor.CreateEditor(sequence) as SequenceEditor; + sequenceEditor.DrawSequenceName(fungusScript); + + sequenceScrollPos = GUILayout.BeginScrollView(sequenceScrollPos, GUILayout.Height(fungusScript.sequenceViewHeight)); sequenceEditor.DrawSequenceGUI(fungusScript); - DestroyImmediate(sequenceEditor); + GUILayout.EndScrollView(); Command inspectCommand = null; if (fungusScript.selectedCommands.Count == 1) @@ -46,9 +54,18 @@ namespace Fungus inspectCommand.parentSequence != sequence) { Repaint(); + DestroyImmediate(sequenceEditor); return; } + ResizeScrollView(fungusScript); + + GUILayout.Space(7); + + sequenceEditor.DrawButtonToolbar(); + + commandScrollPos = GUILayout.BeginScrollView(commandScrollPos); + if (inspectCommand != null) { CommandEditor commandEditor = Editor.CreateEditor(inspectCommand) as CommandEditor; @@ -56,7 +73,53 @@ namespace Fungus DestroyImmediate(commandEditor); } + GUILayout.EndScrollView(); + Repaint(); + + DestroyImmediate(sequenceEditor); + } + + private void ResizeScrollView(FungusScript fungusScript) + { + Rect cursorChangeRect = new Rect(0, fungusScript.sequenceViewHeight + topPanelHeight, Screen.width, 4f); + + GUI.color = Color.grey; + GUI.DrawTexture(cursorChangeRect, EditorGUIUtility.whiteTexture); + GUI.color = Color.white; + + EditorGUIUtility.AddCursorRect(cursorChangeRect, MouseCursor.ResizeVertical); + + if (Event.current.type == EventType.mouseDown && cursorChangeRect.Contains(Event.current.mousePosition)) + { + resize = true; + } + + if (resize) + { + float height = Event.current.mousePosition.y - topPanelHeight; + height = Mathf.Max(200, height); + height = Mathf.Min(Screen.height - 200,height); + + Undo.RecordObject(fungusScript, "Resize view"); + fungusScript.sequenceViewHeight = height; + } + + // Stop resizing if mouse is outside inspector window. + // This isn't standard Unity UI behavior but it is robust and safe. + if (resize && Event.current.type == EventType.mouseDrag) + { + Rect windowRect = new Rect(0, 0, Screen.width, Screen.height); + if (!windowRect.Contains(Event.current.mousePosition)) + { + resize = false; + } + } + + if (Event.current.type == EventType.MouseUp) + { + resize = false; + } } } diff --git a/Assets/Fungus/FungusScript/Scripts/FungusScript.cs b/Assets/Fungus/FungusScript/Scripts/FungusScript.cs index 448cd0d6..247df422 100644 --- a/Assets/Fungus/FungusScript/Scripts/FungusScript.cs +++ b/Assets/Fungus/FungusScript/Scripts/FungusScript.cs @@ -28,9 +28,18 @@ namespace Fungus [HideInInspector] public Vector2 variablesScrollPos; + /** + * Show the variables pane. + */ [HideInInspector] public bool variablesExpanded = true; + /** + * Height of command sequence view in inspector. + */ + [HideInInspector] + public float sequenceViewHeight = 400; + /** * Zoom level of Fungus Script editor window */