1
0
mirror of https://github.com/Siccity/xNode.git synced 2026-03-26 22:49:02 +08:00

Finished implementing smarter noodles

This commit is contained in:
Thor Brigsted 2019-04-14 18:53:25 +02:00
parent 27e68b0ee1
commit 6522b76a6c
2 changed files with 68 additions and 60 deletions

View File

@ -428,19 +428,19 @@ namespace XNodeEditor {
public void DrawDraggedConnection() { public void DrawDraggedConnection() {
if (IsDraggingPort) { if (IsDraggingPort) {
Color col = NodeEditorPreferences.GetTypeColor(draggedOutput.ValueType); Color col = NodeEditorPreferences.GetTypeColor(draggedOutput.ValueType);
col.a = draggedOutputTarget != null ? 1.0f : 0.6f;
Rect fromRect; Rect fromRect;
if (!_portConnectionPoints.TryGetValue(draggedOutput, out fromRect)) return; if (!_portConnectionPoints.TryGetValue(draggedOutput, out fromRect)) return;
Vector2 from = fromRect.center; List<Vector2> gridPoints = new List<Vector2>();
col.a = draggedOutputTarget != null ? 1.0f : 0.6f; gridPoints.Add(fromRect.center);
Vector2 to = Vector2.zero;
for (int i = 0; i < draggedOutputReroutes.Count; i++) { for (int i = 0; i < draggedOutputReroutes.Count; i++) {
to = draggedOutputReroutes[i]; gridPoints.Add(draggedOutputReroutes[i]);
DrawNoodle(from, i == 0 ? draggedOutput : null, to, null, col);
from = to;
} }
to = draggedOutputTarget != null ? portConnectionPoints[draggedOutputTarget].center : WindowToGridPosition(Event.current.mousePosition); if (draggedOutputTarget != null) gridPoints.Add(portConnectionPoints[draggedOutputTarget].center);
DrawNoodle(from, draggedOutputReroutes.Any() ? null : draggedOutput, to, draggedOutputTarget, col); else gridPoints.Add(WindowToGridPosition(Event.current.mousePosition));
DrawNoodle(col, gridPoints);
Color bgcol = Color.black; Color bgcol = Color.black;
Color frcol = col; Color frcol = col;

View File

@ -114,57 +114,70 @@ namespace XNodeEditor {
if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets(); if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
} }
/// <summary> Draw a bezier from startpoint to endpoint, both in grid coordinates </summary> /// <summary> Draw a bezier from output to input in grid coordinates </summary>
public void DrawNoodle(Vector2 startPoint, XNode.NodePort startPort, Vector2 endPoint, XNode.NodePort endPort, Color col) { public void DrawNoodle(Color col, List<Vector2> gridPoints) {
startPoint = GridToWindowPosition(startPoint); Vector2[] windowPoints = gridPoints.Select(x => GridToWindowPosition(x)).ToArray();
endPoint = GridToWindowPosition(endPoint); Handles.color = col;
int length = gridPoints.Count;
switch (NodeEditorPreferences.GetSettings().noodleType) { switch (NodeEditorPreferences.GetSettings().noodleType) {
case NodeEditorPreferences.NoodleType.Curve: case NodeEditorPreferences.NoodleType.Curve:
float minTangent = Vector2.Distance(endPoint, startPoint)*0.5f; Vector2 outputTangent = Vector2.right;
for (int i = 0; i < length - 1; i++) {
Vector2 inputTangent = Vector2.left;
Vector2 startTangent = Vector2.zero; if (i == 0) outputTangent = Vector2.right * Vector2.Distance(windowPoints[i], windowPoints[i + 1]) * 0.01f * zoom;
startTangent.x = (endPoint.x - startPoint.x) * 0.7f; if (i < length - 2) {
startTangent.x = Mathf.Sign(startTangent.x) * Mathf.Max(Mathf.Abs(startTangent.x), minTangent); Vector2 ab = (windowPoints[i + 1] - windowPoints[i]).normalized;
if (startPort != null) startTangent.x = Mathf.Abs(startTangent.x); Vector2 cb = (windowPoints[i + 1] - windowPoints[i + 2]).normalized;
Vector2 ac = (windowPoints[i + 2] - windowPoints[i]).normalized;
Vector2 p = (ab + cb) * 0.5f;
float tangentLength = (Vector2.Distance(windowPoints[i], windowPoints[i + 1]) + Vector2.Distance(windowPoints[i + 1], windowPoints[i + 2])) * 0.005f * zoom;
float side = ((ac.x * (windowPoints[i + 1].y - windowPoints[i].y)) - (ac.y * (windowPoints[i + 1].x - windowPoints[i].x)));
Vector2 endTangent = Vector2.zero; p = new Vector2(-p.y, p.x) * Mathf.Sign(side) * tangentLength;
endTangent.x = (startPoint.x - endPoint.x) * 0.7f; inputTangent = p;
endTangent.x = Mathf.Sign(endTangent.x) * Mathf.Max(Mathf.Abs(endTangent.x), minTangent); }
if (endPort != null) endTangent.x = -Mathf.Abs(endTangent.x); else {
inputTangent = Vector2.left * Vector2.Distance(windowPoints[i], windowPoints[i + 1]) * 0.01f * zoom;
}
Handles.DrawBezier(startPoint, endPoint, startPoint + startTangent, endPoint + endTangent, col, null, 4); Handles.DrawBezier(windowPoints[i], windowPoints[i + 1], windowPoints[i] + ((outputTangent * 50) / zoom), windowPoints[i + 1] + ((inputTangent * 50) / zoom), col, null, 4);
outputTangent = -inputTangent;
}
break; break;
case NodeEditorPreferences.NoodleType.Line: case NodeEditorPreferences.NoodleType.Line:
Handles.color = col; for (int i = 0; i < length - 1; i++) {
Handles.DrawAAPolyLine(5, startPoint, endPoint); Handles.DrawAAPolyLine(5, windowPoints[i], windowPoints[i + 1]);
}
break; break;
case NodeEditorPreferences.NoodleType.Angled: case NodeEditorPreferences.NoodleType.Angled:
Handles.color = col; for (int i = 0; i < length - 1; i++) {
if (startPoint.x <= endPoint.x - (50 / zoom)) { if (i == length - 1) continue; // Skip last index
float midpoint = (startPoint.x + endPoint.x) * 0.5f; if (windowPoints[i].x <= windowPoints[i + 1].x - (50 / zoom)) {
Vector2 start_1 = startPoint; float midpoint = (windowPoints[i].x + windowPoints[i + 1].x) * 0.5f;
Vector2 end_1 = endPoint; Vector2 start_1 = windowPoints[i];
start_1.x = midpoint; Vector2 end_1 = windowPoints[i + 1];
end_1.x = midpoint; start_1.x = midpoint;
Handles.DrawAAPolyLine(5, startPoint, start_1); end_1.x = midpoint;
Handles.DrawAAPolyLine(5, start_1, end_1); Handles.DrawAAPolyLine(5, windowPoints[i], start_1);
Handles.DrawAAPolyLine(5, end_1, endPoint); Handles.DrawAAPolyLine(5, start_1, end_1);
} else { Handles.DrawAAPolyLine(5, end_1, windowPoints[i + 1]);
float midpoint = (startPoint.y + endPoint.y) * 0.5f; } else {
Vector2 start_1 = startPoint; float midpoint = (windowPoints[i].y + windowPoints[i + 1].y) * 0.5f;
Vector2 end_1 = endPoint; Vector2 start_1 = windowPoints[i];
start_1.x += 25 / zoom; Vector2 end_1 = windowPoints[i + 1];
end_1.x -= 25 / zoom; start_1.x += 25 / zoom;
Vector2 start_2 = start_1; end_1.x -= 25 / zoom;
Vector2 end_2 = end_1; Vector2 start_2 = start_1;
start_2.y = midpoint; Vector2 end_2 = end_1;
end_2.y = midpoint; start_2.y = midpoint;
Handles.DrawAAPolyLine(5, startPoint, start_1); end_2.y = midpoint;
Handles.DrawAAPolyLine(5, start_1, start_2); Handles.DrawAAPolyLine(5, windowPoints[i], start_1);
Handles.DrawAAPolyLine(5, start_2, end_2); Handles.DrawAAPolyLine(5, start_1, start_2);
Handles.DrawAAPolyLine(5, end_2, end_1); Handles.DrawAAPolyLine(5, start_2, end_2);
Handles.DrawAAPolyLine(5, end_1, endPoint); Handles.DrawAAPolyLine(5, end_2, end_1);
Handles.DrawAAPolyLine(5, end_1, windowPoints[i + 1]);
}
} }
break; break;
} }
@ -198,18 +211,13 @@ namespace XNodeEditor {
Rect toRect; Rect toRect;
if (!_portConnectionPoints.TryGetValue(input, out toRect)) continue; if (!_portConnectionPoints.TryGetValue(input, out toRect)) continue;
Vector2 from = fromRect.center;
Vector2 to = Vector2.zero;
List<Vector2> reroutePoints = output.GetReroutePoints(k); List<Vector2> reroutePoints = output.GetReroutePoints(k);
// Loop through reroute points and draw the path
for (int i = 0; i < reroutePoints.Count; i++) {
to = reroutePoints[i];
DrawNoodle(from, i == 0 ? output : null, to, null, connectionColor);
from = to;
}
to = toRect.center;
DrawNoodle(from, reroutePoints.Any() ? null : output, to, input, connectionColor); List<Vector2> gridPoints = new List<Vector2>();
gridPoints.Add(fromRect.center);
gridPoints.AddRange(reroutePoints);
gridPoints.Add(toRect.center);
DrawNoodle(connectionColor, gridPoints);
// Loop through reroute points again and draw the points // Loop through reroute points again and draw the points
for (int i = 0; i < reroutePoints.Count; i++) { for (int i = 0; i < reroutePoints.Count; i++) {