/// 参数化草图绘制:几何(直线 / 圆 / 弧)、点(固定点高亮)、 /// 约束图标(H / V / ∥ / ⊥ / = / ◎ / T)、驱动尺寸标注与预览。 /// /// 所有尺寸按 `1 / zoom` 逆缩放,保持屏幕像素恒定(CAD 观感)。 library; import 'dart:math' as math; import 'dart:typed_data'; import 'package:flutter/material.dart'; import '../theme/tokens.dart'; import 'sketch_models.dart'; /// 草图配色(浅 / 深色主题下都可读)。 const int kSketchLineColor = 0xFF2F6FEB; const int kSketchPointColor = 0xFF64748B; const int kSketchFixedColor = 0xFFDC2626; const int kSketchDimColor = 0xFF0F766E; const int kSketchConsColor = 0xFF7C3AED; const int kSketchSelColor = 0xFFF97316; const int kSketchPreviewColor = 0xFF0EA5E9; /// 草图预览信息(绘制中的橡皮筋)。 class SketchPreview { /// 构造。 const SketchPreview({required this.pts, this.kind = SketchKind.line, this.closed = false}); /// 预览点(世界坐标)。 final List pts; /// 预览类型(line:折线描边;circle / arc 用第一点为圆心)。 final SketchKind kind; /// 是否闭合描边。 final bool closed; } /// 绘制草图内容(调用方已完成 translate + scale)。 void paintSketch( Canvas canvas, SketchDoc doc, Tokens t, { Set sel = const {}, Offset? hover, SketchPreview? preview, Offset? snapMark, }) { // 逆缩放因子:通过画布变换取得真实缩放,保证线宽 / 字号恒定。 final double scale = _canvasScale(canvas); final double s = scale <= 0 ? 1 : 1 / scale; // ---- 几何 ---- for (final SketchEntity e in doc.entities) { final bool selected = sel.contains(e.id); final Paint paint = Paint() ..style = PaintingStyle.stroke ..strokeWidth = (selected ? 2.4 : 1.6) * s ..strokeCap = StrokeCap.round ..strokeJoin = StrokeJoin.round ..color = Color(selected ? kSketchSelColor : kSketchLineColor); switch (e.kind) { case SketchKind.line: final Offset? a = doc.pointById(e.p1)?.pos; final Offset? b = doc.pointById(e.p2)?.pos; if (a == null || b == null) break; canvas.drawLine(a, b, paint); case SketchKind.circle: final Offset? c = doc.pointById(e.p1)?.pos; if (c == null) break; canvas.drawCircle(c, e.r, paint); case SketchKind.arc: final Offset? c = doc.pointById(e.p1)?.pos; final Offset? ps = doc.pointById(e.p2)?.pos; final Offset? pe = doc.pointById(e.p3)?.pos; if (c == null || ps == null || pe == null) break; final (double a1, double sweep) = sketchArcAngles(c, ps, pe, e.ccw); final Path path = Path() ..addArc(Rect.fromCircle(center: c, radius: e.r), e.ccw ? a1 : a1 - sweep, sweep); canvas.drawPath(path, paint); } } // ---- 点 ---- for (final SketchPoint p in doc.points) { final bool selected = sel.contains(p.id); final bool hovered = hover != null && (hover - p.pos).distance < 7 * s; final double r = (hovered ? 3.6 : 2.8) * s; canvas.drawRect( Rect.fromCenter(center: p.pos, width: r * 2, height: r * 2), Paint() ..color = Color(selected ? kSketchSelColor : (p.fixed ? kSketchFixedColor : kSketchPointColor)), ); if (p.fixed) { // 固定点:外圈提示锚定。 canvas.drawCircle( p.pos, r * 2.2, Paint() ..style = PaintingStyle.stroke ..strokeWidth = 1.2 * s ..color = Color(kSketchFixedColor), ); } } // ---- 约束图标 ---- for (final SketchConstraint k in doc.constraints) { if (k.internal) continue; final List anchors = _consAnchors(doc, k); for (final Offset a in anchors) { _label(canvas, _consGlyph(k.type), a, s, color: Color(kSketchConsColor), bg: t.panel.withValues(alpha: 0.92)); } } // ---- 驱动尺寸 ---- for (final SketchDim d in doc.dims) { _dim(canvas, doc, d, t, s, sel.contains(d.id)); } // ---- 预览 ---- final SketchPreview? pv = preview; if (pv != null && pv.pts.isNotEmpty) { final Paint pp = Paint() ..style = PaintingStyle.stroke ..strokeWidth = 1.8 * s ..strokeCap = StrokeCap.round ..color = Color(kSketchPreviewColor); switch (pv.kind) { case SketchKind.line: if (pv.pts.length >= 2) { final Path path = Path()..moveTo(pv.pts.first.dx, pv.pts.first.dy); for (int i = 1; i < pv.pts.length; i++) { path.lineTo(pv.pts[i].dx, pv.pts[i].dy); } if (pv.closed) path.close(); canvas.drawPath(path, pp); } case SketchKind.circle: if (pv.pts.length >= 2) { canvas.drawCircle(pv.pts.first, (pv.pts.last - pv.pts.first).distance, pp); } case SketchKind.arc: if (pv.pts.length >= 3) { final Offset c = pv.pts.first; final Offset ps = pv.pts[1]; final Offset pe = pv.pts.last; final double r = (ps - c).distance; final (double a1, double sweep) = sketchArcAngles(c, ps, pe, true); canvas.drawPath( Path() ..addArc( Rect.fromCircle(center: c, radius: r), a1, sweep), pp, ); } } } // ---- 捕捉标记 ---- final Offset? sm = snapMark; if (sm != null) { canvas.drawCircle( sm, 4.5 * s, Paint() ..style = PaintingStyle.stroke ..strokeWidth = 1.6 * s ..color = Color(kSketchPreviewColor), ); } } /// 取画布当前缩放的倒数(保持屏幕像素恒定的线宽 / 文字)。 double _canvasScale(Canvas canvas) { final Float64List m = canvas.getTransform(); final double sx = math.sqrt(m[0] * m[0] + m[1] * m[1]); final double sy = math.sqrt(m[4] * m[4] + m[5] * m[5]); final double s = (sx + sy) / 2; return s <= 0 ? 1 : s; } /// 约束图标锚点(每个约束可画多处)。 List _consAnchors(SketchDoc doc, SketchConstraint k) { Offset? midEntity(String id) { final SketchEntity? e = doc.entityById(id); if (e == null) return null; if (e.kind == SketchKind.line) { final Offset? a = doc.pointById(e.p1)?.pos; final Offset? b = doc.pointById(e.p2)?.pos; if (a == null || b == null) return null; final Offset m = Offset.lerp(a, b, 0.5)!; final Offset d = b - a; final double len = d.distance; if (len < 1e-6) return m; return m + Offset(-d.dy / len, d.dx / len) * 12; } final Offset? c = doc.pointById(e.p1)?.pos; if (c == null) return null; return c + Offset(e.r * 0.35, -e.r * 0.35); } switch (k.type) { case SketchConsType.horizontal: case SketchConsType.vertical: final Offset? m = midEntity(k.a); return m == null ? [] : [m]; case SketchConsType.pointOnEntity: final Offset? p = doc.pointById(k.a)?.pos; return p == null ? [] : [p + const Offset(10, -10)]; case SketchConsType.concentric: final Offset? c = doc.pointById(doc.entityById(k.a)?.p1)?.pos; return c == null ? [] : [c + const Offset(14, -14)]; default: final List out = []; final Offset? m1 = midEntity(k.a); if (m1 != null) out.add(m1); return out; } } String _consGlyph(SketchConsType t2) => switch (t2) { SketchConsType.horizontal => 'H', SketchConsType.vertical => 'V', SketchConsType.parallel => '∥', SketchConsType.perpendicular => '⊥', SketchConsType.equal => '=', SketchConsType.concentric => '◎', SketchConsType.tangent => 'T', SketchConsType.pointOnEntity => '⌐', }; /// 画一条尺寸标注。 void _dim( Canvas canvas, SketchDoc doc, SketchDim d, Tokens t, double s, bool selected, ) { final Offset? anchor = sketchDimAnchor(doc, d); if (anchor == null) return; final Color color = Color(selected ? kSketchSelColor : kSketchDimColor); final Paint line = Paint() ..style = PaintingStyle.stroke ..strokeWidth = 1.2 * s ..color = color; switch (d.type) { case SketchDimType.length: case SketchDimType.distance: final (Offset p1, Offset p2) = d.type == SketchDimType.length ? _entityEnds(doc, d.a) : (doc.pointById(d.a)?.pos ?? Offset.zero, doc.pointById(d.b)?.pos ?? Offset.zero); final Offset dir = p2 - p1; final double len = dir.distance; if (len < 1e-6) return; final Offset n = Offset(-dir.dy / len, dir.dx / len); final double off = d.type == SketchDimType.length ? 18 : -20; final Offset a2 = p1 + n * (off * s); final Offset b2 = p2 + n * (off * s); canvas.drawLine(a2, b2, line); canvas.drawLine(p1, a2 + n * 3 * s, line); canvas.drawLine(p2, b2 + n * 3 * s, line); _tick(canvas, a2, n, s, color); _tick(canvas, b2, n, s, color); case SketchDimType.radius: final SketchEntity? e = doc.entityById(d.a); final Offset? c = doc.pointById(e?.p1)?.pos; if (e == null || c == null) return; final Offset dir = const Offset(0.7071, -0.7071); final Offset end = c + dir * e.r; canvas.drawLine(c, end, line); canvas.drawLine( c, c + dir * (e.r * 0.45), Paint() ..strokeWidth = 3.2 * s ..color = color); case SketchDimType.angle: final Offset? a1 = doc.pointById(doc.entityById(d.a)?.p1)?.pos; final Offset? a2 = doc.pointById(doc.entityById(d.a)?.p2)?.pos; if (a1 == null || a2 == null) return; // 简化:在第一条线的中点画一个角度弧示意。 final Offset m = Offset.lerp(a1, a2, 0.5)!; canvas.drawCircle( m, 10 * s, Paint() ..style = PaintingStyle.stroke ..strokeWidth = 1.2 * s ..color = color, ); } _label(canvas, sketchDimText(d), anchor, s, color: color, bg: t.panel.withValues(alpha: 0.94), border: color); } (Offset, Offset) _entityEnds(SketchDoc doc, String id) { final SketchEntity? e = doc.entityById(id); if (e == null) return (Offset.zero, Offset.zero); return (doc.pointById(e.p1)?.pos ?? Offset.zero, doc.pointById(e.p2)?.pos ?? Offset.zero); } /// 尺寸界线端点的小斜线(CAD 风格刻度)。 void _tick(Canvas canvas, Offset p, Offset n, double s, Color color) { canvas.drawLine( p + Offset(-n.dy, n.dx) * 3.5 * s, p + Offset(n.dy, -n.dx) * 3.5 * s, Paint() ..strokeWidth = 1.2 * s ..color = color, ); } /// 小圆角标签(逆缩放保持屏幕字号恒定)。 void _label( Canvas canvas, String text, Offset anchor, double s, { required Color color, required Color bg, Color? border, }) { final TextPainter tp = TextPainter( text: TextSpan( text: text, style: TextStyle( color: color, fontSize: 10 * s, fontWeight: FontWeight.w600, ), ), textDirection: TextDirection.ltr, )..layout(); final Rect r = Rect.fromCenter( center: anchor, width: tp.width + 8 * s, height: tp.height + 3 * s, ); final RRect rr = RRect.fromRectAndRadius(r, Radius.circular(4 * s)); canvas.drawRRect(rr, Paint()..color = bg); canvas.drawRRect( rr, Paint() ..style = PaintingStyle.stroke ..strokeWidth = 1 * s ..color = border ?? color.withValues(alpha: 0.45), ); tp.paint(canvas, r.center - Offset(tp.width / 2, tp.height / 2)); }