1
0
mirror of https://github.com/Siccity/xNode.git synced 2025-12-20 09:16:01 +08:00

Show Node Create list When Dragging To Nothing (#183)

And a user setting to disable it, of course
This commit is contained in:
MowfaqAlarbi 2019-09-06 19:13:00 +02:00 committed by Thor Brigsted
parent b7749e3b99
commit 5433e36837
3 changed files with 32 additions and 3 deletions

View File

@ -22,6 +22,7 @@ namespace XNodeEditor {
[NonSerialized] private XNode.NodePort hoveredPort = null; [NonSerialized] private XNode.NodePort hoveredPort = null;
[NonSerialized] private XNode.NodePort draggedOutput = null; [NonSerialized] private XNode.NodePort draggedOutput = null;
[NonSerialized] private XNode.NodePort draggedOutputTarget = null; [NonSerialized] private XNode.NodePort draggedOutputTarget = null;
[NonSerialized] private XNode.NodePort autoConnectOutput = null;
[NonSerialized] private List<Vector2> draggedOutputReroutes = new List<Vector2>(); [NonSerialized] private List<Vector2> draggedOutputReroutes = new List<Vector2>();
private RerouteReference hoveredReroute = new RerouteReference(); private RerouteReference hoveredReroute = new RerouteReference();
private List<RerouteReference> selectedReroutes = new List<RerouteReference>(); private List<RerouteReference> selectedReroutes = new List<RerouteReference>();
@ -145,8 +146,10 @@ namespace XNodeEditor {
if (IsHoveringPort) { if (IsHoveringPort) {
if (hoveredPort.IsOutput) { if (hoveredPort.IsOutput) {
draggedOutput = hoveredPort; draggedOutput = hoveredPort;
autoConnectOutput = hoveredPort;
} else { } else {
hoveredPort.VerifyConnections(); hoveredPort.VerifyConnections();
autoConnectOutput = null;
if (hoveredPort.IsConnected) { if (hoveredPort.IsConnected) {
XNode.Node node = hoveredPort.node; XNode.Node node = hoveredPort.node;
XNode.NodePort output = hoveredPort.Connection; XNode.NodePort output = hoveredPort.Connection;
@ -214,6 +217,12 @@ namespace XNodeEditor {
EditorUtility.SetDirty(graph); 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 //Release dragged connection
draggedOutput = null; draggedOutput = null;
draggedOutputTarget = null; draggedOutputTarget = null;
@ -491,5 +500,21 @@ namespace XNodeEditor {
Rect windowRect = new Rect(nodePos, new Vector2(width / zoom, 30 / zoom)); Rect windowRect = new Rect(nodePos, new Vector2(width / zoom, 30 / zoom));
return windowRect.Contains(mousePos); return windowRect.Contains(mousePos);
} }
/// <summary> Attempt to connect dragged output to target node </summary>
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();
}
} }
} }

View File

@ -32,6 +32,7 @@ namespace XNodeEditor {
public Color32 highlightColor = new Color32(255, 255, 255, 255); public Color32 highlightColor = new Color32(255, 255, 255, 255);
public bool gridSnap = true; public bool gridSnap = true;
public bool autoSave = true; public bool autoSave = true;
public bool dragToCreate = true;
public bool zoomToMouse = true; public bool zoomToMouse = true;
public bool portTooltips = true; public bool portTooltips = true;
[SerializeField] private string typeColorsData = ""; [SerializeField] private string typeColorsData = "";
@ -149,6 +150,7 @@ namespace XNodeEditor {
settings.highlightColor = EditorGUILayout.ColorField("Selection", settings.highlightColor); settings.highlightColor = EditorGUILayout.ColorField("Selection", settings.highlightColor);
settings.noodleType = (NoodleType) EditorGUILayout.EnumPopup("Noodle type", (Enum) settings.noodleType); settings.noodleType = (NoodleType) EditorGUILayout.EnumPopup("Noodle type", (Enum) settings.noodleType);
settings.portTooltips = EditorGUILayout.Toggle("Port Tooltips", settings.portTooltips); 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) { if (GUI.changed) {
SavePrefs(key, settings); SavePrefs(key, settings);
NodeEditorWindow.RepaintAll(); NodeEditorWindow.RepaintAll();

View File

@ -52,7 +52,8 @@ namespace XNodeEditor {
if (string.IsNullOrEmpty(path)) continue; if (string.IsNullOrEmpty(path)) continue;
menu.AddItem(new GUIContent(path), false, () => { menu.AddItem(new GUIContent(path), false, () => {
CreateNode(type, pos); XNode.Node node = CreateNode(type, pos);
NodeEditorWindow.current.AutoConnect(node);
}); });
} }
menu.AddSeparator(""); menu.AddSeparator("");
@ -87,13 +88,14 @@ namespace XNodeEditor {
} }
/// <summary> Create a node and save it in the graph asset </summary> /// <summary> Create a node and save it in the graph asset </summary>
public virtual void CreateNode(Type type, Vector2 position) { public virtual XNode.Node CreateNode(Type type, Vector2 position) {
XNode.Node node = target.AddNode(type); XNode.Node node = target.AddNode(type);
node.position = position; node.position = position;
if (node.name == null || node.name.Trim() == "") node.name = NodeEditorUtilities.NodeDefaultName(type); if (node.name == null || node.name.Trim() == "") node.name = NodeEditorUtilities.NodeDefaultName(type);
AssetDatabase.AddObjectToAsset(node, target); AssetDatabase.AddObjectToAsset(node, target);
if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets(); if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
NodeEditorWindow.RepaintAll(); NodeEditorWindow.RepaintAll();
return node;
} }
/// <summary> Creates a copy of the original node in the graph </summary> /// <summary> Creates a copy of the original node in the graph </summary>
@ -114,7 +116,7 @@ namespace XNodeEditor {
[AttributeUsage(AttributeTargets.Class)] [AttributeUsage(AttributeTargets.Class)]
public class CustomNodeGraphEditorAttribute : Attribute, public class CustomNodeGraphEditorAttribute : Attribute,
XNodeEditor.Internal.NodeEditorBase<NodeGraphEditor, NodeGraphEditor.CustomNodeGraphEditorAttribute, XNode.NodeGraph>.INodeEditorAttrib { XNodeEditor.Internal.NodeEditorBase<NodeGraphEditor, NodeGraphEditor.CustomNodeGraphEditorAttribute, XNode.NodeGraph>.INodeEditorAttrib {
private Type inspectedType; private Type inspectedType;
public string editorPrefsKey; public string editorPrefsKey;
/// <summary> Tells a NodeGraphEditor which Graph type it is an editor for </summary> /// <summary> Tells a NodeGraphEditor which Graph type it is an editor for </summary>