From f4adf025b0df20f0ee3542705c351c1482570706 Mon Sep 17 00:00:00 2001 From: desktop-maesty/steve Date: Mon, 30 Jul 2018 21:00:20 +1000 Subject: [PATCH] ExceuteOnEvent dropdown is searchable -can double click to circumvent the search drop down -can remove event by setting None -use search similar to add command method --- Assets/Fungus/Scripts/Editor/BlockEditor.cs | 4 +- .../Editor/EventSelectorPopupWindowContent.cs | 104 ++++++++++++------ 2 files changed, 74 insertions(+), 34 deletions(-) diff --git a/Assets/Fungus/Scripts/Editor/BlockEditor.cs b/Assets/Fungus/Scripts/Editor/BlockEditor.cs index cb095f28..2fa6266c 100644 --- a/Assets/Fungus/Scripts/Editor/BlockEditor.cs +++ b/Assets/Fungus/Scripts/Editor/BlockEditor.cs @@ -47,8 +47,8 @@ namespace Fungus.EditorUtils static void CacheEventHandlerTypes() { - eventHandlerTypes = EditorExtensions.FindDerivedTypes(typeof(EventHandler)).ToList(); - commandTypes = EditorExtensions.FindDerivedTypes(typeof(Command)).ToList(); + eventHandlerTypes = EditorExtensions.FindDerivedTypes(typeof(EventHandler)).Where(x=>!x.IsAbstract).ToList(); + commandTypes = EditorExtensions.FindDerivedTypes(typeof(Command)).Where(x => !x.IsAbstract).ToList(); } [UnityEditor.Callbacks.DidReloadScripts] diff --git a/Assets/Fungus/Scripts/Editor/EventSelectorPopupWindowContent.cs b/Assets/Fungus/Scripts/Editor/EventSelectorPopupWindowContent.cs index d805964d..a09b474c 100644 --- a/Assets/Fungus/Scripts/Editor/EventSelectorPopupWindowContent.cs +++ b/Assets/Fungus/Scripts/Editor/EventSelectorPopupWindowContent.cs @@ -25,38 +25,56 @@ namespace Fungus.EditorUtils public Type eventHandlerType; } + public class FilteredListItem + { + public FilteredListItem(int index, string str) + { + origIndex = index; + name = str; + lowerName = str.ToLowerInvariant(); + } + public int origIndex; + public string name, lowerName; + } + private string currentHandlerName; private Block block; private List eventHandlerTypes; private int hoverIndex; private readonly string SEARCH_CONTROL_NAME = "PopupSearchControlName"; private readonly float ROW_HEIGHT = EditorGUIUtility.singleLineHeight; - private List allItems = new List(), visibleItems = new List(); - private string currentFilter; + private List allItems = new List(), visibleItems = new List(); + private string currentFilter = string.Empty; private Vector2 scroll; private int scrollToIndex; private float scrollOffset; private int currentIndex; + static readonly char[] SEARCH_SPLITS = new char[]{ '/', ' ' }; + public EventSelectorPopupWindowContent(string currentHandlerName, Block block, List eventHandlerTypes) { this.currentHandlerName = currentHandlerName; this.block = block; this.eventHandlerTypes = eventHandlerTypes; + int i = 0; foreach (System.Type type in eventHandlerTypes) { EventHandlerInfoAttribute info = EventHandlerEditor.GetEventHandlerInfo(type); if (info != null) { - allItems.Add(info.Category + "/" + info.EventHandlerName); + allItems.Add(new FilteredListItem(i, (info.Category.Length > 0 ? info.Category + "/" : "") + info.EventHandlerName)); } else { - allItems.Add(type.Name); + allItems.Add(new FilteredListItem(i, type.Name)); } + + i++; } - visibleItems.AddRange(allItems); + allItems.Sort((lhs, rhs) => lhs.name.CompareTo(rhs.name)); + UpdateFilter(); } public override void OnGUI(Rect rect) @@ -82,24 +100,41 @@ namespace Fungus.EditorUtils GUI.FocusControl(SEARCH_CONTROL_NAME); GUI.SetNextControlName(SEARCH_CONTROL_NAME); - string newText = GUI.TextField(searchRect, "FilterBy"); - - //if (list.UpdateFilter(newText)) - //{ - // hoverIndex = 0; - // scroll = Vector2.zero; - //} - - //searchRect.x = searchRect.xMax; - //searchRect.width = CancelButton.fixedWidth; - - //if (string.IsNullOrEmpty(list.Filter)) - // GUI.Box(searchRect, GUIContent.none, DisabledCancelButton); - //else if (GUI.Button(searchRect, "x", CancelButton)) - //{ - // list.UpdateFilter(""); - // scroll = Vector2.zero; - //} + var prevFilter = currentFilter; + currentFilter = GUI.TextField(searchRect, currentFilter); + + if (prevFilter != currentFilter) + { + UpdateFilter(); + } + } + + private void UpdateFilter() + { + var curlower = currentFilter.ToLowerInvariant(); + var lowers = curlower.Split(SEARCH_SPLITS); + lowers = lowers.Where(x => x.Length > 0).ToArray(); + + if (lowers == null || lowers.Length == 0) + { + visibleItems.AddRange(allItems); + } + else + { + visibleItems = allItems.Where(x => + { + foreach (var item in lowers) + { + if (x.lowerName.Contains(currentFilter)) + return true; + } + return false; + }).ToList(); + } + + hoverIndex = 0; + scroll = Vector2.zero; + visibleItems.Insert(0, new FilteredListItem(-1, "None")); } private void DrawSelectionArea(Rect scrollRect) @@ -133,7 +168,7 @@ namespace Fungus.EditorUtils if (Event.current.type == EventType.MouseDown) { //onSelectionMade(list.Entries[i].Index); - SelectByName(visibleItems[i]); + SelectByOrigIndex(visibleItems[i].origIndex); EditorWindow.focusedWindow.Close(); } } @@ -164,7 +199,7 @@ namespace Fungus.EditorUtils Rect labelRect = new Rect(rowRect); //labelRect.xMin += ROW_INDENT; - GUI.Label(labelRect, visibleItems[i]); + GUI.Label(labelRect, visibleItems[i].name); } /// @@ -194,7 +229,7 @@ namespace Fungus.EditorUtils { if (hoverIndex >= 0 && hoverIndex < visibleItems.Count) { - SelectByName(visibleItems[hoverIndex]); + SelectByOrigIndex(visibleItems[hoverIndex].origIndex); //onSelectionMade(list.Entries[hoverIndex].Index); EditorWindow.focusedWindow.Close(); } @@ -214,7 +249,7 @@ namespace Fungus.EditorUtils PopupWindow.Show(rect, win); //old method - + SetEventHandlerOperation noneOperation = new SetEventHandlerOperation(); noneOperation.block = block; noneOperation.eventHandlerType = null; @@ -252,10 +287,7 @@ namespace Fungus.EditorUtils } } - eventHandlerMenu.ShowAsContext(); - - } @@ -289,10 +321,18 @@ namespace Fungus.EditorUtils protected void SelectByName(string name) { - var loc = allItems.IndexOf(name); + var loc = allItems.First(x => x.name == name); + SetEventHandlerOperation operation = new SetEventHandlerOperation(); + operation.block = block; + operation.eventHandlerType = eventHandlerTypes[loc.origIndex]; + OnSelectEventHandler(operation); + } + + protected void SelectByOrigIndex(int index) + { SetEventHandlerOperation operation = new SetEventHandlerOperation(); operation.block = block; - operation.eventHandlerType = eventHandlerTypes[loc]; + operation.eventHandlerType = (index >= 0 && index