From 507eb490d091d4fa6674609aada5b0f657e8c845 Mon Sep 17 00:00:00 2001 From: desktop-maesty/steve Date: Tue, 7 Aug 2018 15:07:50 +1000 Subject: [PATCH] Add Variable moved from FlowchartEditor to popup --- .../Scripts/Editor/BasePopupWindowContent.cs | 33 ++--- .../Fungus/Scripts/Editor/FlowchartEditor.cs | 84 +----------- .../Fungus/Scripts/Editor/PopupContent.meta | 8 ++ .../CommandSelectorPopupWindowContent.cs | 23 +--- .../CommandSelectorPopupWindowContent.cs.meta | 0 .../EventSelectorPopupWindowContent.cs | 9 +- .../EventSelectorPopupWindowContent.cs.meta | 0 .../VariableSelectPopupWindowContent.cs | 125 ++++++++++++++++++ .../VariableSelectPopupWindowContent.cs.meta | 11 ++ 9 files changed, 173 insertions(+), 120 deletions(-) create mode 100644 Assets/Fungus/Scripts/Editor/PopupContent.meta rename Assets/Fungus/Scripts/Editor/{ => PopupContent}/CommandSelectorPopupWindowContent.cs (86%) rename Assets/Fungus/Scripts/Editor/{ => PopupContent}/CommandSelectorPopupWindowContent.cs.meta (100%) rename Assets/Fungus/Scripts/Editor/{ => PopupContent}/EventSelectorPopupWindowContent.cs (94%) rename Assets/Fungus/Scripts/Editor/{ => PopupContent}/EventSelectorPopupWindowContent.cs.meta (100%) create mode 100644 Assets/Fungus/Scripts/Editor/PopupContent/VariableSelectPopupWindowContent.cs create mode 100644 Assets/Fungus/Scripts/Editor/PopupContent/VariableSelectPopupWindowContent.cs.meta diff --git a/Assets/Fungus/Scripts/Editor/BasePopupWindowContent.cs b/Assets/Fungus/Scripts/Editor/BasePopupWindowContent.cs index 786c95ec..3c7339de 100644 --- a/Assets/Fungus/Scripts/Editor/BasePopupWindowContent.cs +++ b/Assets/Fungus/Scripts/Editor/BasePopupWindowContent.cs @@ -42,7 +42,6 @@ namespace Fungus.EditorUtils public GUIContent content; } - protected Block block; protected int hoverIndex; protected readonly string SEARCH_CONTROL_NAME = "PopupSearchControlName"; protected readonly float ROW_HEIGHT = EditorGUIUtility.singleLineHeight; @@ -55,16 +54,25 @@ namespace Fungus.EditorUtils protected int currentIndex; protected Vector2 size; - static readonly char[] SEARCH_SPLITS = new char[]{ '/', ' ' }; + static readonly char[] SEARCH_SPLITS = new char[]{ CATEGORY_CHAR, ' ' }; + protected static readonly char CATEGORY_CHAR = '/'; - public BasePopupWindowContent(string currentHandlerName, Block block, int width, int height) + public BasePopupWindowContent(string currentHandlerName, int width, int height) { - this.block = block; this.size = new Vector2(width, height); PrepareAllItems(); - allItems.Sort((lhs, rhs) => lhs.lowerName.CompareTo(rhs.lowerName)); + allItems.Sort((lhs, rhs) => + { + //order root level objects first + var islhsRoot = lhs.lowerName.IndexOf(CATEGORY_CHAR) != -1; + var isrhsRoot = rhs.lowerName.IndexOf(CATEGORY_CHAR) != -1; + + if(islhsRoot == isrhsRoot) + return lhs.lowerName.CompareTo(rhs.lowerName); + return islhsRoot ? 1 : -1; + }); UpdateFilter(); currentIndex = Mathf.Max(0, visibleItems.FindIndex(x=>x.name.Contains(currentHandlerName))); hoverIndex = currentIndex; @@ -120,12 +128,13 @@ namespace Fungus.EditorUtils { visibleItems = allItems.Where(x => { + //we want all tokens foreach (var item in lowers) { - if (x.lowerName.Contains(currentFilter)) - return true; + if (!x.lowerName.Contains(item)) + return false; } - return false; + return true; }).ToList(); } @@ -246,13 +255,5 @@ namespace Fungus.EditorUtils } } } - - - //protected void SelectByName(string name) - //{ - // var loc = allItems.FindIndex(x => x.name == name); - // SelectByOrigIndex(loc); - //} - } } \ No newline at end of file diff --git a/Assets/Fungus/Scripts/Editor/FlowchartEditor.cs b/Assets/Fungus/Scripts/Editor/FlowchartEditor.cs index 52d79d61..337d8c24 100644 --- a/Assets/Fungus/Scripts/Editor/FlowchartEditor.cs +++ b/Assets/Fungus/Scripts/Editor/FlowchartEditor.cs @@ -13,11 +13,6 @@ namespace Fungus.EditorUtils [CustomEditor (typeof(Flowchart))] public class FlowchartEditor : Editor { - protected class AddVariableInfo - { - public Flowchart flowchart; - public System.Type variableType; - } protected SerializedProperty descriptionProp; protected SerializedProperty colorCommandsProp; @@ -179,90 +174,15 @@ namespace Fungus.EditorUtils if (!Application.isPlaying && GUI.Button(plusRect, addTexture)) { - GenericMenu menu = new GenericMenu (); - List types = FindAllDerivedTypes(); - - // Add variable types without a category - foreach (var type in types) - { - VariableInfoAttribute variableInfo = VariableEditor.GetVariableInfo(type); - if (variableInfo == null || - variableInfo.Category != "") - { - continue; - } - - AddVariableInfo addVariableInfo = new AddVariableInfo(); - addVariableInfo.flowchart = t; - addVariableInfo.variableType = type; - - GUIContent typeName = new GUIContent(variableInfo.VariableType); - - menu.AddItem(typeName, false, AddVariable, addVariableInfo); - } - - // Add types with a category - foreach (var type in types) - { - VariableInfoAttribute variableInfo = VariableEditor.GetVariableInfo(type); - if (variableInfo == null || - variableInfo.Category == "") - { - continue; - } - - AddVariableInfo info = new AddVariableInfo(); - info.flowchart = t; - info.variableType = type; - - GUIContent typeName = new GUIContent(variableInfo.Category + "/" + variableInfo.VariableType); - - menu.AddItem(typeName, false, AddVariable, info); - } - - menu.ShowAsContext (); + Rect popRect = plusRect; + VariableSelectPopupWindowContent.DoAddVariable(popRect, "", t); } } serializedObject.ApplyModifiedProperties(); } - protected virtual void AddVariable(object obj) - { - AddVariableInfo addVariableInfo = obj as AddVariableInfo; - if (addVariableInfo == null) - { - return; - } - - var flowchart = addVariableInfo.flowchart; - System.Type variableType = addVariableInfo.variableType; - - Undo.RecordObject(flowchart, "Add Variable"); - Variable newVariable = flowchart.gameObject.AddComponent(variableType) as Variable; - newVariable.Key = flowchart.GetUniqueVariableKey(""); - flowchart.Variables.Add(newVariable); - - // Because this is an async call, we need to force prefab instances to record changes - PrefabUtility.RecordPrefabInstancePropertyModifications(flowchart); - } - - public static List FindAllDerivedTypes() - { - return FindAllDerivedTypes(Assembly.GetAssembly(typeof(T))); - } - public static List FindAllDerivedTypes(Assembly assembly) - { - var derivedType = typeof(T); - return assembly - .GetTypes() - .Where(t => - t != derivedType && - derivedType.IsAssignableFrom(t) - ).ToList(); - - } /// /// When modifying custom editor code you can occasionally end up with orphaned editor instances. diff --git a/Assets/Fungus/Scripts/Editor/PopupContent.meta b/Assets/Fungus/Scripts/Editor/PopupContent.meta new file mode 100644 index 00000000..d6f72bbb --- /dev/null +++ b/Assets/Fungus/Scripts/Editor/PopupContent.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: b99cf5f1ea08b914c94ee372bc4d76a7 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Fungus/Scripts/Editor/CommandSelectorPopupWindowContent.cs b/Assets/Fungus/Scripts/Editor/PopupContent/CommandSelectorPopupWindowContent.cs similarity index 86% rename from Assets/Fungus/Scripts/Editor/CommandSelectorPopupWindowContent.cs rename to Assets/Fungus/Scripts/Editor/PopupContent/CommandSelectorPopupWindowContent.cs index 62449ebe..c3ca20b8 100644 --- a/Assets/Fungus/Scripts/Editor/CommandSelectorPopupWindowContent.cs +++ b/Assets/Fungus/Scripts/Editor/PopupContent/CommandSelectorPopupWindowContent.cs @@ -8,16 +8,9 @@ namespace Fungus.EditorUtils { /// /// Searchable Popup Window for adding a command to a block - /// - /// Inspired by https://github.com/roboryantron/UnityEditorJunkie/blob/master/Assets/SearchableEnum/Code/Editor/SearchablePopup.cs /// public class CommandSelectorPopupWindowContent : BasePopupWindowContent { - private static readonly char[] SPLIT_INPUT_ON = new char[] { ' ', '/', '\\' }; - private static readonly int MAX_PREVIEW_GRID = 7; - private static readonly string ELIPSIS = "..."; - - static List commandTypes; static void CacheCommandTypes() @@ -34,8 +27,8 @@ namespace Fungus.EditorUtils static Block curBlock; static protected List> filteredAttributes; - public CommandSelectorPopupWindowContent(string currentHandlerName, Block block, int width, int height) - : base(currentHandlerName, block, width, height) + public CommandSelectorPopupWindowContent(string currentHandlerName, int width, int height) + : base(currentHandlerName, width, height) { } @@ -53,12 +46,12 @@ namespace Fungus.EditorUtils } - filteredAttributes = GetFilteredSupportedCommands(block.GetFlowchart()); + filteredAttributes = GetFilteredSupportedCommands(curBlock.GetFlowchart()); int i = 0; foreach (var item in filteredAttributes) { - allItems.Add(new FilteredListItem(i, (item.Value.Category.Length > 0 ? item.Value.Category + "/" : "") + item.Value.CommandName, item.Value.HelpText)); + allItems.Add(new FilteredListItem(i, (item.Value.Category.Length > 0 ? item.Value.Category + CATEGORY_CHAR : "") + item.Value.CommandName, item.Value.HelpText)); i++; } @@ -68,7 +61,7 @@ namespace Fungus.EditorUtils { curBlock = block; - var win = new CommandSelectorPopupWindowContent(currentHandlerName, block, + var win = new CommandSelectorPopupWindowContent(currentHandlerName, width, (int)(height - EditorGUIUtility.singleLineHeight * 3)); PopupWindow.Show(position, win); @@ -87,14 +80,10 @@ namespace Fungus.EditorUtils return filteredAttributes; } - - static protected void DoOlderMenu() { - var flowchart = (Flowchart)curBlock.GetFlowchart(); - GenericMenu commandMenu = new GenericMenu(); // Build menu list @@ -108,7 +97,7 @@ namespace Fungus.EditorUtils } else { - menuItem = new GUIContent(keyPair.Value.Category + "/" + keyPair.Value.CommandName); + menuItem = new GUIContent(keyPair.Value.Category + CATEGORY_CHAR + keyPair.Value.CommandName); } commandMenu.AddItem(menuItem, false, AddCommandCallback, keyPair.Key); diff --git a/Assets/Fungus/Scripts/Editor/CommandSelectorPopupWindowContent.cs.meta b/Assets/Fungus/Scripts/Editor/PopupContent/CommandSelectorPopupWindowContent.cs.meta similarity index 100% rename from Assets/Fungus/Scripts/Editor/CommandSelectorPopupWindowContent.cs.meta rename to Assets/Fungus/Scripts/Editor/PopupContent/CommandSelectorPopupWindowContent.cs.meta diff --git a/Assets/Fungus/Scripts/Editor/EventSelectorPopupWindowContent.cs b/Assets/Fungus/Scripts/Editor/PopupContent/EventSelectorPopupWindowContent.cs similarity index 94% rename from Assets/Fungus/Scripts/Editor/EventSelectorPopupWindowContent.cs rename to Assets/Fungus/Scripts/Editor/PopupContent/EventSelectorPopupWindowContent.cs index 77d5faf1..af9562ab 100644 --- a/Assets/Fungus/Scripts/Editor/EventSelectorPopupWindowContent.cs +++ b/Assets/Fungus/Scripts/Editor/PopupContent/EventSelectorPopupWindowContent.cs @@ -8,8 +8,6 @@ namespace Fungus.EditorUtils { /// /// Searchable Popup Window for selecting Event type, used by block editor - /// - /// Inspired by https://github.com/roboryantron/UnityEditorJunkie/blob/master/Assets/SearchableEnum/Code/Editor/SearchablePopup.cs /// public class EventSelectorPopupWindowContent : BasePopupWindowContent { @@ -32,10 +30,11 @@ namespace Fungus.EditorUtils public Type eventHandlerType; } - + protected Block block; public EventSelectorPopupWindowContent(string currentHandlerName, Block block, int width, int height) - :base(currentHandlerName, block, width, height) + :base(currentHandlerName, width, height) { + this.block = block; } protected override void PrepareAllItems() @@ -51,7 +50,7 @@ namespace Fungus.EditorUtils EventHandlerInfoAttribute info = EventHandlerEditor.GetEventHandlerInfo(type); if (info != null) { - allItems.Add(new FilteredListItem(i, (info.Category.Length > 0 ? info.Category + "/" : "") + info.EventHandlerName, info.HelpText)); + allItems.Add(new FilteredListItem(i, (info.Category.Length > 0 ? info.Category + CATEGORY_CHAR : "") + info.EventHandlerName, info.HelpText)); } else { diff --git a/Assets/Fungus/Scripts/Editor/EventSelectorPopupWindowContent.cs.meta b/Assets/Fungus/Scripts/Editor/PopupContent/EventSelectorPopupWindowContent.cs.meta similarity index 100% rename from Assets/Fungus/Scripts/Editor/EventSelectorPopupWindowContent.cs.meta rename to Assets/Fungus/Scripts/Editor/PopupContent/EventSelectorPopupWindowContent.cs.meta diff --git a/Assets/Fungus/Scripts/Editor/PopupContent/VariableSelectPopupWindowContent.cs b/Assets/Fungus/Scripts/Editor/PopupContent/VariableSelectPopupWindowContent.cs new file mode 100644 index 00000000..38630489 --- /dev/null +++ b/Assets/Fungus/Scripts/Editor/PopupContent/VariableSelectPopupWindowContent.cs @@ -0,0 +1,125 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEditor; +using System.Linq; + +namespace Fungus.EditorUtils +{ + public class VariableSelectPopupWindowContent : BasePopupWindowContent + { + static readonly int POPUP_WIDTH = 200, POPUP_HEIGHT = 200; + static List types; + + static void CacheVariableTypes() + { + var derivedType = typeof(Variable); + types = EditorExtensions.FindDerivedTypes(derivedType) + .Where(x => !x.IsAbstract && derivedType.IsAssignableFrom(x)) + .ToList(); + } + + [UnityEditor.Callbacks.DidReloadScripts] + private static void OnScriptsReloaded() + { + CacheVariableTypes(); + } + + protected override void PrepareAllItems() + { + if(types == null || types.Count == 0) + { + CacheVariableTypes(); + } + + int i = 0; + foreach (var item in types) + { + VariableInfoAttribute variableInfo = VariableEditor.GetVariableInfo(item); + if (variableInfo != null) + { + allItems.Add(new FilteredListItem(i, (variableInfo.Category.Length > 0 ? variableInfo.Category + CATEGORY_CHAR : "") + variableInfo.VariableType)); + } + + i++; + } + } + + protected override void SelectByOrigIndex(int index) + { + AddVariable(types[index]); + } + + static public void DoAddVariable(Rect position, string currentHandlerName, Flowchart flowchart) + { + curFlowchart = flowchart; + //new method + VariableSelectPopupWindowContent win = new VariableSelectPopupWindowContent(currentHandlerName, POPUP_WIDTH, POPUP_HEIGHT); + PopupWindow.Show(position, win); + + //old method + DoOlderMenu(flowchart); + } + + static protected void DoOlderMenu(Flowchart flowchart) + { + GenericMenu menu = new GenericMenu(); + + // Add variable types without a category + foreach (var type in types) + { + VariableInfoAttribute variableInfo = VariableEditor.GetVariableInfo(type); + if (variableInfo == null || + variableInfo.Category != "") + { + continue; + } + + GUIContent typeName = new GUIContent(variableInfo.VariableType); + + menu.AddItem(typeName, false, AddVariable, type); + } + + // Add types with a category + foreach (var type in types) + { + VariableInfoAttribute variableInfo = VariableEditor.GetVariableInfo(type); + if (variableInfo == null || + variableInfo.Category == "") + { + continue; + } + + GUIContent typeName = new GUIContent(variableInfo.Category + CATEGORY_CHAR + variableInfo.VariableType); + + menu.AddItem(typeName, false, AddVariable, type); + } + + menu.ShowAsContext(); + } + + private static Flowchart curFlowchart; + + public VariableSelectPopupWindowContent(string currentHandlerName, int width, int height) + : base(currentHandlerName, width, height) + { + } + + protected static void AddVariable(object obj) + { + System.Type t = obj as System.Type; + if (t == null) + { + return; + } + + Undo.RecordObject(curFlowchart, "Add Variable"); + Variable newVariable = curFlowchart.gameObject.AddComponent(t) as Variable; + newVariable.Key = curFlowchart.GetUniqueVariableKey(""); + curFlowchart.Variables.Add(newVariable); + + // Because this is an async call, we need to force prefab instances to record changes + PrefabUtility.RecordPrefabInstancePropertyModifications(curFlowchart); + } + } +} \ No newline at end of file diff --git a/Assets/Fungus/Scripts/Editor/PopupContent/VariableSelectPopupWindowContent.cs.meta b/Assets/Fungus/Scripts/Editor/PopupContent/VariableSelectPopupWindowContent.cs.meta new file mode 100644 index 00000000..bbf8feb1 --- /dev/null +++ b/Assets/Fungus/Scripts/Editor/PopupContent/VariableSelectPopupWindowContent.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 933b6fc31e5eac04ea3b2a1fa9a3b418 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: