Files
EasyDesignCollection/lib/layout/dep_routes.dart
T
admin b902fa06c4 feat(deps): 依赖连线自动错开 + 拖拽调整走线(三种视图);发布 v1.3.3
- 新增共享走线模块 lib/layout/dep_routes.dart:条形 / 圆周的走线、标签
  挂钩与外移判定、命中测试统一到同一份几何(绘制与命中不再各算一套);
- 自动错开:同一走线分组(同泳道 / 相邻泳道对 / 跨行对)内的多条依赖
  依次 ±6px 错开,解决“多条线完全重叠”;
- 手动走线偏移:Dependency.routeOffset(随工程保存)——
  · 条形 / 流程图:上下拖动平移横线,跨行走廊为左右拖动;
  · 圆周:沿半径方向(法线)拖动;
  · 直角拐点随平移保留,双击连线或右键「复位走线偏移」恢复自动位置;
  · 选中连线以强调色加粗高亮,状态栏给出操作提示;
- 画布命中判定改用「按下位置」(此前用手势验收点,细目标会抓空),
  端点把手与连线重叠时按“谁更近”判定优先级;
- 帮助中心补充「连线重叠怎么办」;版本号 1.3.3+19;
  更新说明 release/RELEASE_NOTES_v1.3.3.md。
2026-10-09 21:27:40 +08:00

361 lines
13 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/// 依赖连线的共享走线计算。
///
/// 条形视图 / 圆周视图的**画布绘制、标签放置与命中测试**(点选连线、
/// 沿法线拖拽调整走线)都使用这里的同一份几何,避免绘制与命中不一致:
/// - 自动错开:同一走线分组内的多条依赖依次错开,先解决“完全重叠”;
/// - 手动偏移:[Dependency.routeOffset] 叠加在自动错开之上(拖拽连线的结果)。
library;
import 'dart:math' as math;
import 'dart:ui';
import '../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});
/// 对应依赖。
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;
/// 依赖键 `from->to`。
String get key => '${dep.fromId}->${dep.toId}';
}
/// 计算条形视图的全部依赖走线(含自动错开与手动偏移)。
///
/// 走线分三种情形:同泳道(水平线)、相邻泳道(Z 形)、跨多行(外侧纵向走廊),
/// 与绘制旧的实现保持一致;新增:同组自动错开 + [Dependency.routeOffset] 手动偏移。
List<BarDepRoute> buildBarDepRoutes(Project project, BarLayout layout) {
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 int ia = laneIndex[from.id] ?? 0;
final int ib = laneIndex[to.id] ?? 0;
final Rect ra = layout.rectFor(from);
final Rect rb = layout.rectFor(to);
const double barHalf = BarLayout.barH / 2;
final double sy = ra.center.dy;
final double ty = rb.center.dy;
final double dir = ty > sy ? 1 : -1;
final double tipY = ty - dir * (barHalf + 3);
final bool sameLane = (ty - sy).abs() < 1;
final bool fs = d.type == DepType.fs;
if (sameLane) {
// 同泳道:一条水平线;同组多条依次上下错开。
final int k = seen.putIfAbsent('S$ia', () => 0);
seen['S$ia'] = k + 1;
final double y = sy + staggerOffset(k) + d.routeOffset;
final double startX = fs ? ra.right + 2 : ra.left - 2;
double endX = rb.left - 2;
if (endX < startX) endX = rb.right + 2;
out.add(BarDepRoute(
d,
<Offset>[Offset(startX, y), Offset(endX, y)],
Offset(endX, y),
Offset(endX >= startX ? 1 : -1, 0),
Offset((startX + endX) / 2, y - (barHalf + 9)),
RouteAxis.vertical,
// 行程太短时标签移出到箭头右侧(宽度由绘制端精确测量)。
chipRun: (endX - startX).abs(),
chipRunMargin: 8,
chipAltAnchor: Offset(endX + 6, y - (barHalf + 9)),
));
} else if ((ib - ia).abs() <= 1) {
// 相邻泳道:在两泳道之间的空隙中线走水平线;同组错开 + 手动偏移。
final int k = seen.putIfAbsent('A$ia→$ib', () => 0);
seen['A$ia→$ib'] = k + 1;
final double mid = (sy + ty) / 2 + staggerOffset(k) + d.routeOffset;
final double sx = fs ? ra.right : ra.left;
final double ex = fs ? sx - 9 : sx + 9;
final double tx = rb.left + 14;
out.add(BarDepRoute(
d,
<Offset>[
Offset(ex, sy + dir * (barHalf + 2)),
Offset(ex, mid),
Offset(tx, mid),
Offset(tx, tipY),
],
Offset(tx, tipY),
Offset(0, dir),
Offset((ex + tx) / 2, mid),
RouteAxis.vertical,
chipRun: (tx - ex).abs(),
chipRunMargin: 10,
chipAltAnchor: Offset(tx + 6, mid),
));
} else {
// 跨多行:扫描区间内所有条块边界,据此在外侧选一条纵向走廊;
// 同组按 ±12px 错开走廊,手动偏移作用于走廊的横向位置。
final int k = seen.putIfAbsent('M$ia→$ib', () => 0);
seen['M$ia→$ib'] = k + 1;
final double sx = fs ? ra.right : ra.left;
final double ex = fs ? sx - 9 : sx + 9;
final double tx = rb.left + 14;
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 = staggerOffset(k) * 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 + dir * (barHalf + 14);
final double gapY = ty - dir * (barHalf + 6);
out.add(BarDepRoute(
d,
<Offset>[
Offset(ex, sy + dir * (barHalf + 2)),
Offset(ex, srcGapY),
Offset(lane, srcGapY),
Offset(lane, gapY),
Offset(tx, gapY),
Offset(tx, tipY),
],
Offset(tx, tipY),
Offset(0, 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);
/// 对应依赖。
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;
/// 依赖键 `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);
final double rB = layout.radiusFor(b.layer);
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;
// 自动错开(步长 9px)+ 手动径向偏移(圆周视图的“法线”即半径方向)。
double rC = (skip ? layout.rMax + 24 : (rA + rB) / 2) + 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 double chipDeg = degenerate ? aStart : (aEnd + aStart) / 2;
double chipR;
if (degenerate) {
chipR = (rA + rB) / 2;
} else {
final double span = (aStart - aEnd).abs();
chipR = skip ? rC : (span < 30 ? rB + layout.th / 2 + 10 : rC);
}
out.add(ArcDepRoute(
d, layout.center, rA, rB, rC, rTip, aEnd, aStart, degenerate, cw, large, chipR, chipDeg));
}
}
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;
}