566 lines
22 KiB
Dart
566 lines
22 KiB
Dart
|
|
/// 依赖连线的共享走线计算。
|
|||
|
|
///
|
|||
|
|
/// 条形视图 / 圆周视图的**画布绘制、标签放置与命中测试**(点选连线、
|
|||
|
|
/// 沿法线拖拽调整走线)都使用这里的同一份几何,避免绘制与命中不一致:
|
|||
|
|
/// - 自动错开:同一走线分组内的多条依赖依次错开,先解决“完全重叠”;
|
|||
|
|
/// - 手动偏移:[Dependency.routeOffset] 叠加在自动错开之上(拖拽连线的结果)。
|
|||
|
|
library;
|
|||
|
|
|
|||
|
|
import 'dart:math' as math;
|
|||
|
|
import 'dart:ui';
|
|||
|
|
|
|||
|
|
import 'package:sequence_diagram/model/models.dart';
|
|||
|
|
import 'layout.dart';
|
|||
|
|
|
|||
|
|
// ===================== 通用工具 =====================
|
|||
|
|
|
|||
|
|
/// 点 [p] 到线段 [a]–[b] 的最短距离。
|
|||
|
|
double distanceToSegment(Offset p, Offset a, Offset b) {
|
|||
|
|
final Offset ab = b - a;
|
|||
|
|
final double len2 = ab.dx * ab.dx + ab.dy * ab.dy;
|
|||
|
|
if (len2 <= 0) return (p - a).distance;
|
|||
|
|
final double t = (((p - a).dx * ab.dx + (p - a).dy * ab.dy) / len2).clamp(0.0, 1.0);
|
|||
|
|
return (p - (a + ab * t)).distance;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// 点 [p] 到折线 [pts] 的最短距离。
|
|||
|
|
double distanceToPolyline(List<Offset> pts, Offset p) {
|
|||
|
|
double best = double.infinity;
|
|||
|
|
for (int i = 0; i + 1 < pts.length; i++) {
|
|||
|
|
final double d = distanceToSegment(p, pts[i], pts[i + 1]);
|
|||
|
|
if (d < best) best = d;
|
|||
|
|
}
|
|||
|
|
return best;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// 点 [p] 到路径 [path] 的近似最短距离(按弧长采样,步长 [step])。
|
|||
|
|
double distanceToPath(Path path, Offset p, {double step = 4}) {
|
|||
|
|
double best = double.infinity;
|
|||
|
|
for (final PathMetric m in path.computeMetrics()) {
|
|||
|
|
for (double d = 0; d < m.length; d += step) {
|
|||
|
|
final Tangent? t = m.getTangentForOffset(d);
|
|||
|
|
if (t == null) continue;
|
|||
|
|
final double dist = (t.position - p).distance;
|
|||
|
|
if (dist < best) best = dist;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return best;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// 第 [k] 档自动错开量(px):0、−6、+6、−12、+12 …
|
|||
|
|
///
|
|||
|
|
/// 同一走线分组内第二条起依次错开,避免多条依赖完全重叠。
|
|||
|
|
double staggerOffset(int k) {
|
|||
|
|
if (k <= 0) return 0;
|
|||
|
|
return (k.isOdd ? -1 : 1) * ((k + 1) ~/ 2) * 6.0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// 拖拽调整走线时的移动轴(走线的“法线”方向)。
|
|||
|
|
///
|
|||
|
|
/// - [vertical]:上下拖动——平移水平走线段(条形 / 流程图的常见形态);
|
|||
|
|
/// - [horizontal]:左右拖动——平移纵向走线走廊(跨行绕行走廊 / 同层绕行)。
|
|||
|
|
enum RouteAxis { vertical, horizontal }
|
|||
|
|
|
|||
|
|
/// 拖拽走线时可用的最大手动偏移(px,双向)。
|
|||
|
|
const double kMaxRouteOffset = 200;
|
|||
|
|
|
|||
|
|
// ===================== 条形视图 =====================
|
|||
|
|
|
|||
|
|
/// 条形视图的一条依赖走线。
|
|||
|
|
class BarDepRoute {
|
|||
|
|
/// 构造。
|
|||
|
|
BarDepRoute(this.dep, this.points, this.arrowTip, this.arrowDir, this.chipCenter, this.axis,
|
|||
|
|
{this.chipRun = double.infinity,
|
|||
|
|
this.chipRunMargin = 0,
|
|||
|
|
this.chipAltAnchor = Offset.zero,
|
|||
|
|
this.junctionOnly = false});
|
|||
|
|
|
|||
|
|
/// 对应依赖。
|
|||
|
|
final Dependency dep;
|
|||
|
|
/// 折线顶点(含起点与箭头端点)。
|
|||
|
|
final List<Offset> points;
|
|||
|
|
/// 箭头端点。
|
|||
|
|
final Offset arrowTip;
|
|||
|
|
/// 箭头方向(分量式)。
|
|||
|
|
final Offset arrowDir;
|
|||
|
|
/// 标签中心(默认位置,避让前)。
|
|||
|
|
final Offset chipCenter;
|
|||
|
|
/// 拖拽方向:vertical = 上下拖动平移横线;horizontal = 左右拖动平移纵向走廊。
|
|||
|
|
final RouteAxis axis;
|
|||
|
|
|
|||
|
|
/// 标签挂钩的水平行程长度(跑长时用于判断是否需要把标签移到箭头外侧)。
|
|||
|
|
///
|
|||
|
|
/// 默认 [double.infinity] 表示不做外移判断(跨行走廊等)。
|
|||
|
|
final double chipRun;
|
|||
|
|
|
|||
|
|
/// 外移判断余量:行程 `< 标签宽 + 余量` 时把标签移到 [chipAltAnchor]。
|
|||
|
|
final double chipRunMargin;
|
|||
|
|
|
|||
|
|
/// 外移时标签的锚点(绘制端会再加半个标签宽)。
|
|||
|
|
final Offset chipAltAnchor;
|
|||
|
|
|
|||
|
|
/// 动作紧贴时只显示连接点标签,隐藏连线与箭头(由绘制层处理)。
|
|||
|
|
final bool junctionOnly;
|
|||
|
|
|
|||
|
|
/// 依赖键 `from->to`。
|
|||
|
|
String get key => '${dep.fromId}->${dep.toId}';
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// 条形依赖在动作条一端的锚点。
|
|||
|
|
///
|
|||
|
|
/// [point] 为线条端点(目标端为箭头尖端);[dir] 为箭头进入方向(源端为
|
|||
|
|
/// 离开方向);[corner] 表示端点已退到条块的上/下角;[vSide] 为角点所在
|
|||
|
|
/// 一侧(-1 上、+1 下)。
|
|||
|
|
class _BarEnd {
|
|||
|
|
const _BarEnd(this.point, this.dir, this.corner, this.vSide);
|
|||
|
|
|
|||
|
|
final Offset point;
|
|||
|
|
final Offset dir;
|
|||
|
|
final bool corner;
|
|||
|
|
final int vSide;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// 判断端点前的水平通道是否被其它动作条遮挡(宽 8px,条高中间 ±3px)。
|
|||
|
|
///
|
|||
|
|
/// 遮挡时把端点退到端部的上/下角,保证线条从角上引出而不是贴着邻条走。
|
|||
|
|
bool _barEndBlocked(double edge, double outward, double centerY, Iterable<Rect> obstacles) {
|
|||
|
|
const double clearance = 9;
|
|||
|
|
final Rect probe = Rect.fromLTRB(
|
|||
|
|
math.min(edge, edge + outward * clearance),
|
|||
|
|
centerY - 3,
|
|||
|
|
math.max(edge, edge + outward * clearance),
|
|||
|
|
centerY + 3,
|
|||
|
|
);
|
|||
|
|
for (final Rect r in obstacles) {
|
|||
|
|
if (r.overlaps(probe)) return true;
|
|||
|
|
}
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// 源端锚点:默认取条块左/右端点中心;端部有遮挡时取朝向目标一侧的上下角。
|
|||
|
|
_BarEnd _startEnd(Rect bar, int side, int cornerSide, Iterable<Rect> obstacles,
|
|||
|
|
{bool forceCorner = false}) {
|
|||
|
|
final double edge = side > 0 ? bar.right : bar.left;
|
|||
|
|
final double outward = side > 0 ? 1 : -1;
|
|||
|
|
final double cy = bar.center.dy;
|
|||
|
|
if (!forceCorner && !_barEndBlocked(edge, outward, cy, obstacles)) {
|
|||
|
|
return _BarEnd(Offset(edge + outward * 2, cy), Offset(outward, 0), false, 0);
|
|||
|
|
}
|
|||
|
|
final int vs = cornerSide == 0 ? 1 : cornerSide;
|
|||
|
|
final double y = vs > 0 ? bar.bottom - 2 : bar.top + 2;
|
|||
|
|
return _BarEnd(Offset(edge + outward * 1.5, y), Offset(0, vs.toDouble()), true, vs);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// 目标端锚点:默认取条块左/右端点中心(箭头停在端点外 2px);端部有遮挡
|
|||
|
|
/// 时取朝向源条一侧的上下角,箭头从角外垂直指向条块。
|
|||
|
|
_BarEnd _targetEnd(Rect bar, int side, int cornerSide, Iterable<Rect> obstacles,
|
|||
|
|
{bool forceCorner = false}) {
|
|||
|
|
final double edge = side > 0 ? bar.right : bar.left;
|
|||
|
|
final double outward = side > 0 ? 1 : -1;
|
|||
|
|
final double cy = bar.center.dy;
|
|||
|
|
if (!forceCorner && !_barEndBlocked(edge, outward, cy, obstacles)) {
|
|||
|
|
return _BarEnd(Offset(edge + outward * 2, cy), Offset(-outward, 0), false, 0);
|
|||
|
|
}
|
|||
|
|
final int vs = cornerSide == 0 ? 1 : cornerSide;
|
|||
|
|
final double y = vs > 0 ? bar.bottom + 2 : bar.top - 2;
|
|||
|
|
return _BarEnd(
|
|||
|
|
Offset(edge + outward * 1.5, y), Offset(0, -vs.toDouble()), true, vs);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// 计算条形视图的全部依赖走线(含自动错开与手动偏移)。
|
|||
|
|
///
|
|||
|
|
/// 端点规则(FS / SS / FF / SF 均按语义取条块前端或后端):
|
|||
|
|
/// 1. 默认从条块的左/右端点中心引出;FS/FF 从后端(右端)出发,
|
|||
|
|
/// SS/SF 从前端(左端)出发;目标端按 FS/SS 取前端、FF/SF 取后端;
|
|||
|
|
/// 2. 当端点临近被其它动作条遮挡(或同泳道走线会穿过条块)时,退到该端部
|
|||
|
|
/// 的上/下角引出,再从角外绕行。
|
|||
|
|
///
|
|||
|
|
/// 走线分三种情形:同泳道(水平线或上下绕行)、相邻泳道(Z 形)、跨多行
|
|||
|
|
/// (外侧纵向走廊);同组自动错开 + [Dependency.routeOffset] 手动偏移。
|
|||
|
|
List<BarDepRoute> buildBarDepRoutes(Project project, BarLayout layout) {
|
|||
|
|
final Map<String, Rect> rectById = <String, Rect>{
|
|||
|
|
for (final Action a in project.actions) a.id: layout.rectFor(a),
|
|||
|
|
};
|
|||
|
|
final Map<String, int> laneIndex = <String, int>{
|
|||
|
|
for (final Action a in project.actions) a.id: layout.laneIndexOf(a.layer),
|
|||
|
|
};
|
|||
|
|
final Map<String, int> seen = <String, int>{};
|
|||
|
|
final List<BarDepRoute> out = <BarDepRoute>[];
|
|||
|
|
for (final Action to in layout.ordered) {
|
|||
|
|
for (final Dependency d in to.deps) {
|
|||
|
|
final Action? from = project.actionById(d.fromId);
|
|||
|
|
if (from == null) continue;
|
|||
|
|
final Rect? ra = rectById[from.id];
|
|||
|
|
final Rect? rb = rectById[to.id];
|
|||
|
|
if (ra == null || rb == null) continue;
|
|||
|
|
// 端点遮挡检查要包含“对方条块”:动作前后贴紧时,源端被目标条块
|
|||
|
|
// 挡住、目标端被源条块挡住,此时应退到上下角并从条块外侧绕行。
|
|||
|
|
final List<Rect> obstaclesForStart = <Rect>[
|
|||
|
|
for (final MapEntry<String, Rect> e in rectById.entries)
|
|||
|
|
if (e.key != from.id) e.value,
|
|||
|
|
];
|
|||
|
|
final List<Rect> obstaclesForEnd = <Rect>[
|
|||
|
|
for (final MapEntry<String, Rect> e in rectById.entries)
|
|||
|
|
if (e.key != to.id) e.value,
|
|||
|
|
];
|
|||
|
|
final int ia = laneIndex[from.id] ?? 0;
|
|||
|
|
final int ib = laneIndex[to.id] ?? 0;
|
|||
|
|
const double barHalf = BarLayout.barH / 2;
|
|||
|
|
final double sy = ra.center.dy;
|
|||
|
|
final double ty = rb.center.dy;
|
|||
|
|
final int dirY = ty > sy ? 1 : (ty < sy ? -1 : 0);
|
|||
|
|
final bool sameRow = dirY == 0;
|
|||
|
|
final bool adjacentLane = (ib - ia).abs() <= 1;
|
|||
|
|
// 端点语义:FS/FF 从后端出发,SS/SF 从前端出发;目标端 FS/SS 为前端,
|
|||
|
|
// FF/SF 为后端。
|
|||
|
|
final int fromSide =
|
|||
|
|
(d.type == DepType.fs || d.type == DepType.ff) ? 1 : -1;
|
|||
|
|
final int toSide =
|
|||
|
|
(d.type == DepType.fs || d.type == DepType.ss) ? -1 : 1;
|
|||
|
|
final String groupKey =
|
|||
|
|
sameRow ? 'S$ia' : (adjacentLane ? 'A$ia→$ib' : 'M$ia→$ib');
|
|||
|
|
final int k = seen.putIfAbsent(groupKey, () => 0);
|
|||
|
|
seen[groupKey] = k + 1;
|
|||
|
|
final double stagger = staggerOffset(k);
|
|||
|
|
// 同泳道无上下方向时,按走线偏移/错开量决定绕上还是绕下。
|
|||
|
|
final int cornerSide = dirY != 0
|
|||
|
|
? dirY
|
|||
|
|
: (stagger + d.routeOffset >= 0 ? 1 : -1);
|
|||
|
|
|
|||
|
|
if (sameRow) {
|
|||
|
|
// 前后动作紧贴(首尾相接)时,连线/箭头会挤在接缝处且不可读:
|
|||
|
|
// 隐藏走线,只在接缝处显示依赖标签。
|
|||
|
|
final bool tight =
|
|||
|
|
(rb.left - ra.right).abs() <= 4 || (ra.left - rb.right).abs() <= 4;
|
|||
|
|
if (tight) {
|
|||
|
|
final bool toRight = rb.center.dx >= ra.center.dx;
|
|||
|
|
final double junctionX =
|
|||
|
|
toRight ? (ra.right + rb.left) / 2 : (ra.left + rb.right) / 2;
|
|||
|
|
out.add(BarDepRoute(
|
|||
|
|
d,
|
|||
|
|
const <Offset>[],
|
|||
|
|
Offset.zero,
|
|||
|
|
const Offset(1, 0),
|
|||
|
|
Offset(junctionX, ra.top - 10),
|
|||
|
|
RouteAxis.vertical,
|
|||
|
|
junctionOnly: true,
|
|||
|
|
));
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
// 同泳道:默认一条水平线;端点被遮挡或语义端点背向对方时,从
|
|||
|
|
// 端部上/下角绕行(避免线穿过两个条块)。
|
|||
|
|
final bool sourceFaces =
|
|||
|
|
(rb.center.dx - ra.center.dx) * fromSide >= 0;
|
|||
|
|
final bool targetFaces =
|
|||
|
|
(ra.center.dx - rb.center.dx) * toSide >= 0;
|
|||
|
|
_BarEnd s = _startEnd(ra, fromSide, cornerSide, obstaclesForStart);
|
|||
|
|
_BarEnd t = _targetEnd(rb, toSide, cornerSide, obstaclesForEnd);
|
|||
|
|
final bool straight = !s.corner && !t.corner && sourceFaces && targetFaces;
|
|||
|
|
if (straight) {
|
|||
|
|
final double y = sy + stagger + d.routeOffset;
|
|||
|
|
final Offset sp = Offset(s.point.dx, y);
|
|||
|
|
final Offset tp = Offset(t.point.dx, y);
|
|||
|
|
out.add(BarDepRoute(
|
|||
|
|
d,
|
|||
|
|
<Offset>[sp, tp],
|
|||
|
|
tp,
|
|||
|
|
t.dir,
|
|||
|
|
Offset((sp.dx + tp.dx) / 2, y - (barHalf + 9)),
|
|||
|
|
RouteAxis.vertical,
|
|||
|
|
chipRun: (tp.dx - sp.dx).abs(),
|
|||
|
|
chipRunMargin: 8,
|
|||
|
|
chipAltAnchor: Offset(tp.dx + 6, y - (barHalf + 9)),
|
|||
|
|
));
|
|||
|
|
} else {
|
|||
|
|
// 只对背向对方 / 被遮挡的那一端退角,另一端仍从端点中心进出。
|
|||
|
|
if (!sourceFaces || s.corner) {
|
|||
|
|
s = _startEnd(ra, fromSide, cornerSide, obstaclesForStart, forceCorner: true);
|
|||
|
|
}
|
|||
|
|
if (!targetFaces || t.corner) {
|
|||
|
|
t = _targetEnd(rb, toSide, cornerSide, obstaclesForEnd, forceCorner: true);
|
|||
|
|
}
|
|||
|
|
final int side = s.corner
|
|||
|
|
? s.vSide
|
|||
|
|
: (t.corner ? t.vSide : cornerSide);
|
|||
|
|
double runY = sy + side * (barHalf + 10) + stagger + d.routeOffset;
|
|||
|
|
runY = side > 0
|
|||
|
|
? math.max(runY, ra.bottom + 6)
|
|||
|
|
: math.min(runY, ra.top - 6);
|
|||
|
|
final double tx =
|
|||
|
|
t.corner ? t.point.dx : t.point.dx - t.dir.dx * 10;
|
|||
|
|
final List<Offset> pts = <Offset>[s.point, Offset(s.point.dx, runY), Offset(tx, runY)];
|
|||
|
|
if (t.corner) {
|
|||
|
|
pts.add(t.point);
|
|||
|
|
} else {
|
|||
|
|
pts.add(Offset(tx, t.point.dy));
|
|||
|
|
pts.add(t.point);
|
|||
|
|
}
|
|||
|
|
out.add(BarDepRoute(
|
|||
|
|
d,
|
|||
|
|
pts,
|
|||
|
|
t.point,
|
|||
|
|
t.dir,
|
|||
|
|
// 绕行线在条块外侧,标签也放到线外侧,避免压住动作条。
|
|||
|
|
Offset((ra.center.dx + rb.center.dx) / 2, runY + side * (barHalf + 9)),
|
|||
|
|
RouteAxis.vertical,
|
|||
|
|
chipRun: (tx - s.point.dx).abs(),
|
|||
|
|
chipRunMargin: 8,
|
|||
|
|
chipAltAnchor: Offset(
|
|||
|
|
t.point.dx + 6,
|
|||
|
|
t.corner ? t.point.dy + side * 10 : t.point.dy - 12,
|
|||
|
|
),
|
|||
|
|
));
|
|||
|
|
}
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
final _BarEnd s = _startEnd(ra, fromSide, cornerSide, obstaclesForStart);
|
|||
|
|
final _BarEnd t = _targetEnd(rb, toSide, -cornerSide, obstaclesForEnd);
|
|||
|
|
if (adjacentLane) {
|
|||
|
|
// 相邻泳道:端部引出后在两泳道之间的中线走水平段;同组错开 + 手动偏移。
|
|||
|
|
final double mid = (sy + ty) / 2 + stagger + d.routeOffset;
|
|||
|
|
final List<Offset> pts = <Offset>[s.point];
|
|||
|
|
if (!s.corner) {
|
|||
|
|
final double sx2 = s.point.dx + s.dir.dx * 10;
|
|||
|
|
pts.add(Offset(sx2, s.point.dy));
|
|||
|
|
pts.add(Offset(sx2, mid));
|
|||
|
|
} else {
|
|||
|
|
pts.add(Offset(s.point.dx, mid));
|
|||
|
|
}
|
|||
|
|
final double tx2 = t.corner ? t.point.dx : t.point.dx - t.dir.dx * 10;
|
|||
|
|
pts.add(Offset(tx2, mid));
|
|||
|
|
if (t.corner) {
|
|||
|
|
pts.add(t.point);
|
|||
|
|
} else {
|
|||
|
|
pts.add(Offset(tx2, t.point.dy));
|
|||
|
|
pts.add(t.point);
|
|||
|
|
}
|
|||
|
|
out.add(BarDepRoute(
|
|||
|
|
d,
|
|||
|
|
pts,
|
|||
|
|
t.point,
|
|||
|
|
t.dir,
|
|||
|
|
Offset((ra.center.dx + rb.center.dx) / 2, mid),
|
|||
|
|
RouteAxis.vertical,
|
|||
|
|
chipRun: (tx2 - s.point.dx).abs(),
|
|||
|
|
chipRunMargin: 10,
|
|||
|
|
chipAltAnchor: Offset(
|
|||
|
|
t.point.dx + 6,
|
|||
|
|
t.point.dy + (t.corner ? t.dir.dy * 10 : -12),
|
|||
|
|
),
|
|||
|
|
));
|
|||
|
|
} else {
|
|||
|
|
// 跨多行:扫描区间内所有条块边界,在外侧选一条纵向走廊;同组按
|
|||
|
|
// ±12px 错开走廊,手动偏移作用于走廊横向位置。
|
|||
|
|
final double sx = s.point.dx;
|
|||
|
|
final double tx = t.point.dx;
|
|||
|
|
double maxRight = 0;
|
|||
|
|
double minLeft = double.infinity;
|
|||
|
|
final List<int> lanes = layout.lanes;
|
|||
|
|
for (int r = math.min(ia, ib) + 1; r < math.max(ia, ib); r++) {
|
|||
|
|
if (r < 0 || r >= lanes.length) continue;
|
|||
|
|
for (final Action aa in layout.actionsInLane(lanes[r])) {
|
|||
|
|
final Rect rr = layout.rectFor(aa);
|
|||
|
|
maxRight = math.max(maxRight, rr.right);
|
|||
|
|
minLeft = math.min(minLeft, rr.left);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
final double shift = stagger * 2 + d.routeOffset;
|
|||
|
|
double lane = math.max(math.max(sx, tx), maxRight) + 28 + shift;
|
|||
|
|
if (lane > layout.plotRight - 16) {
|
|||
|
|
lane = math.min(math.min(sx, tx), minLeft) - 28 - shift;
|
|||
|
|
}
|
|||
|
|
final double srcGapY = sy + dirY * (barHalf + 14);
|
|||
|
|
final double gapY = ty - dirY * (barHalf + 6);
|
|||
|
|
final List<Offset> pts = <Offset>[s.point];
|
|||
|
|
if (!s.corner) {
|
|||
|
|
final double sx2 = s.point.dx + s.dir.dx * 10;
|
|||
|
|
pts.add(Offset(sx2, s.point.dy));
|
|||
|
|
pts.add(Offset(sx2, srcGapY));
|
|||
|
|
} else {
|
|||
|
|
pts.add(Offset(s.point.dx, srcGapY));
|
|||
|
|
}
|
|||
|
|
pts.add(Offset(lane, srcGapY));
|
|||
|
|
pts.add(Offset(lane, gapY));
|
|||
|
|
if (t.corner) {
|
|||
|
|
pts.add(Offset(t.point.dx, gapY));
|
|||
|
|
pts.add(t.point);
|
|||
|
|
} else {
|
|||
|
|
final double tx2 = t.point.dx - t.dir.dx * 10;
|
|||
|
|
pts.add(Offset(tx2, gapY));
|
|||
|
|
pts.add(Offset(tx2, t.point.dy));
|
|||
|
|
pts.add(t.point);
|
|||
|
|
}
|
|||
|
|
out.add(BarDepRoute(
|
|||
|
|
d,
|
|||
|
|
pts,
|
|||
|
|
t.point,
|
|||
|
|
t.dir,
|
|||
|
|
Offset((lane + tx) / 2, gapY - (barHalf + 1)),
|
|||
|
|
RouteAxis.horizontal,
|
|||
|
|
));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return out;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// 命中条形依赖走线:返回最近且距离小于 [maxDist] 的走线(无命中返回 null)。
|
|||
|
|
BarDepRoute? hitBarDep(List<BarDepRoute> routes, Offset p, double maxDist) {
|
|||
|
|
BarDepRoute? best;
|
|||
|
|
double bestD = maxDist;
|
|||
|
|
for (final BarDepRoute r in routes) {
|
|||
|
|
final double d = distanceToPolyline(r.points, p);
|
|||
|
|
if (d < bestD) {
|
|||
|
|
bestD = d;
|
|||
|
|
best = r;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return best;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ===================== 圆周视图 =====================
|
|||
|
|
|
|||
|
|
/// 圆周视图的一条依赖走线(径向 → 圆弧 → 径向)。
|
|||
|
|
class ArcDepRoute {
|
|||
|
|
/// 构造。
|
|||
|
|
ArcDepRoute(this.dep, this.center, this.rA, this.rB, this.rC, this.rTip, this.aEnd, this.aStart,
|
|||
|
|
this.degenerate, this.cw, this.large, this.chipR, this.chipDeg,
|
|||
|
|
{this.junctionOnly = false});
|
|||
|
|
|
|||
|
|
/// 对应依赖。
|
|||
|
|
final Dependency dep;
|
|||
|
|
/// 圆心。
|
|||
|
|
final Offset center;
|
|||
|
|
/// 源环半径。
|
|||
|
|
final double rA;
|
|||
|
|
/// 目标环半径。
|
|||
|
|
final double rB;
|
|||
|
|
/// 绕行半径(自动错开 + 手动偏移作用于此)。
|
|||
|
|
final double rC;
|
|||
|
|
/// 箭头落点半径。
|
|||
|
|
final double rTip;
|
|||
|
|
/// 源弧末端角(度)。
|
|||
|
|
final double aEnd;
|
|||
|
|
/// 目标弧起点角(度)。
|
|||
|
|
final double aStart;
|
|||
|
|
/// 起止角几乎重合(退化)时改画短箭头。
|
|||
|
|
final bool degenerate;
|
|||
|
|
/// 圆弧是否顺时针。
|
|||
|
|
final bool cw;
|
|||
|
|
/// 是否大弧。
|
|||
|
|
final int large;
|
|||
|
|
/// 标签极径。
|
|||
|
|
final double chipR;
|
|||
|
|
/// 标签极角。
|
|||
|
|
final double chipDeg;
|
|||
|
|
|
|||
|
|
/// 动作紧贴时只显示连接点标签,隐藏圆弧与箭头。
|
|||
|
|
final bool junctionOnly;
|
|||
|
|
|
|||
|
|
/// 依赖键 `from->to`。
|
|||
|
|
String get key => '${dep.fromId}->${dep.toId}';
|
|||
|
|
|
|||
|
|
/// 起点(源环上)。
|
|||
|
|
Offset get p0 => polar(center, rA, aEnd);
|
|||
|
|
/// 绕行起点(半径 rC 上)。
|
|||
|
|
Offset get p1 => polar(center, rC, aEnd);
|
|||
|
|
/// 绕行终点(半径 rC 上)。
|
|||
|
|
Offset get p2 => polar(center, rC, aStart);
|
|||
|
|
/// 箭头端点。
|
|||
|
|
Offset get p3 => polar(center, rTip, aStart);
|
|||
|
|
/// 退化短箭头的起点。
|
|||
|
|
Offset get q0 => polar(center, rTip - (rTip > rB ? 28 : -28), aStart);
|
|||
|
|
|
|||
|
|
/// 生成绘制路径(绘制与命中测试同源)。
|
|||
|
|
Path buildPath() {
|
|||
|
|
final Path p = Path();
|
|||
|
|
if (degenerate) {
|
|||
|
|
p.moveTo(q0.dx, q0.dy);
|
|||
|
|
p.lineTo(p3.dx, p3.dy);
|
|||
|
|
return p;
|
|||
|
|
}
|
|||
|
|
final Offset a0 = p0;
|
|||
|
|
final Offset a1 = p1;
|
|||
|
|
final Offset a2 = p2;
|
|||
|
|
final Offset a3 = p3;
|
|||
|
|
p.moveTo(a0.dx, a0.dy);
|
|||
|
|
p.lineTo(a1.dx, a1.dy);
|
|||
|
|
p.arcToPoint(a2, radius: Radius.circular(rC), largeArc: large == 1, clockwise: cw);
|
|||
|
|
p.lineTo(a3.dx, a3.dy);
|
|||
|
|
return p;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// 箭头方向(分量式)。
|
|||
|
|
Offset get tipDir => degenerate ? (p3 - q0) : (p3 - p2);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// 计算圆周视图的全部依赖走线(含自动错开与手动偏移)。
|
|||
|
|
List<ArcDepRoute> buildArcDepRoutes(Project project, CircularLayout layout) {
|
|||
|
|
final Map<String, int> seen = <String, int>{};
|
|||
|
|
final List<ArcDepRoute> out = <ArcDepRoute>[];
|
|||
|
|
for (final Action to in project.actions) {
|
|||
|
|
for (final Dependency d in to.deps) {
|
|||
|
|
final Action? a = project.actionById(d.fromId);
|
|||
|
|
final Action? b = project.actionById(d.toId);
|
|||
|
|
if (a == null || b == null) continue;
|
|||
|
|
final double rA = layout.radiusFor(a.layer, layout.slotOf(a));
|
|||
|
|
final double rB = layout.radiusFor(b.layer, layout.slotOf(b));
|
|||
|
|
final double aEnd = math.min(a.endAngle, 360);
|
|||
|
|
final double aStart = b.startAngle;
|
|||
|
|
// 层差大于 1 时从最外圈绕行,避免穿过中间层弧线。
|
|||
|
|
final bool skip = (a.layer - b.layer).abs() > 1;
|
|||
|
|
final String groupKey = '${skip ? 'K' : 'N'}${a.layer}→${b.layer}';
|
|||
|
|
final int k = seen.putIfAbsent(groupKey, () => 0);
|
|||
|
|
seen[groupKey] = k + 1;
|
|||
|
|
// 同一环 / 相邻环的依赖如果画在环半径上会压住动作弧:把绕行半径
|
|||
|
|
// 放到环间空隙(同一环时即为外侧空隙),标签也跟随到空隙里。
|
|||
|
|
final double ringGap = math.max(layout.pitch - layout.th, 8);
|
|||
|
|
final bool sameRing = (rA - rB).abs() < layout.pitch * 0.35;
|
|||
|
|
final double baseC = skip
|
|||
|
|
? layout.rMax + 24
|
|||
|
|
: (sameRing ? rA + ringGap * 0.55 : (rA + rB) / 2);
|
|||
|
|
// 自动错开(步长 9px)+ 手动径向偏移(圆周视图的“法线”即半径方向)。
|
|||
|
|
double rC = baseC + staggerOffset(k) * 1.5 + d.routeOffset;
|
|||
|
|
rC = rC.clamp(18.0, layout.rMax + 170);
|
|||
|
|
final double rTip = rC < rB ? rB - layout.th / 2 - 4 : rB + layout.th / 2 + 4;
|
|||
|
|
final bool cw = aStart >= aEnd;
|
|||
|
|
final int large = (aStart - aEnd).abs() > 180 ? 1 : 0;
|
|||
|
|
final bool degenerate = (aStart - aEnd).abs() < 0.5;
|
|||
|
|
final bool junctionOnly = (aStart - aEnd).abs() <= 2.0;
|
|||
|
|
final double chipDeg = degenerate ? aStart : (aEnd + aStart) / 2;
|
|||
|
|
final double chipR = rC;
|
|||
|
|
out.add(ArcDepRoute(
|
|||
|
|
d,
|
|||
|
|
layout.center,
|
|||
|
|
rA,
|
|||
|
|
rB,
|
|||
|
|
rC,
|
|||
|
|
rTip,
|
|||
|
|
aEnd,
|
|||
|
|
aStart,
|
|||
|
|
degenerate,
|
|||
|
|
cw,
|
|||
|
|
large,
|
|||
|
|
chipR,
|
|||
|
|
chipDeg,
|
|||
|
|
junctionOnly: junctionOnly));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return out;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// 命中圆周依赖走线:返回最近且距离小于 [maxDist] 的走线(无命中返回 null)。
|
|||
|
|
ArcDepRoute? hitArcDep(List<ArcDepRoute> routes, Offset p, double maxDist) {
|
|||
|
|
ArcDepRoute? best;
|
|||
|
|
double bestD = maxDist;
|
|||
|
|
for (final ArcDepRoute r in routes) {
|
|||
|
|
final double d = distanceToPath(r.buildPath(), p);
|
|||
|
|
if (d < bestD) {
|
|||
|
|
bestD = d;
|
|||
|
|
best = r;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return best;
|
|||
|
|
}
|