diff --git a/README.md b/README.md index ceb5f5c..82ce970 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ - **深色 / 浅色双主题**:右上角 ☀ / ☾ 一键切换,语义颜色不变,导出 SVG 与屏幕一致 - **导出 SVG**:矢量、分层、带语义 `id` / `data-*` 元数据,可直接进工艺卡与 MES -## 正式版 V1.0.0 +## 正式版 V1.0.1 **可运行的 Flutter Windows 桌面正式版**(源码在 `lib/`),设计人:呆头鹅。 @@ -95,6 +95,6 @@ Flutter 3.29+(Desktop) · CustomPainter 自绘 · Riverpod · 不可变模 - **软件**:时序图设计器 · Sequence Diagram Studio - **设计人**:呆头鹅 -- **版本**:V1.0.0 +- **版本**:V1.0.1 - **版权**:Copyright © 2026 呆头鹅。保留所有权利。 - **架构文档**:[`docs/ARCHITECTURE.md`](docs/ARCHITECTURE.md) · **使用帮助**:[`docs/HELP.md`](docs/HELP.md) diff --git a/docs/HELP.md b/docs/HELP.md index 2b87ae4..051d6b4 100644 --- a/docs/HELP.md +++ b/docs/HELP.md @@ -78,6 +78,19 @@ --- +## C2. 泳道 / 轨道:一个执行器可放多个动作 + +**泳道(条形)与轨道(圆周)对应同一个物理执行器**,一条泳道 / 轨道可以容纳**多个互不重叠**的动作(类似音频轨道): + +- 拖拽动作**上下移动**(条形视图)或 **Shift + 径向拖拽**(圆周视图)即可换道; +- 只要该时间段目标泳道里没有别的动作,就能放上去;若时间重叠会被拒绝并提示; +- 泳道标题会把该道内的多个动作**合并显示**(如 `A1 上料 / A2 定位夹紧`),最多两行,用 `/` 分隔; +- 左侧列表也按泳道分组,单动作泳道显示为普通行,多动作泳道显示合并标题。 + +> 含义:一条泳道 = 一个气缸 / 伺服轴等执行器;一个色块 = 该执行器上的一个动作。 + +--- + ## D. 依赖关系:先后顺序与并行(多输入 / 多输出) **依赖类型** @@ -240,7 +253,7 @@ ms = angle / 360 × cycleMs - **软件**:时序图设计器 · Sequence Diagram Studio - **设计人**:呆头鹅 -- **版本**:V1.0.0 +- **版本**:V1.0.1 - **版权**:Copyright © 2026 呆头鹅。保留所有权利。 --- diff --git a/lib/layout/layout.dart b/lib/layout/layout.dart index 86ca928..95c00ea 100644 --- a/lib/layout/layout.dart +++ b/lib/layout/layout.dart @@ -58,35 +58,43 @@ List> splitArcs(double a0, double a1) { /// 计算动作在条形视图中的行顺序。 /// -/// 规则: -/// 1. 先按「工位 order → 起始角 → id」得到**自动顺序**; -/// 2. 若存在显式行序(`Action.row >= 0`),则显式行序优先,其余按自动顺序追加。 -/// -/// 这样一旦用户上下拖拽过行,行位置就固定下来,**不会再随相位变化而跳动**, -/// 依赖箭头也就不会在水平拖动时乱跳。 +/// **泳道模型**:`Action.layer` 同时作为「泳道 / 圆周轨道」编号, +/// 对应一个物理执行器;同一泳道可容纳**多个互不重叠**的动作。 +/// 排序规则:先按泳道号,再按起始角,最后按 id。 List orderActions(Project project) { final List list = List.of(project.actions); list.sort((Action a, Action b) { - final int sa = project.stationById(a.stationId)?.order ?? 0; - final int sb = project.stationById(b.stationId)?.order ?? 0; - if (sa != sb) return sa.compareTo(sb); + if (a.layer != b.layer) return a.layer.compareTo(b.layer); final int c = a.startAngle.compareTo(b.startAngle); if (c != 0) return c; return a.id.compareTo(b.id); }); - final bool hasExplicit = list.any((Action a) => a.row >= 0); - if (hasExplicit) { - final List explicit = list.where((Action a) => a.row >= 0).toList() - ..sort((Action a, Action b) => a.row.compareTo(b.row)); - final List auto = list.where((Action a) => a.row < 0).toList(); - list - ..clear() - ..addAll(explicit) - ..addAll(auto); - } return list; } +/// 判断两条角度区间是否重叠(用于同一泳道内“动作不可重叠”的约束)。 +/// +/// 参数均为角度区间 `[start, end]`;跨圈(end > 360)按模 360 归一后判断。 +bool angleRangesOverlap(double s1, double e1, double s2, double e2) { + // 归一到 [0,360),并把跨圈区间拆成两段后逐一比较。 + List> seg(double s, double e) { + double a = s % 360; + if (a < 0) a += 360; + double b = e % 360; + if (b < 0) b += 360; + if (e - s >= 360) return >[[0, 360]]; + if (b >= a) return >[[a, b]]; + return >[[a, 360], [0, b]]; + } + + for (final List x in seg(s1, e1)) { + for (final List y in seg(s2, e2)) { + if (x[0] < y[1] - 0.001 && y[0] < x[1] - 0.001) return true; + } + } + return false; +} + /// 条形(甘特)视图布局。 /// /// 横轴为相位角(0°–360°),纵轴为动作行。行顺序由 [ordered] 决定, @@ -97,8 +105,8 @@ class BarLayout { /// 参数 [project]:项目数据;[size]:可用画布尺寸。 /// 副作用:初始化 [pitch]。 BarLayout(this.project, this.size) { - // 可用高度 = 总高 - 顶部留白 - 底部预留;按动作数均分并夹取到 [46, 66]。 - final int n = project.actions.length; + // 可用高度 = 总高 - 顶部留白 - 底部预留;按**泳道数**均分并夹取到 [46, 66]。 + final int n = lanes.length; final double usable = size.height - topPad - bottomReserve; pitch = n == 0 ? 66 : (usable / n).clamp(46, 66); } @@ -139,18 +147,41 @@ class BarLayout { /// 决定行先后的动作列表;见顶层函数 [orderActions]。 List get ordered => orderActions(project); + /// 泳道编号(= 物理执行器轨道):所有动作 `layer` 的去重升序列表。 + List get lanes { + final Set s = {for (final Action a in project.actions) a.layer}; + final List l = s.toList()..sort(); + return l; + } + + /// 泳道号在 [lanes] 中的序号。 + int laneIndexOf(int lane) => lanes.indexOf(lane); + + /// 泳道内包含的动作(按起始角排序)。 + List actionsInLane(int lane) => + ordered.where((Action a) => a.layer == lane).toList(); + + /// 泳道号转纵坐标(该泳道顶部 y)。 + double yForLane(int lane) => topPad + laneIndexOf(lane) * pitch; + + /// 行索引转纵坐标(按泳道序号)。 + double yFor(int index) => topPad + index * pitch; + + /// 由纵坐标反查最近的泳道号(用于拖拽换道)。 + int laneAt(double y) { + if (lanes.isEmpty) return 0; + final int i = ((y - topPad) / pitch).round().clamp(0, lanes.length - 1); + return lanes[i]; + } + /// 相位角(度)转横坐标;角度先钳制到 `[0, 360]`。 double xFor(double deg) => plotLeft + (deg.clamp(0, 360) / 360) * span; - /// 行索引转纵坐标(该行顶部 y)。 - double yFor(int index) => topPad + index * pitch; - - /// 计算动作 [a] 的条形矩形。 + /// 计算动作 [a] 的条形矩形(按所属泳道的 y)。 /// /// 宽度至少为 3px,避免极短动作不可见;跨圈部分会被截断到 360°。 Rect rectFor(Action a) { - final int i = ordered.indexOf(a); - final double y = yFor(i); + final double y = yForLane(a.layer); final double x0 = xFor(a.startAngle); final double x1 = xFor(math.min(a.endAngle, 360)); return Rect.fromLTWH(x0, y, math.max(x1 - x0, 3), barH); @@ -183,10 +214,11 @@ class CircularLayout { CircularLayout(this.project, this.size) { // 圆心略微下移 18px 以平衡顶部刻度标签的视觉重心。 center = Offset(size.width / 2, size.height / 2 + 18); - final int layers = project.maxLayer + 1; + // 轨道数 = 泳道数(layer 去重),同一轨道可放多个互不重叠的动作。 + final int layers = lanes.length; // 最大半径受较短边约束,减去 74px 留给刻度与边距。 final double maxR = math.min(size.width, size.height) / 2 - 74; - // 在 r0 与 maxR 之间均分各层,环间距夹取到 [24, 46]。 + // 在 r0 与 maxR 之间均分各轨道,环间距夹取到 [24, 46]。 pitch = layers <= 1 ? 40 : ((maxR - r0) / (layers - 1)).clamp(24, 46); // 动作环带宽度:相对行距收窄(约 0.56),并夹取到 [12, 26], // 让环与环之间留出呼吸空间、文字仍能容纳。 @@ -211,11 +243,31 @@ class CircularLayout { /// 环的视觉宽度(像素)。 late final double ringWidth; - /// 第 [layer] 层对应的环半径。 - double radiusFor(int layer) => r0 + layer * pitch; + /// 轨道编号(= 泳道):所有动作 `layer` 的去重升序列表。 + List get lanes { + final Set s = {for (final Action a in project.actions) a.layer}; + final List l = s.toList()..sort(); + return l; + } + + /// 轨道号在 [lanes] 中的序号。 + int laneIndexOf(int lane) => lanes.indexOf(lane); + + /// 由半径反查最近的轨道号(用于拖拽换轨)。 + int laneAtRadius(double r) { + if (lanes.isEmpty) return 0; + final int i = ((r - r0) / pitch).round().clamp(0, lanes.length - 1); + return lanes[i]; + } + + /// 第 [lane] 条轨道对应的环半径(未知轨道回退到最内圈)。 + double radiusFor(int lane) { + final int i = laneIndexOf(lane); + return r0 + (i < 0 ? 0 : i) * pitch; + } /// 最外层半径。 - double get rMax => radiusFor(project.maxLayer); + double get rMax => lanes.isEmpty ? r0 : radiusFor(lanes.last); /// 刻度标签所在的半径(最外层再向外偏移)。 /// 刻度环半径:在外层动作环之外留出足够间距, diff --git a/lib/model/models.dart b/lib/model/models.dart index 174305f..e264e2a 100644 --- a/lib/model/models.dart +++ b/lib/model/models.dart @@ -820,6 +820,11 @@ class Project { return m; } + /// 泳道 / 轨道数量(= `layer` 去重后的个数)。 + /// + /// 同一泳道可容纳多个互不重叠的动作,对应一个物理执行器。 + int get laneCount => {for (final Action a in actions) a.layer}.length; + /// 基于当前实例创建副本,未提供的参数保持原值。 Project copyWith({ String? name, diff --git a/lib/render/bar_painter.dart b/lib/render/bar_painter.dart index 479c789..2d07ea5 100644 --- a/lib/render/bar_painter.dart +++ b/lib/render/bar_painter.dart @@ -234,12 +234,13 @@ class BarPainter extends CustomPainter { /// /// 参数 [layout] 提供行列几何,[u]/[cycle] 用于时长文本换算。 void _rows(Canvas canvas, BarLayout layout, UnitSettings u, double cycle) { - final List ordered = layout.ordered; - for (int i = 0; i < ordered.length; i++) { - final Action a = ordered[i]; - final double y = layout.yFor(i); - // 隔行背景色带,弱化行边界。 - if (i.isEven) { + final List lanes = layout.lanes; + for (int li = 0; li < lanes.length; li++) { + final int lane = lanes[li]; + final double y = layout.yFor(li); + final List acts = layout.actionsInLane(lane); + // 隔泳道背景色带,弱化边界。 + if (li.isEven) { canvas.drawRRect( RRect.fromRectAndRadius( Rect.fromLTWH(20, y - 8, layout.plotRight - 20, BarLayout.rowVisual), @@ -248,24 +249,9 @@ class BarPainter extends CustomPainter { Paint()..color = tokens.panel2.withValues(alpha: 0.5), ); } - // 左侧序号徽标:动作自身颜色作底,叠加白色序号。 - final Station? st = project.stationById(a.stationId); - final Color c = Color(a.color); - canvas.drawRRect( - RRect.fromRectAndRadius(Rect.fromLTWH(32, y + 8, 18, 18), const Radius.circular(5)), - Paint()..color = c, - ); - _text(canvas, '${i + 1}', Offset(41, y + 17), - color: tokens.chip, size: 10, align: TextAlign.center, weight: FontWeight.w700); - // 动作标识与工位/驱动信息(两行文本)。 - // 标题列文字按列宽截断,避免越界压到图表。 - final double labelMax = BarLayout.leftGutter - 68; - _textMax(canvas, '${i + 1}. ${a.id} ${a.name}', Offset(60, y + 6), labelMax, - color: tokens.text, size: 12, weight: FontWeight.w600); - _textMax(canvas, '${st?.name ?? a.stationId} · ${a.driver}', Offset(60, y + 21), labelMax, - color: tokens.dim, size: 10); - - // 行底分隔线,颜色较淡。 + // 泳道标题(合并显示,最多两行,用 / 分隔)。 + _laneHeader(canvas, layout, li, acts, y); + // 泳道底分隔线。 canvas.drawLine( Offset(layout.plotLeft, y + BarLayout.rowVisual - 4), Offset(layout.plotRight, y + BarLayout.rowVisual - 4), @@ -273,69 +259,104 @@ class BarPainter extends CustomPainter { ..color = tokens.line.withValues(alpha: 0.5) ..strokeWidth = 1, ); + // 该泳道内的所有动作条(互不重叠)。 + for (final Action a in acts) { + _bar(canvas, layout, a, u, cycle); + } + } + } - // 动作条块:先投影,再填充,最后描边。 - 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), - ); - // 选中描边:选中时更亮,未选中时仅保留很淡的轮廓。 + /// 绘制一条泳道的标题:序号徽标 + 合并动作名(≤2 行,用 / 分隔)+ 副标题。 + void _laneHeader(Canvas canvas, BarLayout layout, int li, List 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 = 1 - ..color = tokens.selOutline.withValues(alpha: sel ? 0.9 : 0.25), + ..strokeWidth = 2 + ..color = tokens.crit, ); - // 关键路径条块额外叠加一圈加粗高亮描边。 - if (crit) { - canvas.drawRRect( - RRect.fromRectAndRadius(r, const Radius.circular(8)), - Paint() - ..style = PaintingStyle.stroke - ..strokeWidth = 2 - ..color = tokens.crit, - ); - } - - // 跨循环延续:把 360° 之后的部分回绕到左侧 0° 起绘制, - // 使用 [Tokens.tailOp] 透明度淡化以区别于主体。 - 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; - if (r.width > 150) { - _text(canvas, a.name, Offset(r.left + 10, r.center.dy - 5), - color: tokens.arcText, size: 11, weight: FontWeight.w700); - _text(canvas, tfmt(durMs, u), Offset(r.left + 10, r.center.dy + 7), - color: tokens.arcText.withValues(alpha: 0.8), size: 10); - } else if (r.right + 170 > layout.plotRight) { - _text(canvas, '${a.name} ${tfmt(durMs, u)}', Offset(r.left - 10, r.center.dy), - color: c, size: 11, weight: FontWeight.w600, align: TextAlign.right); - } else { - _text(canvas, '${a.name} ${tfmt(durMs, u)}', 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); } + 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; + if (r.width > 150) { + _text(canvas, a.name, Offset(r.left + 10, r.center.dy - 5), + color: tokens.arcText, size: 11, weight: FontWeight.w700); + _text(canvas, tfmt(durMs, u), Offset(r.left + 10, r.center.dy + 7), + color: tokens.arcText.withValues(alpha: 0.8), size: 10); + } else if (r.right + 170 > layout.plotRight) { + _text(canvas, '${a.name} ${tfmt(durMs, u)}', Offset(r.left - 10, r.center.dy), + color: c, size: 11, weight: FontWeight.w600, align: TextAlign.right); + } else { + _text(canvas, '${a.name} ${tfmt(durMs, u)}', 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); } /// 绘制动作依赖连线(正交折线)。 @@ -352,10 +373,10 @@ class BarPainter extends CustomPainter { /// 参数 [layout] 提供条块矩形;[u] 当前未直接使用,保留以对齐调用接口。 void _deps(Canvas canvas, BarLayout layout, UnitSettings u) { final List ordered = layout.ordered; - final Map index = {}; - for (int i = 0; i < ordered.length; i++) { - index[ordered[i].id] = i; - } + // 以「泳道序号」判断依赖的相邻关系(同泳道多个动作不再误判)。 + final Map index = { + 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) { @@ -390,10 +411,14 @@ class BarPainter extends CustomPainter { // 跨多行:扫描区间内所有条块边界,据此在外侧选一条竖直泳道。 double maxRight = 0; double minLeft = double.infinity; + final List lanes = layout.lanes; for (int r = math.min(ia, ib) + 1; r < math.max(ia, ib); r++) { - final Rect rr = layout.rectFor(ordered[r]); - maxRight = math.max(maxRight, rr.right); - minLeft = math.min(minLeft, rr.left); + 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) { diff --git a/lib/render/circular_painter.dart b/lib/render/circular_painter.dart index 18da435..d702907 100644 --- a/lib/render/circular_painter.dart +++ b/lib/render/circular_painter.dart @@ -39,6 +39,7 @@ class CircularPainter extends CustomPainter { required this.criticalActions, required this.criticalDeps, required this.selectedIds, + this.guideAngle, }); /// 待绘制的工程数据源。 @@ -52,6 +53,9 @@ class CircularPainter extends CustomPainter { /// 当前选中的动作 id 集合(支持多选)。 final Set selectedIds; + /// 拖拽动作弧端点时的参考角(度);非空时绘制一条径向虚线参考。 + final double? guideAngle; + /// 绘制整个圆形视图。 /// /// 参数 [canvas]/[size] 遵循 Flutter 绘制约定。 @@ -76,12 +80,13 @@ class CircularPainter extends CustomPainter { ..color = tokens.line2, ); - // 圆环背景:每层用一条粗描边构成环带,奇偶层交替着色, + // 圆环背景:每条轨道用一条粗描边构成环带,奇偶交替着色, // 并在环带内外沿各画一条细描边作为边界。 - final int layers = project.maxLayer + 1; + final List lanes = layout.lanes; + final int layers = lanes.length; if (project.viewState.showRingBg) { for (int i = 0; i < layers; i++) { - final double r = layout.radiusFor(i); + final double r = layout.radiusFor(lanes[i]); canvas.drawCircle( c, r, @@ -116,11 +121,11 @@ class CircularPainter extends CustomPainter { // 象限辅助线(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 double r = layout.radiusFor(lanes[i]); + final double free = _freeAngle(lanes[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); } @@ -140,6 +145,9 @@ class CircularPainter extends CustomPainter { _action(canvas, layout, a, u); } + // 拖拽端点时的径向虚线参考。 + if (guideAngle != null) _guide(canvas, layout); + // 起点标记、顺时针方向提示与外围刻度。 _startMarker(canvas, layout); _cwArrow(canvas, layout); @@ -409,7 +417,8 @@ class CircularPainter extends CustomPainter { final bool degenerate = (aStart - aEnd).abs() < 0.5; final Path p; final Offset tipDir; - final Offset chipPos; + final double chipR; + final double chipDeg; if (degenerate) { final double r0 = rTip - (rTip > rB ? 28 : -28); final Offset q0 = polar(c, r0, aStart); @@ -417,7 +426,9 @@ class CircularPainter extends CustomPainter { ..moveTo(q0.dx, q0.dy) ..lineTo(p3.dx, p3.dy); tipDir = p3 - q0; - chipPos = polar(c, (rA + rB) / 2, aStart + 12); + // 标签放在源环与目标环之间的径向空隙中,沿切向排布。 + chipR = (rA + rB) / 2; + chipDeg = aStart; } else { p = Path() ..moveTo(p0.dx, p0.dy) @@ -426,8 +437,8 @@ class CircularPainter extends CustomPainter { ..lineTo(p3.dx, p3.dy); tipDir = p3 - p2; final double span = (aStart - aEnd).abs(); - final double rChip = skip ? rC : (span < 30 ? rB + layout.th / 2 + 10 : rC); - chipPos = polar(c, rChip, (aEnd + aStart) / 2); + chipR = skip ? rC : (span < 30 ? rB + layout.th / 2 + 10 : rC); + chipDeg = (aEnd + aStart) / 2; } if (crit) { @@ -453,7 +464,37 @@ class CircularPainter extends CustomPainter { 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, chipPos, '${A.id}→${B.id} $label', col, crit); + _arcChip(canvas, c, chipR, chipDeg, '${A.id}→${B.id} $label', col, crit); + } + + /// 沿圆弧切向绘制依赖标签胶囊,使其贴合环间空隙、不压住动作弧。 + void _arcChip(Canvas canvas, Offset c, double r, double midDeg, String label, Color col, bool crit) { + final TextPainter tp = _tp(label, 10, col, FontWeight.w600); + final double w = tp.width + 16 + (crit ? 18 : 0); + const double h = 17; + 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); + final Rect rect = Rect.fromCenter(center: Offset.zero, width: w, height: h); + canvas.drawRRect(RRect.fromRectAndRadius(rect, const Radius.circular(6)), Paint()..color = tokens.chip); + canvas.drawRRect( + RRect.fromRectAndRadius(rect, const Radius.circular(6)), + Paint() + ..style = PaintingStyle.stroke + ..strokeWidth = 1 + ..color = col, + ); + double tx = rect.left + 8; + if (crit) { + canvas.drawCircle(Offset(rect.left + 8, 0), 6.5, Paint()..color = tokens.crit); + _text(canvas, '★', Offset(rect.left + 8, 0), color: tokens.onAccent, size: 10, align: TextAlign.center); + tx = rect.left + 18; + } + tp.paint(canvas, Offset(tx, -tp.height / 2)); + canvas.restore(); } /// 在 [tip] 处绘制指向 [dir] 方向的实心三角箭头。 @@ -474,29 +515,30 @@ class CircularPainter extends CustomPainter { 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; + /// 绘制拖拽端点时的径向虚线参考与角度标注。 + 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; + } } - tp.paint(canvas, Offset(tx, center.dy - tp.height / 2)); + _text(canvas, '${deg.toStringAsFixed(1)}°', polar(c, layout.tickR + 22, deg), + color: tokens.accent, size: 11, align: TextAlign.center, weight: FontWeight.w700); } /// 绘制 0° 起点标记:顶部朝下的三角与“0° · 12 点钟”文字。 diff --git a/lib/state/app_controller.dart b/lib/state/app_controller.dart index 0cb3c31..deeaf97 100644 --- a/lib/state/app_controller.dart +++ b/lib/state/app_controller.dart @@ -635,31 +635,36 @@ class AppController extends ChangeNotifier { updateAction(id, (Action x) => x.copyWith(layer: nl), status: '层 L${nl + 1}'); } - /// 将动作 [id] 移动到条形视图的第 [targetIndex] 行(上下拖拽排序)。 + /// 将动作 [id] 移动到第 [targetLane] 条泳道 / 轨道(上下拖拽换道)。 /// - /// 会把所有动作的显式行序 `row` 重写为紧凑的 `0..n-1`,从而**固定行位置**, - /// 之后水平拖动只改相位、不再改变行。 - void moveActionRow(String id, int targetIndex) { + /// 泳道可容纳多个动作;仅当目标泳道内存在**时间重叠**的动作时才拒绝, + /// 并给出状态提示(与音频轨道“不能重叠”的语义一致)。 + void moveActionLane(String id, int targetLane) { final Action? moving = _project.actionById(id); if (moving == null) return; - final List list = orderActions(_project); - final int from = list.indexWhere((Action a) => a.id == id); - if (from < 0) return; - final Action a = list.removeAt(from); - final int to = targetIndex.clamp(0, list.length); - if (to == from) { - // 行序未变,但如果此前没有显式行序,则补齐一次。 - final bool already = _project.actions.every((Action x) => x.row >= 0); - if (already) return; + final int lane = targetLane.clamp(0, 63); + if (lane == moving.layer) return; + final bool overlap = _project.actions.any((Action o) => + o.id != id && + o.layer == lane && + angleRangesOverlap(moving.startAngle, moving.endAngle, o.startAngle, o.endAngle)); + if (overlap) { + _status = '轨道 L${lane + 1} 在该时间段已有动作,无法放入'; + notifyListeners(); + return; } - list.insert(to, a); - final Map rowById = { - for (int i = 0; i < list.length; i++) list[i].id: i, - }; - final List actions = _project.actions - .map((Action x) => x.copyWith(row: rowById[x.id] ?? x.row)) - .toList(); - _mutate(_project.copyWith(actions: actions), status: '调整 $id 行序'); + updateAction(id, (Action x) => x.copyWith(layer: lane, row: lane), + status: '移至轨道 L${lane + 1}'); + } + + /// 目标泳道 [lane] 是否能容纳动作 [id](不与其内动作时间重叠)。 + bool canPlaceInLane(String id, int lane) { + final Action? a = _project.actionById(id); + if (a == null) return false; + return !_project.actions.any((Action o) => + o.id != id && + o.layer == lane && + angleRangesOverlap(a.startAngle, a.endAngle, o.startAngle, o.endAngle)); } /// 移动某个相位分区的一条边界(用于在条形图顶部直接拖拽分区)。 diff --git a/lib/ui/app_shell.dart b/lib/ui/app_shell.dart index 0cadf6a..1e0b969 100644 --- a/lib/ui/app_shell.dart +++ b/lib/ui/app_shell.dart @@ -968,7 +968,7 @@ class _AppShellState extends State { Row( children: [ Flexible( - child: _sectionLabel(t, bar ? '动作 / 工位泳道' : '动作列表(按层 L1→L${c.project.maxLayer + 1})'), + child: _sectionLabel(t, bar ? '动作 / 执行器泳道' : '动作列表(按轨道 L1→L${c.project.laneCount})'), ), const Spacer(), _headerAction(t, Icons.add, '新建', t.ok, t.onAccent, () => c.addAction(startAngle: 0)), @@ -984,7 +984,8 @@ class _AppShellState extends State { ], ), const SizedBox(height: 8), - for (int i = 0; i < actions.length; i++) _actionRow(t, actions[i], i, bar), + // 按泳道 / 轨道分组:同一泳道内的多个动作合并显示(最多两行,/ 分隔)。 + ..._laneRows(t, bar), const SizedBox(height: 12), _sectionLabel(t, '视图模式'), const SizedBox(height: 8), @@ -1080,6 +1081,124 @@ class _AppShellState extends State { ); } + /// 左侧栏:按泳道 / 轨道分组的行列表。 + List _laneRows(Tokens t, bool bar) { + final List lanes = {for (final Action a in c.project.actions) a.layer}.toList()..sort(); + final List out = []; + for (int li = 0; li < lanes.length; li++) { + final int lane = lanes[li]; + final List acts = c.project.actions.where((Action a) => a.layer == lane).toList() + ..sort((Action a, Action b) => a.startAngle.compareTo(b.startAngle)); + // 单动作泳道沿用普通动作行;多动作泳道合并显示。 + out.add(acts.length == 1 + ? _actionRow(t, acts.first, li, bar) + : _laneRow(t, lane, li, acts, bar)); + } + return out; + } + + /// 单条泳道 / 轨道行:多个动作合并显示(≤2 行,/ 分隔)。 + Widget _laneRow(Tokens t, int lane, int li, List acts, bool bar) { + if (acts.isEmpty) return const SizedBox.shrink(); + final Action primary = + acts.firstWhere((Action a) => c.isSelected(a.id), orElse: () => acts.first); + final bool sel = c.isSelected(primary.id); + final Station? st = c.project.stationById(primary.stationId); + final String joined = acts.map((Action a) => '${a.id} ${a.name}').join(' / '); + double a0 = acts.first.startAngle; + double a1 = acts.first.endAngle; + for (final Action a in acts) { + a0 = math.min(a0, a.startAngle); + a1 = math.max(a1, a.endAngle); + } + return Padding( + padding: const EdgeInsets.only(bottom: 6), + child: InkWell( + onTap: () => c.select( + primary.id, + toggle: HardwareKeyboard.instance.isControlPressed || + HardwareKeyboard.instance.isShiftPressed, + ), + borderRadius: BorderRadius.circular(10), + child: Container( + padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 8), + decoration: BoxDecoration( + color: sel ? t.sel : t.panel2, + borderRadius: BorderRadius.circular(10), + border: Border.all(color: sel ? t.accent : t.line, width: sel ? 1.4 : 1), + ), + child: Row( + children: [ + Container( + width: 22, + height: 22, + decoration: BoxDecoration( + color: Color(primary.color).withValues(alpha: 0.9), + borderRadius: BorderRadius.circular(6), + ), + alignment: Alignment.center, + child: Text('${li + 1}', style: TextStyle(color: t.chip, fontSize: 11, fontWeight: FontWeight.w700)), + ), + const SizedBox(width: 9), + Expanded( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + joined, + maxLines: 2, + overflow: TextOverflow.ellipsis, + style: TextStyle( + color: sel ? t.text : t.text2, + fontSize: 12.5, + height: 1.25, + fontWeight: sel ? FontWeight.w600 : FontWeight.normal, + ), + ), + Text( + '${st?.name ?? primary.stationId} · ${primary.driver}' + '${acts.length > 1 ? ' · 共 ${acts.length} 个' : ''}', + maxLines: 1, + overflow: TextOverflow.ellipsis, + style: TextStyle(color: t.dim, fontSize: 10), + ), + ], + ), + ), + const SizedBox(width: 6), + Column( + crossAxisAlignment: CrossAxisAlignment.end, + children: [ + Text( + bar ? '${a0.toStringAsFixed(0)}° – ${math.min(a1, 360).toStringAsFixed(0)}°' : 'L${li + 1}', + style: TextStyle(color: t.sub, fontSize: 11), + ), + Text('${acts.length} 个动作', style: TextStyle(color: t.dim, fontSize: 10)), + ], + ), + const SizedBox(width: 6), + InkWell( + onTap: () => showDialog( + context: context, builder: (_) => ActionEditorDialog(controller: c, action: primary)), + borderRadius: BorderRadius.circular(7), + child: Container( + width: 28, + height: 24, + alignment: Alignment.center, + decoration: BoxDecoration( + borderRadius: BorderRadius.circular(7), + border: Border.all(color: t.line2), + ), + child: Icon(Icons.edit, size: 13, color: sel ? t.accent : t.dim), + ), + ), + ], + ), + ), + ), + ); + } + /// 左侧栏中的单条动作行:点击选中,右侧显示相位/层信息与编辑入口。 Widget _actionRow(Tokens t, Action a, int i, bool bar) { final bool sel = c.isSelected(a.id); @@ -1367,7 +1486,7 @@ class _AppShellState extends State { else ...[ Row( children: [ - Expanded(child: _wideBtn(t, Icons.add, '新增外层 L${c.project.maxLayer + 2}', t.accent2, Colors.white, + Expanded(child: _wideBtn(t, Icons.add, '新增轨道 L${c.project.laneCount + 1}', t.accent2, Colors.white, () => c.changeLayer(a.id, 1))), const SizedBox(width: 8), Expanded(child: _wideBtn(t, Icons.keyboard_arrow_up, '重排 / 合并层', t.panel2, t.sub, @@ -1882,8 +2001,8 @@ class _AppShellState extends State { '相位 360° = 100%', '动作 ${c.project.actions.length}', '依赖 ${_depCount()}', - circular ? '层数 L1–L${c.project.maxLayer + 1}' : '工位 ${c.project.stations.length}', - circular ? '同心圆 ${c.project.maxLayer + 1}' : '时间轴 0–${tfmt(c.project.cycle.durationMs, u)}', + circular ? '轨道 L1–L${c.project.laneCount}' : '工位 ${c.project.stations.length}', + circular ? '同心圆 ${c.project.laneCount}' : '时间轴 0–${tfmt(c.project.cycle.durationMs, u)}', '单位 ${timeUnitLabel(u.timeUnit)} / ${phaseUnitLabel(u.phaseUnit)}', '1° = ${tfmtAuto(c.project.cycle.durationMs / 360)}', c.status, diff --git a/lib/ui/canvas_view.dart b/lib/ui/canvas_view.dart index ff9e338..0458235 100644 --- a/lib/ui/canvas_view.dart +++ b/lib/ui/canvas_view.dart @@ -7,6 +7,8 @@ // · 点选、双击新建、拖拽移动相位、拖拽端点改持续角、Shift 拖拽改层; // · 空白区域拖拽平移、Ctrl + 滚轮缩放; // · 右键上下文菜单。 +import 'dart:math' as math; + import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart' hide Action; import 'package:flutter/services.dart'; @@ -132,6 +134,14 @@ class _CanvasViewState extends State { if (next != _cursor) setState(() => _cursor = next); } + /// 判断两个角度是否足够接近(用于圆周视图命中弧端点)。 + bool _angleNear(double a, double b, double tolDeg) { + double d = (a - b) % 360; + if (d > 180) d -= 360; + if (d < -180) d += 360; + return d.abs() <= tolDeg; + } + /// 动作端点弱吸附:贴合到整 5°、分区边界或其它动作端点(容差 2.5°)。 double _snapActionAngle(double raw, String excludeId) { const double tol = 2.5; @@ -302,7 +312,14 @@ class _CanvasViewState extends State { final Action? hit = layout.hitTest(p); if (hit != null) { final double a = angleOf(layout.center, p); - _mode = _DragMode.move; + final double rad = (p - layout.center).distance; + // 距端点约 9px 以内视为拖拽端点(改起止角),否则整体旋转。 + final double tolDeg = math.atan2(9, math.max(rad, 1)) * 180 / math.pi; + final bool nearStart = _angleNear(a, hit.startAngle, tolDeg); + final bool nearEnd = _angleNear(a, math.min(hit.endAngle, 360), tolDeg); + _mode = nearStart + ? _DragMode.resizeStart + : (nearEnd ? _DragMode.resize : _DragMode.move); _dragAction = hit; _startPointer = p; _startAngle = hit.startAngle; @@ -358,14 +375,9 @@ class _CanvasViewState extends State { (Action x) => x.copyWith(startAngle: ns, durationAngle: fixedEnd - ns), status: '调整起始角', ); - } else if (shift) { - // Shift + 垂直拖拽:按每 26px 一层调整层号,并限制在 0–63。 - final int deltaLayer = (dy / 26).round(); - c.setLayer(_dragAction!.id, (_dragAction!.layer - deltaLayer).clamp(0, 63)); } else if (dy.abs() > dx.abs() + 6 && dy.abs() > 12) { - // 以垂直为主:上下拖拽调整行序,动作可自由换行(不再被相位锁死)。 - final int target = ((p.dy - BarLayout.topPad) / layout.pitch).round().clamp(0, layout.ordered.length - 1); - c.moveActionRow(_dragAction!.id, target); + // 以垂直为主:上下拖拽换泳道(同泳道可放多个不重叠动作)。 + c.moveActionLane(_dragAction!.id, layout.laneAt(p.dy)); } else { c.setActionStart(_dragAction!.id, _startAngle + dx / layout.span * 360); } @@ -377,12 +389,24 @@ class _CanvasViewState extends State { if (delta > 180) delta -= 360; if (delta < -180) delta += 360; if (_mode == _DragMode.resize) { - c.setActionDuration(_dragAction!.id, _startDuration + delta); + // 拖弧末端:固定起始角,改结束角(弱吸附 + 径向虚线参考)。 + final double end = _snapActionAngle(_startAngle + _startDuration + delta, _dragAction!.id); + _zoneGuideAngle = end; + c.setActionDuration(_dragAction!.id, (end - _dragAction!.startAngle).clamp(5, 360)); + } else if (_mode == _DragMode.resizeStart) { + // 拖弧起始端:固定结束角,改起始角。 + final double fixedEnd = _startAngle + _startDuration; + final double ns = _snapActionAngle(_startAngle + delta, _dragAction!.id).clamp(0, fixedEnd - 5); + _zoneGuideAngle = ns; + c.updateAction( + _dragAction!.id, + (Action x) => x.copyWith(startAngle: ns, durationAngle: fixedEnd - ns), + status: '调整起始角', + ); } else if (shift) { - // Shift + 径向拖拽:按层间距 pitch 换算层号增量。 - final double dr = (p - layout.center).distance - (_startPointer - layout.center).distance; - final int dl = (dr / layout.pitch).round(); - c.setLayer(_dragAction!.id, (_dragAction!.layer + dl).clamp(0, 63)); + // Shift + 径向拖拽:换轨道(同轨道可放多个不重叠动作)。 + final double rad = (p - layout.center).distance; + c.moveActionLane(_dragAction!.id, layout.laneAtRadius(rad)); } else { c.setActionStart(_dragAction!.id, _startAngle + delta); } @@ -556,6 +580,7 @@ class _CanvasViewState extends State { criticalActions: c.criticalActions, criticalDeps: c.criticalDeps, selectedIds: c.selectedIds.toSet(), + guideAngle: _zoneGuideAngle, ), ), ), diff --git a/lib/ui/dialogs.dart b/lib/ui/dialogs.dart index ebd0ac2..7abcd56 100644 --- a/lib/ui/dialogs.dart +++ b/lib/ui/dialogs.dart @@ -1291,6 +1291,7 @@ class HelpDialog extends StatelessWidget { ['我要删除动作', '选中后:左侧「删除」/ 行内删除图标 / 右侧删除 / Delete 键 / 右键菜单。'], ['我要改动作属性', '选中动作 → 右侧「编辑」,可改名称、工位、驱动与颜色。'], ['我要复制 / 对齐', '右侧「编辑操作」→「复制」/「对齐相位」;圆周视图还可改层。'], + ['一条泳道放多个动作', '泳道/轨道=一个执行器:上下拖拽(条形)或 Shift+径向拖拽(圆周)换道;不重叠即可共存,标题合并显示(/ 分隔,≤2 行)。'], ]), // 目标:调整时间轴与动作间的先后 / 并行关系。 _section(t, >[ @@ -1335,7 +1336,7 @@ class HelpDialog extends StatelessWidget { _section(t, >[ ['软件名称', '时序图设计器 · Sequence Diagram Studio'], ['设计人', '呆头鹅'], - ['版本', 'V1.0.0'], + ['版本', 'V1.0.1'], ['版权声明', 'Copyright © 2026 呆头鹅。保留所有权利。'], ['用途', '面向 PLC 流程节拍 / 相位角设计;未经许可不得用于商业再分发。'], ['技术栈', 'Flutter Desktop · 自绘 Canvas · 纯 Dart 几何 · JSON 持久化 · SVG 导出。'], diff --git a/pubspec.yaml b/pubspec.yaml index e5a46f8..c17a236 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # In Windows, build-name is used as the major, minor, and patch parts # of the product and file versions while build-number is used as the build suffix. -version: 1.0.0+1 +version: 1.0.1+2 environment: sdk: ^3.13.3