v1.3.13: 模块化注册架构 + PDS流程设计套件更名 + 依赖/圆周交互修复
- 模块化:EdcModule 契约 + registry + 模块独立目录;发布包增加 modules/registry.json - 更名:PDS流程设计套件 / ProcessDesignSuite;同步构建、发布脚本与文档 - 时序图:依赖偏移以时间显示/编辑;紧贴依赖隐藏箭头并在接缝显示标签;动作条磁吸 - 圆周:依赖标签沿圆弧方向、环间空隙;动作旋转磁吸增强;短弧标签优先动作名 - 清理:release 移至源码同级(本地不入库);tool 合并到 tools;发布说明迁移 docs/release-notes
This commit is contained in:
@@ -0,0 +1,679 @@
|
||||
/// 条形(时间轴)视图绘制器。
|
||||
///
|
||||
/// 职责:把 [Project] 中的动作按“双轴”条形图渲染到 Flutter 画布上——
|
||||
/// 横轴同时表达相位角度与等价的绝对时间,纵轴按工位顺序排列动作行。
|
||||
///
|
||||
/// 架构位置:
|
||||
/// - 全部几何计算委托给 [BarLayout](`modules/sequence/layout/layout.dart`),
|
||||
/// 本绘制器只负责取几何结果并落笔,不自行推导坐标。
|
||||
/// - 颜色一律取自 [Tokens],因此与圆形视图及 SVG 导出保持同一视觉体系。
|
||||
/// - 关键路径信息([criticalActions]、[criticalDeps])由上层分析后传入,
|
||||
/// 本类不参与计算,仅负责高亮呈现。
|
||||
///
|
||||
/// 绘制顺序(见 [paint]):
|
||||
/// 相位带 → 时间/相位标尺 → 循环边界 → 动作行 → 依赖连线。
|
||||
library;
|
||||
|
||||
import 'dart:math' as math;
|
||||
import 'dart:ui' as ui;
|
||||
|
||||
import 'package:flutter/material.dart' hide Action;
|
||||
|
||||
import 'package:sequence_diagram/modules/sequence/layout/dep_routes.dart';
|
||||
import 'package:sequence_diagram/modules/sequence/layout/layout.dart';
|
||||
import 'package:sequence_diagram/model/models.dart';
|
||||
import 'package:sequence_diagram/model/units.dart';
|
||||
import 'package:sequence_diagram/theme/tokens.dart';
|
||||
|
||||
/// 条形时间轴视图的 [CustomPainter] 实现。
|
||||
///
|
||||
/// 坐标约定:横轴 `x` 表示 0°–360° 相位(并线性映射到 0–cycle 时间),
|
||||
/// 纵轴 `y` 表示动作行。角度到像素的换算由 [BarLayout.xFor] 完成。
|
||||
class BarPainter extends CustomPainter {
|
||||
/// 创建条形视图绘制器。
|
||||
///
|
||||
/// 参数:
|
||||
/// - [project]:待绘制的工程数据(动作、工位、依赖、视图开关)。
|
||||
/// - [tokens]:当前主题令牌,决定所有颜色。
|
||||
/// - [criticalActions]:关键路径上的动作 id 集合,用于描边高亮。
|
||||
/// - [criticalDeps]:关键路径上的依赖集合,键格式 `fromId->toId`。
|
||||
/// - [selectedId]:当前选中动作 id,可为空。
|
||||
/// - [selectedDep]:当前选中的依赖连线键(`from->to`),用于高亮。
|
||||
///
|
||||
/// 无副作用;实例不缓存布局,每次 [paint] 都重新计算。
|
||||
BarPainter({
|
||||
required this.project,
|
||||
required this.tokens,
|
||||
required this.criticalActions,
|
||||
required this.criticalDeps,
|
||||
required this.selectedIds,
|
||||
this.selectedDep,
|
||||
this.guideAngle,
|
||||
this.dropLane,
|
||||
this.dropValid = true,
|
||||
});
|
||||
|
||||
/// 待绘制的工程数据源。
|
||||
final Project project;
|
||||
/// 当前主题令牌。
|
||||
final Tokens tokens;
|
||||
/// 关键路径上的动作 id 集合。
|
||||
final Set<String> criticalActions;
|
||||
/// 关键路径上的依赖边集合,元素格式为 `fromId->toId`。
|
||||
final Set<String> criticalDeps;
|
||||
/// 当前选中的动作 id 集合(支持多选)。
|
||||
final Set<String> selectedIds;
|
||||
|
||||
/// 当前选中的依赖连线键(`from->to`);非空时该走线以强调色高亮。
|
||||
final String? selectedDep;
|
||||
|
||||
/// 拖拽相位分区边界时的参考角(度);非空时绘制一条竖直虚线参考。
|
||||
final double? guideAngle;
|
||||
|
||||
/// 上下拖拽换泳道时,光标所在的目标泳道号;非空时高亮该泳道。
|
||||
final int? dropLane;
|
||||
|
||||
/// 目标泳道是否可放置(时间不重叠);false 时用警示色高亮。
|
||||
final bool dropValid;
|
||||
|
||||
/// 标尺横线所在的 y 坐标(画布坐标)。
|
||||
static const double axisTop = 164;
|
||||
/// 相位色带的顶部 y 坐标。
|
||||
static const double phaseBandTop = 92;
|
||||
/// 相位刻度值所在行的 y(分区带与刻度线之间,上排)。
|
||||
static const double phaseValY = 136;
|
||||
/// 时间刻度值所在行的 y(分区带与刻度线之间,下排)。
|
||||
static const double timeValY = 150;
|
||||
|
||||
/// 绘制整张条形视图。
|
||||
///
|
||||
/// 参数 [canvas]:目标画布;[size]:可用尺寸(用于构建 [BarLayout])。
|
||||
/// 副作用:向 [canvas] 写入若干图元,不修改任何模型状态。
|
||||
/// 图层顺序遵循“先背景后前景”:相位带、标尺、边界、动作行、依赖。
|
||||
@override
|
||||
void paint(Canvas canvas, Size size) {
|
||||
final BarLayout layout = BarLayout(project, size);
|
||||
final UnitSettings u = project.units;
|
||||
final double cycle = project.cycle.durationMs;
|
||||
|
||||
if (project.viewState.showPhaseBands) _phaseBands(canvas, layout);
|
||||
// 换泳道拖拽:高亮目标泳道(可放置=强调色,重叠=警示色)。
|
||||
// 泳道加宽(多槽位)后高亮覆盖该泳道的全部槽位。
|
||||
if (dropLane != null && layout.lanes.contains(dropLane)) {
|
||||
final double y = layout.yForLane(dropLane!);
|
||||
final double h = layout.slotsOf(dropLane!) * layout.pitch;
|
||||
final Color c = dropValid ? tokens.accent : tokens.danger;
|
||||
canvas.drawRRect(
|
||||
RRect.fromRectAndRadius(
|
||||
Rect.fromLTWH(20, y - 8, layout.plotRight - 20, h),
|
||||
const Radius.circular(10),
|
||||
),
|
||||
Paint()..color = c.withValues(alpha: 0.12),
|
||||
);
|
||||
canvas.drawRRect(
|
||||
RRect.fromRectAndRadius(
|
||||
Rect.fromLTWH(20, y - 8, layout.plotRight - 20, h),
|
||||
const Radius.circular(10),
|
||||
),
|
||||
Paint()
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = 1.4
|
||||
..color = c.withValues(alpha: 0.8),
|
||||
);
|
||||
}
|
||||
if (project.viewState.showRuler) _ruler(canvas, layout, u, cycle);
|
||||
_boundaries(canvas, layout);
|
||||
_rows(canvas, layout, u, cycle);
|
||||
if (project.viewState.showDeps) _deps(canvas, layout, u);
|
||||
// 分区拖拽参考线绘制在最上层,保证清晰可见。
|
||||
if (guideAngle != null) _guide(canvas, layout, u);
|
||||
}
|
||||
|
||||
/// 绘制相位分区拖拽时的竖直虚线参考线,并在顶部标注当前角度。
|
||||
void _guide(Canvas canvas, BarLayout layout, UnitSettings u) {
|
||||
final double deg = guideAngle!;
|
||||
final double x = layout.xFor(deg.clamp(0, 360));
|
||||
final double bottom = layout.plotBottom - 4;
|
||||
final Paint p = Paint()
|
||||
..color = tokens.accent
|
||||
..strokeWidth = 1.4;
|
||||
// 虚线:从相位带一直画到图表底部。
|
||||
const double dash = 6;
|
||||
const double gap = 6;
|
||||
double y = phaseBandTop - 8;
|
||||
while (y < bottom) {
|
||||
canvas.drawLine(Offset(x, y), Offset(x, math.min(y + dash, bottom)), p);
|
||||
y += dash + gap;
|
||||
}
|
||||
_text(canvas, '${deg.toStringAsFixed(1)}°', Offset(x, phaseBandTop - 18),
|
||||
color: tokens.accent, size: 11, align: TextAlign.center, weight: FontWeight.w700);
|
||||
}
|
||||
|
||||
/// 过滤出有效(跨度 > 0)的相位分区。
|
||||
///
|
||||
/// 相位分区来自 [Project.phaseZones],可在工艺参数面板中编辑;
|
||||
/// 这里只做基本合法性过滤,保证异常数据不会画出负宽度色带。
|
||||
List<PhaseZone> _phaseZones() {
|
||||
return project.phaseZones.where((PhaseZone z) => z.endAngle > z.startAngle).toList();
|
||||
}
|
||||
|
||||
/// 绘制相位色带。
|
||||
///
|
||||
/// 算法:把每个相位分区映射到横轴像素,绘制一个圆角矩形,填充强调色的
|
||||
/// 低透明度版本并描边;宽度足够(> 64px)时居中标注分区名称。
|
||||
/// 若分区跨越 360°(结束角 > 360),则在左侧补画从 0° 起的续段,
|
||||
/// 对应“跨循环”语义。
|
||||
void _phaseBands(Canvas canvas, BarLayout layout) {
|
||||
for (final PhaseZone z in _phaseZones()) {
|
||||
final double x0 = layout.xFor(z.startAngle);
|
||||
final double x1 = layout.xFor(z.endAngle);
|
||||
final Rect r = Rect.fromLTWH(x0, phaseBandTop, math.max(x1 - x0, 2), 32);
|
||||
canvas.drawRRect(
|
||||
RRect.fromRectAndRadius(r, const Radius.circular(6)),
|
||||
Paint()..color = tokens.accent.withValues(alpha: 0.08),
|
||||
);
|
||||
canvas.drawRRect(
|
||||
RRect.fromRectAndRadius(r, const Radius.circular(6)),
|
||||
Paint()
|
||||
..style = PaintingStyle.stroke
|
||||
..color = tokens.line2,
|
||||
);
|
||||
// 名称居中且按色带宽度截断,保证不越出色块。
|
||||
if (r.width > 26) {
|
||||
_textClipped(canvas, z.name, Offset(r.center.dx, r.center.dy), r.width - 10,
|
||||
color: tokens.sub, size: 11);
|
||||
}
|
||||
if (z.endAngle > 360) {
|
||||
final double wx = layout.xFor(z.endAngle - 360) - layout.plotLeft;
|
||||
canvas.drawRRect(
|
||||
RRect.fromRectAndRadius(Rect.fromLTWH(layout.plotLeft, phaseBandTop, wx, 32), const Radius.circular(6)),
|
||||
Paint()..color = tokens.accent.withValues(alpha: 0.08),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 绘制双轴标尺。
|
||||
///
|
||||
/// 每 15° 一个刻度,30° 为主刻度(更长);每 60° 标注文字:
|
||||
/// 下排为相位(度,强调色),上排为等价的绝对时间(由 [tvalStr] 格式化)。
|
||||
/// 参数 [u] 提供单位设置,[cycle] 为一个完整循环的毫秒数。
|
||||
void _ruler(Canvas canvas, BarLayout layout, UnitSettings u, double cycle) {
|
||||
canvas.drawLine(Offset(layout.plotLeft, axisTop), Offset(layout.plotRight, axisTop),
|
||||
Paint()..color = tokens.line2..strokeWidth = 1);
|
||||
for (int d = 0; d <= 360; d += 15) {
|
||||
final double x = layout.xFor(d.toDouble());
|
||||
final bool major = d % 30 == 0;
|
||||
canvas.drawLine(
|
||||
Offset(x, axisTop),
|
||||
Offset(x, axisTop + (major ? 28 : 14)),
|
||||
Paint()
|
||||
..color = major ? tokens.line2 : tokens.line
|
||||
..strokeWidth = 1,
|
||||
);
|
||||
if (d % 60 == 0) {
|
||||
// 上排:相位刻度值;下排:时间刻度值(均位于分区带与刻度线之间)。
|
||||
_text(canvas, pfmt(d.toDouble(), u, dec: 0), Offset(x, phaseValY),
|
||||
color: tokens.accent, size: 10, align: TextAlign.center);
|
||||
_text(canvas, tvalStr(d / 360 * cycle, u, dec: u.timeUnit == TimeUnit.ms ? 0 : 1),
|
||||
Offset(x, timeValY), color: tokens.sub, size: 10, align: TextAlign.center);
|
||||
}
|
||||
}
|
||||
// 轴单位图例:与对应刻度值同排,置于最左侧。
|
||||
_text(canvas, '相位 / ${phaseUnitLabel(u.phaseUnit)}', Offset(layout.plotLeft - 12, phaseValY),
|
||||
color: tokens.accent, size: 10, align: TextAlign.right);
|
||||
_text(canvas, '时间 / ${timeUnitLabel(u.timeUnit)}', Offset(layout.plotLeft - 12, timeValY),
|
||||
color: tokens.sub, size: 10, align: TextAlign.right);
|
||||
}
|
||||
|
||||
/// 绘制循环起止边界线(0° 与 360°)。
|
||||
///
|
||||
/// 0° 使用 [Tokens.ok]、360° 使用 [Tokens.warn] 的虚线竖线表示;
|
||||
/// 不再额外标注文字(坐标轴已能说明起止)。虚线由 [_dashedLine] 手工分段绘制。
|
||||
void _boundaries(Canvas canvas, BarLayout layout) {
|
||||
for (final double d in <double>[0, 360]) {
|
||||
final double x = layout.xFor(d);
|
||||
final Color col = d == 0 ? tokens.ok : tokens.warn;
|
||||
final Paint p = Paint()
|
||||
..color = col.withValues(alpha: 0.7)
|
||||
..strokeWidth = 1.4;
|
||||
_dashedLine(canvas, Offset(x, axisTop + 28), Offset(x, layout.plotBottom - 4), p);
|
||||
}
|
||||
}
|
||||
|
||||
/// 在两点之间绘制固定 dash/gap 的虚线。
|
||||
///
|
||||
/// 参数 [a]/[b]:线段端点;[p]:画笔(颜色与线宽由调用方决定)。
|
||||
/// 通过沿单位方向向量步进生成短线段,不依赖 dashPathEffect。
|
||||
void _dashedLine(Canvas canvas, Offset a, Offset b, Paint p) {
|
||||
const double dash = 6;
|
||||
const double gap = 6;
|
||||
final double total = (b - a).distance;
|
||||
final Offset dir = (b - a) / total;
|
||||
double t = 0;
|
||||
while (t < total) {
|
||||
final double end = math.min(t + dash, total);
|
||||
canvas.drawLine(a + dir * t, a + dir * end, p);
|
||||
t = end + gap;
|
||||
}
|
||||
}
|
||||
|
||||
/// 绘制所有动作行。
|
||||
///
|
||||
/// 每行包含:
|
||||
/// - 隔行背景(偶数行填充 [Tokens.panel2] 半透明色,便于横向阅读);
|
||||
/// - 左侧序号色块,以及 `序号. id 名称`、`工位 · 驱动` 文本;
|
||||
/// - 行底分隔线;
|
||||
/// - 主条块(带阴影、选中描边、关键路径描边);
|
||||
/// - 跨循环延续段(当 [Action.wrap] 且开启自动拆分时);
|
||||
/// - 条块内或条块外的名称与时长标注。
|
||||
///
|
||||
/// 参数 [layout] 提供行列几何,[u]/[cycle] 用于时长文本换算。
|
||||
void _rows(Canvas canvas, BarLayout layout, UnitSettings u, double cycle) {
|
||||
final List<int> lanes = layout.lanes;
|
||||
for (int li = 0; li < lanes.length; li++) {
|
||||
final int lane = lanes[li];
|
||||
final double y = layout.yForLane(lane);
|
||||
final int slots = layout.slotsOf(lane);
|
||||
final double laneH = slots * layout.pitch;
|
||||
final List<Action> acts = layout.actionsInLane(lane);
|
||||
// 单槽泳道保持与旧版完全一致的背景带 / 底线几何;多槽位覆盖整个泳道带。
|
||||
final double bandH = slots == 1 ? BarLayout.rowVisual : laneH;
|
||||
// 隔泳道背景色带(覆盖该泳道全部槽位),弱化边界。
|
||||
if (li.isEven) {
|
||||
canvas.drawRRect(
|
||||
RRect.fromRectAndRadius(
|
||||
Rect.fromLTWH(20, y - 8, layout.plotRight - 20, bandH),
|
||||
const Radius.circular(10),
|
||||
),
|
||||
Paint()..color = tokens.panel2.withValues(alpha: 0.5),
|
||||
);
|
||||
}
|
||||
// 多槽位泳道:槽位之间用浅色虚线分隔,提示并列关系。
|
||||
if (slots > 1) {
|
||||
for (int s = 1; s < slots; s++) {
|
||||
final double sy = y + s * layout.pitch - 8;
|
||||
_dashedLine(
|
||||
canvas,
|
||||
Offset(layout.plotLeft, sy),
|
||||
Offset(layout.plotRight, sy),
|
||||
Paint()
|
||||
..color = tokens.line2.withValues(alpha: 0.75)
|
||||
..strokeWidth = 1,
|
||||
);
|
||||
}
|
||||
}
|
||||
// 泳道标题(合并显示;单槽最多两行,多槽位按泳道高度持续换行)。
|
||||
_laneHeader(canvas, layout, li, acts,
|
||||
slots == 1 ? y : y + laneH / 2 - BarLayout.rowVisual / 2, slots, laneH);
|
||||
// 泳道底分隔线(单槽时与旧版一致;加深颜色便于区分泳道)。
|
||||
canvas.drawLine(
|
||||
Offset(layout.plotLeft, slots == 1 ? y + BarLayout.rowVisual - 4 : y + laneH - 4),
|
||||
Offset(layout.plotRight, slots == 1 ? y + BarLayout.rowVisual - 4 : y + laneH - 4),
|
||||
Paint()
|
||||
..color = tokens.line2
|
||||
..strokeWidth = 1.3,
|
||||
);
|
||||
// 该泳道内的所有动作条(按槽位分行放置)。
|
||||
for (final Action a in acts) {
|
||||
_bar(canvas, layout, a, u, cycle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 绘制一条泳道的标题:序号徽标 + 合并动作名 + 副标题。
|
||||
///
|
||||
/// 合并动作名在宽度 [BarLayout.leftGutter]-68 内自动换行:单槽泳道最多两行
|
||||
/// (保持旧版外观);多槽位泳道按泳道高度**持续换行**直到放不下为止
|
||||
/// (副标题跟随实际行数下移)。
|
||||
void _laneHeader(Canvas canvas, BarLayout layout, int li, List<Action> acts, double y,
|
||||
int slots, double laneH) {
|
||||
final double labelMax = BarLayout.leftGutter - 68;
|
||||
final Color c0 = acts.isEmpty ? tokens.accent : Color(acts.first.color);
|
||||
canvas.drawRRect(
|
||||
RRect.fromRectAndRadius(Rect.fromLTWH(32, y + 8, 18, 18), const Radius.circular(5)),
|
||||
Paint()..color = c0,
|
||||
);
|
||||
_text(canvas, '${li + 1}', Offset(41, y + 17),
|
||||
color: tokens.chip, size: 10, align: TextAlign.center, weight: FontWeight.w700);
|
||||
if (acts.isEmpty) return;
|
||||
|
||||
// 先按足够大的行数测量“真实行高”(字体回退下行高可能大于字号,
|
||||
// 之前按固定 13px 估算导致标题换行多行后压住灰色副标题)。
|
||||
final String joined = acts.map((Action a) => '${a.id} ${a.name}').join(' / ');
|
||||
final TextPainter tp = TextPainter(
|
||||
text: TextSpan(
|
||||
text: joined,
|
||||
style: TextStyle(
|
||||
color: tokens.text,
|
||||
fontSize: 11,
|
||||
fontWeight: FontWeight.w600,
|
||||
fontFamily: 'Microsoft YaHei UI',
|
||||
),
|
||||
),
|
||||
textDirection: ui.TextDirection.ltr,
|
||||
maxLines: 40,
|
||||
ellipsis: '…',
|
||||
)..layout(maxWidth: labelMax);
|
||||
final int totalLines = tp.computeLineMetrics().length;
|
||||
final double lineH = totalLines == 0 ? 14 : tp.height / totalLines;
|
||||
|
||||
// 行数上限:单槽保持两行(旧版外观);多槽位按泳道高度换算并给副标题留一行。
|
||||
final bool single = slots <= 1;
|
||||
final int maxLines =
|
||||
single ? 2 : ((laneH - lineH - 8) / lineH).floor().clamp(2, 40);
|
||||
if (tp.computeLineMetrics().length != maxLines && maxLines < totalLines) {
|
||||
tp.maxLines = maxLines;
|
||||
tp.ellipsis = '…';
|
||||
tp.layout(maxWidth: labelMax);
|
||||
}
|
||||
tp.paint(canvas, Offset(60, y + 2));
|
||||
|
||||
final Station? st = project.stationById(acts.first.stationId);
|
||||
final String sub = '${st?.name ?? acts.first.stationId} · ${acts.first.driver}'
|
||||
'${acts.length > 1 ? ' · 共 ${acts.length} 个' : ''}';
|
||||
// 副标题紧随实际标题高度;单槽保持旧版下边距(y+32)但绝不高于标题底部,
|
||||
// 多槽位下若剩余高度不足则不再绘制,避免压住标题文字。
|
||||
final double subY = single
|
||||
? math.max(y + 36, y + 2 + tp.height + 6)
|
||||
: y + 2 + tp.height + 6;
|
||||
if (single || subY + 11 <= y + laneH) {
|
||||
_textMax(canvas, sub, Offset(60, subY), labelMax, color: tokens.dim, size: 10);
|
||||
}
|
||||
}
|
||||
|
||||
/// 绘制单个动作条块(投影 / 填充 / 描边 / 跨圈尾段 / 文字标注)。
|
||||
void _bar(Canvas canvas, BarLayout layout, Action a, UnitSettings u, double cycle) {
|
||||
final Color c = Color(a.color);
|
||||
final Rect r = layout.rectFor(a);
|
||||
final bool sel = selectedIds.contains(a.id);
|
||||
final bool crit = criticalActions.contains(a.id);
|
||||
canvas.drawRRect(
|
||||
RRect.fromRectAndRadius(r.shift(const Offset(1.5, 2)), const Radius.circular(3)),
|
||||
Paint()..color = tokens.shadow.withValues(alpha: tokens.shadowOp),
|
||||
);
|
||||
canvas.drawRRect(
|
||||
RRect.fromRectAndRadius(r, const Radius.circular(3)),
|
||||
Paint()..color = c.withValues(alpha: 0.92),
|
||||
);
|
||||
canvas.drawRRect(
|
||||
RRect.fromRectAndRadius(r, const Radius.circular(3)),
|
||||
Paint()
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = 1
|
||||
..color = tokens.selOutline.withValues(alpha: sel ? 0.9 : 0.25),
|
||||
);
|
||||
if (crit) {
|
||||
canvas.drawRRect(
|
||||
RRect.fromRectAndRadius(r, const Radius.circular(3)),
|
||||
Paint()
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = 2
|
||||
..color = tokens.crit,
|
||||
);
|
||||
}
|
||||
// 选中时在两端绘制把手:提示可拖拽端点改时长(触屏目标更大、更好抓)。
|
||||
if (sel) {
|
||||
_handle(canvas, Offset(r.left, r.center.dy));
|
||||
_handle(canvas, Offset(r.right, r.center.dy));
|
||||
}
|
||||
if (a.wrap && u.autoSplitWrap) {
|
||||
final double wx = layout.xFor(a.endAngle - 360) - layout.plotLeft;
|
||||
final Rect tail = Rect.fromLTWH(layout.plotLeft, r.top, math.max(wx, 2), r.height);
|
||||
canvas.drawRRect(
|
||||
RRect.fromRectAndRadius(tail, const Radius.circular(3)),
|
||||
Paint()..color = c.withValues(alpha: tokens.tailOp),
|
||||
);
|
||||
_text(canvas, '跨循环延续 ${(a.endAngle - 360).toStringAsFixed(0)}°',
|
||||
Offset(layout.plotLeft + wx + 18, r.center.dy), color: tokens.warn, size: 10);
|
||||
}
|
||||
final double durMs = a.durationAngle / 360 * cycle;
|
||||
final String full = '${a.name} ${tfmt(durMs, u)}';
|
||||
// 默认「名称 + 时长」一起写在条块内、紧跟名称之后(单行,条块更苗条);
|
||||
// 仅当条块太短、放不下时才移到条块外侧。
|
||||
final double fullW = _tp(full, 11, tokens.arcText, FontWeight.w700).width;
|
||||
if (r.width >= fullW + 20) {
|
||||
_text(canvas, full, Offset(r.left + 10, r.center.dy),
|
||||
color: tokens.arcText, size: 11, weight: FontWeight.w700);
|
||||
} else if (r.right + 190 > layout.plotRight) {
|
||||
_text(canvas, full, Offset(r.left - 10, r.center.dy),
|
||||
color: c, size: 11, weight: FontWeight.w600, align: TextAlign.right);
|
||||
} else {
|
||||
_text(canvas, full, Offset(r.right + 10, r.center.dy),
|
||||
color: c, size: 11, weight: FontWeight.w600);
|
||||
}
|
||||
_text(canvas, 'Δ${a.durationAngle.toStringAsFixed(0)}°', Offset(layout.plotRight - 4, r.center.dy),
|
||||
color: tokens.dim, size: 10, align: TextAlign.right);
|
||||
}
|
||||
|
||||
/// 绘制端点把手:白边圆点,提示可拖拽端点改时长。
|
||||
void _handle(Canvas canvas, Offset o) {
|
||||
canvas.drawCircle(o, 6, Paint()..color = tokens.accent);
|
||||
canvas.drawCircle(
|
||||
o,
|
||||
6,
|
||||
Paint()
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = 2
|
||||
..color = Colors.white,
|
||||
);
|
||||
}
|
||||
|
||||
/// 绘制动作依赖连线(正交折线)。
|
||||
///
|
||||
/// 算法要点:
|
||||
/// - 建立“动作 id → 行号”索引,用于判断两行相对位置。
|
||||
/// - FS 依赖从源条块右端出发;SS 依赖从源条块左端出发。
|
||||
/// - 相邻行(行距 ≤ 1)走短折线;跨多行时在行区间外侧选一条“泳道”
|
||||
/// 作为竖直通道,泳道位置按区间内条块的最右/最左边界外扩,
|
||||
/// 并叠加 `(k % 3) * 16` 的错位,避免多条线完全重叠。
|
||||
/// - 关键路径依赖先画一层加粗低透明度的“光晕”,再画主线。
|
||||
/// - 末端绘制箭头与类型标签(FS 顺序 / SS +偏移时间)。
|
||||
///
|
||||
/// 参数 [layout] 提供条块矩形;[u] 当前未直接使用,保留以对齐调用接口。
|
||||
void _deps(Canvas canvas, BarLayout layout, UnitSettings u) {
|
||||
// 走线统一由 [buildBarDepRoutes] 计算:同组自动错开 + 手动偏移,
|
||||
// 与画布的命中测试 / 拖拽调整共用同一份几何。
|
||||
final List<BarDepRoute> routes = buildBarDepRoutes(project, layout);
|
||||
// 依赖标签先收集、后统一避让放置(见方法末尾 _placeChips)。
|
||||
final List<_ChipSpec> chips = <_ChipSpec>[];
|
||||
for (final BarDepRoute r in routes) {
|
||||
final Dependency d = r.dep;
|
||||
final Color col = d.type == DepType.fs ? tokens.fs : tokens.ss;
|
||||
final bool crit = criticalDeps.contains(r.key);
|
||||
final bool sel = selectedDep == r.key;
|
||||
final Color draw = sel ? tokens.accent : col;
|
||||
if (!r.junctionOnly) {
|
||||
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 (crit) {
|
||||
canvas.drawPath(
|
||||
p,
|
||||
Paint()
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = 5
|
||||
..strokeCap = StrokeCap.round
|
||||
..color = col.withValues(alpha: 0.18),
|
||||
);
|
||||
}
|
||||
canvas.drawPath(
|
||||
p,
|
||||
Paint()
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = sel ? 2.0 : (crit ? 1.1 : 0.85)
|
||||
..strokeCap = StrokeCap.round
|
||||
..color = sel ? draw : draw.withValues(alpha: 0.95),
|
||||
);
|
||||
_arrow(canvas, r.arrowTip, r.arrowDir, draw);
|
||||
}
|
||||
final String label = switch (d.type) {
|
||||
DepType.fs => 'FS',
|
||||
DepType.ss => d.offsetAngle == 0
|
||||
? 'SS'
|
||||
: 'SS+${deg2msStr(d.offsetAngle, project.units, project.cycle.durationMs, dec: 2)}',
|
||||
DepType.ff => 'FF',
|
||||
DepType.sf => 'SF',
|
||||
};
|
||||
// 标签固定在两端动作之间的走线中点;不再因为行程短而横向挪到
|
||||
// 目标条块上方。统一避让时会同时避开动作条与其它标签。
|
||||
chips.add(_ChipSpec(r.chipCenter, label, draw));
|
||||
}
|
||||
_placeChips(canvas, layout, chips);
|
||||
}
|
||||
|
||||
/// 在 [tip] 处绘制指向 [dir] 方向的实心三角箭头。
|
||||
///
|
||||
/// 参数 [col] 为填充色;[dir] 只需提供方向(本函数不归一化,直接按分量使用)。
|
||||
void _arrow(Canvas canvas, Offset tip, Offset dir, Color col) {
|
||||
const double s = 6.5;
|
||||
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);
|
||||
}
|
||||
|
||||
/// 统一放置并绘制依赖标签,避免标签之间相互重叠。
|
||||
///
|
||||
/// 对每个标签依次尝试:原位 → 上下错开 → 上下再错开 → 左右错开 → 斜向错开,
|
||||
/// 取第一个不与已放置标签相交的位置,实现“智能避让”。标签内不再绘制五角星
|
||||
/// (关键路径改用线条光晕表示),字号与色块同时收小,画面更干净。
|
||||
void _placeChips(Canvas canvas, BarLayout layout, List<_ChipSpec> chips) {
|
||||
final List<Rect> placed = <Rect>[];
|
||||
final List<Rect> bars = <Rect>[
|
||||
for (final Action a in project.actions) layout.rectFor(a).inflate(1),
|
||||
];
|
||||
for (final _ChipSpec chip in chips) {
|
||||
final TextPainter tp = _tp(chip.label, 8.5, chip.color, FontWeight.w600);
|
||||
final double w = tp.width + 12;
|
||||
const double h = 14;
|
||||
final List<Offset> trials = <Offset>[
|
||||
Offset.zero,
|
||||
Offset(0, -(h + 3)), Offset(0, h + 3),
|
||||
Offset(0, -2 * (h + 3)), Offset(0, 2 * (h + 3)),
|
||||
Offset(0, -3 * (h + 3)), Offset(0, 3 * (h + 3)),
|
||||
Offset(w + 6, 0), Offset(-w - 6, 0),
|
||||
Offset(w + 6, -(h + 3)), Offset(-w - 6, -(h + 3)),
|
||||
Offset(w + 6, h + 3), Offset(-w - 6, h + 3),
|
||||
];
|
||||
Rect r = Rect.fromCenter(center: chip.center, width: w, height: h);
|
||||
for (final Offset t in trials) {
|
||||
final Rect cand = r.shift(t);
|
||||
final bool hitChip =
|
||||
placed.any((Rect o) => o.inflate(1.5).overlaps(cand));
|
||||
final bool hitBar = bars.any((Rect o) => o.overlaps(cand));
|
||||
if (!hitChip && !hitBar) {
|
||||
r = cand;
|
||||
break;
|
||||
}
|
||||
}
|
||||
placed.add(r);
|
||||
_chipRect(canvas, r, tp, chip.color);
|
||||
}
|
||||
}
|
||||
|
||||
/// 按给定矩形绘制一颗标签胶囊(无星标)。
|
||||
void _chipRect(Canvas canvas, Rect r, TextPainter tp, Color col) {
|
||||
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]。
|
||||
///
|
||||
/// 统一使用微软雅黑字体;参数 [s] 文本、[size] 字号、[color] 颜色、
|
||||
/// [weight] 字重、[align] 对齐方式。返回已完成 layout 的实例。
|
||||
TextPainter _tp(String s, double size, Color color, FontWeight weight, {TextAlign align = TextAlign.left}) {
|
||||
final TextPainter tp = TextPainter(
|
||||
text: TextSpan(
|
||||
text: s,
|
||||
style: TextStyle(color: color, fontSize: size, fontWeight: weight, fontFamily: 'Microsoft YaHei UI'),
|
||||
),
|
||||
textDirection: ui.TextDirection.ltr,
|
||||
textAlign: align,
|
||||
)..layout();
|
||||
return tp;
|
||||
}
|
||||
|
||||
/// 便捷文本绘制。
|
||||
///
|
||||
/// 在 [at] 处按 [align] 对齐绘制字符串 [s](默认左对齐、垂直居中)。
|
||||
/// [color] 缺省时使用 [Tokens.text]。
|
||||
/// 在 [at] 处绘制左对齐的单行文字,并按 [maxWidth] 截断(超出显示省略号)。
|
||||
void _textMax(Canvas canvas, String s, Offset at, double maxWidth,
|
||||
{Color? color, double size = 12, FontWeight weight = FontWeight.normal}) {
|
||||
if (maxWidth <= 4) return;
|
||||
final TextPainter tp = TextPainter(
|
||||
text: TextSpan(
|
||||
text: s,
|
||||
style: TextStyle(color: color ?? tokens.text, fontSize: size, fontWeight: weight, fontFamily: 'Microsoft YaHei UI'),
|
||||
),
|
||||
textDirection: ui.TextDirection.ltr,
|
||||
maxLines: 1,
|
||||
ellipsis: '…',
|
||||
)..layout(maxWidth: maxWidth);
|
||||
tp.paint(canvas, Offset(at.dx, at.dy - tp.height / 2));
|
||||
}
|
||||
|
||||
/// 在 [center] 处绘制居中的单行文字,并按 [maxWidth] 截断(超出显示省略号)。
|
||||
void _textClipped(Canvas canvas, String s, Offset center, double maxWidth,
|
||||
{Color? color, double size = 11, FontWeight weight = FontWeight.w600}) {
|
||||
if (maxWidth <= 4) return;
|
||||
final TextPainter tp = TextPainter(
|
||||
text: TextSpan(
|
||||
text: s,
|
||||
style: TextStyle(color: color ?? tokens.text, fontSize: size, fontWeight: weight, fontFamily: 'Microsoft YaHei UI'),
|
||||
),
|
||||
textDirection: ui.TextDirection.ltr,
|
||||
maxLines: 1,
|
||||
ellipsis: '…',
|
||||
)..layout(maxWidth: maxWidth);
|
||||
tp.paint(canvas, Offset(center.dx - tp.width / 2, center.dy - tp.height / 2));
|
||||
}
|
||||
|
||||
void _text(Canvas canvas, String s, Offset at,
|
||||
{Color? color, double size = 12, FontWeight weight = FontWeight.normal, TextAlign align = TextAlign.left}) {
|
||||
final TextPainter tp = _tp(s, size, color ?? tokens.text, 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));
|
||||
}
|
||||
|
||||
/// 始终返回 `true`。
|
||||
///
|
||||
/// 绘制完全依赖每次重建的布局与外部状态,且本绘制器不做缓存,
|
||||
/// 因此保守地要求每次重绘。
|
||||
@override
|
||||
bool shouldRepaint(covariant BarPainter old) => true;
|
||||
}
|
||||
|
||||
/// 依赖标签的待放置描述:中心点、文本与颜色。
|
||||
///
|
||||
/// 在依赖遍历中收集,随后由 `BarPainter._placeChips` 统一做避让放置。
|
||||
class _ChipSpec {
|
||||
const _ChipSpec(this.center, this.label, this.color);
|
||||
|
||||
/// 期望的标签中心(逻辑坐标)。
|
||||
final Offset center;
|
||||
/// 标签文本(如 `FS` / `SS+0.20 s`)。
|
||||
final String label;
|
||||
/// 边框与文字颜色(按依赖类型区分)。
|
||||
final Color color;
|
||||
}
|
||||
@@ -0,0 +1,783 @@
|
||||
/// 圆形(极坐标)视图绘制器。
|
||||
///
|
||||
/// 职责:把 [Project] 中的动作绘制为同心圆环——角度表示相位
|
||||
/// (0° 位于 12 点钟方向,顺时针递增),半径表示动作所属层级(layer)。
|
||||
///
|
||||
/// 架构位置:
|
||||
/// - 几何换算(圆心、各层半径、刻度半径)来自 [CircularLayout];
|
||||
/// 极坐标换算复用 `layout.dart` 中的 [polar] 与 [arcPath]。
|
||||
/// - 颜色取自 [Tokens],与条形视图及 SVG 导出保持同一视觉体系。
|
||||
/// - 跨循环动作通过 [splitArcs] 拆成两段弧绘制,保证角度连续、视觉闭合。
|
||||
library;
|
||||
|
||||
import 'dart:math' as math;
|
||||
import 'dart:ui' as ui;
|
||||
|
||||
import 'package:flutter/material.dart' hide Action;
|
||||
|
||||
import 'package:sequence_diagram/modules/sequence/layout/dep_routes.dart';
|
||||
import 'package:sequence_diagram/modules/sequence/layout/layout.dart';
|
||||
import 'package:sequence_diagram/model/models.dart';
|
||||
import 'package:sequence_diagram/model/units.dart';
|
||||
import 'package:sequence_diagram/theme/tokens.dart';
|
||||
|
||||
/// 圆形视图的 [CustomPainter] 实现。
|
||||
///
|
||||
/// 所有角度以“12 点钟为 0°、顺时针为正”为约定,由 [polar] 完成换算。
|
||||
class CircularPainter extends CustomPainter {
|
||||
/// 创建圆形视图绘制器。
|
||||
///
|
||||
/// 参数:
|
||||
/// - [project]:待绘制的工程数据(动作、依赖、视图开关)。
|
||||
/// - [tokens]:当前主题令牌,决定所有颜色。
|
||||
/// - [criticalActions]:关键路径上的动作 id 集合。
|
||||
/// - [criticalDeps]:关键路径上的依赖集合,键格式 `fromId->toId`。
|
||||
/// - [selectedId]:当前选中动作 id,可为空。
|
||||
///
|
||||
/// 无副作用;实例不缓存布局,每次 [paint] 都重新计算。
|
||||
CircularPainter({
|
||||
required this.project,
|
||||
required this.tokens,
|
||||
required this.criticalActions,
|
||||
required this.criticalDeps,
|
||||
required this.selectedIds,
|
||||
this.selectedDep,
|
||||
this.guideAngle,
|
||||
});
|
||||
|
||||
/// 待绘制的工程数据源。
|
||||
final Project project;
|
||||
/// 当前主题令牌。
|
||||
final Tokens tokens;
|
||||
/// 关键路径上的动作 id 集合。
|
||||
final Set<String> criticalActions;
|
||||
/// 关键路径上的依赖边集合,元素格式为 `fromId->toId`。
|
||||
final Set<String> criticalDeps;
|
||||
/// 当前选中的动作 id 集合(支持多选)。
|
||||
final Set<String> selectedIds;
|
||||
|
||||
/// 当前选中的依赖连线键(`from->to`);非空时该走线以强调色高亮。
|
||||
final String? selectedDep;
|
||||
|
||||
/// 拖拽动作弧端点时的参考角(度);非空时绘制一条径向虚线参考。
|
||||
final double? guideAngle;
|
||||
|
||||
/// 绘制整个圆形视图。
|
||||
///
|
||||
/// 参数 [canvas]/[size] 遵循 Flutter 绘制约定。
|
||||
/// 图层顺序:表盘底 → 圆环背景 → 象限线 → 层号 → 依赖 → 动作弧
|
||||
/// → 起点标记 / 方向箭头 / 刻度。
|
||||
@override
|
||||
void paint(Canvas canvas, Size size) {
|
||||
final CircularLayout layout = CircularLayout(project, size);
|
||||
final UnitSettings u = project.units;
|
||||
final Offset c = layout.center;
|
||||
final double th = layout.th;
|
||||
final double tickR = layout.tickR;
|
||||
|
||||
// 表盘底板:半径略大于最外圈刻度,形成一圈底色与边框。
|
||||
canvas.drawCircle(c, tickR + 10, Paint()..color = tokens.dial);
|
||||
canvas.drawCircle(
|
||||
c,
|
||||
tickR + 10,
|
||||
Paint()
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = 1.2
|
||||
..color = tokens.line2,
|
||||
);
|
||||
|
||||
// 圆环背景:每条轨道用一条粗描边构成环带,奇偶交替着色,
|
||||
// 并在环带内外沿各画一条细描边作为边界。
|
||||
final List<int> lanes = layout.lanes;
|
||||
final int layers = lanes.length;
|
||||
if (project.viewState.showRingBg) {
|
||||
for (int i = 0; i < layers; i++) {
|
||||
final int lane = lanes[i];
|
||||
final int slots = layout.slotsOf(lane);
|
||||
final Color band = i.isEven ? tokens.ringA : tokens.ringB;
|
||||
if (slots <= 1) {
|
||||
// 单槽轨道:与旧版完全一致(一条环带 + 内外沿细描边)。
|
||||
final double r = layout.radiusFor(lane, 0);
|
||||
canvas.drawCircle(
|
||||
c,
|
||||
r,
|
||||
Paint()
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = th
|
||||
..color = band,
|
||||
);
|
||||
canvas.drawCircle(
|
||||
c,
|
||||
r - th / 2,
|
||||
Paint()
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = 1
|
||||
..color = tokens.ringEdge,
|
||||
);
|
||||
canvas.drawCircle(
|
||||
c,
|
||||
r + th / 2,
|
||||
Paint()
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = 1
|
||||
..color = tokens.ringEdge,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
// 多槽位轨道:整条轨道用一条加宽的色带(同一泳道同一颜色),
|
||||
// 槽位之间用浅色虚线分隔;不同轨道之间仍按奇偶交替(与现在一致)。
|
||||
final double rIn = layout.radiusFor(lane, 0);
|
||||
final double rOut = layout.radiusFor(lane, slots - 1);
|
||||
final double rc = (rIn + rOut) / 2;
|
||||
canvas.drawCircle(
|
||||
c,
|
||||
rc,
|
||||
Paint()
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = (rOut - rIn) + th
|
||||
..color = band,
|
||||
);
|
||||
canvas.drawCircle(
|
||||
c,
|
||||
rIn - th / 2,
|
||||
Paint()
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = 1
|
||||
..color = tokens.ringEdge,
|
||||
);
|
||||
canvas.drawCircle(
|
||||
c,
|
||||
rOut + th / 2,
|
||||
Paint()
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = 1
|
||||
..color = tokens.ringEdge,
|
||||
);
|
||||
final Paint dash = Paint()
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = 1
|
||||
..color = tokens.ringEdge.withValues(alpha: 0.95);
|
||||
for (int s = 1; s < slots; s++) {
|
||||
_dashedCircle(canvas, c, rIn + (s - 0.5) * layout.pitch, dash);
|
||||
}
|
||||
}
|
||||
}
|
||||
canvas.drawCircle(c, math.max(CircularLayout.r0 - th / 2, 8), Paint()..color = tokens.dial);
|
||||
|
||||
// 相位分区环:在最外动作环与刻度环之间画一圈分区色带并标注名称。
|
||||
if (project.viewState.showPhaseBands) _phaseRing(canvas, layout);
|
||||
|
||||
// 轨道标签:为每条轨道挑选一个空位角度,避免压住该轨道的动作弧;
|
||||
// 多槽位轨道把标签放在轨道带的垂直中心(半径方向)。
|
||||
if (project.viewState.showLayerLabels) {
|
||||
for (int i = 0; i < layers; i++) {
|
||||
final int lane = lanes[i];
|
||||
final int slots = layout.slotsOf(lane);
|
||||
final double r = layout.radiusFor(lane) + (slots - 1) * layout.pitch / 2;
|
||||
final double free = _freeAngle(lane);
|
||||
final Offset lp = polar(c, r, free);
|
||||
_text(canvas, 'L${i + 1}', lp, color: tokens.ringLabel, size: 10, align: TextAlign.center, weight: FontWeight.w700);
|
||||
}
|
||||
}
|
||||
|
||||
// 依赖连线先于动作弧绘制,使其位于弧之下、不被遮挡。
|
||||
if (project.viewState.showDeps) {
|
||||
// 走线统一由 [buildArcDepRoutes] 计算(同组自动错开 + 手动径向偏移),
|
||||
// 与画布命中测试 / 拖拽调整共用同一份几何。
|
||||
final List<ArcDepRoute> routes = buildArcDepRoutes(project, layout);
|
||||
final List<_ArcChip> chips = <_ArcChip>[];
|
||||
for (final ArcDepRoute r in routes) {
|
||||
final _ArcChip? chip = _drawDepRoute(canvas, layout, r);
|
||||
if (chip != null) chips.add(chip);
|
||||
}
|
||||
_placeArcChips(canvas, layout, chips);
|
||||
}
|
||||
|
||||
// 逐个绘制动作弧。
|
||||
for (final Action a in project.actions) {
|
||||
_action(canvas, layout, a, u);
|
||||
}
|
||||
|
||||
// 拖拽端点时的径向虚线参考。
|
||||
if (guideAngle != null) _guide(canvas, layout);
|
||||
|
||||
// 起点标记、循环方向提示与外围刻度。
|
||||
_startMarker(canvas, layout);
|
||||
_directionArrow(canvas, layout);
|
||||
if (project.viewState.showRuler) _ticks(canvas, layout);
|
||||
}
|
||||
|
||||
/// 绘制相位分区环:在最外动作环与刻度环之间画一圈分区色带,标注分区名称,
|
||||
/// 并在每条边界处画一条径向虚线,便于与条形视图的分区对应。
|
||||
void _phaseRing(Canvas canvas, CircularLayout layout) {
|
||||
final List<PhaseZone> zones =
|
||||
project.phaseZones.where((PhaseZone z) => z.endAngle > z.startAngle).toList();
|
||||
if (zones.isEmpty) return;
|
||||
final Offset c = layout.center;
|
||||
// 加宽分区带(16px);tickR 已在外层动作环之外留出更大间距,避免挤在一起。
|
||||
final double rBand = layout.tickR - 16;
|
||||
const double bandW = 16;
|
||||
|
||||
// 底环,作为分区的“轨道”。
|
||||
canvas.drawCircle(
|
||||
c,
|
||||
rBand,
|
||||
Paint()
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = bandW
|
||||
..color = tokens.track,
|
||||
);
|
||||
|
||||
// 分区配色(循环取用强调色系)。
|
||||
const List<Color> pal = <Color>[
|
||||
Color(0xFF4CC9F0),
|
||||
Color(0xFF7C5CFF),
|
||||
Color(0xFF34D399),
|
||||
Color(0xFFFFD166),
|
||||
Color(0xFFF472B6),
|
||||
];
|
||||
|
||||
for (int i = 0; i < zones.length; i++) {
|
||||
final PhaseZone z = zones[i];
|
||||
final Color col = pal[i % pal.length];
|
||||
// 分区色带(跨圈会拆成两段)。
|
||||
for (final List<double> s in splitArcs(z.startAngle, z.endAngle)) {
|
||||
canvas.drawPath(
|
||||
arcPath(c, rBand, s[0], s[1]),
|
||||
Paint()
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = bandW
|
||||
..color = col.withValues(alpha: 0.85),
|
||||
);
|
||||
}
|
||||
// 名称沿圆弧排列(切向),并按弧长截断,保证不超出色带。
|
||||
final double mid = ((z.startAngle + z.endAngle) / 2) % 360;
|
||||
final double span = (z.endAngle - z.startAngle).clamp(0, 360);
|
||||
final double arcLen = 2 * math.pi * rBand * (span / 360);
|
||||
_arcLabel(canvas, c, rBand, mid, z.name, tokens.arcText, 11, math.max(arcLen - 10, 22));
|
||||
// 边界径向虚线。
|
||||
for (final double b in <double>[z.startAngle, z.endAngle]) {
|
||||
final Offset p1 = polar(c, rBand - bandW / 2 - 5, b);
|
||||
final Offset p2 = polar(c, rBand + bandW / 2 + 5, b);
|
||||
_dashed(canvas, p1, p2, col.withValues(alpha: 0.9));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 在两点之间画一条虚线(用于分区边界参考)。
|
||||
void _dashed(Canvas canvas, Offset a, Offset b, Color col) {
|
||||
const double dash = 3;
|
||||
const double gap = 3;
|
||||
final double total = (b - a).distance;
|
||||
if (total <= 0) return;
|
||||
final Offset dir = (b - a) / total;
|
||||
double t = 0;
|
||||
final Paint paint = Paint()
|
||||
..color = col
|
||||
..strokeWidth = 1.4
|
||||
..strokeCap = StrokeCap.round;
|
||||
while (t < total) {
|
||||
final double end = math.min(t + dash, total);
|
||||
canvas.drawLine(a + dir * t, a + dir * end, paint);
|
||||
t = end + gap;
|
||||
}
|
||||
}
|
||||
|
||||
/// 沿圆弧切向绘制文字,并按 [maxWidth] 截断,保证不超出色带。
|
||||
///
|
||||
/// 文字位于半径 [r]、角度 [midDeg] 处,旋转到该点的切线方向;
|
||||
/// 当文字位于左半圈(90°–270°)时翻转 180°,避免倒置。
|
||||
void _arcLabel(Canvas canvas, Offset c, double r, double midDeg, String text, Color col, double size, double maxWidth) {
|
||||
final TextPainter tp = TextPainter(
|
||||
text: TextSpan(
|
||||
text: text,
|
||||
style: TextStyle(color: col, fontSize: size, fontWeight: FontWeight.w700, fontFamily: 'Microsoft YaHei UI'),
|
||||
),
|
||||
textDirection: ui.TextDirection.ltr,
|
||||
maxLines: 1,
|
||||
ellipsis: '…',
|
||||
)..layout(maxWidth: maxWidth);
|
||||
final Offset pos = polar(c, r, midDeg);
|
||||
final double rad = midDeg * math.pi / 180;
|
||||
final bool flip = midDeg > 90 && midDeg < 270;
|
||||
canvas.save();
|
||||
canvas.translate(pos.dx, pos.dy);
|
||||
canvas.rotate(flip ? rad + math.pi : rad);
|
||||
tp.paint(canvas, Offset(-tp.width / 2, -tp.height / 2));
|
||||
canvas.restore();
|
||||
}
|
||||
|
||||
/// 为指定 [layer] 选一个尽量不被动作弧覆盖的角度,用于放置层号“L1/L2…”。
|
||||
///
|
||||
/// 从候选角列表(围绕正上方 270° 左右摆动)中挑选第一个与本层任何
|
||||
/// 动作弧都不重叠的角度;若全部冲突则回退到 270°。
|
||||
double _freeAngle(int layer) {
|
||||
const List<double> cands = <double>[270, 278, 262, 286, 254, 294, 246, 302, 238, 310];
|
||||
final List<Action> inLayer = project.actions.where((Action a) => a.layer == layer).toList();
|
||||
for (final double cand in cands) {
|
||||
bool ok = true;
|
||||
for (final Action a in inLayer) {
|
||||
if (_inArc(a, cand)) {
|
||||
ok = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ok) return cand;
|
||||
}
|
||||
return 270;
|
||||
}
|
||||
|
||||
/// 判断角度 [deg] 是否落在动作 [a] 的弧范围内。
|
||||
///
|
||||
/// 处理两类情况:普通动作(结束角 ≤ 360)用闭区间判定;
|
||||
/// 跨循环动作(结束角 > 360)用“大于起点 或 小于回绕终点”的并集判定。
|
||||
/// 两侧各留 0.5° 容差以吸收浮点误差。
|
||||
bool _inArc(Action a, double deg) {
|
||||
final double n = deg % 360;
|
||||
if (a.endAngle <= 360) return n >= a.startAngle - 0.5 && n <= a.endAngle + 0.5;
|
||||
return n >= a.startAngle - 0.5 || n <= (a.endAngle - 360) + 0.5;
|
||||
}
|
||||
|
||||
/// 绘制单个动作弧。
|
||||
///
|
||||
/// 算法:
|
||||
/// - 通过 [splitArcs] 把可能跨循环的动作拆成一到两段,逐段绘制。
|
||||
/// - 每段先画加宽阴影弧,再画本体弧;跨循环的“尾巴”段(第二段)
|
||||
/// 以 [Tokens.tailOp] 淡化,并额外叠一层高透明度描边以示区分。
|
||||
/// - 关键动作沿弧画 [Tokens.crit] 描边;选中动作在外侧画强调色细弧。
|
||||
/// - 在弧中点附近放置 `序号. id 名称` 与起止角度文本;
|
||||
/// 短弧(时长 < 38°)使用更小字号并收紧行距。
|
||||
///
|
||||
/// 参数 [u] 为当前单位设置(保留以对齐调用接口,当前未直接使用)。
|
||||
void _action(Canvas canvas, CircularLayout layout, Action a, UnitSettings u) {
|
||||
final Offset c = layout.center;
|
||||
final double r = layout.radiusFor(a.layer, layout.slotOf(a));
|
||||
final double th = layout.th;
|
||||
final Color col = Color(a.color);
|
||||
final bool sel = selectedIds.contains(a.id);
|
||||
final bool crit = criticalActions.contains(a.id);
|
||||
// 跨循环动作会被拆成 [a0,360] 与 [0,a1-360] 两段。
|
||||
final List<List<double>> segs = splitArcs(a.startAngle, a.endAngle);
|
||||
|
||||
for (int k = 0; k < segs.length; k++) {
|
||||
// 第二段且为跨循环时视为“尾巴”,需淡化处理。
|
||||
final bool tail = a.wrap && k == 1;
|
||||
// 方头 + 小圆角:路径角度严格等于真实起止角,弧带长度即真实时长
|
||||
//(原来的圆头端帽会在两端各多出半个环宽,造成长度误读)。
|
||||
final Path p = _bandPath(c, r, th, segs[k][0], segs[k][1], 3);
|
||||
// 圆周色块不加描边 / 阴影,保持干净。
|
||||
canvas.drawPath(p, Paint()..color = col.withValues(alpha: tail ? tokens.tailOp : 1));
|
||||
if (tail) {
|
||||
canvas.drawPath(p, Paint()..color = col.withValues(alpha: 0.9));
|
||||
}
|
||||
}
|
||||
if (crit) {
|
||||
for (final List<double> s in segs) {
|
||||
canvas.drawPath(
|
||||
arcPath(c, r, s[0], s[1]),
|
||||
Paint()
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = 2
|
||||
..color = tokens.crit,
|
||||
);
|
||||
}
|
||||
}
|
||||
if (sel) {
|
||||
for (final List<double> s in segs) {
|
||||
canvas.drawPath(
|
||||
arcPath(c, r + th / 2 + 8, s[0], s[1]),
|
||||
Paint()
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = 1.6
|
||||
..color = tokens.accent,
|
||||
);
|
||||
}
|
||||
// 选中时在弧两端绘制把手:提示可拖拽端点改持续角(触屏更好抓)。
|
||||
for (final Offset h in <Offset>[
|
||||
polar(c, r, a.startAngle),
|
||||
polar(c, r, math.min(a.endAngle, 360)),
|
||||
]) {
|
||||
canvas.drawCircle(h, 6, Paint()..color = tokens.accent);
|
||||
canvas.drawCircle(
|
||||
h,
|
||||
6,
|
||||
Paint()
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = 2
|
||||
..color = Colors.white,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// 文字沿圆弧排列并按弧长截断;短弧优先保留动作名,避免只剩
|
||||
// “1. A1 动…” 这类难以辨认的省略文本。
|
||||
final double mid = (a.startAngle + a.endAngle) / 2;
|
||||
final int step = project.actions.indexOf(a) + 1;
|
||||
final double span = a.wrap ? (360 - a.startAngle) : a.durationAngle;
|
||||
final double arcLen = 2 * math.pi * r * (span.clamp(0, 360) / 360);
|
||||
final double maxW = math.max(arcLen - 12, 20);
|
||||
double widthOf(String text) {
|
||||
final TextPainter tp = TextPainter(
|
||||
text: TextSpan(
|
||||
text: text,
|
||||
style: const TextStyle(
|
||||
fontSize: 11,
|
||||
fontWeight: FontWeight.w700,
|
||||
fontFamily: 'Microsoft YaHei UI',
|
||||
),
|
||||
),
|
||||
textDirection: ui.TextDirection.ltr,
|
||||
maxLines: 1,
|
||||
)..layout();
|
||||
final double w = tp.width;
|
||||
tp.dispose();
|
||||
return w;
|
||||
}
|
||||
String label = '$step. ${a.id} ${a.name}';
|
||||
if (widthOf(label) > maxW) label = '$step. ${a.name}';
|
||||
if (widthOf(label) > maxW) label = a.name;
|
||||
if (widthOf(label) > maxW) label = '$step. ${a.id}';
|
||||
_arcLabel(canvas, c, r, mid, label, tokens.arcText, 11, maxW);
|
||||
}
|
||||
|
||||
/// 构造「方头 + 小圆角」的圆环动作带(填充路径)。
|
||||
///
|
||||
/// 与「圆头描边」不同,路径覆盖的角度范围严格等于 [a0] → [a1](顺时针):
|
||||
/// 两端为径向直边、四角带 [cornerR] 的小圆角,因此弧带的长度就是动作的
|
||||
/// 真实时长,不会再像圆头端帽那样在两端各多出半个环宽造成长度误读。
|
||||
Path _bandPath(Offset c, double r, double w, double a0, double a1, double cornerR) {
|
||||
final double ro = r + w / 2;
|
||||
final double ri = r - w / 2;
|
||||
final double rc = math.min(cornerR, w / 2 - 0.5);
|
||||
final double span = a1 - a0;
|
||||
final Path p = Path();
|
||||
if (rc <= 0.5 || span <= 0.5) {
|
||||
// 退化情形:直接方头四边形。
|
||||
final Offset a = polar(c, ro, a0);
|
||||
p.moveTo(a.dx, a.dy);
|
||||
final Offset b = polar(c, ro, a1);
|
||||
p.lineTo(b.dx, b.dy);
|
||||
final Offset d = polar(c, ri, a1);
|
||||
p.lineTo(d.dx, d.dy);
|
||||
final Offset e = polar(c, ri, a0);
|
||||
p.lineTo(e.dx, e.dy);
|
||||
p.close();
|
||||
return p;
|
||||
}
|
||||
// 小圆角在内 / 外缘占用的角向宽度(度),不超过跨度的 40%。
|
||||
final double dOut = math.min(rc / ro * 180 / math.pi, span * 0.4);
|
||||
final double dIn = math.min(rc / ri * 180 / math.pi, span * 0.4);
|
||||
final Offset oStart = polar(c, ro, a0 + dOut);
|
||||
final Offset oEnd = polar(c, ro, a1 - dOut);
|
||||
final Offset iEnd = polar(c, ri, a1 - dIn);
|
||||
final Offset iStart = polar(c, ri, a0 + dIn);
|
||||
p.moveTo(oStart.dx, oStart.dy);
|
||||
// 外缘弧(顺时针)
|
||||
p.arcToPoint(oEnd,
|
||||
radius: Radius.circular(ro), largeArc: span - 2 * dOut > 180, clockwise: true);
|
||||
// 尾端:外角 → 小圆角 → 端面 → 小圆角 → 内缘
|
||||
final Offset co = polar(c, ro, a1);
|
||||
final Offset fo = polar(c, ro - rc, a1);
|
||||
p.quadraticBezierTo(co.dx, co.dy, fo.dx, fo.dy);
|
||||
final Offset fi = polar(c, ri + rc, a1);
|
||||
p.lineTo(fi.dx, fi.dy);
|
||||
final Offset ci = polar(c, ri, a1);
|
||||
p.quadraticBezierTo(ci.dx, ci.dy, iEnd.dx, iEnd.dy);
|
||||
// 内缘弧(逆时针返回)
|
||||
p.arcToPoint(iStart,
|
||||
radius: Radius.circular(ri), largeArc: span - 2 * dIn > 180, clockwise: false);
|
||||
// 起始端:内角 → 小圆角 → 端面 → 小圆角 → 外角
|
||||
final Offset ci0 = polar(c, ri, a0);
|
||||
final Offset fi0 = polar(c, ri + rc, a0);
|
||||
p.quadraticBezierTo(ci0.dx, ci0.dy, fi0.dx, fi0.dy);
|
||||
final Offset fo0 = polar(c, ro - rc, a0);
|
||||
p.lineTo(fo0.dx, fo0.dy);
|
||||
final Offset co0 = polar(c, ro, a0);
|
||||
p.quadraticBezierTo(co0.dx, co0.dy, oStart.dx, oStart.dy);
|
||||
p.close();
|
||||
return p;
|
||||
}
|
||||
|
||||
/// 绘制一条圆周依赖走线(几何来自 [ArcDepRoute],与命中测试 / 拖拽共用)。
|
||||
///
|
||||
/// 关键依赖先画低透明度粗描边、再画主线;选中的连线以强调色加粗高亮。
|
||||
/// 标签不在此处直接绘制:收集后统一避让放置(见 [_placeArcChips])。
|
||||
_ArcChip? _drawDepRoute(Canvas canvas, CircularLayout layout, ArcDepRoute r) {
|
||||
final Dependency d = r.dep;
|
||||
final Color col = d.type == DepType.fs ? tokens.fs : tokens.ss;
|
||||
final bool crit = criticalDeps.contains(r.key);
|
||||
final bool sel = selectedDep == r.key;
|
||||
final Color draw = sel ? tokens.accent : col;
|
||||
|
||||
if (!r.junctionOnly) {
|
||||
final Path p = r.buildPath();
|
||||
if (crit) {
|
||||
canvas.drawPath(
|
||||
p,
|
||||
Paint()
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = 5
|
||||
..strokeCap = StrokeCap.round
|
||||
..color = col.withValues(alpha: 0.2),
|
||||
);
|
||||
}
|
||||
canvas.drawPath(
|
||||
p,
|
||||
Paint()
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = sel ? 2.2 : (crit ? 1.2 : (d.type == DepType.fs ? 1.0 : 0.9))
|
||||
..strokeCap = StrokeCap.round
|
||||
..color = draw,
|
||||
);
|
||||
_arrow(canvas, r.p3, r.tipDir, draw);
|
||||
}
|
||||
|
||||
String label = d.type == DepType.fs
|
||||
? 'FS'
|
||||
: (d.offsetAngle == 0
|
||||
? 'SS'
|
||||
: 'SS+${deg2msStr(d.offsetAngle, project.units, project.cycle.durationMs, dec: 2)}');
|
||||
if (d.type == DepType.ff) label = 'FF';
|
||||
if (d.type == DepType.sf) label = 'SF';
|
||||
return _ArcChip(r.chipR, r.chipDeg, '${d.fromId}→${d.toId} $label', draw);
|
||||
}
|
||||
|
||||
/// 统一绘制圆周视图的依赖标签:沿半径方向逐级外移 / 内移避让,避免相互重叠。
|
||||
///
|
||||
/// 估算每个标签的占位矩形(文本长度 × 字号),若与已放置标签相交则按
|
||||
/// +14 / −14 / +28 / −28 … 的步长沿半径方向挪动,直到找到空位。
|
||||
void _placeArcChips(Canvas canvas, CircularLayout layout, List<_ArcChip> chips) {
|
||||
final Offset c = layout.center;
|
||||
final List<Rect> placed = <Rect>[];
|
||||
const double size = 8.5;
|
||||
for (final _ArcChip chip in chips) {
|
||||
final TextPainter tp = _tp(chip.text, size, chip.color, FontWeight.w600);
|
||||
final double w = tp.width + 10;
|
||||
const double h = 14;
|
||||
double r = chip.r;
|
||||
Rect rect = Rect.fromCenter(center: polar(c, r, chip.deg), width: w, height: h);
|
||||
for (int i = 1; i <= 10; i++) {
|
||||
if (!placed.any((Rect o) => o.inflate(1.5).overlaps(rect))) break;
|
||||
final double step = 14.0 * ((i + 1) ~/ 2);
|
||||
r = chip.r + (i.isOdd ? step : -step);
|
||||
rect = Rect.fromCenter(center: polar(c, r, chip.deg), width: w, height: h);
|
||||
}
|
||||
placed.add(rect);
|
||||
// 标签沿圆弧切线方向旋转(与动作弧同向),放在环间空隙中。
|
||||
final Offset pos = polar(c, r, chip.deg);
|
||||
final double rad = chip.deg * math.pi / 180;
|
||||
final bool flip = chip.deg > 90 && chip.deg < 270;
|
||||
canvas.save();
|
||||
canvas.translate(pos.dx, pos.dy);
|
||||
canvas.rotate(flip ? rad + math.pi : rad);
|
||||
final Rect local = Rect.fromCenter(
|
||||
center: Offset.zero,
|
||||
width: w,
|
||||
height: h,
|
||||
);
|
||||
canvas.drawRRect(
|
||||
RRect.fromRectAndRadius(local, const Radius.circular(5)),
|
||||
Paint()..color = tokens.chip,
|
||||
);
|
||||
canvas.drawRRect(
|
||||
RRect.fromRectAndRadius(local, const Radius.circular(5)),
|
||||
Paint()
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = 1
|
||||
..color = chip.color,
|
||||
);
|
||||
tp.paint(canvas, Offset(-tp.width / 2, -tp.height / 2));
|
||||
canvas.restore();
|
||||
}
|
||||
}
|
||||
|
||||
/// 在 [tip] 处绘制指向 [dir] 方向的实心三角箭头。
|
||||
///
|
||||
/// 与条形视图不同,这里会先把 [dir] 归一化;长度过短时直接返回。
|
||||
void _arrow(Canvas canvas, Offset tip, Offset dir, Color col) {
|
||||
final double len = dir.distance;
|
||||
if (len < 0.001) return;
|
||||
final Offset u = dir / len;
|
||||
const double s = 6;
|
||||
final Offset b = tip - u * s;
|
||||
final Offset n = Offset(-u.dy, u.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);
|
||||
}
|
||||
|
||||
/// 以虚线绘制圆周(用于多槽位轨道内部的槽位分隔线)。
|
||||
void _dashedCircle(Canvas canvas, Offset center, double r, Paint paint,
|
||||
{double dash = 6, double gap = 5}) {
|
||||
final Path path = Path()..addOval(Rect.fromCircle(center: center, radius: r));
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 绘制拖拽端点时的径向虚线参考与角度标注。
|
||||
void _guide(Canvas canvas, CircularLayout layout) {
|
||||
final Offset c = layout.center;
|
||||
final double deg = guideAngle!;
|
||||
final Offset p1 = polar(c, math.max(CircularLayout.r0 - 24, 8), deg);
|
||||
final Offset p2 = polar(c, layout.tickR + 6, deg);
|
||||
final Paint paint = Paint()
|
||||
..color = tokens.accent
|
||||
..strokeWidth = 1.4
|
||||
..strokeCap = StrokeCap.round;
|
||||
const double dash = 6;
|
||||
const double gap = 6;
|
||||
final double total = (p2 - p1).distance;
|
||||
if (total > 0) {
|
||||
final Offset dir = (p2 - p1) / total;
|
||||
double t = 0;
|
||||
while (t < total) {
|
||||
final double end = math.min(t + dash, total);
|
||||
canvas.drawLine(p1 + dir * t, p1 + dir * end, paint);
|
||||
t = end + gap;
|
||||
}
|
||||
}
|
||||
_text(canvas, '${deg.toStringAsFixed(1)}°', polar(c, layout.tickR + 22, deg),
|
||||
color: tokens.accent, size: 11, align: TextAlign.center, weight: FontWeight.w700);
|
||||
}
|
||||
|
||||
/// 绘制 0° 起点标记:顶部朝下的三角与“0° · 12 点钟”文字。
|
||||
void _startMarker(Canvas canvas, CircularLayout layout) {
|
||||
final Offset c = layout.center;
|
||||
// 放在刻度数字之外,避免与顶部的“0°”刻度标签重叠。
|
||||
final double r = layout.tickR + 46;
|
||||
final Offset tip = Offset(c.dx, c.dy - r - 22);
|
||||
final Path p = Path()
|
||||
..moveTo(tip.dx, tip.dy)
|
||||
..lineTo(tip.dx - 9, tip.dy - 15)
|
||||
..lineTo(tip.dx + 9, tip.dy - 15)
|
||||
..close();
|
||||
canvas.drawPath(p, Paint()..color = tokens.accent);
|
||||
_text(canvas, '0° · 12 点钟', Offset(c.dx, tip.dy - 30),
|
||||
color: tokens.accent, size: 11, align: TextAlign.center, weight: FontWeight.w600);
|
||||
}
|
||||
|
||||
/// 绘制循环方向指示:外圈一段短弧 + 沿切线的箭头,标注顺时针。
|
||||
void _directionArrow(Canvas canvas, CircularLayout layout) {
|
||||
final Offset c = layout.center;
|
||||
// 放在度数 / 时间两排标签之外,避免箭头与文字相撞。
|
||||
final double r = layout.tickR + 72;
|
||||
canvas.drawPath(
|
||||
arcPath(c, r, 25, 60),
|
||||
Paint()
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = 1.8
|
||||
..strokeCap = StrokeCap.round
|
||||
..color = tokens.accent2,
|
||||
);
|
||||
// 箭头指向弧的终点,方向与切线一致(顺时针)。
|
||||
const double te = 60;
|
||||
final double trad = te * math.pi / 180;
|
||||
final Offset tip = polar(c, r, te);
|
||||
final Offset tan = Offset(math.cos(trad), math.sin(trad));
|
||||
const double s = 7;
|
||||
final Offset b = tip - tan * s;
|
||||
final Offset n = Offset(-tan.dy, tan.dx) * s * 0.5;
|
||||
final Path head = 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(head, Paint()..color = tokens.accent2);
|
||||
_text(canvas, '顺时针 CW', polar(c, r + 20, 42),
|
||||
color: tokens.accent2, size: 10, align: TextAlign.center, weight: FontWeight.w600);
|
||||
}
|
||||
|
||||
/// 绘制外围角度刻度。
|
||||
///
|
||||
/// 每 5° 一个刻度,10° 为中刻度,30° 为主刻度并标注度数;
|
||||
/// 90° 的整数倍使用强调色加粗。度数外侧再标注等价的绝对时间。
|
||||
void _ticks(Canvas canvas, CircularLayout layout) {
|
||||
final Offset c = layout.center;
|
||||
final double r = layout.tickR;
|
||||
final UnitSettings u = project.units;
|
||||
final double cycle = project.cycle.durationMs;
|
||||
for (int d = 0; d < 360; d += 5) {
|
||||
final bool major = d % 30 == 0;
|
||||
final bool mid = d % 10 == 0;
|
||||
final Offset p1 = polar(c, r, d.toDouble());
|
||||
final Offset p2 = polar(c, r + (major ? 20 : (mid ? 13 : 7)), d.toDouble());
|
||||
canvas.drawLine(
|
||||
p1,
|
||||
p2,
|
||||
Paint()
|
||||
..strokeWidth = major ? 1.4 : 1
|
||||
..color = major ? tokens.line2 : tokens.line,
|
||||
);
|
||||
if (major) {
|
||||
final Offset lp = polar(c, r + 34, d.toDouble());
|
||||
_text(canvas, '$d°', lp,
|
||||
color: d % 90 == 0 ? tokens.accent : tokens.sub,
|
||||
size: 10,
|
||||
align: TextAlign.center,
|
||||
weight: d % 90 == 0 ? FontWeight.w700 : FontWeight.normal);
|
||||
// 度数外侧增加一个时间数值(字号与度数保持一致)。
|
||||
final Offset tp = polar(c, r + 48, d.toDouble());
|
||||
_text(canvas, tvalStr(d / 360 * cycle, u, dec: u.timeUnit == TimeUnit.ms ? 0 : 1), tp,
|
||||
color: tokens.sub, size: 10, align: TextAlign.center);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 构造并布局一个 [TextPainter]。
|
||||
///
|
||||
/// 统一使用微软雅黑字体;返回已完成 layout 的实例。
|
||||
TextPainter _tp(String s, double size, Color color, FontWeight weight) {
|
||||
return TextPainter(
|
||||
text: TextSpan(
|
||||
text: s,
|
||||
style: TextStyle(color: color, fontSize: size, fontWeight: weight, fontFamily: 'Microsoft YaHei UI'),
|
||||
),
|
||||
textDirection: ui.TextDirection.ltr,
|
||||
)..layout();
|
||||
}
|
||||
|
||||
/// 便捷文本绘制。
|
||||
///
|
||||
/// 在 [at] 处按 [align] 对齐绘制字符串 [s](默认左对齐、垂直居中)。
|
||||
/// [color] 缺省时使用 [Tokens.text]。
|
||||
void _text(Canvas canvas, String s, Offset at,
|
||||
{Color? color, double size = 12, FontWeight weight = FontWeight.normal, TextAlign align = TextAlign.left}) {
|
||||
final TextPainter tp = _tp(s, size, color ?? tokens.text, weight);
|
||||
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));
|
||||
}
|
||||
|
||||
/// 始终返回 `true`。
|
||||
///
|
||||
/// 绘制完全依赖每次重建的布局与外部状态,且本绘制器不做缓存,
|
||||
/// 因此保守地要求每次重绘。
|
||||
@override
|
||||
bool shouldRepaint(covariant CircularPainter old) => true;
|
||||
}
|
||||
|
||||
/// 圆周视图依赖标签的待放置描述:半径、角度、文本与颜色。
|
||||
///
|
||||
/// 先在依赖遍历中收集,再由 [CircularPainter] 统一做径向避让绘制。
|
||||
class _ArcChip {
|
||||
const _ArcChip(this.r, this.deg, this.text, this.color);
|
||||
|
||||
/// 期望的标签半径(像素)。
|
||||
final double r;
|
||||
/// 期望的标签角度(度,0° = 12 点钟、顺时针)。
|
||||
final double deg;
|
||||
/// 标签文本(如 `A1→A2 SS+0.20 s`)。
|
||||
final String text;
|
||||
/// 文本颜色(按依赖类型区分)。
|
||||
final Color color;
|
||||
}
|
||||
@@ -0,0 +1,284 @@
|
||||
/// 流程图绘制器:把时序图渲染为纵向泳道流程图(只读视图)。
|
||||
///
|
||||
/// 视觉体系与条形 / 圆周视图一致:泳道背景用环带色、依赖用 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<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 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;
|
||||
}
|
||||
Reference in New Issue
Block a user