diff --git a/Scripts/Editor/NodeEditorAction.cs b/Scripts/Editor/NodeEditorAction.cs index 7995786..d8d36cb 100644 --- a/Scripts/Editor/NodeEditorAction.cs +++ b/Scripts/Editor/NodeEditorAction.cs @@ -264,7 +264,9 @@ namespace XNodeEditor { NodeEditor.GetEditor(hoveredNode).AddContextMenuItems(menu); menu.DropDown(new Rect(Event.current.mousePosition, Vector2.zero)); } else if (!IsHoveringNode) { - ShowGraphContextMenu(); + GenericMenu menu = new GenericMenu(); + graphEditor.AddContextMenuItems(menu); + menu.DropDown(new Rect(Event.current.mousePosition, Vector2.zero)); } } isPanning = false; @@ -325,15 +327,6 @@ namespace XNodeEditor { panOffset = Vector2.zero; } - public void CreateNode(Type type, Vector2 position) { - XNode.Node node = graph.AddNode(type); - node.position = position; - node.name = UnityEditor.ObjectNames.NicifyVariableName(type.Name); - AssetDatabase.AddObjectToAsset(node, graph); - if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets(); - Repaint(); - } - /// Remove nodes in the graph in Selection.objects public void RemoveSelectedNodes() { // We need to delete reroutes starting at the highest point index to avoid shifting indices diff --git a/Scripts/Editor/NodeEditorGUI.cs b/Scripts/Editor/NodeEditorGUI.cs index d9cd057..9f48f0e 100644 --- a/Scripts/Editor/NodeEditorGUI.cs +++ b/Scripts/Editor/NodeEditorGUI.cs @@ -116,27 +116,6 @@ namespace XNodeEditor { if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets(); } - /// Show right-click context menu for current graph - void ShowGraphContextMenu() { - GenericMenu contextMenu = new GenericMenu(); - Vector2 pos = WindowToGridPosition(Event.current.mousePosition); - for (int i = 0; i < nodeTypes.Length; i++) { - Type type = nodeTypes[i]; - - //Get node context menu path - string path = graphEditor.GetNodeMenuName(type); - if (string.IsNullOrEmpty(path)) continue; - - contextMenu.AddItem(new GUIContent(path), false, () => { - CreateNode(type, pos); - }); - } - contextMenu.AddSeparator(""); - contextMenu.AddItem(new GUIContent("Preferences"), false, () => OpenPreferences()); - AddCustomContextMenuItems(contextMenu, graph); - contextMenu.DropDown(new Rect(Event.current.mousePosition, Vector2.zero)); - } - /// 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/NodeGraphEditor.cs b/Scripts/Editor/NodeGraphEditor.cs index 1c32a6e..20f5657 100644 --- a/Scripts/Editor/NodeGraphEditor.cs +++ b/Scripts/Editor/NodeGraphEditor.cs @@ -38,10 +38,39 @@ namespace XNodeEditor { return ObjectNames.NicifyVariableName(type.ToString().Replace('.', '/')); } + /// Add items for the context menu when right-clicking this node. Override to add custom menu items. + public virtual void AddContextMenuItems(GenericMenu menu) { + Vector2 pos = NodeEditorWindow.current.WindowToGridPosition(Event.current.mousePosition); + for (int i = 0; i < NodeEditorWindow.nodeTypes.Length; i++) { + Type type = NodeEditorWindow.nodeTypes[i]; + + //Get node context menu path + string path = GetNodeMenuName(type); + if (string.IsNullOrEmpty(path)) continue; + + menu.AddItem(new GUIContent(path), false, () => { + CreateNode(type, pos); + }); + } + menu.AddSeparator(""); + menu.AddItem(new GUIContent("Preferences"), false, () => NodeEditorWindow.OpenPreferences()); + NodeEditorWindow.AddCustomContextMenuItems(menu, target); + } + public virtual Color GetTypeColor(Type type) { return NodeEditorPreferences.GetTypeColor(type); } + /// Create a node and save it in the graph asset + public virtual void CreateNode(Type type, Vector2 position) { + XNode.Node node = target.AddNode(type); + node.position = position; + node.name = UnityEditor.ObjectNames.NicifyVariableName(type.Name); + AssetDatabase.AddObjectToAsset(node, target); + if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets(); + NodeEditorWindow.RepaintAll(); + } + /// Creates a copy of the original node in the graph public XNode.Node CopyNode(XNode.Node original) { XNode.Node node = target.CopyNode(original);