Files
EasyDesignCollection/lib/render/circular_painter.dart
T

672 lines
26 KiB
Dart
Raw Normal View History

2026-09-18 16:50:12 +08:00
/// 圆形(极坐标)视图绘制器。
///
/// 职责:把 [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 '../layout/dep_routes.dart';
2026-09-18 16:50:12 +08:00
import '../layout/layout.dart';
import '../model/models.dart';
import '../model/units.dart';
2026-09-18 16:50:12 +08:00
import '../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,
2026-09-18 16:50:12 +08:00
});
/// 待绘制的工程数据源。
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;
2026-09-18 16:50:12 +08:00
/// 绘制整个圆形视图。
///
/// 参数 [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,
);
// 圆环背景:每条轨道用一条粗描边构成环带,奇偶交替着色,
2026-09-18 16:50:12 +08:00
// 并在环带内外沿各画一条细描边作为边界。
final List<int> lanes = layout.lanes;
final int layers = lanes.length;
2026-09-18 16:50:12 +08:00
if (project.viewState.showRingBg) {
for (int i = 0; i < layers; i++) {
final double r = layout.radiusFor(lanes[i]);
2026-09-18 16:50:12 +08:00
canvas.drawCircle(
c,
r,
Paint()
..style = PaintingStyle.stroke
..strokeWidth = th
..color = i.isEven ? tokens.ringA : tokens.ringB,
);
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,
);
}
}
canvas.drawCircle(c, math.max(CircularLayout.r0 - th / 2, 8), Paint()..color = tokens.dial);
// 相位分区环:在最外动作环与刻度环之间画一圈分区色带并标注名称。
if (project.viewState.showPhaseBands) _phaseRing(canvas, layout);
// 轨道标签:为每条轨道挑选一个空位角度,避免压住该轨道的动作弧。
2026-09-18 16:50:12 +08:00
if (project.viewState.showLayerLabels) {
for (int i = 0; i < layers; i++) {
final double r = layout.radiusFor(lanes[i]);
final double free = _freeAngle(lanes[i]);
2026-09-18 16:50:12 +08:00
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);
2026-09-18 16:50:12 +08:00
}
_placeArcChips(canvas, layout, chips);
2026-09-18 16:50:12 +08:00
}
// 逐个绘制动作弧。
for (final Action a in project.actions) {
_action(canvas, layout, a, u);
}
// 拖拽端点时的径向虚线参考。
if (guideAngle != null) _guide(canvas, layout);
// 起点标记、循环方向提示与外围刻度。
2026-09-18 16:50:12 +08:00
_startMarker(canvas, layout);
_directionArrow(canvas, layout);
2026-09-18 16:50:12 +08:00
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);
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));
2026-09-18 16:50:12 +08:00
if (tail) {
canvas.drawPath(p, Paint()..color = col.withValues(alpha: 0.9));
2026-09-18 16:50:12 +08:00
}
}
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,
);
}
2026-09-18 16:50:12 +08:00
}
// 文字沿圆弧排列并按弧长截断,保证始终位于动作色带之内。
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);
_arcLabel(canvas, c, r, mid, '$step. ${a.id} ${a.name}', tokens.arcText, 11, math.max(arcLen - 12, 20));
}
/// 构造「方头 + 小圆角」的圆环动作带(填充路径)。
///
/// 与「圆头描边」不同,路径覆盖的角度范围严格等于 [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],与命中测试 / 拖拽共用)。
2026-09-18 16:50:12 +08:00
///
/// 关键依赖先画低透明度粗描边、再画主线;选中的连线以强调色加粗高亮。
/// 标签不在此处直接绘制:收集后统一避让放置(见 [_placeArcChips])。
_ArcChip? _drawDepRoute(Canvas canvas, CircularLayout layout, ArcDepRoute r) {
final Dependency d = r.dep;
2026-09-18 16:50:12 +08:00
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;
2026-09-18 16:50:12 +08:00
final Path p = r.buildPath();
2026-09-18 16:50:12 +08:00
if (crit) {
canvas.drawPath(
p,
Paint()
..style = PaintingStyle.stroke
..strokeWidth = 5
2026-09-18 16:50:12 +08:00
..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))
2026-09-18 16:50:12 +08:00
..strokeCap = StrokeCap.round
..color = draw,
2026-09-18 16:50:12 +08:00
);
_arrow(canvas, r.p3, r.tipDir, draw);
2026-09-18 16:50:12 +08:00
String label = d.type == DepType.fs ? 'FS' : 'SS+${d.offsetAngle.toStringAsFixed(0)}°';
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 double w = chip.text.runes.length * size * 0.62 + 8;
final double h = size * 1.7;
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 double arcLen = 2 * math.pi * r * 0.9;
_arcLabel(canvas, c, r, chip.deg, chip.text, chip.color, size,
math.max(math.min(arcLen, 200), 40));
}
2026-09-18 16:50:12 +08:00
}
/// 在 [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;
2026-09-18 16:50:12 +08:00
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 _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;
}
2026-09-18 16:50:12 +08:00
}
_text(canvas, '${deg.toStringAsFixed(1)}°', polar(c, layout.tickR + 22, deg),
color: tokens.accent, size: 11, align: TextAlign.center, weight: FontWeight.w700);
2026-09-18 16:50:12 +08:00
}
/// 绘制 0° 起点标记:顶部朝下的三角与“0° · 12 点钟”文字。
void _startMarker(Canvas canvas, CircularLayout layout) {
final Offset c = layout.center;
// 放在刻度数字之外,避免与顶部的“0°”刻度标签重叠。
final double r = layout.tickR + 46;
2026-09-18 16:50:12 +08:00
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) {
2026-09-18 16:50:12 +08:00
final Offset c = layout.center;
// 放在度数 / 时间两排标签之外,避免箭头与文字相撞。
final double r = layout.tickR + 72;
2026-09-18 16:50:12 +08:00
canvas.drawPath(
arcPath(c, r, 25, 60),
2026-09-18 16:50:12 +08:00
Paint()
..style = PaintingStyle.stroke
..strokeWidth = 1.8
2026-09-18 16:50:12 +08:00
..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)
2026-09-18 16:50:12 +08:00
..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);
2026-09-18 16:50:12 +08:00
}
/// 绘制外围角度刻度。
///
/// 每 5° 一个刻度,10° 为中刻度,30° 为主刻度并标注度数;
/// 90° 的整数倍使用强调色加粗。度数外侧再标注等价的绝对时间。
2026-09-18 16:50:12 +08:00
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;
2026-09-18 16:50:12 +08:00
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());
2026-09-18 16:50:12 +08:00
_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);
2026-09-18 16:50:12 +08:00
}
}
}
/// 构造并布局一个 [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+30°`)。
final String text;
/// 文本颜色(按依赖类型区分)。
final Color color;
}