mirror of
https://github.com/Siccity/xNode.git
synced 2025-12-20 09:16:01 +08:00
Connections can now be dragged, not created.
This commit is contained in:
parent
36ecfde3fe
commit
2a6341728b
@ -20,25 +20,34 @@ public static class NodeEditorAction {
|
||||
break;
|
||||
case EventType.MouseDrag:
|
||||
if (e.button == 0) {
|
||||
if (window.activeNode != null) {
|
||||
if (window.hoveredPort != null || window.tempConnection != null) {
|
||||
if (window.tempConnection == null) {
|
||||
if (window.hoveredPort.direction == NodePort.IO.Output) {
|
||||
dragging = true;
|
||||
int inputNodeId = window.graph.GetNodeId(window.activeNode);
|
||||
int outputPortId = window.activeNode.GetOutputPortId(window.hoveredPort);
|
||||
window.tempConnection = new NodeConnection(inputNodeId, outputPortId, -1,-1);
|
||||
}
|
||||
else {
|
||||
Debug.Log("input");
|
||||
/*int outputNodeId = window.graph.GetNodeId(window.activeNode);
|
||||
int outputPortId = window.activeNode.GetInputPortId(window.hoveredPort);
|
||||
window.tempConnection = new NodeConnection(-1,-1,outputNodeId,outputPortId);*/
|
||||
}
|
||||
if (window.HasSelectedNode) {
|
||||
//If we are currently dragging a connection, check if we are hovering any matching port to connect to
|
||||
if (window.IsDraggingConnection) {
|
||||
if (window.IsHoveringPort && window.IsHoveringNode && window.hoveredPort.IsInput) {
|
||||
window.draggedConnection.outputNodeId = window.graph.GetNodeId(window.hoveredNode);
|
||||
window.draggedConnection.inputPortId = window.hoveredNode.GetInputPortId(window.hoveredPort);
|
||||
} else {
|
||||
window.draggedConnection.outputNodeId = -1;
|
||||
window.draggedConnection.inputPortId = -1;
|
||||
}
|
||||
}
|
||||
//If we just started dragging from a port, grab connection
|
||||
else if (window.IsHoveringPort) {
|
||||
if (window.hoveredPort.direction == NodePort.IO.Output) {
|
||||
dragging = true;
|
||||
int inputNodeId = window.graph.GetNodeId(window.selectedNode);
|
||||
int outputPortId = window.selectedNode.GetOutputPortId(window.hoveredPort);
|
||||
window.draggedConnection = new NodeConnection(inputNodeId, outputPortId, -1, -1);
|
||||
}
|
||||
else {
|
||||
Debug.Log("input");
|
||||
/*int outputNodeId = window.graph.GetNodeId(window.activeNode);
|
||||
int outputPortId = window.activeNode.GetInputPortId(window.hoveredPort);
|
||||
window.tempConnection = new NodeConnection(-1,-1,outputNodeId,outputPortId);*/
|
||||
}
|
||||
}
|
||||
else {
|
||||
window.activeNode.position.position = window.WindowToGridPosition(e.mousePosition) + dragOffset;
|
||||
window.selectedNode.position.position = window.WindowToGridPosition(e.mousePosition) + dragOffset;
|
||||
window.Repaint();
|
||||
}
|
||||
}
|
||||
@ -61,7 +70,7 @@ public static class NodeEditorAction {
|
||||
window.Repaint();
|
||||
break;
|
||||
case EventType.MouseUp:
|
||||
window.tempConnection = null;
|
||||
window.draggedConnection.enabled = false;
|
||||
if (dragging) return;
|
||||
|
||||
if (e.button == 1) {
|
||||
|
||||
@ -7,13 +7,17 @@ using UNEC;
|
||||
public class NodeEditorWindow : EditorWindow {
|
||||
|
||||
private Dictionary<NodePort, Vector2> portConnectionPoints = new Dictionary<NodePort, Vector2>();
|
||||
|
||||
public bool IsDraggingConnection { get { return draggedConnection.enabled; } }
|
||||
public bool IsHoveringPort { get { return hoveredPort != null; } }
|
||||
public bool IsHoveringNode { get { return hoveredNode != null; } }
|
||||
public bool HasSelectedNode { get { return selectedNode != null; } }
|
||||
public NodeGraph graph { get { return _graph != null ? _graph : _graph = new NodeGraph(); } }
|
||||
public NodeGraph _graph;
|
||||
public Node hoveredNode;
|
||||
public Node activeNode { get; private set; }
|
||||
/// <summary> Currently selected node </summary>
|
||||
public Node selectedNode { get; private set; }
|
||||
public NodePort hoveredPort;
|
||||
public NodeConnection? tempConnection;
|
||||
public NodeConnection draggedConnection;
|
||||
|
||||
public Vector2 panOffset { get { return _panOffset; } set { _panOffset = value; Repaint(); } }
|
||||
private Vector2 _panOffset;
|
||||
@ -34,20 +38,26 @@ public class NodeEditorWindow : EditorWindow {
|
||||
|
||||
NodeEditorGUI.DrawGrid(position, zoom, panOffset);
|
||||
DrawNodes();
|
||||
DrawTempConnection();
|
||||
DrawDraggedConnection();
|
||||
NodeEditorGUI.DrawToolbar(this);
|
||||
|
||||
GUI.matrix = m;
|
||||
}
|
||||
|
||||
/// <summary> Draw a connection as we are dragging it </summary>
|
||||
private void DrawTempConnection() {
|
||||
if (tempConnection.HasValue) {
|
||||
Node inputNode = graph.GetNode(tempConnection.Value.inputNodeId);
|
||||
private void DrawDraggedConnection() {
|
||||
if (IsDraggingConnection) {
|
||||
Node inputNode = graph.GetNode(draggedConnection.inputNodeId);
|
||||
Node outputNode = graph.GetNode(draggedConnection.outputNodeId);
|
||||
if (inputNode != null) {
|
||||
NodePort outputPort = inputNode.GetOutput(tempConnection.Value.outputPortId);
|
||||
NodePort outputPort = inputNode.GetOutput(draggedConnection.outputPortId);
|
||||
Vector2 startPoint = GridToWindowPosition( portConnectionPoints[outputPort]);
|
||||
Vector2 endPoint = Event.current.mousePosition;
|
||||
if (outputNode != null) {
|
||||
NodePort inputPort = outputNode.GetInput(draggedConnection.inputPortId);
|
||||
if (inputPort != null) endPoint = GridToWindowPosition(portConnectionPoints[inputPort]);
|
||||
}
|
||||
|
||||
Vector2 startTangent = startPoint;
|
||||
startTangent.x = Mathf.Lerp(startPoint.x,endPoint.x,0.7f);
|
||||
Vector2 endTangent = endPoint;
|
||||
@ -78,7 +88,7 @@ public class NodeEditorWindow : EditorWindow {
|
||||
Rect windowRect = new Rect(nodePos, new Vector2(200, 200));
|
||||
if (windowRect.Contains(e.mousePosition)) hoveredNode = node;
|
||||
|
||||
GUIStyle style = (node == activeNode) ? (GUIStyle)"flow node 0 on" : (GUIStyle)"flow node 0";
|
||||
GUIStyle style = (node == selectedNode) ? (GUIStyle)"flow node 0 on" : (GUIStyle)"flow node 0";
|
||||
GUILayout.BeginArea(windowRect, node.ToString(), style);
|
||||
GUILayout.BeginHorizontal();
|
||||
|
||||
@ -150,7 +160,7 @@ public class NodeEditorWindow : EditorWindow {
|
||||
}
|
||||
|
||||
public void SelectNode(Node node) {
|
||||
activeNode = node;
|
||||
selectedNode = node;
|
||||
}
|
||||
|
||||
}
|
||||
@ -5,18 +5,17 @@ using UnityEngine;
|
||||
namespace UNEC {
|
||||
/// <summary> Data travels from Input Node's Output port to Output Node's Input port </summary>
|
||||
public struct NodeConnection {
|
||||
public int inputNodeId { get { return _inputNodeId; } }
|
||||
public int inputPortId { get { return _inputPortId; } }
|
||||
public int outputNodeId { get { return _outputNodeId; } }
|
||||
public int outputPortId { get { return _outputPortId; } }
|
||||
[SerializeField] private int _inputNodeId, _inputPortId, _outputNodeId, _outputPortId;
|
||||
public bool enabled;
|
||||
/// <summary> Data travels from Input Node's Output port to Output Node's Input port </summary>
|
||||
public int inputNodeId, inputPortId, outputNodeId, outputPortId;
|
||||
|
||||
/// <summary> Data travels from Input Node's Output port to Output Node's Input port </summary>
|
||||
public NodeConnection(int inputNodeId, int outputPortId, int outputNodeId, int inputPortId) {
|
||||
_inputNodeId = inputNodeId;
|
||||
_outputPortId = outputPortId;
|
||||
_outputNodeId = outputNodeId;
|
||||
_inputPortId = inputPortId;
|
||||
this.inputNodeId = inputNodeId;
|
||||
this.outputPortId = outputPortId;
|
||||
this.outputNodeId = outputNodeId;
|
||||
this.inputPortId = inputPortId;
|
||||
enabled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -17,6 +17,10 @@ public class NodePort {
|
||||
return IO.None;
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsInput { get { return direction == IO.Input; } }
|
||||
public bool IsOutput { get { return direction == IO.Output; } }
|
||||
|
||||
public Node node { get; private set; }
|
||||
public string name { get { return _name; } set { _name = value; } }
|
||||
[SerializeField]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user