1
0
mirror of https://github.com/Siccity/xNode.git synced 2026-03-26 22:49:02 +08:00

Moved ShowNodeContextMenu to NodeEditor.GetContextMenu to enable more control

This commit is contained in:
Thor Brigsted 2018-12-12 10:24:24 +01:00
parent a0eee5b9ca
commit 8d09e54fd8
4 changed files with 33 additions and 34 deletions

View File

@ -81,6 +81,27 @@ namespace XNodeEditor {
return NodeEditorResources.styles.nodeBody;
}
/// <summary> Show right-click context menu for selected nodes </summary>
public virtual GenericMenu GetContextMenu() {
GenericMenu contextMenu = new GenericMenu();
// If only one node is selected
if (Selection.objects.Length == 1 && Selection.activeObject is XNode.Node) {
XNode.Node node = Selection.activeObject as XNode.Node;
contextMenu.AddItem(new GUIContent("Move To Top"), false, () => NodeEditorWindow.current.MoveNodeToTop(node));
contextMenu.AddItem(new GUIContent("Rename"), false, NodeEditorWindow.current.RenameSelectedNode);
}
contextMenu.AddItem(new GUIContent("Duplicate"), false, NodeEditorWindow.current.DuplicateSelectedNodes);
contextMenu.AddItem(new GUIContent("Remove"), false, NodeEditorWindow.current.RemoveSelectedNodes);
// If only one node is selected
if (Selection.objects.Length == 1 && Selection.activeObject is XNode.Node) {
XNode.Node node = Selection.activeObject as XNode.Node;
NodeEditorWindow.AddCustomContextMenuItems(contextMenu, node);
}
return contextMenu;
}
public void InitiateRename() {
renaming = 1;
}

View File

@ -260,7 +260,7 @@ namespace XNodeEditor {
ShowPortContextMenu(hoveredPort);
} else if (IsHoveringNode && IsHoveringTitle(hoveredNode)) {
if (!Selection.Contains(hoveredNode)) SelectNode(hoveredNode, false);
ShowNodeContextMenu();
NodeEditor.GetEditor(hoveredNode).GetContextMenu().DropDown(new Rect(Event.current.mousePosition, Vector2.zero));
} else if (!IsHoveringNode) {
ShowGraphContextMenu();
}

View File

@ -116,28 +116,6 @@ namespace XNodeEditor {
if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
}
/// <summary> Show right-click context menu for selected nodes </summary>
public void ShowNodeContextMenu() {
GenericMenu contextMenu = new GenericMenu();
// If only one node is selected
if (Selection.objects.Length == 1 && Selection.activeObject is XNode.Node) {
XNode.Node node = Selection.activeObject as XNode.Node;
contextMenu.AddItem(new GUIContent("Move To Top"), false, () => MoveNodeToTop(node));
contextMenu.AddItem(new GUIContent("Rename"), false, RenameSelectedNode);
}
contextMenu.AddItem(new GUIContent("Duplicate"), false, DuplicateSelectedNodes);
contextMenu.AddItem(new GUIContent("Remove"), false, RemoveSelectedNodes);
// If only one node is selected
if (Selection.objects.Length == 1 && Selection.activeObject is XNode.Node) {
XNode.Node node = Selection.activeObject as XNode.Node;
AddCustomContextMenuItems(contextMenu, node);
}
contextMenu.DropDown(new Rect(Event.current.mousePosition, Vector2.zero));
}
/// <summary> Show right-click context menu for current graph </summary>
void ShowGraphContextMenu() {
GenericMenu contextMenu = new GenericMenu();
@ -159,17 +137,6 @@ namespace XNodeEditor {
contextMenu.DropDown(new Rect(Event.current.mousePosition, Vector2.zero));
}
void AddCustomContextMenuItems(GenericMenu contextMenu, object obj) {
KeyValuePair<ContextMenu, System.Reflection.MethodInfo>[] items = GetContextMenuMethods(obj);
if (items.Length != 0) {
contextMenu.AddSeparator("");
for (int i = 0; i < items.Length; i++) {
KeyValuePair<ContextMenu, System.Reflection.MethodInfo> kvp = items[i];
contextMenu.AddItem(new GUIContent(kvp.Key.menuItem), false, () => kvp.Value.Invoke(obj, null));
}
}
}
/// <summary> Draw a bezier from startpoint to endpoint, both in grid coordinates </summary>
public void DrawConnection(Vector2 startPoint, Vector2 endPoint, Color col) {
startPoint = GridToWindowPosition(startPoint);

View File

@ -71,6 +71,17 @@ namespace XNodeEditor {
return types.ToArray();
}
public static void AddCustomContextMenuItems(GenericMenu contextMenu, object obj) {
KeyValuePair<ContextMenu, System.Reflection.MethodInfo>[] items = GetContextMenuMethods(obj);
if (items.Length != 0) {
contextMenu.AddSeparator("");
for (int i = 0; i < items.Length; i++) {
KeyValuePair<ContextMenu, System.Reflection.MethodInfo> kvp = items[i];
contextMenu.AddItem(new GUIContent(kvp.Key.menuItem), false, () => kvp.Value.Invoke(obj, null));
}
}
}
public static KeyValuePair<ContextMenu, MethodInfo>[] GetContextMenuMethods(object obj) {
Type type = obj.GetType();
MethodInfo[] methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);