/// 流程图绘制器:把时序图渲染为纵向泳道流程图(只读视图)。 /// /// 视觉体系与条形 / 圆周视图一致:泳道背景用环带色、依赖用 FS 绿 / SS 琥珀、 /// 关键路径加粗带光晕、选中节点高亮描边。 library; import 'dart:math' as math; import 'dart:ui' as ui; import 'package:flutter/material.dart'; import 'package:sequence_diagram/modules/sequence/layout/flow_layout.dart'; import 'package:sequence_diagram/model/models.dart'; import 'package:sequence_diagram/model/units.dart'; import 'package:sequence_diagram/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 criticalActions; /// 关键路径依赖(`from->to`)。 final Set criticalDeps; /// 选中动作 id。 final Set 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 top = layout.bounds.top + 20; final double bottom = layout.bounds.bottom - 20; for (int i = 0; i < layout.lanes.length; i++) { final double left = layout.laneStartX(i) - 26; final double right = layout.laneStartX(i) + layout.laneWidth(i) + 26; final Rect band = Rect.fromLTRB(left, top, right, 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((left + right) / 2, 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 => e.offset == 0 ? 'SS' : 'SS+${deg2msStr(e.offset, project.units, project.cycle.durationMs, dec: 2)}', 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; }