1
0
mirror of https://github.com/Siccity/xNode.git synced 2025-12-20 17:26:02 +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 draggedOutput = null;
[NonSerialized] private XNode.NodePort draggedOutputTarget = null;
[NonSerialized] private XNode.NodePort autoConnectOutput = null;
[NonSerialized] private List<Vector2> draggedOutputReroutes = new List<Vector2>();
private RerouteReference hoveredReroute = new RerouteReference();
private List<RerouteReference> selectedReroutes = new List<RerouteReference>();
@ -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);
}
/// <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 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();

View File

@ -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 {
}
/// <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);
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;
}
/// <summary> Creates a copy of the original node in the graph </summary>
@ -114,7 +116,7 @@ namespace XNodeEditor {
[AttributeUsage(AttributeTargets.Class)]
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;
public string editorPrefsKey;
/// <summary> Tells a NodeGraphEditor which Graph type it is an editor for </summary>