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