Files
EasyDesignCollection/lib/render/flow_painter.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

287 lines
11 KiB
Dart

/// 流程图绘制器:把时序图渲染为纵向泳道流程图(只读视图)。
///
/// 视觉体系与条形 / 圆周视图一致:泳道背景用环带色、依赖用 FS 绿 / SS 琥珀、
/// 关键路径加粗带光晕、选中节点高亮描边。
library;
import 'dart:math' as math;
import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import '../layout/flow_layout.dart';
import '../model/models.dart';
import '../theme/tokens.dart';
/// 流程图 [CustomPainter] 实现。
class FlowchartPainter extends CustomPainter {
/// 构造。
const FlowchartPainter({
required this.project,
required this.tokens,
required this.criticalActions,
required this.criticalDeps,
required this.selectedIds,
this.selectedDep,
});
/// 项目数据。
final Project project;
/// 主题令牌。
final Tokens tokens;
/// 关键路径动作 id。
final Set<String> criticalActions;
/// 关键路径依赖(`from->to`)。
final Set<String> criticalDeps;
/// 选中动作 id。
final Set<String> selectedIds;
/// 当前选中的依赖连线键(`from->to`);非空时该走线以强调色高亮。
final String? selectedDep;
@override
void paint(Canvas canvas, Size size) {
final FlowLayout layout = FlowLayout(
project,
criticalActions: criticalActions,
criticalDeps: criticalDeps,
);
if (layout.nodes.isEmpty) return;
_phaseRail(canvas, layout);
_lanes(canvas, layout);
_edges(canvas, layout);
_nodes(canvas, layout);
}
/// 绘制左侧相位阶段栏:把相位色带「竖起来」——窄竖带 + 旋转 90° 的分区名,
/// 纵向与流程行严格对齐;重叠的分区台阶式向右错位(每级 20px)。
void _phaseRail(Canvas canvas, FlowLayout layout) {
if (layout.spans.isEmpty) return;
final double rowPitch = FlowLayout.nodeH + FlowLayout.rankGap;
for (final FlowPhaseSpan s in layout.spans) {
// 上下边界对齐到动作行边缘(统一 6px 内缩)。
final double top = FlowLayout.marginY + s.rankTop * rowPitch - 6;
final double bottom =
FlowLayout.marginY + s.rankBottom * rowPitch + FlowLayout.nodeH + 6;
final double left = FlowLayout.railX + s.slot * FlowLayout.railStep;
final Rect r = Rect.fromLTRB(left, top, left + FlowLayout.railW, bottom);
final RRect rr = RRect.fromRectAndRadius(r, const Radius.circular(9));
// 视觉语言与条形视图的相位色带一致(强调色低透明度 + 细描边)。
canvas.drawRRect(rr, Paint()..color = tokens.accent.withValues(alpha: 0.10));
canvas.drawRRect(
rr,
Paint()
..style = PaintingStyle.stroke
..strokeWidth = 1
..color = tokens.accent.withValues(alpha: 0.45),
);
// 分区名:旋转 90° 竖排(从下往上读),居中于竖带。
final TextPainter tp = _tp(s.zone.name, 10.5, tokens.sub, FontWeight.w700);
canvas.save();
canvas.translate(r.center.dx, r.center.dy);
canvas.rotate(-math.pi / 2);
tp.paint(canvas, Offset(-tp.width / 2, -tp.height / 2));
canvas.restore();
}
}
/// 绘制泳道背景带与标题(泳道 = 轨道)。
void _lanes(Canvas canvas, FlowLayout layout) {
final double lanePitch = FlowLayout.nodeW + FlowLayout.laneGap;
final double top = layout.bounds.top + 20;
final double bottom = layout.bounds.bottom - 20;
for (int i = 0; i < layout.lanes.length; i++) {
final double cx = FlowLayout.marginX + i * lanePitch + FlowLayout.nodeW / 2;
final Rect band = Rect.fromLTRB(
cx - FlowLayout.nodeW / 2 - 26,
top,
cx + FlowLayout.nodeW / 2 + 26,
bottom,
);
canvas.drawRRect(
RRect.fromRectAndRadius(band, const Radius.circular(14)),
Paint()..color = (i.isEven ? tokens.ringA : tokens.ringB).withValues(alpha: 0.55),
);
_text(canvas, '轨道 L${i + 1}', Offset(cx, top + 16),
color: tokens.sub, size: 11, weight: FontWeight.w700, align: TextAlign.center);
}
}
/// 绘制连线:FS 实线、SS 虚线(带偏移角标注)、关键路径加粗光晕。
///
/// 走线由 [FlowLayout.routes] 统一计算(与 SVG / drawio 导出共用同一几何)。
void _edges(Canvas canvas, FlowLayout layout) {
for (final FlowEdgeRoute r in layout.routes) {
final FlowEdge e = r.edge;
final Color col = e.type == null
? tokens.sub
: (e.type == DepType.fs ? tokens.fs : tokens.ss);
// 选中的依赖连线以强调色加粗高亮(拖拽调整走线时可见)。
final bool sel = selectedDep == r.key;
final Color draw = sel ? tokens.accent : col;
final Path p = Path()..moveTo(r.points.first.dx, r.points.first.dy);
for (int i = 1; i < r.points.length; i++) {
p.lineTo(r.points[i].dx, r.points[i].dy);
}
if (e.critical) {
canvas.drawPath(
p,
Paint()
..style = PaintingStyle.stroke
..strokeWidth = 5
..strokeJoin = StrokeJoin.round
..strokeCap = StrokeCap.round
..color = col.withValues(alpha: 0.18),
);
}
final Paint line = Paint()
..style = PaintingStyle.stroke
..strokeWidth = sel ? 2.4 : (e.critical ? 1.8 : 1.2)
..strokeJoin = StrokeJoin.round
..strokeCap = StrokeCap.round
..color = sel ? draw : draw.withValues(alpha: e.type == null ? 0.7 : 0.95);
if (r.dashed) {
_dashPath(canvas, p, line);
} else {
canvas.drawPath(p, line);
}
_arrow(canvas, r.tip, r.dir, draw);
// FS / SS+偏移 标签
if (e.type != null) {
final String text = switch (e.type!) {
DepType.fs => 'FS',
DepType.ss => 'SS+${e.offset.toStringAsFixed(0)}°',
DepType.ff => 'FF',
DepType.sf => 'SF',
};
_chip(canvas, r.labelAt, text, draw);
}
}
}
/// 绘制节点:动作 = 圆角矩形(标题 + 时长/工位),开始 / 结束 = 胶囊。
void _nodes(Canvas canvas, FlowLayout layout) {
for (final FlowNode n in layout.nodes) {
final bool sel = n.action != null && selectedIds.contains(n.id);
final bool crit = n.action != null && criticalActions.contains(n.id);
if (n.isStart || n.isEnd) {
final RRect rr = RRect.fromRectAndRadius(n.rect, Radius.circular(n.rect.height / 2));
canvas.drawRRect(rr, Paint()..color = tokens.panel2);
canvas.drawRRect(
rr,
Paint()
..style = PaintingStyle.stroke
..strokeWidth = 1.2
..color = tokens.accent,
);
_text(canvas, n.title, n.rect.center,
color: tokens.accent, size: 13, weight: FontWeight.w700, align: TextAlign.center);
continue;
}
final Color c = Color(n.action!.color);
final RRect rr = RRect.fromRectAndRadius(n.rect, const Radius.circular(10));
canvas.drawRRect(
rr.shift(const Offset(1.5, 2)),
Paint()..color = tokens.shadow.withValues(alpha: tokens.shadowOp),
);
canvas.drawRRect(rr, Paint()..color = c.withValues(alpha: 0.92));
canvas.drawRRect(
rr,
Paint()
..style = PaintingStyle.stroke
..strokeWidth = sel ? 1.8 : 1
..color = sel ? tokens.accent : tokens.selOutline.withValues(alpha: 0.25),
);
if (crit) {
canvas.drawRRect(
rr,
Paint()
..style = PaintingStyle.stroke
..strokeWidth = 2
..color = tokens.crit,
);
}
_text(canvas, n.title, Offset(n.rect.center.dx, n.rect.top + 21),
color: tokens.arcText, size: 12, weight: FontWeight.w700, align: TextAlign.center);
_text(canvas, n.subtitle, Offset(n.rect.center.dx, n.rect.top + 41),
color: tokens.arcText.withValues(alpha: 0.85), size: 10, align: TextAlign.center);
}
}
/// 按虚线绘制路径(SS 依赖)。
void _dashPath(Canvas canvas, Path path, Paint paint, {double dash = 6, double gap = 4}) {
for (final ui.PathMetric metric in path.computeMetrics()) {
double dist = 0;
while (dist < metric.length) {
final double end = math.min(dist + dash, metric.length);
canvas.drawPath(metric.extractPath(dist, end), paint);
dist = end + gap;
}
}
}
/// 在 [tip] 处绘制指向 [dir] 方向的实心三角箭头。
void _arrow(Canvas canvas, Offset tip, Offset dir, Color col) {
const double s = 7;
final Offset b = tip - dir * s;
final Offset n = Offset(-dir.dy, dir.dx) * s * 0.5;
final Path p = Path()
..moveTo(tip.dx, tip.dy)
..lineTo(b.dx + n.dx, b.dy + n.dy)
..lineTo(b.dx - n.dx, b.dy - n.dy)
..close();
canvas.drawPath(p, Paint()..color = col);
}
/// 绘制 FS / SS 标签胶囊(与条形视图风格一致,字号略小)。
void _chip(Canvas canvas, Offset center, String text, Color col) {
final TextPainter tp = _tp(text, 8.5, col, FontWeight.w600);
final double w = tp.width + 12;
const double h = 14;
final Rect r = Rect.fromCenter(center: center, width: w, height: h);
canvas.drawRRect(
RRect.fromRectAndRadius(r, const Radius.circular(5)),
Paint()..color = tokens.chip,
);
canvas.drawRRect(
RRect.fromRectAndRadius(r, const Radius.circular(5)),
Paint()
..style = PaintingStyle.stroke
..strokeWidth = 1
..color = col,
);
tp.paint(canvas, Offset(r.left + 6, r.center.dy - tp.height / 2));
}
/// 构造并布局一个 [TextPainter](与其它视图一致的字体设置)。
TextPainter _tp(String s, double size, Color color, FontWeight weight,
{TextAlign align = TextAlign.left}) {
return TextPainter(
text: TextSpan(
text: s,
style: TextStyle(
color: color, fontSize: size, fontWeight: weight, fontFamily: 'Microsoft YaHei UI'),
),
textDirection: ui.TextDirection.ltr,
textAlign: align,
)..layout();
}
/// 便捷文本绘制(按 [align] 相对 [at] 对齐,垂直居中)。
void _text(Canvas canvas, String s, Offset at,
{required Color color,
double size = 12,
FontWeight weight = FontWeight.normal,
TextAlign align = TextAlign.left}) {
final TextPainter tp = _tp(s, size, color, weight, align: align);
double dx = at.dx;
if (align == TextAlign.center) dx -= tp.width / 2;
if (align == TextAlign.right) dx -= tp.width;
tp.paint(canvas, Offset(dx, at.dy - tp.height / 2));
}
/// 视图数据实时联动,始终重绘。
@override
bool shouldRepaint(covariant FlowchartPainter old) => true;
}