From 5433e368373adbae9d3ced42d54a768456a2dff0 Mon Sep 17 00:00:00 2001 From: MowfaqAlarbi <54871067+MowfaqAlarbi@users.noreply.github.com> Date: Fri, 6 Sep 2019 19:13:00 +0200 Subject: [PATCH] Show Node Create list When Dragging To Nothing (#183) And a user setting to disable it, of course --- Scripts/Editor/NodeEditorAction.cs | 25 +++++++++++++++++++++++++ Scripts/Editor/NodeEditorPreferences.cs | 2 ++ Scripts/Editor/NodeGraphEditor.cs | 8 +++++--- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/Scripts/Editor/NodeEditorAction.cs b/Scripts/Editor/NodeEditorAction.cs index 1caecd9..cf52351 100644 --- a/Scripts/Editor/NodeEditorAction.cs +++ b/Scripts/Editor/NodeEditorAction.cs @@ -22,6 +22,7 @@ namespace XNodeEditor { [NonSerialized] private XNode.NodePort hoveredPort = null; [NonSerialized] private XNode.NodePort draggedOutput = null; [NonSerialized] private XNode.NodePort draggedOutputTarget = null; + [NonSerialized] private XNode.NodePort autoConnectOutput = null; [NonSerialized] private List draggedOutputReroutes = new List(); private RerouteReference hoveredReroute = new RerouteReference(); private List selectedReroutes = new List(); @@ -145,8 +146,10 @@ namespace XNodeEditor { if (IsHoveringPort) { if (hoveredPort.IsOutput) { draggedOutput = hoveredPort; + autoConnectOutput = hoveredPort; } else { hoveredPort.VerifyConnections(); + autoConnectOutput = null; if (hoveredPort.IsConnected) { XNode.Node node = hoveredPort.node; XNode.NodePort output = hoveredPort.Connection; @@ -214,6 +217,12 @@ namespace XNodeEditor { EditorUtility.SetDirty(graph); } } + // Open context menu for auto-connection + else if (NodeEditorPreferences.GetSettings().dragToCreate && autoConnectOutput != null) { + GenericMenu menu = new GenericMenu(); + graphEditor.AddContextMenuItems(menu); + menu.DropDown(new Rect(Event.current.mousePosition, Vector2.zero)); + } //Release dragged connection draggedOutput = null; draggedOutputTarget = null; @@ -491,5 +500,21 @@ namespace XNodeEditor { Rect windowRect = new Rect(nodePos, new Vector2(width / zoom, 30 / zoom)); return windowRect.Contains(mousePos); } + + /// Attempt to connect dragged output to target node + public void AutoConnect(XNode.Node node) { + if (autoConnectOutput == null) return; + + // Find input port of same type + XNode.NodePort inputPort = node.Ports.FirstOrDefault(x => x.IsInput && x.ValueType == autoConnectOutput.ValueType); + // Fallback to input port + if (inputPort == null) inputPort = node.Ports.FirstOrDefault(x => x.IsInput); + // Autoconnect + if (inputPort != null) autoConnectOutput.Connect(inputPort); + + // Save changes + EditorUtility.SetDirty(graph); + if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets(); + } } } \ No newline at end of file diff --git a/Scripts/Editor/NodeEditorPreferences.cs b/Scripts/Editor/NodeEditorPreferences.cs index c28c064..9e9bb64 100644 --- a/Scripts/Editor/NodeEditorPreferences.cs +++ b/Scripts/Editor/NodeEditorPreferences.cs @@ -32,6 +32,7 @@ namespace XNodeEditor { public Color32 highlightColor = new Color32(255, 255, 255, 255); public bool gridSnap = true; public bool autoSave = true; + public bool dragToCreate = true; public bool zoomToMouse = true; public bool portTooltips = true; [SerializeField] private string typeColorsData = ""; @@ -149,6 +150,7 @@ namespace XNodeEditor { settings.highlightColor = EditorGUILayout.ColorField("Selection", settings.highlightColor); settings.noodleType = (NoodleType) EditorGUILayout.EnumPopup("Noodle type", (Enum) settings.noodleType); settings.portTooltips = EditorGUILayout.Toggle("Port Tooltips", settings.portTooltips); + settings.dragToCreate = EditorGUILayout.Toggle(new GUIContent("Drag to Create", "Drag a port connection anywhere on the grid to create and connect a node"), settings.dragToCreate); if (GUI.changed) { SavePrefs(key, settings); NodeEditorWindow.RepaintAll(); diff --git a/Scripts/Editor/NodeGraphEditor.cs b/Scripts/Editor/NodeGraphEditor.cs index a45d566..bdd4fa0 100644 --- a/Scripts/Editor/NodeGraphEditor.cs +++ b/Scripts/Editor/NodeGraphEditor.cs @@ -52,7 +52,8 @@ namespace XNodeEditor { if (string.IsNullOrEmpty(path)) continue; menu.AddItem(new GUIContent(path), false, () => { - CreateNode(type, pos); + XNode.Node node = CreateNode(type, pos); + NodeEditorWindow.current.AutoConnect(node); }); } menu.AddSeparator(""); @@ -87,13 +88,14 @@ namespace XNodeEditor { } /// Create a node and save it in the graph asset - public virtual void CreateNode(Type type, Vector2 position) { + public virtual XNode.Node CreateNode(Type type, Vector2 position) { XNode.Node node = target.AddNode(type); node.position = position; if (node.name == null || node.name.Trim() == "") node.name = NodeEditorUtilities.NodeDefaultName(type); AssetDatabase.AddObjectToAsset(node, target); if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets(); NodeEditorWindow.RepaintAll(); + return node; } /// Creates a copy of the original node in the graph @@ -114,7 +116,7 @@ namespace XNodeEditor { [AttributeUsage(AttributeTargets.Class)] public class CustomNodeGraphEditorAttribute : Attribute, - XNodeEditor.Internal.NodeEditorBase.INodeEditorAttrib { + XNodeEditor.Internal.NodeEditorBase.INodeEditorAttrib { private Type inspectedType; public string editorPrefsKey; /// Tells a NodeGraphEditor which Graph type it is an editor for