You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
154 lines
5.5 KiB
154 lines
5.5 KiB
6 years ago
|
using UnityEditor;
|
||
|
using UnityEngine;
|
||
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
|
||
|
namespace Fungus.EditorUtils
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// Searchable Popup Window for selecting Event type, used by block editor
|
||
|
/// </summary>
|
||
6 years ago
|
public class EventSelectorPopupWindowContent : BasePopupWindowContent
|
||
6 years ago
|
{
|
||
6 years ago
|
static List<System.Type> eventHandlerTypes;
|
||
|
|
||
|
static void CacheEventHandlerTypes()
|
||
|
{
|
||
|
eventHandlerTypes = EditorExtensions.FindDerivedTypes(typeof(EventHandler)).Where(x => !x.IsAbstract).ToList();
|
||
|
}
|
||
|
|
||
|
[UnityEditor.Callbacks.DidReloadScripts]
|
||
|
private static void OnScriptsReloaded()
|
||
|
{
|
||
|
CacheEventHandlerTypes();
|
||
|
}
|
||
|
|
||
6 years ago
|
protected class SetEventHandlerOperation
|
||
|
{
|
||
|
public Block block;
|
||
|
public Type eventHandlerType;
|
||
|
}
|
||
|
|
||
6 years ago
|
protected Block block;
|
||
6 years ago
|
public EventSelectorPopupWindowContent(string currentHandlerName, Block block, int width, int height)
|
||
6 years ago
|
:base(currentHandlerName, width, height)
|
||
6 years ago
|
{
|
||
6 years ago
|
this.block = block;
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
protected override void PrepareAllItems()
|
||
6 years ago
|
{
|
||
6 years ago
|
if (eventHandlerTypes == null || eventHandlerTypes.Count == 0)
|
||
|
{
|
||
|
CacheEventHandlerTypes();
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
int i = 0;
|
||
6 years ago
|
foreach (System.Type type in eventHandlerTypes)
|
||
|
{
|
||
|
EventHandlerInfoAttribute info = EventHandlerEditor.GetEventHandlerInfo(type);
|
||
|
if (info != null)
|
||
|
{
|
||
6 years ago
|
allItems.Add(new FilteredListItem(i, (info.Category.Length > 0 ? info.Category + CATEGORY_CHAR : "") + info.EventHandlerName, info.HelpText));
|
||
6 years ago
|
}
|
||
|
else
|
||
|
{
|
||
6 years ago
|
allItems.Add(new FilteredListItem(i, type.Name, info.HelpText));
|
||
6 years ago
|
}
|
||
6 years ago
|
|
||
|
i++;
|
||
6 years ago
|
}
|
||
|
}
|
||
6 years ago
|
|
||
|
override protected void SelectByOrigIndex(int index)
|
||
6 years ago
|
{
|
||
6 years ago
|
SetEventHandlerOperation operation = new SetEventHandlerOperation();
|
||
|
operation.block = block;
|
||
|
operation.eventHandlerType = (index >= 0 && index < eventHandlerTypes.Count) ? eventHandlerTypes[index] : null;
|
||
|
OnSelectEventHandler(operation);
|
||
6 years ago
|
}
|
||
|
|
||
|
|
||
6 years ago
|
static public void DoEventHandlerPopUp(Rect position, string currentHandlerName, Block block, int width, int height)
|
||
6 years ago
|
{
|
||
|
//new method
|
||
6 years ago
|
EventSelectorPopupWindowContent win = new EventSelectorPopupWindowContent(currentHandlerName, block, width, height);
|
||
6 years ago
|
PopupWindow.Show(position, win);
|
||
6 years ago
|
|
||
|
//old method
|
||
6 years ago
|
DoOlderMenu(block);
|
||
|
}
|
||
|
|
||
|
static protected void DoOlderMenu(Block block)
|
||
|
{
|
||
6 years ago
|
|
||
6 years ago
|
SetEventHandlerOperation noneOperation = new SetEventHandlerOperation();
|
||
|
noneOperation.block = block;
|
||
|
noneOperation.eventHandlerType = null;
|
||
|
|
||
|
GenericMenu eventHandlerMenu = new GenericMenu();
|
||
|
eventHandlerMenu.AddItem(new GUIContent("None"), false, OnSelectEventHandler, noneOperation);
|
||
|
|
||
|
// Add event handlers with no category first
|
||
|
foreach (System.Type type in eventHandlerTypes)
|
||
|
{
|
||
|
EventHandlerInfoAttribute info = EventHandlerEditor.GetEventHandlerInfo(type);
|
||
|
if (info != null &&
|
||
|
info.Category.Length == 0)
|
||
|
{
|
||
|
SetEventHandlerOperation operation = new SetEventHandlerOperation();
|
||
|
operation.block = block;
|
||
|
operation.eventHandlerType = type;
|
||
|
|
||
|
eventHandlerMenu.AddItem(new GUIContent(info.EventHandlerName), false, OnSelectEventHandler, operation);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Add event handlers with a category afterwards
|
||
|
foreach (System.Type type in eventHandlerTypes)
|
||
|
{
|
||
|
EventHandlerInfoAttribute info = EventHandlerEditor.GetEventHandlerInfo(type);
|
||
|
if (info != null &&
|
||
|
info.Category.Length > 0)
|
||
|
{
|
||
|
SetEventHandlerOperation operation = new SetEventHandlerOperation();
|
||
|
operation.block = block;
|
||
|
operation.eventHandlerType = type;
|
||
|
string typeName = info.Category + "/" + info.EventHandlerName;
|
||
|
eventHandlerMenu.AddItem(new GUIContent(typeName), false, OnSelectEventHandler, operation);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
eventHandlerMenu.ShowAsContext();
|
||
|
}
|
||
|
|
||
|
static protected void OnSelectEventHandler(object obj)
|
||
|
{
|
||
|
SetEventHandlerOperation operation = obj as SetEventHandlerOperation;
|
||
|
Block block = operation.block;
|
||
|
System.Type selectedType = operation.eventHandlerType;
|
||
|
if (block == null)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
Undo.RecordObject(block, "Set Event Handler");
|
||
|
|
||
|
if (block._EventHandler != null)
|
||
|
{
|
||
|
Undo.DestroyObjectImmediate(block._EventHandler);
|
||
|
}
|
||
|
|
||
|
if (selectedType != null)
|
||
|
{
|
||
|
EventHandler newHandler = Undo.AddComponent(block.gameObject, selectedType) as EventHandler;
|
||
|
newHandler.ParentBlock = block;
|
||
|
block._EventHandler = newHandler;
|
||
|
}
|
||
|
|
||
|
// Because this is an async call, we need to force prefab instances to record changes
|
||
|
PrefabUtility.RecordPrefabInstancePropertyModifications(block);
|
||
|
}
|
||
|
}
|
||
|
}
|