/// 架构图绘制引擎:标准图形(矩形 / 圆 / 云 / 圆柱 / 折线 / 曲线……)、 /// 连线(直线 / 正交折线,箭头 + 标签)、手绘墨迹、框选预览、 /// 「墨迹 → 标准图形」转化动画与多选选中框。 /// /// 纯绘制:不持有状态,所有数据经构造函数传入;画布坐标(世界坐标)通过 /// [ArchDoc.zoom] / [ArchDoc.panX] / [ArchDoc.panY] 变换到屏幕。 library; import 'dart:math' as math; import 'dart:ui' as ui; import 'package:flutter/material.dart'; import '../library/symbol_library.dart'; import '../sketch/sketch_painter.dart'; import '../theme/tokens.dart'; import 'arch_models.dart'; /// 一次「墨迹 → 标准图形」的转化动画记录。 class ArchConversion { /// 构造转化记录;[startMs] 为开始时间(毫秒时间戳)。 ArchConversion({ required this.pts, required this.shape, required this.startMs, this.undo = true, this.removeIds = const {}, }); /// 原始笔迹点。 final List pts; /// 识别出的标准图形。 final ArchShape shape; /// 动画开始时间(毫秒时间戳)。 final double startMs; /// 完成后写入文档时是否记录撤销历史。 final bool undo; /// 完成后要移除的元素 id(两笔合并 / 圆柱拼接 / 识别演示批量转化用)。 final Set removeIds; /// 动画时长(毫秒)。 static const double durMs = 280; /// 当前动画进度(0..1)。 double progress(double nowMs) => ((nowMs - startMs) / durMs).clamp(0.0, 1.0).toDouble(); } /// 由图形几何构造路径(绘制、墨迹采样、命中测试共用)。 Path archShapePath(ArchShape s) { final Rect r = s.rect; switch (s.type) { case ArchShapeType.rect: return Path()..addRect(r); case ArchShapeType.roundRect: case ArchShapeType.server: case ArchShapeType.client: case ArchShapeType.mobile: case ArchShapeType.firewall: case ArchShapeType.netSwitch: case ArchShapeType.queue: case ArchShapeType.symbol: final double rad = switch (s.type) { ArchShapeType.server => math.min(12.0, math.min(r.width, r.height) * 0.12), ArchShapeType.client => math.min(10.0, r.height * 0.08), ArchShapeType.mobile => math.min(16.0, r.width * 0.22), ArchShapeType.firewall => math.min(6.0, r.height * 0.10), ArchShapeType.netSwitch => math.min(10.0, r.height * 0.20), ArchShapeType.queue => r.height * 0.5, _ => math.min(s.radius, math.min(r.width, r.height) * 0.45), }; return Path() ..addRRect(RRect.fromRectAndRadius(r, Radius.circular(math.max(0, rad)))); case ArchShapeType.ellipse: case ArchShapeType.circle: return Path()..addOval(r); case ArchShapeType.triangle: return _polyPath(archTrianglePoints(s), close: true); case ArchShapeType.diamond: return _polyPath(archDiamondPoints(r), close: true); case ArchShapeType.hexagon: return _polyPath(archHexagonPoints(r), close: true); case ArchShapeType.cylinder: final (Path body, Path lid) = archCylinderPaths(r); return Path() ..addPath(body, Offset.zero) ..addPath(lid, Offset.zero); case ArchShapeType.cloud: return archCloudPath(r); case ArchShapeType.text: return Path()..addRect(r); case ArchShapeType.line: final Offset a = s.a ?? r.topLeft; final Offset b = s.b ?? r.bottomRight; return Path() ..moveTo(a.dx, a.dy) ..lineTo(b.dx, b.dy); case ArchShapeType.polyline: return _polyPath(s.pts, close: false); case ArchShapeType.curve: return archSplinePath(s.pts); } } /// 样条路径:以 [pts] 为通过点做 Catmull-Rom 平滑(转三次贝塞尔)。 Path archSplinePath(List pts) { final Path path = Path(); if (pts.isEmpty) return path; path.moveTo(pts.first.dx, pts.first.dy); if (pts.length == 2) { path.lineTo(pts[1].dx, pts[1].dy); return path; } for (int i = 0; i < pts.length - 1; i++) { final Offset p0 = i == 0 ? pts[i] : pts[i - 1]; final Offset p1 = pts[i]; final Offset p2 = pts[i + 1]; final Offset p3 = i + 2 < pts.length ? pts[i + 2] : pts[i + 1]; final Offset c1 = p1 + (p2 - p0) / 6; final Offset c2 = p2 - (p3 - p1) / 6; path.cubicTo(c1.dx, c1.dy, c2.dx, c2.dy, p2.dx, p2.dy); } return path; } /// 三角形顶点(按 [ArchShape.dir] 朝向)。 List archTrianglePoints(ArchShape s) { final Rect r = s.rect; switch (s.dir) { case 1: return [ Offset(r.left, r.top), Offset(r.right, r.center.dy), Offset(r.left, r.bottom), ]; case 2: return [ Offset(r.left, r.top), Offset(r.right, r.top), Offset(r.center.dx, r.bottom), ]; case 3: return [ Offset(r.right, r.top), Offset(r.left, r.center.dy), Offset(r.right, r.bottom), ]; default: return [ Offset(r.left, r.bottom), Offset(r.center.dx, r.top), Offset(r.right, r.bottom), ]; } } /// 菱形顶点(外接矩形四边中点)。 List archDiamondPoints(Rect r) => [ Offset(r.center.dx, r.top), Offset(r.right, r.center.dy), Offset(r.center.dx, r.bottom), Offset(r.left, r.center.dy), ]; /// 六边形顶点(长边方向自适应:宽 → 左右尖,高 → 上下尖)。 List archHexagonPoints(Rect r) { final double w = r.width; final double h = r.height; if (w >= h) { final double d = w * 0.25; return [ Offset(r.left, r.center.dy), Offset(r.left + d, r.top), Offset(r.right - d, r.top), Offset(r.right, r.center.dy), Offset(r.right - d, r.bottom), Offset(r.left + d, r.bottom), ]; } final double d = h * 0.25; return [ Offset(r.center.dx, r.top), Offset(r.right, r.top + d), Offset(r.right, r.bottom - d), Offset(r.center.dx, r.bottom), Offset(r.left, r.bottom - d), Offset(r.left, r.top + d), ]; } /// 圆柱路径:柱体(含底部弧)+ 顶盖椭圆。 (Path, Path) archCylinderPaths(Rect r) { final double ry = math.min(r.height * 0.20, r.width * 0.32); final double l = r.left; final double rt = r.right; final double tp = r.top; final double bt = r.bottom; final Path body = Path() ..moveTo(l, tp + ry) ..lineTo(l, bt - ry) ..arcTo(Rect.fromLTRB(l, bt - 2 * ry, rt, bt), math.pi, -math.pi, false) ..lineTo(rt, tp + ry) ..close(); final Path lid = Path()..addOval(Rect.fromLTRB(l, tp, rt, tp + 2 * ry)); return (body, lid); } /// 云路径:4 个圆弧 + 底座圆角矩形求并集。 Path archCloudPath(Rect r) { final double w = r.width; final double h = r.height; final double cx = r.left; final double cy = r.top; Path acc = Path() ..addOval(Rect.fromCircle( center: Offset(cx + w * 0.24, cy + h * 0.62), radius: h * 0.26, )); acc = Path.combine( PathOperation.union, acc, Path() ..addOval(Rect.fromCircle( center: Offset(cx + w * 0.42, cy + h * 0.40), radius: h * 0.30, )), ); acc = Path.combine( PathOperation.union, acc, Path() ..addOval(Rect.fromCircle( center: Offset(cx + w * 0.64, cy + h * 0.34), radius: h * 0.28, )), ); acc = Path.combine( PathOperation.union, acc, Path() ..addOval(Rect.fromCircle( center: Offset(cx + w * 0.82, cy + h * 0.58), radius: h * 0.24, )), ); acc = Path.combine( PathOperation.union, acc, Path() ..addRRect(RRect.fromRectAndRadius( Rect.fromLTRB(cx + w * 0.06, cy + h * 0.55, cx + w * 0.94, cy + h * 0.86), Radius.circular(h * 0.22), )), ); return acc; } /// 平滑折线路径(手绘墨迹用:以中点为锚做二次贝塞尔)。 Path archSmoothPath(List pts) { final Path p = Path(); if (pts.isEmpty) return p; p.moveTo(pts.first.dx, pts.first.dy); if (pts.length == 2) { p.lineTo(pts[1].dx, pts[1].dy); return p; } for (int i = 1; i < pts.length - 1; i++) { final Offset mid = (pts[i] + pts[i + 1]) / 2; p.quadraticBezierTo(pts[i].dx, pts[i].dy, mid.dx, mid.dy); } p.lineTo(pts.last.dx, pts.last.dy); return p; } /// 选中手柄(8 个)的世界坐标矩形;顺序:0 左上 / 1 上 / 2 右上 / 3 右 / /// 4 右下 / 5 下 / 6 左下 / 7 左。 List archHandleRects(Rect r, double zoom) { final double z = zoom < 0.05 ? 1 : zoom; final double s = 8 / z; final double h = s / 2; final List cs = [ r.topLeft, Offset(r.center.dx, r.top), r.topRight, Offset(r.right, r.center.dy), r.bottomRight, Offset(r.center.dx, r.bottom), r.bottomLeft, Offset(r.left, r.center.dy), ]; return [ for (final Offset c in cs) Rect.fromLTWH(c.dx - h, c.dy - h, s, s), ]; } /// 架构图画笔:一次绘制完整文档(含草稿 / 框选 / 动画 / 多选)。 class ArchPainter extends CustomPainter { /// 构造画笔。 ArchPainter({ required this.doc, required this.t, this.selectedIds = const {}, this.conversions = const [], this.nowMs = 0, this.draft, this.penColor = 0xFF4CC9F0, this.connectPts, this.connectTargetId, this.moveDeltas, this.resizeId, this.resizeRect, this.marquee, this.previewShape, this.previewConnector, this.guideXs = const [], this.guideYs = const [], this.cursorWorld, this.showRulers = true, this.sketchMode = false, this.sketchSel = const {}, this.sketchPreview, this.sketchSnap, }); /// 当前文档。 final ArchDoc doc; /// 主题令牌。 final Tokens t; /// 选中元素 id 集合(图形 / 连线 / 墨迹)。 final Set selectedIds; /// 进行中的转化动画。 final List conversions; /// 当前时间(毫秒时间戳,驱动转化动画)。 final double nowMs; /// 正在手绘的草稿点(世界坐标)。 final List? draft; /// 手绘草稿颜色(ARGB)。 final int penColor; /// 连线拖拽预览的折点(世界坐标)。 final List? connectPts; /// 连线拖拽悬停的目标图形 id。 final String? connectTargetId; /// 多选移动预览:图形 id → 位移。 final Map? moveDeltas; /// 缩放预览:拖拽中的图形 id 与新矩形。 final String? resizeId; final Rect? resizeRect; /// 框选预览矩形(世界坐标)。 final Rect? marquee; /// 折点拖动预览(覆盖对应图形,用于折线 / 曲线编点)。 final ArchShape? previewShape; /// 连线折点拖动预览(覆盖对应连线)。 final ArchConnector? previewConnector; /// 移动时的磁力对齐虚线(世界坐标 x)。 final List guideXs; /// 移动时的磁力对齐虚线(世界坐标 y)。 final List guideYs; /// 鼠标当前位置(世界坐标;四边标尺上的滑动虚线用)。 final Offset? cursorWorld; /// 是否显示四边标尺。 final bool showRulers; /// 参数化草图模式:只绘制草图内容。 final bool sketchMode; /// 草图选中 id(点 / 实体 / 尺寸)。 final Set sketchSel; /// 草图绘制预览。 final SketchPreview? sketchPreview; /// 草图捕捉标记。 final Offset? sketchSnap; /// 取图形预览(拖拽中应用临时位移 / 尺寸 / 折点)。 ArchShape _preview(ArchShape s) { final ArchShape? ps = previewShape; if (ps != null && ps.id == s.id) return ps; final Map? deltas = moveDeltas; if (deltas != null && deltas.containsKey(s.id)) { return s.translated(deltas[s.id]!); } if (resizeId != null && resizeRect != null && s.id == resizeId) { return s.copyWith(rect: resizeRect); } return s; } /// 绘制。 @override void paint(Canvas canvas, Size size) { _grid(canvas, size); canvas.save(); canvas.translate(doc.panX, doc.panY); canvas.scale(doc.zoom); if (sketchMode) { // 参数化草图模式:只显示草图内容(架构图形 / 墨迹 / 连线隐藏)。 paintSketch(canvas, doc.sketch, t, sel: sketchSel, preview: sketchPreview, snapMark: sketchSnap); } else { // A0 图纸边界(1 世界单位 = 1 mm;浅色描边示意画布范围)。 canvas.drawRect( Rect.fromLTWH(0, 0, kArchPageSize.width, kArchPageSize.height), Paint() ..style = PaintingStyle.stroke ..strokeWidth = 1 / (doc.zoom < 0.05 ? 1 : doc.zoom) ..color = t.line2, ); // 连线在底层(图形遮住线头,视觉更干净;隐藏图层不绘制)。 for (final ArchConnector c in doc.connectors) { if (!doc.isLayerVisible(c.layerId)) continue; _connector(canvas, c); } for (final ArchShape s in doc.shapes) { if (!doc.isLayerVisible(s.layerId)) continue; _drawShape(canvas, _preview(s)); } for (final ArchStroke s in doc.strokes) { if (!doc.isLayerVisible(s.layerId)) continue; canvas.drawPath( archSmoothPath(s.pts), Paint() ..style = PaintingStyle.stroke ..strokeWidth = s.w ..strokeCap = StrokeCap.round ..strokeJoin = StrokeJoin.round ..color = Color(s.color), ); } // 转化动画:墨迹淡出 + 标准图形浮现。 for (final ArchConversion c in conversions) { final double p = c.progress(nowMs); canvas.drawPath( archSmoothPath(c.pts), Paint() ..style = PaintingStyle.stroke ..strokeWidth = 2.2 ..strokeCap = StrokeCap.round ..strokeJoin = StrokeJoin.round ..color = Color(c.shape.stroke).withValues(alpha: (1 - p) * 0.9), ); canvas.saveLayer( c.shape.bounds.inflate(12), Paint()..color = Color.fromARGB((255 * p).round(), 0, 0, 0), ); _drawShape(canvas, c.shape); canvas.restore(); } } _selection(canvas); // 磁力对齐虚线(移动吸附时显示,贯穿当前可视区域)。 if (guideXs.isNotEmpty || guideYs.isNotEmpty) { final double z = doc.zoom < 0.05 ? 1 : doc.zoom; final double x0 = -doc.panX / z; final double y0 = -doc.panY / z; final double x1 = (size.width - doc.panX) / z; final double y1 = (size.height - doc.panY) / z; final Paint gp = Paint() ..style = PaintingStyle.stroke ..strokeWidth = 1.2 / z ..color = t.accent; for (final double x in guideXs) { _dash( canvas, Path() ..moveTo(x, y0) ..lineTo(x, y1), gp, ); } for (final double y in guideYs) { _dash( canvas, Path() ..moveTo(x0, y) ..lineTo(x1, y), gp, ); } } // 框选预览。 final Rect? mq = marquee; if (mq != null) { canvas.drawRect( mq, Paint()..color = t.accent.withValues(alpha: 0.10), ); _dash( canvas, Path()..addRect(mq), Paint() ..style = PaintingStyle.stroke ..strokeWidth = 1.2 / doc.zoom ..color = t.accent, ); } // 手绘草稿(实时墨迹)。 final List? d = draft; if (d != null && d.length > 1) { canvas.drawPath( archSmoothPath(d), Paint() ..style = PaintingStyle.stroke ..strokeWidth = 2.2 ..strokeCap = StrokeCap.round ..strokeJoin = StrokeJoin.round ..color = Color(penColor), ); } // 连线拖拽预览(直线 / 正交)。 final List? cp = connectPts; if (cp != null && cp.length >= 2) { final List clean = []; for (final Offset p in cp) { if (clean.isEmpty || (p - clean.last).distance > 0.5) clean.add(p); } final Path path = Path()..moveTo(clean.first.dx, clean.first.dy); for (int i = 1; i < clean.length; i++) { path.lineTo(clean[i].dx, clean[i].dy); } _dash( canvas, path, Paint() ..style = PaintingStyle.stroke ..strokeWidth = 1.6 ..color = t.accent, ); if (clean.length >= 2) { _arrowHead(canvas, clean[clean.length - 2], clean.last, t.accent); } final ArchShape? target = doc.shapeById(connectTargetId); if (target != null) { canvas.drawPath( archShapePath(_preview(target)), Paint() ..style = PaintingStyle.stroke ..strokeWidth = 2 ..color = t.accent.withValues(alpha: 0.8), ); } } canvas.restore(); // 四边标尺 + 鼠标滑动虚线(屏幕空间,最后绘制覆盖在内容之上)。 _rulers(canvas, size); } /// 四边标尺 + 鼠标位置实线(Adobe 风格:上标尺 + 右标尺带数字,单位强制 mm)。 /// /// 约定:世界坐标 1 单位 = 1 毫米(绘图 / 标尺统一按 mm 显示)。 void _rulers(Canvas canvas, Size size) { if (!showRulers) return; final double z = doc.zoom < 0.05 ? 1 : doc.zoom; const double band = 16; final Paint bandPaint = Paint()..color = t.panel.withValues(alpha: 0.94); final Paint edgePaint = Paint() ..style = PaintingStyle.stroke ..strokeWidth = 1 ..color = t.line2; final Paint tickPaint = Paint() ..style = PaintingStyle.stroke ..strokeWidth = 1 ..color = t.dim; canvas.drawRect(Rect.fromLTWH(0, 0, size.width, band), bandPaint); canvas.drawRect( Rect.fromLTWH(0, size.height - band, size.width, band), bandPaint); canvas.drawRect(Rect.fromLTWH(0, 0, band, size.height), bandPaint); canvas.drawRect( Rect.fromLTWH(size.width - band, 0, band, size.height), bandPaint); // 主刻度步长:屏幕上约 80px 一格。 double niceStep(double raw) { final double mag = math .pow(10, (math.log(raw) / math.ln10).floorToDouble()) .toDouble(); for (final double m in [1, 2, 5, 10]) { if (mag * m >= raw) return mag * m; } return mag * 10; } final double step = niceStep(80 / z); final double x0 = -doc.panX / z; final double x1 = (size.width - doc.panX) / z; final double y0 = -doc.panY / z; final double y1 = (size.height - doc.panY) / z; for (double wx = (x0 / step).floorToDouble() * step; wx <= x1; wx += step) { final double sx = wx * z + doc.panX; canvas.drawLine(Offset(sx, band - 5), Offset(sx, band), tickPaint); canvas.drawLine(Offset(sx, size.height - band), Offset(sx, size.height - band + 5), tickPaint); // 水平轴:数字在上标尺。 _rulerLabel(canvas, _fmtMm(wx), Offset(sx + 2, band - 10)); } for (double wy = (y0 / step).floorToDouble() * step; wy <= y1; wy += step) { final double sy = wy * z + doc.panY; canvas.drawLine(Offset(band - 5, sy), Offset(band, sy), tickPaint); canvas.drawLine(Offset(size.width - band, sy), Offset(size.width - band + 5, sy), tickPaint); // 垂直轴:数字在右标尺(旋转 90°),左侧不显示数字。 _rulerLabelVertical( canvas, _fmtMm(wy), size.width - band + 2.5, sy); } // 单位徽标(强制 mm):左上角。 _rulerLabel(canvas, 'mm', const Offset(2, 3)); // 鼠标滑动实线:四条标尺上各显示一段(指示当前位置)。 final Offset? cw = cursorWorld; if (cw != null) { final double sx = cw.dx * z + doc.panX; final double sy = cw.dy * z + doc.panY; final Paint solid = Paint() ..style = PaintingStyle.stroke ..strokeWidth = 1.2 ..color = t.accent; canvas.drawLine(Offset(sx, 0), Offset(sx, band), solid); canvas.drawLine( Offset(sx, size.height - band), Offset(sx, size.height), solid); canvas.drawLine(Offset(0, sy), Offset(band, sy), solid); canvas.drawLine( Offset(size.width - band, sy), Offset(size.width, sy), solid); } canvas.drawLine(Offset(0, band), Offset(size.width, band), edgePaint); canvas.drawLine(Offset(0, size.height - band), Offset(size.width, size.height - band), edgePaint); canvas.drawLine(Offset(band, 0), Offset(band, size.height), edgePaint); canvas.drawLine(Offset(size.width - band, 0), Offset(size.width - band, size.height), edgePaint); } /// 标尺刻度数字。 void _rulerLabel(Canvas canvas, String s, Offset at) { _labelPainter(s).paint(canvas, at); } /// 垂直标尺数字(旋转 90°,沿右刻度带向上排布;[centerY] 为刻度线位置)。 void _rulerLabelVertical( Canvas canvas, String s, double x, double centerY) { final TextPainter tp = _labelPainter(s); canvas.save(); canvas.translate(x, centerY + tp.width / 2); canvas.rotate(-math.pi / 2); tp.paint(canvas, Offset.zero); canvas.restore(); } /// 标尺数字的文本排版(9px,等宽数字观感)。 TextPainter _labelPainter(String s) => TextPainter( text: TextSpan( text: s, style: TextStyle( color: t.sub, fontSize: 9, fontFamily: 'Microsoft YaHei UI', fontFamilyFallback: const ['Microsoft YaHei', 'Segoe UI'], ), ), textDirection: TextDirection.ltr, )..layout(); /// mm 数值显示:整数不带小数,其余保留一位小数。 static String _fmtMm(double v) { if ((v - v.roundToDouble()).abs() < 0.05) return v.round().toString(); return v.toStringAsFixed(1); } /// 背景点阵(间距 = 文档网格粒度,随缩放自适应避免过密)。 void _grid(Canvas canvas, Size size) { final double z = doc.zoom < 0.05 ? 1 : doc.zoom; double step = doc.gridSize <= 4 ? 0 : doc.gridSize; if (step <= 0) return; while (step * z < 9) { step *= 2; } final double x0 = (0 - doc.panX) / z; final double y0 = (0 - doc.panY) / z; final double x1 = (size.width - doc.panX) / z; final double y1 = (size.height - doc.panY) / z; final Paint dot = Paint()..color = t.grid.withValues(alpha: 0.6); final double r = 1.1 / z; for (double x = (x0 / step).floorToDouble() * step; x <= x1; x += step) { for (double y = (y0 / step).floorToDouble() * step; y <= y1; y += step) { canvas.drawCircle(Offset(x, y), r, dot); } } } /// 服务器(机架抽屉式):外框 + 槽位线 + 状态灯。 void _drawServer(Canvas canvas, ArchShape s, Paint fill, Paint line) { final Rect r = s.rect; final Path frame = archShapePath(s); if (s.fill != 0) canvas.drawPath(frame, fill); canvas.drawPath(frame, line); final Color c = Color(s.stroke); final Paint slot = Paint() ..style = PaintingStyle.stroke ..strokeWidth = math.max(1, s.sw * 0.7) ..color = c.withValues(alpha: 0.8); for (int i = 1; i <= 3; i++) { final double y = r.top + r.height * i / 4; canvas.drawLine( Offset(r.left + r.width * 0.10, y), Offset(r.right - r.width * 0.10, y), slot, ); } final Paint led = Paint()..color = c.withValues(alpha: 0.85); for (int i = 0; i < 4; i++) { final double y = r.top + r.height * (i + 0.5) / 4; canvas.drawCircle( Offset(r.left + r.width * 0.16, y), math.min(2.4, r.width * 0.035), led, ); } } /// 客户端 / PC:显示器 + 支架 + 底座。 void _drawClient(Canvas canvas, ArchShape s, Paint fill, Paint line) { final Rect r = s.rect; final Rect screen = Rect.fromLTWH(r.left, r.top, r.width, r.height * 0.72); final Path p = Path() ..addRRect(RRect.fromRectAndRadius( screen, Radius.circular(math.min(8, screen.height * 0.12)))); if (s.fill != 0) canvas.drawPath(p, fill); canvas.drawPath(p, line); canvas.drawLine( Offset(r.center.dx, screen.bottom), Offset(r.center.dx, r.bottom - r.height * 0.10), line, ); canvas.drawLine( Offset(r.center.dx - r.width * 0.18, r.bottom - r.height * 0.08), Offset(r.center.dx + r.width * 0.18, r.bottom - r.height * 0.08), line, ); } /// 移动端:手机外形(圆角矩形 + 听筒线 + 底部横线)。 void _drawMobile(Canvas canvas, ArchShape s, Paint fill, Paint line) { final Rect r = s.rect; final Path p = archShapePath(s); if (s.fill != 0) canvas.drawPath(p, fill); canvas.drawPath(p, line); final Color c = Color(s.stroke); final Paint thin = Paint() ..style = PaintingStyle.stroke ..strokeWidth = math.max(1, s.sw * 0.7) ..color = c.withValues(alpha: 0.85); canvas.drawLine( Offset(r.center.dx - r.width * 0.14, r.top + r.height * 0.055), Offset(r.center.dx + r.width * 0.14, r.top + r.height * 0.055), thin, ); canvas.drawLine( Offset(r.center.dx - r.width * 0.12, r.bottom - r.height * 0.05), Offset(r.center.dx + r.width * 0.12, r.bottom - r.height * 0.05), thin, ); } /// 防火墙:砖墙(错缝)+ 外框。 void _drawFirewall(Canvas canvas, ArchShape s, Paint fill, Paint line) { final Rect r = s.rect; final Path p = archShapePath(s); if (s.fill != 0) canvas.drawPath(p, fill); canvas.drawPath(p, line); final Color c = Color(s.stroke); final Paint brick = Paint() ..style = PaintingStyle.stroke ..strokeWidth = math.max(1, s.sw * 0.7) ..color = c.withValues(alpha: 0.85); const int rows = 3; for (int i = 1; i < rows; i++) { final double y = r.top + r.height * i / rows; canvas.drawLine(Offset(r.left, y), Offset(r.right, y), brick); } for (int i = 0; i < rows; i++) { final double y0 = r.top + r.height * i / rows; final double y1 = r.top + r.height * (i + 1) / rows; final double offset = i.isEven ? 0.5 : 0.25; for (double fx = offset; fx < 0.95; fx += 0.5) { final double x = r.left + r.width * fx; canvas.drawLine(Offset(x, y0), Offset(x, y1), brick); } } } /// 交换机:扁盒 + 端口方块 + 状态灯。 void _drawNetSwitch(Canvas canvas, ArchShape s, Paint fill, Paint line) { final Rect r = s.rect; final Path p = archShapePath(s); if (s.fill != 0) canvas.drawPath(p, fill); canvas.drawPath(p, line); final Color c = Color(s.stroke); final Paint port = Paint()..color = c.withValues(alpha: 0.85); final double size = math.min(r.height * 0.20, 8.0); for (int i = 0; i < 6; i++) { final double x = r.left + r.width * (0.10 + i * 0.135); canvas.drawRRect( RRect.fromRectAndRadius( Rect.fromLTWH(x, r.bottom - r.height * 0.42, size * 1.4, size), const Radius.circular(1.5), ), port, ); } } /// 消息队列 / 数据流:管道 + 三个箭头。 void _drawQueue(Canvas canvas, ArchShape s, Paint fill, Paint line) { final Rect r = s.rect; final Path p = archShapePath(s); if (s.fill != 0) canvas.drawPath(p, fill); canvas.drawPath(p, line); final Color c = Color(s.stroke); final Paint arrow = Paint() ..style = PaintingStyle.stroke ..strokeWidth = math.max(1.2, s.sw * 0.9) ..strokeCap = StrokeCap.round ..strokeJoin = StrokeJoin.round ..color = c.withValues(alpha: 0.9); final double h = r.height * 0.30; for (int i = 0; i < 3; i++) { final double x = r.left + r.width * (0.30 + i * 0.18); canvas.drawPath( Path() ..moveTo(x - h * 0.35, r.center.dy - h * 0.5) ..lineTo(x + h * 0.35, r.center.dy) ..lineTo(x - h * 0.35, r.center.dy + h * 0.5), arrow, ); } } /// 图元库符号:按 100×100 归一化基元绘制到图形矩形; /// 图元缺失(文件来自其它设备且未导入)时画占位框 + 虚线提示。 void _drawSymbol(Canvas canvas, ArchShape s, Paint fill, Paint line) { final LibSymbol? sym = SymbolCache.byId(s.symbolId); if (sym == null || s.symbolId.isEmpty) { final RRect rr = RRect.fromRectAndRadius( s.rect, const Radius.circular(6), ); canvas.drawRRect(rr, line); _dash( canvas, Path()..addRRect(rr.deflate(4)), Paint() ..style = PaintingStyle.stroke ..strokeWidth = 1 ..color = Color(s.stroke).withValues(alpha: 0.5), ); return; } final Color c = Color(s.stroke); for (final SymPrim p in sym.prims) { if (p.kind == SymPrimKind.text) { if (p.pts.length < 2 || p.label.isEmpty) continue; final Offset tl = symPt(p.pts[0], p.pts[1], s.rect); final double size = (s.rect.height * 0.14).clamp(7.0, 18.0).toDouble(); final TextPainter tp = TextPainter( text: TextSpan( text: p.label, style: TextStyle( color: c, fontSize: size, fontWeight: FontWeight.w600, fontFamily: 'Microsoft YaHei UI', fontFamilyFallback: const [ 'Microsoft YaHei', 'Segoe UI', ], ), ), textDirection: TextDirection.ltr, )..layout(); tp.paint(canvas, tl); continue; } final Path? path = symPrimPath(p, s.rect); if (path == null) continue; if (s.fill != 0 && (p.kind == SymPrimKind.rect || p.kind == SymPrimKind.circle)) { canvas.drawPath(path, fill); } canvas.drawPath(path, line); } } /// 绘制一个标准图形(含图内文字)。 void _drawShape(Canvas canvas, ArchShape s) { final Paint fill = Paint() ..style = PaintingStyle.fill ..color = Color(s.fill); final Paint line = Paint() ..style = PaintingStyle.stroke ..strokeWidth = s.sw ..strokeJoin = StrokeJoin.round ..strokeCap = StrokeCap.round ..color = Color(s.stroke); switch (s.type) { case ArchShapeType.rect: case ArchShapeType.roundRect: case ArchShapeType.ellipse: case ArchShapeType.circle: case ArchShapeType.triangle: case ArchShapeType.diamond: case ArchShapeType.hexagon: case ArchShapeType.cloud: final Path p = archShapePath(s); if (s.fill != 0) canvas.drawPath(p, fill); canvas.drawPath(p, line); break; case ArchShapeType.cylinder: final (Path body, Path lid) = archCylinderPaths(s.rect); if (s.fill != 0) { canvas.drawPath(body, fill); // 顶盖用稍亮的填充,呈现「桶口」层次。 canvas.drawPath( lid, Paint() ..style = PaintingStyle.fill ..color = Color(s.fill).withValues( alpha: (Color(s.fill).a + 0.10).clamp(0.0, 1.0), ), ); } canvas.drawPath(body, line); canvas.drawPath(lid, line); break; case ArchShapeType.text: break; case ArchShapeType.line: final Offset a = s.a ?? s.rect.topLeft; final Offset b = s.b ?? s.rect.bottomRight; canvas.drawLine(a, b, line); if (s.arrow) _arrowHead(canvas, a, b, Color(s.stroke)); break; case ArchShapeType.polyline: case ArchShapeType.curve: if (s.pts.length >= 2) { final Path path = s.type == ArchShapeType.curve ? archSplinePath(s.pts) : _polyPath(s.pts, close: false); canvas.drawPath(path, line); } break; case ArchShapeType.server: _drawServer(canvas, s, fill, line); break; case ArchShapeType.client: _drawClient(canvas, s, fill, line); break; case ArchShapeType.mobile: _drawMobile(canvas, s, fill, line); break; case ArchShapeType.firewall: _drawFirewall(canvas, s, fill, line); break; case ArchShapeType.netSwitch: _drawNetSwitch(canvas, s, fill, line); break; case ArchShapeType.queue: _drawQueue(canvas, s, fill, line); break; case ArchShapeType.symbol: _drawSymbol(canvas, s, fill, line); break; } if (s.text.isNotEmpty) { final Color tc = s.type == ArchShapeType.text ? Color(s.stroke) : t.text; Offset center = s.rect.center; if (s.type == ArchShapeType.line) { center = center - const Offset(0, 14); } else if (s.type == ArchShapeType.cloud) { center = center.translate(0, s.rect.height * 0.12); } else if (s.type == ArchShapeType.cylinder) { center = center.translate( 0, math.min(s.rect.height * 0.20, s.rect.width * 0.32) * 0.8, ); } else if (s.type == ArchShapeType.client) { center = Offset(s.rect.center.dx, s.rect.top + s.rect.height * 0.32); } else if (s.type == ArchShapeType.netSwitch) { center = Offset(s.rect.center.dx, s.rect.top + s.rect.height * 0.30); } _label( canvas, s.text, center, tc, maxWidth: math.max(24, s.rect.width - 12), size: s.type == ArchShapeType.text ? (s.rect.height * 0.72).clamp(10.0, 120.0) : 12.5, ); } // 尺寸标注(识别后图形 / 曲线 / 图元可直接改值缩放;标签屏幕字号恒定)。 if (s.dims.isNotEmpty) { final double z = doc.zoom < 0.05 ? 1 : doc.zoom; final List kinds = [ for (final ArchDimKind k in archShapeDimKinds(s)) if (s.dims.contains(k)) k, ]; for (int i = 0; i < kinds.length; i++) { final ArchDimKind k = kinds[i]; _dimBadge(canvas, s, k, archShapeDimLabelPos(s, k, i, z), z); } } } /// 尺寸标签(小圆角底片 + 数值;屏幕尺寸恒定,双击可改值)。 void _dimBadge( Canvas canvas, ArchShape s, ArchDimKind k, Offset center, double z, ) { final double inv = 1 / z; final double v = archShapeDimValue(s, k); final String txt = switch (k) { ArchDimKind.width => 'W ${_fmtDim(v)}', ArchDimKind.height => 'H ${_fmtDim(v)}', ArchDimKind.length => 'L ${_fmtDim(v)}', ArchDimKind.diameter => '⌀ ${_fmtDim(v)}', }; final TextPainter tp = TextPainter( text: TextSpan( text: txt, style: TextStyle( color: const Color(0xFF0F766E), fontSize: 10 * inv, fontWeight: FontWeight.w600, fontFamily: 'Microsoft YaHei UI', fontFamilyFallback: const ['Microsoft YaHei', 'Segoe UI'], ), ), textDirection: TextDirection.ltr, )..layout(); final Rect r = Rect.fromCenter( center: center, width: tp.width + 8 * inv, height: tp.height + 3 * inv, ); final RRect rr = RRect.fromRectAndRadius(r, Radius.circular(4 * inv)); canvas.drawRRect(rr, Paint()..color = t.panel.withValues(alpha: 0.94)); canvas.drawRRect( rr, Paint() ..style = PaintingStyle.stroke ..strokeWidth = 1 * inv ..color = const Color(0xFF0F766E).withValues(alpha: 0.5), ); tp.paint(canvas, r.center - Offset(tp.width / 2, tp.height / 2)); } static String _fmtDim(double v) => (v - v.roundToDouble()).abs() < 0.05 ? v.round().toString() : v.toStringAsFixed(1); /// 绘制连线(吸附端实时求边框交点;直线 / 正交折线;箭头 + 标签)。 void _connector(Canvas canvas, ArchConnector c) { final ArchConnGeometry g = archConnectorGeometry(doc, c, override: _preview); final Color color = Color(c.color); final Paint p = Paint() ..style = PaintingStyle.stroke ..strokeWidth = 1.6 ..strokeCap = StrokeCap.round ..color = color; if (c.dash) { _dash(canvas, g.path, p); } else { canvas.drawPath(g.path, p); } _arrowHead(canvas, g.pts[g.pts.length - 2], g.end, color); if (c.label.isNotEmpty) { _connLabel(canvas, g.label, c.label); } } /// 连线标签(底片 + 文本)。 void _connLabel(Canvas canvas, Offset mid, String text) { final TextPainter tp = TextPainter( text: TextSpan( text: text, style: TextStyle( color: t.text2, fontSize: 10.5, fontFamily: 'Microsoft YaHei UI', fontFamilyFallback: const [ 'Microsoft YaHei', 'Segoe UI', 'sans-serif', ], ), ), textDirection: TextDirection.ltr, maxLines: 1, ellipsis: '…', )..layout(maxWidth: 180); final Rect bg = Rect.fromCenter( center: mid, width: tp.width + 10, height: tp.height + 6, ); canvas.drawRRect( RRect.fromRectAndRadius(bg, const Radius.circular(5)), Paint()..color = t.panel, ); canvas.drawRRect( RRect.fromRectAndRadius(bg, const Radius.circular(5)), Paint() ..style = PaintingStyle.stroke ..strokeWidth = 1 ..color = t.line2, ); tp.paint(canvas, mid - Offset(tp.width / 2, tp.height / 2)); } /// 图形 / 文本标签(居中,多行按 `\n` 拆分)。 void _label( Canvas canvas, String text, Offset center, Color color, { required double maxWidth, double size = 12.5, }) { final List lines = text.split('\n'); final List tps = []; double totalH = 0; for (final String ln in lines) { final TextPainter tp = TextPainter( text: TextSpan( text: ln, style: TextStyle( color: color, fontSize: size, fontWeight: FontWeight.w600, height: 1.25, fontFamily: 'Microsoft YaHei UI', fontFamilyFallback: const [ 'Microsoft YaHei', 'Segoe UI', 'sans-serif', ], ), ), textDirection: TextDirection.ltr, maxLines: 1, ellipsis: '…', )..layout(maxWidth: maxWidth); tps.add(tp); totalH += tp.height; } double y = center.dy - totalH / 2; for (final TextPainter tp in tps) { tp.paint(canvas, Offset(center.dx - tp.width / 2, y)); y += tp.height; } } /// 箭头头部。 void _arrowHead(Canvas canvas, Offset from, Offset to, Color color) { final Offset d = to - from; final double len = d.distance; if (len < 1) return; final Offset u = d / len; final Offset n = Offset(-u.dy, u.dx); const double hl = 11; const double hw = 5.5; final Path path = Path() ..moveTo(to.dx, to.dy) ..lineTo(to.dx - u.dx * hl + n.dx * hw, to.dy - u.dy * hl + n.dy * hw) ..lineTo(to.dx - u.dx * hl - n.dx * hw, to.dy - u.dy * hl - n.dy * hw) ..close(); canvas.drawPath(path, Paint()..color = color); } /// 虚线绘制。 void _dash(Canvas canvas, Path path, Paint paint) { for (final ui.PathMetric m in path.computeMetrics()) { double d = 0; while (d < m.length) { final double end = math.min(d + 8, m.length); canvas.drawPath(m.extractPath(d, end), paint); d = end + 6; } } } /// 选中框与手柄(支持多选)。 void _selection(Canvas canvas) { if (selectedIds.isEmpty) return; final double z = doc.zoom < 0.05 ? 1 : doc.zoom; final Paint sp = Paint() ..style = PaintingStyle.stroke ..strokeWidth = 1.3 / z ..color = t.accent; Rect? combined; for (final String id in selectedIds) { final ArchShape? s = doc.shapeById(id); if (s != null) { if (!doc.isLayerVisible(s.layerId)) continue; final ArchShape ps = _preview(s); final Rect box = ps.isLinear ? ps.bounds : ps.rect.inflate(4); combined = combined == null ? box : combined.expandToInclude(box); if (ps.isLinear) { final List vs = [ if (ps.type == ArchShapeType.line && ps.a != null && ps.b != null) ...[ps.a!, ps.b!] else ...ps.pts, ]; for (final Offset p in vs) { _handleDot(canvas, p, z); } } else { _dash( canvas, Path() ..addRRect(RRect.fromRectAndRadius(box, const Radius.circular(4))), sp, ); for (final Rect h in archHandleRects(ps.rect, z)) { _handleRect(canvas, h, z); } } continue; } final ArchConnector? c = doc.connectorById(id); if (c != null) { if (!doc.isLayerVisible(c.layerId)) continue; final ArchConnGeometry g = archConnectorGeometry(doc, c, override: _preview); for (final Offset p in g.pts) { _handleDot(canvas, p, z); } combined = combined == null ? Rect.fromPoints(g.start, g.end).inflate(6) : combined.expandToInclude(Rect.fromPoints(g.start, g.end)); continue; } final ArchStroke? k = doc.strokeById(id); if (k != null) { if (!doc.isLayerVisible(k.layerId)) continue; final Rect box = k.bounds.inflate(5); combined = combined == null ? box : combined.expandToInclude(box); _dash( canvas, Path() ..addRRect(RRect.fromRectAndRadius(box, const Radius.circular(4))), sp, ); } } // 多选时加一圈总框。 if (selectedIds.length > 1 && combined != null) { _dash( canvas, Path() ..addRRect(RRect.fromRectAndRadius(combined.inflate(8), const Radius.circular(6))), Paint() ..style = PaintingStyle.stroke ..strokeWidth = 1.2 / z ..color = t.accent.withValues(alpha: 0.6), ); } } /// 方形手柄。 void _handleRect(Canvas canvas, Rect r, double z) { canvas.drawRect( r, Paint() ..style = PaintingStyle.fill ..color = t.panel, ); canvas.drawRect( r, Paint() ..style = PaintingStyle.stroke ..strokeWidth = 1.3 / z ..color = t.accent, ); } /// 圆形手柄(线类端点 / 折点 / 连线折点)。 void _handleDot(Canvas canvas, Offset c, double z) { final double r = 4 / z; canvas.drawCircle(c, r, Paint()..color = t.panel); canvas.drawCircle( c, r, Paint() ..style = PaintingStyle.stroke ..strokeWidth = 1.3 / z ..color = t.accent, ); } /// 任何输入变化都重绘(画布尺寸有限,开销可接受)。 @override bool shouldRepaint(covariant ArchPainter oldDelegate) => true; } /// 构造多边形路径。 Path _polyPath(List pts, {required bool close}) { final Path p = Path(); if (pts.isEmpty) return p; p.moveTo(pts.first.dx, pts.first.dy); for (int i = 1; i < pts.length; i++) { p.lineTo(pts[i].dx, pts[i].dy); } if (close) p.close(); return p; }