mirror of
https://github.com/Siccity/xNode.git
synced 2026-02-04 14:24:54 +08:00
Improved noodle tangents (#136)
This commit is contained in:
parent
7eaa15af4a
commit
4cf7113740
@ -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]);
|
||||||
DrawConnection(from, to, col);
|
|
||||||
from = to;
|
|
||||||
}
|
}
|
||||||
to = draggedOutputTarget != null ? portConnectionPoints[draggedOutputTarget].center : WindowToGridPosition(Event.current.mousePosition);
|
if (draggedOutputTarget != null) gridPoints.Add(portConnectionPoints[draggedOutputTarget].center);
|
||||||
DrawConnection(from, to, 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;
|
||||||
|
|||||||
@ -114,52 +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 DrawConnection(Vector2 startPoint, Vector2 endPoint, 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:
|
||||||
Vector2 startTangent = startPoint;
|
Vector2 outputTangent = Vector2.right;
|
||||||
if (startPoint.x < endPoint.x) startTangent.x = Mathf.LerpUnclamped(startPoint.x, endPoint.x, 0.7f);
|
for (int i = 0; i < length - 1; i++) {
|
||||||
else startTangent.x = Mathf.LerpUnclamped(startPoint.x, endPoint.x, -0.7f);
|
Vector2 inputTangent = Vector2.left;
|
||||||
|
|
||||||
Vector2 endTangent = endPoint;
|
if (i == 0) outputTangent = Vector2.right * Vector2.Distance(windowPoints[i], windowPoints[i + 1]) * 0.01f * zoom;
|
||||||
if (startPoint.x > endPoint.x) endTangent.x = Mathf.LerpUnclamped(endPoint.x, startPoint.x, -0.7f);
|
if (i < length - 2) {
|
||||||
else endTangent.x = Mathf.LerpUnclamped(endPoint.x, startPoint.x, 0.7f);
|
Vector2 ab = (windowPoints[i + 1] - windowPoints[i]).normalized;
|
||||||
Handles.DrawBezier(startPoint, endPoint, startTangent, endTangent, col, null, 4);
|
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)));
|
||||||
|
|
||||||
|
p = new Vector2(-p.y, p.x) * Mathf.Sign(side) * tangentLength;
|
||||||
|
inputTangent = p;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
inputTangent = Vector2.left * Vector2.Distance(windowPoints[i], windowPoints[i + 1]) * 0.01f * zoom;
|
||||||
|
}
|
||||||
|
|
||||||
|
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];
|
||||||
|
Vector2 end_1 = windowPoints[i + 1];
|
||||||
start_1.x = midpoint;
|
start_1.x = midpoint;
|
||||||
end_1.x = midpoint;
|
end_1.x = midpoint;
|
||||||
Handles.DrawAAPolyLine(5, startPoint, start_1);
|
Handles.DrawAAPolyLine(5, windowPoints[i], start_1);
|
||||||
Handles.DrawAAPolyLine(5, start_1, end_1);
|
Handles.DrawAAPolyLine(5, start_1, end_1);
|
||||||
Handles.DrawAAPolyLine(5, end_1, endPoint);
|
Handles.DrawAAPolyLine(5, end_1, windowPoints[i + 1]);
|
||||||
} else {
|
} else {
|
||||||
float midpoint = (startPoint.y + endPoint.y) * 0.5f;
|
float midpoint = (windowPoints[i].y + windowPoints[i + 1].y) * 0.5f;
|
||||||
Vector2 start_1 = startPoint;
|
Vector2 start_1 = windowPoints[i];
|
||||||
Vector2 end_1 = endPoint;
|
Vector2 end_1 = windowPoints[i + 1];
|
||||||
start_1.x += 25 / zoom;
|
start_1.x += 25 / zoom;
|
||||||
end_1.x -= 25 / zoom;
|
end_1.x -= 25 / zoom;
|
||||||
Vector2 start_2 = start_1;
|
Vector2 start_2 = start_1;
|
||||||
Vector2 end_2 = end_1;
|
Vector2 end_2 = end_1;
|
||||||
start_2.y = midpoint;
|
start_2.y = midpoint;
|
||||||
end_2.y = midpoint;
|
end_2.y = midpoint;
|
||||||
Handles.DrawAAPolyLine(5, startPoint, start_1);
|
Handles.DrawAAPolyLine(5, windowPoints[i], start_1);
|
||||||
Handles.DrawAAPolyLine(5, start_1, start_2);
|
Handles.DrawAAPolyLine(5, start_1, start_2);
|
||||||
Handles.DrawAAPolyLine(5, start_2, end_2);
|
Handles.DrawAAPolyLine(5, start_2, end_2);
|
||||||
Handles.DrawAAPolyLine(5, end_2, end_1);
|
Handles.DrawAAPolyLine(5, end_2, end_1);
|
||||||
Handles.DrawAAPolyLine(5, end_1, endPoint);
|
Handles.DrawAAPolyLine(5, end_1, windowPoints[i + 1]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -193,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];
|
|
||||||
DrawConnection(from, to, connectionColor);
|
|
||||||
from = to;
|
|
||||||
}
|
|
||||||
to = toRect.center;
|
|
||||||
|
|
||||||
DrawConnection(from, to, 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++) {
|
||||||
|
|||||||
@ -417,6 +417,8 @@ namespace XNodeEditor {
|
|||||||
instancePorts = indexedPorts.OrderBy(x => x.index).Select(x => x.port).ToList();
|
instancePorts = indexedPorts.OrderBy(x => x.index).Select(x => x.port).ToList();
|
||||||
|
|
||||||
int index = rl.index;
|
int index = rl.index;
|
||||||
|
|
||||||
|
if (instancePorts.Count > index) {
|
||||||
// Clear the removed ports connections
|
// Clear the removed ports connections
|
||||||
instancePorts[index].ClearConnections();
|
instancePorts[index].ClearConnections();
|
||||||
// Move following connections one step up to replace the missing connection
|
// Move following connections one step up to replace the missing connection
|
||||||
@ -431,6 +433,10 @@ namespace XNodeEditor {
|
|||||||
node.RemoveInstancePort(instancePorts[instancePorts.Count() - 1].fieldName);
|
node.RemoveInstancePort(instancePorts[instancePorts.Count() - 1].fieldName);
|
||||||
serializedObject.Update();
|
serializedObject.Update();
|
||||||
EditorUtility.SetDirty(node);
|
EditorUtility.SetDirty(node);
|
||||||
|
} else {
|
||||||
|
Debug.LogWarning("InstancePorts[" + index + "] out of range. Length was " + instancePorts.Count + ". Skipping.");
|
||||||
|
}
|
||||||
|
|
||||||
if (hasArrayData) {
|
if (hasArrayData) {
|
||||||
arrayData.DeleteArrayElementAtIndex(index);
|
arrayData.DeleteArrayElementAtIndex(index);
|
||||||
// Error handling. If the following happens too often, file a bug report at https://github.com/Siccity/xNode/issues
|
// Error handling. If the following happens too often, file a bug report at https://github.com/Siccity/xNode/issues
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user