public void AddItem(GUIContent content, bool on, PopupMenu.MenuFunctionData func, object userData) { popup.AddItem(content, on, func, userData); }
public static void SetupInput() { eventHandlers = new List <KeyValuePair <EventHandlerAttribute, Delegate> >(); hotkeyHandlers = new List <KeyValuePair <HotkeyAttribute, Delegate> >(); contextEntries = new List <KeyValuePair <ContextEntryAttribute, PopupMenu.MenuFunctionData> >(); contextFillers = new List <KeyValuePair <ContextFillerAttribute, Delegate> >(); IEnumerable <Assembly> enumerable = from assembly in AppDomain.CurrentDomain.GetAssemblies() where assembly.FullName.Contains("Assembly") select assembly; foreach (Assembly item in enumerable) { Type[] types = item.GetTypes(); foreach (Type type in types) { MethodInfo[] methods = type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy); foreach (MethodInfo methodInfo in methods) { Delegate actionDelegate = null; object[] customAttributes = methodInfo.GetCustomAttributes(true); foreach (object obj in customAttributes) { Type type2 = obj.GetType(); if (type2 == typeof(EventHandlerAttribute)) { if (EventHandlerAttribute.AssureValidity(methodInfo, obj as EventHandlerAttribute)) { if ((object)actionDelegate == null) { actionDelegate = Delegate.CreateDelegate(typeof(Action <NodeEditorInputInfo>), methodInfo); } eventHandlers.Add(new KeyValuePair <EventHandlerAttribute, Delegate>(obj as EventHandlerAttribute, actionDelegate)); } } else if (type2 == typeof(HotkeyAttribute)) { if (HotkeyAttribute.AssureValidity(methodInfo, obj as HotkeyAttribute)) { if ((object)actionDelegate == null) { actionDelegate = Delegate.CreateDelegate(typeof(Action <NodeEditorInputInfo>), methodInfo); } hotkeyHandlers.Add(new KeyValuePair <HotkeyAttribute, Delegate>(obj as HotkeyAttribute, actionDelegate)); } } else if (type2 == typeof(ContextEntryAttribute)) { if (ContextEntryAttribute.AssureValidity(methodInfo, obj as ContextEntryAttribute)) { if ((object)actionDelegate == null) { actionDelegate = Delegate.CreateDelegate(typeof(Action <NodeEditorInputInfo>), methodInfo); } PopupMenu.MenuFunctionData value = delegate(object callbackObj) { if (!(callbackObj is NodeEditorInputInfo)) { throw new UnityException("Callback Object passed by context is not of type NodeEditorMenuCallback!"); } actionDelegate.DynamicInvoke(callbackObj as NodeEditorInputInfo); }; contextEntries.Add(new KeyValuePair <ContextEntryAttribute, PopupMenu.MenuFunctionData>(obj as ContextEntryAttribute, value)); } } else if (type2 == typeof(ContextFillerAttribute) && ContextFillerAttribute.AssureValidity(methodInfo, obj as ContextFillerAttribute)) { Delegate value2 = Delegate.CreateDelegate(typeof(Action <NodeEditorInputInfo, GenericMenu>), methodInfo); contextFillers.Add(new KeyValuePair <ContextFillerAttribute, Delegate>(obj as ContextFillerAttribute, value2)); } } } } } eventHandlers.Sort((KeyValuePair <EventHandlerAttribute, Delegate> handlerA, KeyValuePair <EventHandlerAttribute, Delegate> handlerB) => handlerA.Key.priority.CompareTo(handlerB.Key.priority)); hotkeyHandlers.Sort((KeyValuePair <HotkeyAttribute, Delegate> handlerA, KeyValuePair <HotkeyAttribute, Delegate> handlerB) => handlerA.Key.priority.CompareTo(handlerB.Key.priority)); }
/// <summary> /// Fetches all event handlers /// </summary> public static void SetupInput() { eventHandlers = new List <KeyValuePair <EventHandlerAttribute, Delegate> > (); hotkeyHandlers = new List <KeyValuePair <HotkeyAttribute, Delegate> > (); contextEntries = new List <KeyValuePair <ContextEntryAttribute, PopupMenu.MenuFunctionData> > (); contextFillers = new List <KeyValuePair <ContextFillerAttribute, Delegate> > (); // Iterate through each static method IEnumerable <Assembly> scriptAssemblies = AppDomain.CurrentDomain.GetAssemblies().Where((Assembly assembly) => assembly.FullName.Contains("Assembly")); foreach (Assembly assembly in scriptAssemblies) { foreach (Type type in assembly.GetTypes()) { foreach (MethodInfo method in type.GetMethods(BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static)) { #region Event-Attributes recognition and storing // Check the method's attributes for input handler definitions Delegate actionDelegate = null; foreach (object attr in method.GetCustomAttributes(true)) { Type attrType = attr.GetType(); if (attrType == typeof(EventHandlerAttribute)) { // Method is an eventHandler if (EventHandlerAttribute.AssureValidity(method, attr as EventHandlerAttribute)) { // Method signature is correct, so we register this handler if (actionDelegate == null) { actionDelegate = Delegate.CreateDelegate(typeof(Action <NodeEditorInputInfo>), method); } eventHandlers.Add(new KeyValuePair <EventHandlerAttribute, Delegate> (attr as EventHandlerAttribute, actionDelegate)); } } else if (attrType == typeof(HotkeyAttribute)) { // Method is an hotkeyHandler if (HotkeyAttribute.AssureValidity(method, attr as HotkeyAttribute)) { // Method signature is correct, so we register this handler if (actionDelegate == null) { actionDelegate = Delegate.CreateDelegate(typeof(Action <NodeEditorInputInfo>), method); } hotkeyHandlers.Add(new KeyValuePair <HotkeyAttribute, Delegate> (attr as HotkeyAttribute, actionDelegate)); } } else if (attrType == typeof(ContextEntryAttribute)) { // Method is an contextEntry if (ContextEntryAttribute.AssureValidity(method, attr as ContextEntryAttribute)) { // Method signature is correct, so we register this handler if (actionDelegate == null) { actionDelegate = Delegate.CreateDelegate(typeof(Action <NodeEditorInputInfo>), method); } // Create a proper MenuFunction as a wrapper for the delegate that converts the object to NodeEditorInputInfo PopupMenu.MenuFunctionData menuFunction = (object callbackObj) => { if (!(callbackObj is NodeEditorInputInfo)) { throw new UnityException("Callback Object passed by context is not of type NodeEditorMenuCallback!"); } actionDelegate.DynamicInvoke(callbackObj as NodeEditorInputInfo); }; contextEntries.Add(new KeyValuePair <ContextEntryAttribute, PopupMenu.MenuFunctionData> (attr as ContextEntryAttribute, menuFunction)); } } else if (attrType == typeof(ContextFillerAttribute)) { // Method is an contextFiller if (ContextFillerAttribute.AssureValidity(method, attr as ContextFillerAttribute)) { // Method signature is correct, so we register this handler Delegate methodDel = Delegate.CreateDelegate(typeof(Action <NodeEditorInputInfo, GenericMenu>), method); contextFillers.Add(new KeyValuePair <ContextFillerAttribute, Delegate> (attr as ContextFillerAttribute, methodDel)); } } } #endregion } } } eventHandlers.Sort((handlerA, handlerB) => handlerA.Key.priority.CompareTo(handlerB.Key.priority)); hotkeyHandlers.Sort((handlerA, handlerB) => handlerA.Key.priority.CompareTo(handlerB.Key.priority)); }
public static void SetupInput() { _eventHandlers = new List <KeyValuePair <EventHandlerAttribute, Delegate> >(); _contextEntries = new List <KeyValuePair <ContextEntryAttribute, PopupMenu.MenuFunctionData> >(); _contextFillers = new List <KeyValuePair <ContextFillerAttribute, Delegate> >(); var scriptAssemblies = AppDomain.CurrentDomain.GetAssemblies() .Where(assembly => assembly.FullName.Contains("Assembly")); foreach (var assembly in scriptAssemblies) { foreach (var type in assembly.GetTypes()) { foreach (var method in type.GetMethods(BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static)) { #region Event Attributes recognition and storing Delegate actionDelegate = null; foreach (var attr in method.GetCustomAttributes(true)) { var attrType = attr.GetType(); if (attrType == typeof(EventHandlerAttribute)) { if (EventHandlerAttribute.IsValid(method, attr as EventHandlerAttribute)) { if (actionDelegate == null) { actionDelegate = Delegate.CreateDelegate(typeof(Action <FolderManagerInputInfo>), method); } _eventHandlers.Add( new KeyValuePair <EventHandlerAttribute, Delegate>(attr as EventHandlerAttribute, actionDelegate)); } } else if (attrType == typeof(ContextEntryAttribute)) { if (ContextEntryAttribute.IsValid(method, attr as ContextEntryAttribute)) { if (actionDelegate == null) { actionDelegate = Delegate.CreateDelegate(typeof(Action <FolderManagerInputInfo>), method); } PopupMenu.MenuFunctionData menuFunction = callbackObj => { if (!(callbackObj is FolderManagerInputInfo)) { throw new UnityException( "Callback Object passed by context is not of type NodeEditorMenuCallback!"); } actionDelegate.DynamicInvoke((FolderManagerInputInfo)callbackObj); }; _contextEntries.Add( new KeyValuePair <ContextEntryAttribute, PopupMenu.MenuFunctionData>( attr as ContextEntryAttribute, menuFunction)); } } else if (attrType == typeof(ContextFillerAttribute)) { if (ContextFillerAttribute.IsValid(method, attr as ContextFillerAttribute)) { var methodDel = Delegate.CreateDelegate(typeof(Action <FolderManagerInputInfo, GenericMenu>), method); _contextFillers.Add( new KeyValuePair <ContextFillerAttribute, Delegate>(attr as ContextFillerAttribute, methodDel)); } } } #endregion } } } _eventHandlers.Sort((handlerA, handlerB) => handlerA.Key.Priority.CompareTo(handlerB.Key.Priority)); }