Files
EasyDesignCollection/lib/render/bar_painter.dart
T
admin e99f144793 feat(touch): 单指点选+按住移动动作、端点把手拖拽改时长(触屏目标加大);空白单指不平移
- 选中动作后两端绘制白色把手,端点抓取半径触屏 28px(桌面保持 8px 精确),命中容差 14px

- 短条两端都够得着时抓离手指更近的一端,居中则整体移动

- 触屏空白单指不再平移画布(单指只做选择/编辑,双指捏合缩放+平移);Windows 操作模式不变

- 重建签名 APK
2026-10-07 22:44:28 +08:00

656 lines
28 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/// 条形(时间轴)视图绘制器。
///
/// 职责:把 [Project] 中的动作按“双轴”条形图渲染到 Flutter 画布上——
/// 横轴同时表达相位角度与等价的绝对时间,纵轴按工位顺序排列动作行。
///
/// 架构位置:
/// - 全部几何计算委托给 [BarLayout](`lib/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 '../layout/layout.dart';
import '../model/models.dart';
import '../model/units.dart';
import '../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,可为空。
///
/// 无副作用;实例不缓存布局,每次 [paint] 都重新计算。
BarPainter({
required this.project,
required this.tokens,
required this.criticalActions,
required this.criticalDeps,
required this.selectedIds,
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;
/// 拖拽相位分区边界时的参考角(度);非空时绘制一条竖直虚线参考。
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 Color c = dropValid ? tokens.accent : tokens.danger;
canvas.drawRRect(
RRect.fromRectAndRadius(
Rect.fromLTWH(20, y - 8, layout.plotRight - 20, BarLayout.rowVisual),
const Radius.circular(10),
),
Paint()..color = c.withValues(alpha: 0.12),
);
canvas.drawRRect(
RRect.fromRectAndRadius(
Rect.fromLTWH(20, y - 8, layout.plotRight - 20, BarLayout.rowVisual),
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.yFor(layout.ordered.length) - 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.yFor(layout.ordered.length) - 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.yFor(li);
final List<Action> acts = layout.actionsInLane(lane);
// 隔泳道背景色带,弱化边界。
if (li.isEven) {
canvas.drawRRect(
RRect.fromRectAndRadius(
Rect.fromLTWH(20, y - 8, layout.plotRight - 20, BarLayout.rowVisual),
const Radius.circular(10),
),
Paint()..color = tokens.panel2.withValues(alpha: 0.5),
);
}
// 泳道标题(合并显示,最多两行,用 / 分隔)。
_laneHeader(canvas, layout, li, acts, y);
// 泳道底分隔线。
canvas.drawLine(
Offset(layout.plotLeft, y + BarLayout.rowVisual - 4),
Offset(layout.plotRight, y + BarLayout.rowVisual - 4),
Paint()
..color = tokens.line2.withValues(alpha: 0.6)
..strokeWidth = 1,
);
// 该泳道内的所有动作条(互不重叠)。
for (final Action a in acts) {
_bar(canvas, layout, a, u, cycle);
}
}
}
/// 绘制一条泳道的标题:序号徽标 + 合并动作名(≤2 行,用 / 分隔)+ 副标题。
void _laneHeader(Canvas canvas, BarLayout layout, int li, List<Action> acts, double y) {
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;
// 多个动作合并为「A1 上料 / A2 定位」,最多两行。
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: 2,
ellipsis: '…',
)..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} 个' : ''}';
_textMax(canvas, sub, Offset(60, y + 32), 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(8)),
Paint()..color = tokens.shadow.withValues(alpha: tokens.shadowOp),
);
canvas.drawRRect(
RRect.fromRectAndRadius(r, const Radius.circular(8)),
Paint()..color = c.withValues(alpha: 0.92),
);
canvas.drawRRect(
RRect.fromRectAndRadius(r, const Radius.circular(8)),
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(8)),
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(8)),
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) {
final List<Action> ordered = layout.ordered;
// 以「泳道序号」判断依赖的相邻关系(同泳道多个动作不再误判)。
final Map<String, int> index = <String, int>{
for (final Action a in project.actions) a.id: layout.laneIndexOf(a.layer),
};
int k = 0;
for (final Action to in ordered) {
for (final Dependency d in to.deps) {
final Action? from = project.actionById(d.fromId);
if (from == null) continue;
final int ia = index[from.id] ?? 0;
final int ib = index[to.id] ?? 0;
final Color col = d.type == DepType.fs ? tokens.fs : tokens.ss;
final bool crit = criticalDeps.contains('${d.fromId}->${d.toId}');
final String label = d.type == DepType.fs ? 'FS 顺序' : 'SS +${d.offsetAngle.toStringAsFixed(0)}°';
final double chipW = _tp(label, 9.5, col, FontWeight.w600).width + 14 + (crit ? 15 : 0);
final Rect ra = layout.rectFor(from);
final Rect rb = layout.rectFor(to);
const double barHalf = BarLayout.barH / 2;
final double sy = ra.center.dy;
final double ty = rb.center.dy;
final double dir = ty > sy ? 1 : -1;
// 箭头落点:目标条块靠源一侧外缘再留 3px。
final double tipY = ty - dir * (barHalf + 3);
final Path p = Path();
double chipX;
double chipY;
Offset arrowTip;
Offset arrowDir;
// 同一泳道内的依赖(源/目标同 y):箭头连接两色块的外侧边缘。
final bool sameLane = (ty - sy).abs() < 1;
if (sameLane) {
final bool fs = d.type == DepType.fs;
final double startX = fs ? ra.right + 2 : ra.left - 2;
double endX = rb.left - 2;
if (endX < startX) endX = rb.right + 2;
p.moveTo(startX, sy);
p.lineTo(endX, sy);
arrowTip = Offset(endX, sy);
arrowDir = Offset(endX >= startX ? 1 : -1, 0);
chipX = (startX + endX) / 2;
chipY = sy - (barHalf + 9);
// 两动作间距太短、图注放不下时,自动移到箭头右侧。
if ((endX - startX).abs() < chipW + 8) chipX = endX + chipW / 2 + 6;
} else if ((ib - ia).abs() <= 1) {
// 相邻泳道:在两泳道之间的空隙中线走水平线,箭头指向目标条块。
final double mid = (sy + ty) / 2;
final double sx = d.type == DepType.fs ? ra.right : ra.left;
final double ex = d.type == DepType.fs ? sx - 9 : sx + 9;
final double tx = rb.left + 14;
p.moveTo(ex, sy + dir * (barHalf + 2));
p.lineTo(ex, mid);
p.lineTo(tx, mid);
p.lineTo(tx, tipY);
arrowTip = Offset(tx, tipY);
arrowDir = Offset(0, dir);
chipX = (ex + tx) / 2;
chipY = mid;
if ((tx - ex).abs() < chipW + 10) chipX = tx + chipW / 2 + 6;
} else {
// 跨多行:扫描区间内所有条块边界,据此在外侧选一条竖直泳道。
final double sx = d.type == DepType.fs ? ra.right : ra.left;
final double ex = d.type == DepType.fs ? sx - 9 : sx + 9;
final double tx = rb.left + 14;
double maxRight = 0;
double minLeft = double.infinity;
final List<int> lanes = layout.lanes;
for (int r = math.min(ia, ib) + 1; r < math.max(ia, ib); r++) {
if (r < 0 || r >= lanes.length) continue;
for (final Action aa in layout.actionsInLane(lanes[r])) {
final Rect rr = layout.rectFor(aa);
maxRight = math.max(maxRight, rr.right);
minLeft = math.min(minLeft, rr.left);
}
}
double lane = math.max(math.max(sx, tx), maxRight) + 28 + (k % 3) * 16;
if (lane > layout.plotRight - 16) {
lane = math.min(math.min(sx, tx), minLeft) - 28 - (k % 3) * 16;
}
final double srcGapY = sy + dir * (barHalf + 14);
final double gapY = ty - dir * (barHalf + 6);
p.moveTo(ex, sy + dir * (barHalf + 2));
p.lineTo(ex, srcGapY);
p.lineTo(lane, srcGapY);
p.lineTo(lane, gapY);
p.lineTo(tx, gapY);
p.lineTo(tx, tipY);
arrowTip = Offset(tx, tipY);
arrowDir = Offset(0, dir);
chipX = (lane + tx) / 2;
chipY = gapY - (barHalf + 1);
}
// 关键路径依赖:先铺一层加粗低透明度光晕,再画主线。
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 = crit ? 1.1 : 0.85
..strokeCap = StrokeCap.round
..color = col.withValues(alpha: 0.95),
);
_arrow(canvas, arrowTip, arrowDir, col);
// 标签置于依赖线旁,避免压住箭头。
_chip(canvas, Offset(chipX, chipY), label, col, crit);
k++;
}
}
}
/// 在 [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);
}
/// 绘制依赖类型标签胶囊。
///
/// 参数 [center]:胶囊中心;[label]:文字;[col]:边框与文字色;
/// [crit]:为真时在左侧追加星形关键标记,并把胶囊加宽。
void _chip(Canvas canvas, Offset center, String label, Color col, bool crit) {
// 图注字号与刻度文字接近,整体更紧凑。
final TextPainter tp = _tp(label, 9.5, col, FontWeight.w600);
final double w = tp.width + 14 + (crit ? 15 : 0);
const double h = 15;
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,
);
if (crit) {
canvas.drawCircle(Offset(r.left + 8, center.dy), 6, Paint()..color = tokens.crit);
_text(canvas, '★', Offset(r.left + 8, center.dy), color: tokens.onAccent, size: 9, align: TextAlign.center);
tp.paint(canvas, Offset(r.left + 16, center.dy - tp.height / 2));
} else {
tp.paint(canvas, Offset(r.left + 7, 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;
}