diff --git a/Scripts/Editor/NodeEditor.cs b/Scripts/Editor/NodeEditor.cs index d5e7c0c..354597a 100644 --- a/Scripts/Editor/NodeEditor.cs +++ b/Scripts/Editor/NodeEditor.cs @@ -21,7 +21,7 @@ namespace XNodeEditor { OnBodyGUI(); } - public void OnHeaderGUI() { + public virtual void OnHeaderGUI() { GUI.color = Color.white; string title = NodeEditorUtilities.PrettifyCamelCase(target.name); GUILayout.Label(title, NodeEditorResources.styles.nodeHeader, GUILayout.Height(30)); @@ -45,6 +45,13 @@ namespace XNodeEditor { public virtual int GetWidth() { return 200; } + + public virtual void ShowNodeContextMenu(Node node) + { + GenericMenu contextMenu = new GenericMenu(); + contextMenu.AddItem(new GUIContent("Remove"), false, () => target.graph.RemoveNode(node)); + contextMenu.DropDown(new Rect(Event.current.mousePosition, Vector2.zero)); + } } [AttributeUsage(AttributeTargets.Class)] diff --git a/Scripts/Editor/NodeEditorGUI.cs b/Scripts/Editor/NodeEditorGUI.cs index 740d69e..ea40684 100644 --- a/Scripts/Editor/NodeEditorGUI.cs +++ b/Scripts/Editor/NodeEditorGUI.cs @@ -73,25 +73,30 @@ namespace XNodeEditor { /// Show right-click context menu public void ShowContextMenu() { + if (hoveredNode != null) { + GetNodeEditor(hoveredNode.GetType()).ShowNodeContextMenu(hoveredNode); + } + else + { + ShowGraphContextMenu(); + } + } + + public virtual void ShowGraphContextMenu() + { GenericMenu contextMenu = new GenericMenu(); Vector2 pos = WindowToGridPosition(Event.current.mousePosition); + for (int i = 0; i < nodeTypes.Length; i++) { + Type type = nodeTypes[i]; - if (hoveredNode != null) { - Node node = hoveredNode; - contextMenu.AddItem(new GUIContent("Remove"), false, () => graph.RemoveNode(node)); - } else { - for (int i = 0; i < nodeTypes.Length; i++) { - Type type = nodeTypes[i]; - - string name = nodeTypes[i].ToString().Replace('.', '/'); - Node.CreateNodeMenuAttribute attrib; - if (NodeEditorUtilities.GetAttrib(type, out attrib)) { - name = attrib.menuName; - } - contextMenu.AddItem(new GUIContent(name), false, () => { - CreateNode(type, pos); - }); + string name = nodeTypes[i].ToString().Replace('.', '/'); + Node.CreateNodeMenuAttribute attrib; + if (NodeEditorUtilities.GetAttrib(type, out attrib)) { + name = attrib.menuName; } + contextMenu.AddItem(new GUIContent(name), false, () => { + CreateNode(type, pos); + }); } contextMenu.DropDown(new Rect(Event.current.mousePosition, Vector2.zero)); }