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 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>();
|
||||||
@ -31,9 +32,6 @@ namespace XNodeEditor {
|
|||||||
private Rect selectionBox;
|
private Rect selectionBox;
|
||||||
private bool isDoubleClick = false;
|
private bool isDoubleClick = false;
|
||||||
|
|
||||||
public static bool stoppedDraggingPort = true;
|
|
||||||
private XNode.NodePort draggedPort;
|
|
||||||
|
|
||||||
private struct RerouteReference {
|
private struct RerouteReference {
|
||||||
public XNode.NodePort port;
|
public XNode.NodePort port;
|
||||||
public int connectionIndex;
|
public int connectionIndex;
|
||||||
@ -166,8 +164,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;
|
||||||
@ -235,6 +235,12 @@ namespace XNodeEditor {
|
|||||||
EditorUtility.SetDirty(graph);
|
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
|
//Release dragged connection
|
||||||
draggedOutput = null;
|
draggedOutput = null;
|
||||||
draggedOutputTarget = null;
|
draggedOutputTarget = null;
|
||||||
@ -498,21 +504,7 @@ namespace XNodeEditor {
|
|||||||
|
|
||||||
NodeEditorGUILayout.DrawPortHandle(rect, bgcol, frcol);
|
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();
|
Repaint();
|
||||||
}
|
}
|
||||||
@ -529,11 +521,20 @@ namespace XNodeEditor {
|
|||||||
return windowRect.Contains(mousePos);
|
return windowRect.Contains(mousePos);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ConnectOnCreate() {
|
/// <summary> Attempt to connect dragged output to target node </summary>
|
||||||
if (graph.nodes.Last().Ports.Where(r => r.IsInput == true).Any(r => r.ValueType == draggedPort.ValueType))
|
public void AutoConnect(XNode.Node node) {
|
||||||
draggedPort.Connect(graph.nodes.Last().Ports.Where(r => r.IsInput == true).Where(r => r.ValueType == draggedPort.ValueType).ToArray() [0]);
|
if (autoConnectOutput == null) return;
|
||||||
else
|
|
||||||
draggedPort.Connect(graph.nodes.Last().Ports.Where(r => r.IsInput == true).ToArray() [0]);
|
// 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;
|
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);
|
||||||
if (!NodeEditorWindow.stoppedDraggingPort) NodeEditorWindow.current.ConnectOnCreate();
|
NodeEditorWindow.current.AutoConnect(node);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
menu.AddSeparator("");
|
menu.AddSeparator("");
|
||||||
@ -88,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>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user