diff --git a/Scripts/Editor/NodeEditor.cs b/Scripts/Editor/NodeEditor.cs index 8b06c26..47984ab 100644 --- a/Scripts/Editor/NodeEditor.cs +++ b/Scripts/Editor/NodeEditor.cs @@ -81,6 +81,27 @@ namespace XNodeEditor { return NodeEditorResources.styles.nodeBody; } + /// Show right-click context menu for selected nodes + 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; } diff --git a/Scripts/Editor/NodeEditorAction.cs b/Scripts/Editor/NodeEditorAction.cs index b5315f8..5395b58 100644 --- a/Scripts/Editor/NodeEditorAction.cs +++ b/Scripts/Editor/NodeEditorAction.cs @@ -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(); } diff --git a/Scripts/Editor/NodeEditorGUI.cs b/Scripts/Editor/NodeEditorGUI.cs index 8d7b95a..d9cd057 100644 --- a/Scripts/Editor/NodeEditorGUI.cs +++ b/Scripts/Editor/NodeEditorGUI.cs @@ -116,28 +116,6 @@ namespace XNodeEditor { if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets(); } - /// Show right-click context menu for selected nodes - 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)); - } - /// Show right-click context menu for current graph 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[] items = GetContextMenuMethods(obj); - if (items.Length != 0) { - contextMenu.AddSeparator(""); - for (int i = 0; i < items.Length; i++) { - KeyValuePair kvp = items[i]; - contextMenu.AddItem(new GUIContent(kvp.Key.menuItem), false, () => kvp.Value.Invoke(obj, null)); - } - } - } - /// Draw a bezier from startpoint to endpoint, both in grid coordinates public void DrawConnection(Vector2 startPoint, Vector2 endPoint, Color col) { startPoint = GridToWindowPosition(startPoint); diff --git a/Scripts/Editor/NodeEditorReflection.cs b/Scripts/Editor/NodeEditorReflection.cs index 42e583c..bfe8307 100644 --- a/Scripts/Editor/NodeEditorReflection.cs +++ b/Scripts/Editor/NodeEditorReflection.cs @@ -71,6 +71,17 @@ namespace XNodeEditor { return types.ToArray(); } + public static void AddCustomContextMenuItems(GenericMenu contextMenu, object obj) { + KeyValuePair[] items = GetContextMenuMethods(obj); + if (items.Length != 0) { + contextMenu.AddSeparator(""); + for (int i = 0; i < items.Length; i++) { + KeyValuePair kvp = items[i]; + contextMenu.AddItem(new GUIContent(kvp.Key.menuItem), false, () => kvp.Value.Invoke(obj, null)); + } + } + } + public static KeyValuePair[] GetContextMenuMethods(object obj) { Type type = obj.GetType(); MethodInfo[] methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);