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:
parent
4e1d9b6721
commit
f8ba6339c8
@ -77,18 +77,28 @@ namespace XNodeEditor {
|
|||||||
foreach (Assembly assembly in assemblies) {
|
foreach (Assembly assembly in assemblies) {
|
||||||
try {
|
try {
|
||||||
types.AddRange(assembly.GetTypes().Where(t => !t.IsAbstract && baseType.IsAssignableFrom(t)).ToArray());
|
types.AddRange(assembly.GetTypes().Where(t => !t.IsAbstract && baseType.IsAssignableFrom(t)).ToArray());
|
||||||
} catch(ReflectionTypeLoadException) {}
|
} catch (ReflectionTypeLoadException) { }
|
||||||
}
|
}
|
||||||
return types.ToArray();
|
return types.ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
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];
|
||||||
contextMenu.AddItem(new GUIContent(kvp.Key.menuItem), false, () => kvp.Value.Invoke(obj, null));
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user