1
0
mirror of https://github.com/Siccity/xNode.git synced 2025-12-20 09:16:01 +08:00

Added validation support for Context Menu items (#162)

* Added validation support for Context Menu items
This commit is contained in:
Hao Wu 2019-06-26 16:25:01 +08:00 committed by Thor Brigsted
parent 4e1d9b6721
commit f8ba6339c8

View File

@ -83,15 +83,25 @@ namespace XNodeEditor {
} }
public static void AddCustomContextMenuItems(GenericMenu contextMenu, object obj) { public static void AddCustomContextMenuItems(GenericMenu contextMenu, object obj) {
KeyValuePair<ContextMenu, System.Reflection.MethodInfo>[] items = GetContextMenuMethods(obj); KeyValuePair<ContextMenu, MethodInfo>[] items = GetContextMenuMethods(obj);
if (items.Length != 0) { if (items.Length != 0) {
contextMenu.AddSeparator(""); contextMenu.AddSeparator("");
List<string> invalidatedEntries = new List<string>();
foreach (KeyValuePair<ContextMenu, MethodInfo> checkValidate in items) {
if (checkValidate.Key.validate && !(bool) checkValidate.Value.Invoke(obj, null)) {
invalidatedEntries.Add(checkValidate.Key.menuItem);
}
}
for (int i = 0; i < items.Length; i++) { for (int i = 0; i < items.Length; i++) {
KeyValuePair<ContextMenu, System.Reflection.MethodInfo> kvp = items[i]; KeyValuePair<ContextMenu, MethodInfo> kvp = items[i];
if (invalidatedEntries.Contains(kvp.Key.menuItem)) {
contextMenu.AddDisabledItem(new GUIContent(kvp.Key.menuItem));
} else {
contextMenu.AddItem(new GUIContent(kvp.Key.menuItem), false, () => kvp.Value.Invoke(obj, null)); contextMenu.AddItem(new GUIContent(kvp.Key.menuItem), false, () => kvp.Value.Invoke(obj, null));
} }
} }
} }
}
public static KeyValuePair<ContextMenu, MethodInfo>[] GetContextMenuMethods(object obj) { public static KeyValuePair<ContextMenu, MethodInfo>[] GetContextMenuMethods(object obj) {
Type type = obj.GetType(); Type type = obj.GetType();