6cfa79507c
- 默认浅色白底主题;字体整数化 + Microsoft YaHei UI 提升清晰度 - 相位分区:圆周/条形显示、沿弧排列、拖拽边界(虚线参考+弱吸附)、增删改(对话框/右键/双击) - 动作:Ctrl 多选批量对齐;上下拖拽自由换行;左右端拖拽改起止(虚线+弱吸附+左右箭头光标) - 动作编辑支持角度与时间双向换算 - 圆周顺时针箭头移至刻度环外;条形左侧标题列收窄 - 侧栏可隐藏/钉住;适应视图按内容自适应;节拍统计改为按钮弹窗 - AI 生成时序图(DeepSeek,Flash/Pro,推理强度),基于 dart:io 无插件 - 窗口标题显示文件名/未命名文件;关于信息(设计人/版本/版权) - 全量中文注释、docs/ARCHITECTURE.md、docs/HELP.md(按任务组织) - 构建脚本输出到 dist/,并附带 dist 生成物
592 lines
22 KiB
Dart
592 lines
22 KiB
Dart
/// 圆形(极坐标)视图绘制器。
|
||
///
|
||
/// 职责:把 [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/layout.dart';
|
||
import '../model/models.dart';
|
||
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,
|
||
});
|
||
|
||
/// 待绘制的工程数据源。
|
||
final Project project;
|
||
/// 当前主题令牌。
|
||
final Tokens tokens;
|
||
/// 关键路径上的动作 id 集合。
|
||
final Set<String> criticalActions;
|
||
/// 关键路径上的依赖边集合,元素格式为 `fromId->toId`。
|
||
final Set<String> criticalDeps;
|
||
/// 当前选中的动作 id 集合(支持多选)。
|
||
final Set<String> selectedIds;
|
||
|
||
/// 绘制整个圆形视图。
|
||
///
|
||
/// 参数 [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 int layers = project.maxLayer + 1;
|
||
if (project.viewState.showRingBg) {
|
||
for (int i = 0; i < layers; i++) {
|
||
final double r = layout.radiusFor(i);
|
||
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);
|
||
|
||
// 象限辅助线(0°/90°/180°/270°)。
|
||
_quadrants(canvas, c, layout);
|
||
|
||
// 层号标签:为每层挑选一个空位角度,避免压住该层的动作弧。
|
||
if (project.viewState.showLayerLabels) {
|
||
for (int i = 0; i < layers; i++) {
|
||
final double r = layout.radiusFor(i);
|
||
final double free = _freeAngle(i);
|
||
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) {
|
||
for (final Action to in project.actions) {
|
||
for (final Dependency d in to.deps) {
|
||
_dep(canvas, layout, d);
|
||
}
|
||
}
|
||
}
|
||
|
||
// 逐个绘制动作弧。
|
||
for (final Action a in project.actions) {
|
||
_action(canvas, layout, a, u);
|
||
}
|
||
|
||
// 起点标记、顺时针方向提示与外围刻度。
|
||
_startMarker(canvas, layout);
|
||
_cwArrow(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;
|
||
}
|
||
|
||
/// 绘制 0°/90°/180°/270° 四条象限辅助线(从内圈延伸到刻度环)。
|
||
void _quadrants(Canvas canvas, Offset c, CircularLayout layout) {
|
||
for (final double d in <double>[0, 90, 180, 270]) {
|
||
final Offset p1 = polar(c, 30, d);
|
||
final Offset p2 = polar(c, layout.tickR + 4, d);
|
||
canvas.drawLine(
|
||
p1,
|
||
p2,
|
||
Paint()
|
||
..color = tokens.line2.withValues(alpha: 0.7)
|
||
..strokeWidth = 1,
|
||
);
|
||
}
|
||
}
|
||
|
||
/// 绘制单个动作弧。
|
||
///
|
||
/// 算法:
|
||
/// - 通过 [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 = arcPath(c, r, segs[k][0], segs[k][1]);
|
||
canvas.drawPath(
|
||
p,
|
||
Paint()
|
||
..style = PaintingStyle.stroke
|
||
..strokeWidth = th + 7
|
||
..strokeCap = a.wrap ? StrokeCap.butt : StrokeCap.round
|
||
..color = tokens.shadow.withValues(alpha: tokens.shadowOp),
|
||
);
|
||
canvas.drawPath(
|
||
p,
|
||
Paint()
|
||
..style = PaintingStyle.stroke
|
||
..strokeWidth = th
|
||
..strokeCap = a.wrap ? StrokeCap.butt : StrokeCap.round
|
||
..color = col.withValues(alpha: tail ? tokens.tailOp : 1),
|
||
);
|
||
if (tail) {
|
||
canvas.drawPath(
|
||
p,
|
||
Paint()
|
||
..style = PaintingStyle.stroke
|
||
..strokeWidth = th
|
||
..strokeCap = StrokeCap.butt
|
||
..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,
|
||
);
|
||
}
|
||
}
|
||
|
||
// 文字沿圆弧排列并按弧长截断,保证始终位于动作色带之内。
|
||
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));
|
||
}
|
||
|
||
/// 绘制圆形视图中的依赖连线。
|
||
///
|
||
/// 路径由四段组成:从源弧末端沿半径到中间半径 [rC] → 沿圆弧走到目标弧
|
||
/// 起点角度 → 再沿半径落到目标弧表面([rTip])→ 末端加箭头。
|
||
/// - 相邻层取两层半径均值作为 [rC];层差大于 1 时改用最外圈 `rMax+24` 绕行。
|
||
/// - [large]/[cw] 决定圆弧走大弧还是小弧、顺时针还是逆时针。
|
||
/// - 关键依赖先画低透明度粗描边,再画主线,最后放置 `A→B 类型` 胶囊。
|
||
void _dep(Canvas canvas, CircularLayout layout, Dependency d) {
|
||
final Action? A = project.actionById(d.fromId);
|
||
final Action? B = project.actionById(d.toId);
|
||
if (A == null || B == null) return;
|
||
final Offset c = layout.center;
|
||
final double rA = layout.radiusFor(A.layer);
|
||
final double rB = layout.radiusFor(B.layer);
|
||
final double aEnd = math.min(A.endAngle, 360);
|
||
final double aStart = B.startAngle;
|
||
// 层差大于 1 时从最外圈绕行,避免穿过中间层弧线。
|
||
final bool skip = (A.layer - B.layer).abs() > 1;
|
||
final double rC = skip ? layout.rMax + 24 : (rA + rB) / 2;
|
||
final bool cw = aStart >= aEnd;
|
||
final int large = (aStart - aEnd).abs() > 180 ? 1 : 0;
|
||
final Offset p0 = polar(c, rA, aEnd);
|
||
final Offset p1 = polar(c, rC, aEnd);
|
||
final Offset p2 = polar(c, rC, aStart);
|
||
final double rTip = rC < rB ? rB - layout.th / 2 - 4 : rB + layout.th / 2 + 4;
|
||
final Offset p3 = polar(c, rTip, aStart);
|
||
final Color col = d.type == DepType.fs ? tokens.fs : tokens.ss;
|
||
final bool crit = criticalDeps.contains('${d.fromId}->${d.toId}');
|
||
|
||
final Path p = Path()
|
||
..moveTo(p0.dx, p0.dy)
|
||
..lineTo(p1.dx, p1.dy)
|
||
..arcToPoint(p2, radius: Radius.circular(rC), largeArc: large == 1, clockwise: cw)
|
||
..lineTo(p3.dx, p3.dy);
|
||
|
||
if (crit) {
|
||
canvas.drawPath(
|
||
p,
|
||
Paint()
|
||
..style = PaintingStyle.stroke
|
||
..strokeWidth = 9
|
||
..strokeCap = StrokeCap.round
|
||
..color = col.withValues(alpha: 0.2),
|
||
);
|
||
}
|
||
canvas.drawPath(
|
||
p,
|
||
Paint()
|
||
..style = PaintingStyle.stroke
|
||
..strokeWidth = crit ? 3 : (d.type == DepType.fs ? 2.4 : 2)
|
||
..strokeCap = StrokeCap.round
|
||
..color = col,
|
||
);
|
||
_arrow(canvas, p3, p3 - p2, col);
|
||
|
||
// 标签胶囊放置在连线中段:跨层时贴最外圈,短跨度时外移避免压线。
|
||
final double theta = (aEnd + aStart) / 2;
|
||
final double span = (aStart - aEnd).abs();
|
||
final double rChip = skip ? rC : (span < 30 ? rB + layout.th / 2 + 10 : rC);
|
||
final Offset mid = polar(c, rChip, theta);
|
||
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';
|
||
_chip(canvas, mid, '${A.id}→${B.id} $label', col, crit);
|
||
}
|
||
|
||
/// 在 [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 = 12;
|
||
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);
|
||
}
|
||
|
||
/// 绘制依赖标签胶囊。
|
||
///
|
||
/// 参数 [center]:胶囊中心;[label]:文字;[col]:边框与文字色;
|
||
/// [crit]:为真时在左侧追加星形关键标记并加宽胶囊。
|
||
void _chip(Canvas canvas, Offset center, String label, Color col, bool crit) {
|
||
final TextPainter tp = _tp(label, 10, col, FontWeight.w600);
|
||
final double w = tp.width + 16 + (crit ? 18 : 0);
|
||
final Rect r = Rect.fromCenter(center: center, width: w, height: 17);
|
||
canvas.drawRRect(RRect.fromRectAndRadius(r, const Radius.circular(6)), Paint()..color = tokens.chip);
|
||
canvas.drawRRect(
|
||
RRect.fromRectAndRadius(r, const Radius.circular(6)),
|
||
Paint()
|
||
..style = PaintingStyle.stroke
|
||
..strokeWidth = 1
|
||
..color = col,
|
||
);
|
||
double tx = r.left + 8;
|
||
if (crit) {
|
||
canvas.drawCircle(Offset(r.left + 8, center.dy), 6.5, Paint()..color = tokens.crit);
|
||
_text(canvas, '★', Offset(r.left + 8, center.dy), color: tokens.onAccent, size: 10, align: TextAlign.center);
|
||
tx = r.left + 18;
|
||
}
|
||
tp.paint(canvas, Offset(tx, center.dy - tp.height / 2));
|
||
}
|
||
|
||
/// 绘制 0° 起点标记:顶部朝下的三角与“0° · 12 点钟”文字。
|
||
void _startMarker(Canvas canvas, CircularLayout layout) {
|
||
final Offset c = layout.center;
|
||
final double r = layout.tickR + 12;
|
||
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);
|
||
}
|
||
|
||
/// 绘制顺时针方向指示:一段外圈圆弧、箭头与“顺时针 CW”文字。
|
||
void _cwArrow(Canvas canvas, CircularLayout layout) {
|
||
final Offset c = layout.center;
|
||
// 放在刻度环之外,避免“冲进”视图内部。
|
||
final double r = layout.tickR + 16;
|
||
final Path p = arcPath(c, r, 20, 70);
|
||
canvas.drawPath(
|
||
p,
|
||
Paint()
|
||
..style = PaintingStyle.stroke
|
||
..strokeWidth = 2.4
|
||
..strokeCap = StrokeCap.round
|
||
..color = tokens.accent2,
|
||
);
|
||
final Offset t1 = polar(c, r, 74);
|
||
final Offset t2 = polar(c, r + 14, 68);
|
||
final Offset t3 = polar(c, r - 14, 66);
|
||
final Path a = Path()
|
||
..moveTo(t1.dx, t1.dy)
|
||
..lineTo(t2.dx, t2.dy)
|
||
..lineTo(t3.dx, t3.dy)
|
||
..close();
|
||
canvas.drawPath(a, Paint()..color = tokens.accent2);
|
||
final Offset label = polar(c, r + 30, 45);
|
||
_text(canvas, '顺时针 CW', label, color: tokens.accent2, size: 11, 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;
|
||
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 + 38, 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);
|
||
}
|
||
}
|
||
}
|
||
|
||
/// 构造并布局一个 [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;
|
||
}
|