From dabd55cf4939293f2cb849718f9741da24ee8208 Mon Sep 17 00:00:00 2001 From: Thor Brigsted Date: Sun, 19 Nov 2017 22:38:05 +0100 Subject: [PATCH] Context menu limited to graph background and node headers --- Scripts/Editor/NodeEditorAction.cs | 8 ++++++- Scripts/Editor/NodeEditorGUI.cs | 34 ++++++++++++++++-------------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/Scripts/Editor/NodeEditorAction.cs b/Scripts/Editor/NodeEditorAction.cs index 869b5b9..ce021e1 100644 --- a/Scripts/Editor/NodeEditorAction.cs +++ b/Scripts/Editor/NodeEditorAction.cs @@ -103,7 +103,13 @@ namespace XNodeEditor { AssetDatabase.SaveAssets(); } } else if (e.button == 1) { - if (!isPanning) ShowContextMenu(); + if (!isPanning) { + if (IsHoveringNode && IsHoveringTitle(hoveredNode)) { + ShowNodeContextMenu(hoveredNode); + } else if (!IsHoveringNode) { + ShowGraphContextMenu(); + } + } isPanning = false; } break; diff --git a/Scripts/Editor/NodeEditorGUI.cs b/Scripts/Editor/NodeEditorGUI.cs index 740d69e..0590e7e 100644 --- a/Scripts/Editor/NodeEditorGUI.cs +++ b/Scripts/Editor/NodeEditorGUI.cs @@ -71,27 +71,29 @@ namespace XNodeEditor { return GUILayout.Button(name, EditorStyles.toolbarDropDown, GUILayout.Width(width)); } - /// Show right-click context menu - public void ShowContextMenu() { + /// Show right-click context menu for a node + public void ShowNodeContextMenu(Node node) { GenericMenu contextMenu = new GenericMenu(); Vector2 pos = WindowToGridPosition(Event.current.mousePosition); + contextMenu.AddItem(new GUIContent("Remove"), false, () => graph.RemoveNode(node)); + contextMenu.DropDown(new Rect(Event.current.mousePosition, Vector2.zero)); + } - 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]; + /// 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]; - 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)); }