mirror of
https://github.com/Siccity/xNode.git
synced 2026-03-26 22:49:02 +08:00
Removed the need for static bool, moved code to their right places
This commit is contained in:
parent
1438b1463a
commit
5283ee3ec7
@ -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>();
|
||||
@ -31,9 +32,6 @@ namespace XNodeEditor {
|
||||
private Rect selectionBox;
|
||||
private bool isDoubleClick = false;
|
||||
|
||||
public static bool stoppedDraggingPort = true;
|
||||
private XNode.NodePort draggedPort;
|
||||
|
||||
private struct RerouteReference {
|
||||
public XNode.NodePort port;
|
||||
public int connectionIndex;
|
||||
@ -166,8 +164,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;
|
||||
@ -235,6 +235,12 @@ namespace XNodeEditor {
|
||||
EditorUtility.SetDirty(graph);
|
||||
}
|
||||
}
|
||||
// Open context menu for auto-connection
|
||||
else if (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;
|
||||
@ -498,21 +504,7 @@ namespace XNodeEditor {
|
||||
|
||||
NodeEditorGUILayout.DrawPortHandle(rect, bgcol, frcol);
|
||||
}
|
||||
stoppedDraggingPort = true;
|
||||
} else {
|
||||
if (stoppedDraggingPort) {
|
||||
if (draggedPort != null && draggedPort.IsOutput) {
|
||||
GenericMenu menu = new GenericMenu();
|
||||
graphEditor.AddContextMenuItems(menu);
|
||||
menu.DropDown(new Rect(Event.current.mousePosition, Vector2.zero));
|
||||
}
|
||||
stoppedDraggingPort = false;
|
||||
}
|
||||
}
|
||||
if (hoveredPort != null)
|
||||
draggedPort = hoveredPort;
|
||||
else if (draggedOutputTarget != null)
|
||||
draggedPort = draggedOutputTarget;
|
||||
|
||||
Repaint();
|
||||
}
|
||||
@ -529,11 +521,20 @@ namespace XNodeEditor {
|
||||
return windowRect.Contains(mousePos);
|
||||
}
|
||||
|
||||
public void ConnectOnCreate() {
|
||||
if (graph.nodes.Last().Ports.Where(r => r.IsInput == true).Any(r => r.ValueType == draggedPort.ValueType))
|
||||
draggedPort.Connect(graph.nodes.Last().Ports.Where(r => r.IsInput == true).Where(r => r.ValueType == draggedPort.ValueType).ToArray() [0]);
|
||||
else
|
||||
draggedPort.Connect(graph.nodes.Last().Ports.Where(r => r.IsInput == true).ToArray() [0]);
|
||||
/// <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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -52,8 +52,8 @@ namespace XNodeEditor {
|
||||
if (string.IsNullOrEmpty(path)) continue;
|
||||
|
||||
menu.AddItem(new GUIContent(path), false, () => {
|
||||
CreateNode(type, pos);
|
||||
if (!NodeEditorWindow.stoppedDraggingPort) NodeEditorWindow.current.ConnectOnCreate();
|
||||
XNode.Node node = CreateNode(type, pos);
|
||||
NodeEditorWindow.current.AutoConnect(node);
|
||||
});
|
||||
}
|
||||
menu.AddSeparator("");
|
||||
@ -88,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>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user