Browse Source

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
master
desktop-maesty/steve 7 years ago
parent
commit
f4adf025b0
  1. 4
      Assets/Fungus/Scripts/Editor/BlockEditor.cs
  2. 102
      Assets/Fungus/Scripts/Editor/EventSelectorPopupWindowContent.cs

4
Assets/Fungus/Scripts/Editor/BlockEditor.cs

@ -47,8 +47,8 @@ namespace Fungus.EditorUtils
static void CacheEventHandlerTypes() static void CacheEventHandlerTypes()
{ {
eventHandlerTypes = EditorExtensions.FindDerivedTypes(typeof(EventHandler)).ToList(); eventHandlerTypes = EditorExtensions.FindDerivedTypes(typeof(EventHandler)).Where(x=>!x.IsAbstract).ToList();
commandTypes = EditorExtensions.FindDerivedTypes(typeof(Command)).ToList(); commandTypes = EditorExtensions.FindDerivedTypes(typeof(Command)).Where(x => !x.IsAbstract).ToList();
} }
[UnityEditor.Callbacks.DidReloadScripts] [UnityEditor.Callbacks.DidReloadScripts]

102
Assets/Fungus/Scripts/Editor/EventSelectorPopupWindowContent.cs

@ -25,38 +25,56 @@ namespace Fungus.EditorUtils
public Type eventHandlerType; 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 string currentHandlerName;
private Block block; private Block block;
private List<Type> eventHandlerTypes; private List<Type> eventHandlerTypes;
private int hoverIndex; private int hoverIndex;
private readonly string SEARCH_CONTROL_NAME = "PopupSearchControlName"; private readonly string SEARCH_CONTROL_NAME = "PopupSearchControlName";
private readonly float ROW_HEIGHT = EditorGUIUtility.singleLineHeight; private readonly float ROW_HEIGHT = EditorGUIUtility.singleLineHeight;
private List<string> allItems = new List<string>(), visibleItems = new List<string>(); private List<FilteredListItem> allItems = new List<FilteredListItem>(), visibleItems = new List<FilteredListItem>();
private string currentFilter; private string currentFilter = string.Empty;
private Vector2 scroll; private Vector2 scroll;
private int scrollToIndex; private int scrollToIndex;
private float scrollOffset; private float scrollOffset;
private int currentIndex; private int currentIndex;
static readonly char[] SEARCH_SPLITS = new char[]{ '/', ' ' };
public EventSelectorPopupWindowContent(string currentHandlerName, Block block, List<Type> eventHandlerTypes) public EventSelectorPopupWindowContent(string currentHandlerName, Block block, List<Type> eventHandlerTypes)
{ {
this.currentHandlerName = currentHandlerName; this.currentHandlerName = currentHandlerName;
this.block = block; this.block = block;
this.eventHandlerTypes = eventHandlerTypes; this.eventHandlerTypes = eventHandlerTypes;
int i = 0;
foreach (System.Type type in eventHandlerTypes) foreach (System.Type type in eventHandlerTypes)
{ {
EventHandlerInfoAttribute info = EventHandlerEditor.GetEventHandlerInfo(type); EventHandlerInfoAttribute info = EventHandlerEditor.GetEventHandlerInfo(type);
if (info != null) if (info != null)
{ {
allItems.Add(info.Category + "/" + info.EventHandlerName); allItems.Add(new FilteredListItem(i, (info.Category.Length > 0 ? info.Category + "/" : "") + info.EventHandlerName));
} }
else 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) public override void OnGUI(Rect rect)
@ -82,24 +100,41 @@ namespace Fungus.EditorUtils
GUI.FocusControl(SEARCH_CONTROL_NAME); GUI.FocusControl(SEARCH_CONTROL_NAME);
GUI.SetNextControlName(SEARCH_CONTROL_NAME); GUI.SetNextControlName(SEARCH_CONTROL_NAME);
string newText = GUI.TextField(searchRect, "FilterBy"); var prevFilter = currentFilter;
currentFilter = GUI.TextField(searchRect, currentFilter);
//if (list.UpdateFilter(newText))
//{ if (prevFilter != currentFilter)
// hoverIndex = 0; {
// scroll = Vector2.zero; UpdateFilter();
//} }
}
//searchRect.x = searchRect.xMax;
//searchRect.width = CancelButton.fixedWidth; private void UpdateFilter()
{
//if (string.IsNullOrEmpty(list.Filter)) var curlower = currentFilter.ToLowerInvariant();
// GUI.Box(searchRect, GUIContent.none, DisabledCancelButton); var lowers = curlower.Split(SEARCH_SPLITS);
//else if (GUI.Button(searchRect, "x", CancelButton)) lowers = lowers.Where(x => x.Length > 0).ToArray();
//{
// list.UpdateFilter(""); if (lowers == null || lowers.Length == 0)
// scroll = Vector2.zero; {
//} 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) private void DrawSelectionArea(Rect scrollRect)
@ -133,7 +168,7 @@ namespace Fungus.EditorUtils
if (Event.current.type == EventType.MouseDown) if (Event.current.type == EventType.MouseDown)
{ {
//onSelectionMade(list.Entries[i].Index); //onSelectionMade(list.Entries[i].Index);
SelectByName(visibleItems[i]); SelectByOrigIndex(visibleItems[i].origIndex);
EditorWindow.focusedWindow.Close(); EditorWindow.focusedWindow.Close();
} }
} }
@ -164,7 +199,7 @@ namespace Fungus.EditorUtils
Rect labelRect = new Rect(rowRect); Rect labelRect = new Rect(rowRect);
//labelRect.xMin += ROW_INDENT; //labelRect.xMin += ROW_INDENT;
GUI.Label(labelRect, visibleItems[i]); GUI.Label(labelRect, visibleItems[i].name);
} }
/// <summary> /// <summary>
@ -194,7 +229,7 @@ namespace Fungus.EditorUtils
{ {
if (hoverIndex >= 0 && hoverIndex < visibleItems.Count) if (hoverIndex >= 0 && hoverIndex < visibleItems.Count)
{ {
SelectByName(visibleItems[hoverIndex]); SelectByOrigIndex(visibleItems[hoverIndex].origIndex);
//onSelectionMade(list.Entries[hoverIndex].Index); //onSelectionMade(list.Entries[hoverIndex].Index);
EditorWindow.focusedWindow.Close(); EditorWindow.focusedWindow.Close();
} }
@ -252,10 +287,7 @@ namespace Fungus.EditorUtils
} }
} }
eventHandlerMenu.ShowAsContext(); eventHandlerMenu.ShowAsContext();
} }
@ -289,10 +321,18 @@ namespace Fungus.EditorUtils
protected void SelectByName(string name) 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(); SetEventHandlerOperation operation = new SetEventHandlerOperation();
operation.block = block; operation.block = block;
operation.eventHandlerType = eventHandlerTypes[loc]; operation.eventHandlerType = (index >= 0 && index<eventHandlerTypes.Count) ? eventHandlerTypes[index] : null;
OnSelectEventHandler(operation); OnSelectEventHandler(operation);
} }
} }

Loading…
Cancel
Save