2026-09-18 16:50:12 +08:00
|
|
|
|
/// 几何布局层:把领域模型 [Project] 映射为屏幕坐标,供绘制与命中测试使用。
|
|
|
|
|
|
///
|
|
|
|
|
|
/// 本层不持有任何可变状态、不修改模型,属于纯计算:
|
|
|
|
|
|
/// - 顶层的极坐标/角度/圆弧工具函数服务于圆周视图;
|
|
|
|
|
|
/// - [BarLayout] 负责条形(甘特)视图的行与列定位;
|
|
|
|
|
|
/// - [CircularLayout] 负责圆周视图的同心环定位与命中测试。
|
|
|
|
|
|
/// 视图层根据这些坐标进行绘制与交互命中判断。
|
|
|
|
|
|
library;
|
|
|
|
|
|
|
|
|
|
|
|
import 'dart:math' as math;
|
|
|
|
|
|
import 'dart:ui';
|
|
|
|
|
|
|
2026-11-22 10:02:48 +08:00
|
|
|
|
import 'package:sequence_diagram/model/models.dart';
|
2026-09-18 16:50:12 +08:00
|
|
|
|
|
|
|
|
|
|
/// 极坐标转直角坐标:以 [c] 为圆心、[r] 为半径、[deg] 为角度(0° 指向正上方)。
|
|
|
|
|
|
Offset polar(Offset c, double r, double deg) {
|
|
|
|
|
|
final double t = (deg - 90) * math.pi / 180;
|
|
|
|
|
|
return Offset(c.dx + r * math.cos(t), c.dy + r * math.sin(t));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 已知圆心 [c] 与点 [p],求该点相对圆心的角度(度,0° 为正上方,范围 [0,360))。
|
|
|
|
|
|
double angleOf(Offset c, Offset p) {
|
|
|
|
|
|
final double a = math.atan2(p.dy - c.dy, p.dx - c.dx) * 180 / math.pi + 90;
|
|
|
|
|
|
return a < 0 ? a + 360 : a;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 构造从角度 [a0] 到 [a1] 的圆弧路径。
|
|
|
|
|
|
///
|
|
|
|
|
|
/// 参数 [c]:圆心;[r]:半径;[a0]/[a1]:起止角(度)。
|
|
|
|
|
|
/// 返回:以起点 moveTo、经 arcToPoint 连接终点的 [Path];根据扫过角度决定
|
|
|
|
|
|
/// 是否使用 largeArc,并根据扫角正负决定顺时针方向。
|
|
|
|
|
|
Path arcPath(Offset c, double r, double a0, double a1) {
|
|
|
|
|
|
final Path p = Path();
|
|
|
|
|
|
final Offset s = polar(c, r, a0);
|
|
|
|
|
|
final Offset e = polar(c, r, a1);
|
|
|
|
|
|
p.moveTo(s.dx, s.dy);
|
|
|
|
|
|
final double sweep = a1 - a0;
|
|
|
|
|
|
final int large = sweep.abs() > 180 ? 1 : 0;
|
|
|
|
|
|
p.arcToPoint(
|
|
|
|
|
|
e,
|
|
|
|
|
|
radius: Radius.circular(r),
|
|
|
|
|
|
largeArc: large == 1,
|
|
|
|
|
|
clockwise: sweep >= 0,
|
|
|
|
|
|
);
|
|
|
|
|
|
return p;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 将跨越 360° 的区间拆分为两段,便于分别绘制。
|
|
|
|
|
|
///
|
|
|
|
|
|
/// 若 [a1] <= 360 直接返回 `[[a0, a1]]`;否则返回 `[[a0, 360], [0, a1-360]]`。
|
|
|
|
|
|
List<List<double>> splitArcs(double a0, double a1) {
|
|
|
|
|
|
if (a1 <= 360) return <List<double>>[<double>[a0, a1]];
|
|
|
|
|
|
return <List<double>>[
|
|
|
|
|
|
<double>[a0, 360],
|
|
|
|
|
|
<double>[0, a1 - 360],
|
|
|
|
|
|
];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 计算动作在条形视图中的行顺序。
|
|
|
|
|
|
///
|
2026-09-18 17:54:35 +08:00
|
|
|
|
/// **泳道模型**:`Action.layer` 同时作为「泳道 / 圆周轨道」编号,
|
|
|
|
|
|
/// 对应一个物理执行器;同一泳道可容纳**多个互不重叠**的动作。
|
|
|
|
|
|
/// 排序规则:先按泳道号,再按起始角,最后按 id。
|
2026-09-18 16:50:12 +08:00
|
|
|
|
List<Action> orderActions(Project project) {
|
|
|
|
|
|
final List<Action> list = List<Action>.of(project.actions);
|
|
|
|
|
|
list.sort((Action a, Action b) {
|
2026-09-18 17:54:35 +08:00
|
|
|
|
if (a.layer != b.layer) return a.layer.compareTo(b.layer);
|
2026-09-18 16:50:12 +08:00
|
|
|
|
final int c = a.startAngle.compareTo(b.startAngle);
|
|
|
|
|
|
if (c != 0) return c;
|
|
|
|
|
|
return a.id.compareTo(b.id);
|
|
|
|
|
|
});
|
|
|
|
|
|
return list;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-18 17:54:35 +08:00
|
|
|
|
/// 判断两条角度区间是否重叠(用于同一泳道内“动作不可重叠”的约束)。
|
|
|
|
|
|
///
|
|
|
|
|
|
/// 参数均为角度区间 `[start, end]`;跨圈(end > 360)按模 360 归一后判断。
|
|
|
|
|
|
bool angleRangesOverlap(double s1, double e1, double s2, double e2) {
|
|
|
|
|
|
// 归一到 [0,360),并把跨圈区间拆成两段后逐一比较。
|
|
|
|
|
|
List<List<double>> 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 <List<double>>[<double>[0, 360]];
|
|
|
|
|
|
if (b >= a) return <List<double>>[<double>[a, b]];
|
|
|
|
|
|
return <List<double>>[<double>[a, 360], <double>[0, b]];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (final List<double> x in seg(s1, e1)) {
|
|
|
|
|
|
for (final List<double> y in seg(s2, e2)) {
|
|
|
|
|
|
if (x[0] < y[1] - 0.001 && y[0] < x[1] - 0.001) return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-10 09:21:45 +08:00
|
|
|
|
/// 动作 [a] 在 [project] 中其泳道内的有效槽位(与泳道槽位数钳制一致)。
|
|
|
|
|
|
///
|
|
|
|
|
|
/// 1 槽泳道恒为 0;旧数据中的历史行序(超出范围 / -1)会被钳制到有效槽位。
|
|
|
|
|
|
int effectiveSlot(Project project, Action a) {
|
|
|
|
|
|
final int slots = project.slotsOf(a.layer);
|
|
|
|
|
|
if (slots <= 1) return 0;
|
|
|
|
|
|
final int r = a.row;
|
|
|
|
|
|
return r < 0 ? 0 : (r >= slots ? slots - 1 : r);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-18 16:50:12 +08:00
|
|
|
|
/// 条形(甘特)视图布局。
|
|
|
|
|
|
///
|
|
|
|
|
|
/// 横轴为相位角(0°–360°),纵轴为动作行。行顺序由 [ordered] 决定,
|
|
|
|
|
|
/// 行高 [pitch] 会根据可用高度自适应(限制在 46–66 之间)。
|
|
|
|
|
|
class BarLayout {
|
|
|
|
|
|
/// 构造条形布局并计算行高。
|
|
|
|
|
|
///
|
|
|
|
|
|
/// 参数 [project]:项目数据;[size]:可用画布尺寸。
|
|
|
|
|
|
/// 副作用:初始化 [pitch]。
|
|
|
|
|
|
BarLayout(this.project, this.size) {
|
2026-10-10 09:21:45 +08:00
|
|
|
|
// 可用高度 = 总高 - 顶部留白 - 底部预留;按**总子行数**(各泳道槽位数之和)
|
|
|
|
|
|
// 均分并夹取到 [44, 58];单槽泳道时与旧版行为完全一致。
|
|
|
|
|
|
// 多槽位工程采用「组内紧凑 + 泳道间拉开」:子行更紧、泳道之间额外留白,
|
|
|
|
|
|
// 一眼能看出哪些子行属于同一条泳道。
|
|
|
|
|
|
final int n = subRowCount;
|
|
|
|
|
|
final int laneN = lanes.length;
|
|
|
|
|
|
final double extraTotal = _grouped && laneN > 1 ? (laneN - 1) * laneExtraGap : 0;
|
2026-09-18 16:50:12 +08:00
|
|
|
|
final double usable = size.height - topPad - bottomReserve;
|
2026-10-10 09:21:45 +08:00
|
|
|
|
pitch = n == 0 ? 58 : ((usable - extraTotal) / n).clamp(_grouped ? 36 : 44, 58);
|
2026-09-18 16:50:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-10 09:21:45 +08:00
|
|
|
|
/// 是否启用「组内紧凑 + 泳道间拉开」的间隔分组(仅多槽位工程)。
|
|
|
|
|
|
bool get _grouped => project.hasMultiSlotLanes;
|
|
|
|
|
|
|
|
|
|
|
|
/// 泳道之间的额外纵向留白(仅多槽位分组时生效,像素)。
|
|
|
|
|
|
static const double laneExtraGap = 16;
|
|
|
|
|
|
|
2026-09-18 16:50:12 +08:00
|
|
|
|
/// 布局所依据的项目数据。
|
|
|
|
|
|
final Project project;
|
|
|
|
|
|
|
|
|
|
|
|
/// 可用画布尺寸。
|
|
|
|
|
|
final Size size;
|
|
|
|
|
|
|
|
|
|
|
|
/// 左侧标签栏宽度(像素):紧贴图表,避免动作名离图太远。
|
|
|
|
|
|
static const double leftGutter = 210;
|
|
|
|
|
|
|
|
|
|
|
|
/// 顶部留白(为标题/刻度等预留)。
|
|
|
|
|
|
static const double topPad = 210;
|
|
|
|
|
|
|
2026-10-08 14:34:04 +08:00
|
|
|
|
/// 单个动作条的高度(像素):单行文字即可,条块更苗条(留出更多泳道间隙放依赖标签)。
|
|
|
|
|
|
static const double barH = 18;
|
2026-09-18 16:50:12 +08:00
|
|
|
|
|
|
|
|
|
|
/// 行的视觉参考高度(像素)。
|
2026-09-18 23:32:36 +08:00
|
|
|
|
static const double rowVisual = 36;
|
2026-09-18 16:50:12 +08:00
|
|
|
|
|
|
|
|
|
|
/// 底部预留空间(像素)。
|
|
|
|
|
|
static const double bottomReserve = 350;
|
|
|
|
|
|
|
|
|
|
|
|
/// 相邻动作行之间的垂直距离(像素)。
|
|
|
|
|
|
late final double pitch;
|
|
|
|
|
|
|
|
|
|
|
|
/// 绘图区左边界。
|
|
|
|
|
|
double get plotLeft => leftGutter;
|
|
|
|
|
|
|
|
|
|
|
|
/// 绘图区右边界(右侧留 56px)。
|
|
|
|
|
|
double get plotRight => size.width - 56;
|
|
|
|
|
|
|
|
|
|
|
|
/// 绘图区横向跨度(像素)。
|
|
|
|
|
|
double get span => plotRight - plotLeft;
|
|
|
|
|
|
|
|
|
|
|
|
/// 决定行先后的动作列表;见顶层函数 [orderActions]。
|
|
|
|
|
|
List<Action> get ordered => orderActions(project);
|
|
|
|
|
|
|
2026-10-10 09:21:45 +08:00
|
|
|
|
/// 泳道编号(= 物理执行器轨道):来自 [Project.renderLanes]。
|
|
|
|
|
|
///
|
|
|
|
|
|
/// 开启「保留空泳道」时包含动作已全部移走的空泳道(序号不变)。
|
|
|
|
|
|
List<int> get lanes => project.renderLanes;
|
2026-09-18 17:54:35 +08:00
|
|
|
|
|
|
|
|
|
|
/// 泳道号在 [lanes] 中的序号。
|
|
|
|
|
|
int laneIndexOf(int lane) => lanes.indexOf(lane);
|
2026-09-18 16:50:12 +08:00
|
|
|
|
|
2026-09-18 17:54:35 +08:00
|
|
|
|
/// 泳道内包含的动作(按起始角排序)。
|
|
|
|
|
|
List<Action> actionsInLane(int lane) =>
|
|
|
|
|
|
ordered.where((Action a) => a.layer == lane).toList();
|
|
|
|
|
|
|
2026-10-10 09:21:45 +08:00
|
|
|
|
/// 泳道 [lane] 的槽位数(1–5)。
|
|
|
|
|
|
int slotsOf(int lane) => project.slotsOf(lane);
|
|
|
|
|
|
|
|
|
|
|
|
/// 动作 [a] 的实际槽位(钳制到所属泳道的有效范围;1 槽泳道恒为 0)。
|
|
|
|
|
|
int slotOf(Action a) => effectiveSlot(project, a);
|
|
|
|
|
|
|
|
|
|
|
|
/// 全部泳道的子行总数(Σ 槽位)。
|
|
|
|
|
|
int get subRowCount {
|
|
|
|
|
|
int n = 0;
|
|
|
|
|
|
for (final int lane in lanes) {
|
|
|
|
|
|
n += slotsOf(lane);
|
|
|
|
|
|
}
|
|
|
|
|
|
return n;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 泳道 [lane] 之前(不含)的纵向高度(子行 + 分组留白)。
|
|
|
|
|
|
double _laneOffsetBefore(int lane) {
|
|
|
|
|
|
double h = 0;
|
|
|
|
|
|
for (final int l in lanes) {
|
|
|
|
|
|
if (l == lane) break;
|
|
|
|
|
|
h += slotsOf(l) * pitch + (_grouped ? laneExtraGap : 0);
|
|
|
|
|
|
}
|
|
|
|
|
|
return h;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-18 17:54:35 +08:00
|
|
|
|
/// 泳道号转纵坐标(该泳道顶部 y)。
|
2026-10-10 09:21:45 +08:00
|
|
|
|
double yForLane(int lane) => topPad + _laneOffsetBefore(lane);
|
2026-09-18 17:54:35 +08:00
|
|
|
|
|
2026-10-10 09:21:45 +08:00
|
|
|
|
/// 泳道内槽位转纵坐标(该槽位所在子行的顶部 y)。
|
|
|
|
|
|
double yForSlot(int lane, int slot) => yForLane(lane) + slot * pitch;
|
|
|
|
|
|
|
|
|
|
|
|
/// 内容底部 y(最后一个子行之后)。
|
|
|
|
|
|
double get plotBottom {
|
|
|
|
|
|
double h = subRowCount * pitch;
|
|
|
|
|
|
if (_grouped && lanes.length > 1) h += (lanes.length - 1) * laneExtraGap;
|
|
|
|
|
|
return topPad + h;
|
|
|
|
|
|
}
|
2026-09-18 16:50:12 +08:00
|
|
|
|
|
2026-09-18 17:54:35 +08:00
|
|
|
|
/// 由纵坐标反查最近的泳道号(用于拖拽换道)。
|
2026-10-10 09:21:45 +08:00
|
|
|
|
int laneAt(double y) => laneSlotAt(y).$1;
|
|
|
|
|
|
|
|
|
|
|
|
/// 由纵坐标反查(泳道号, 槽位)——拖拽换道 / 换槽位时使用。
|
|
|
|
|
|
(int, int) laneSlotAt(double y) {
|
|
|
|
|
|
if (lanes.isEmpty) return (0, 0);
|
|
|
|
|
|
double top = topPad;
|
|
|
|
|
|
for (int li = 0; li < lanes.length; li++) {
|
|
|
|
|
|
final int lane = lanes[li];
|
|
|
|
|
|
final int s = slotsOf(lane);
|
|
|
|
|
|
final double h = s * pitch;
|
|
|
|
|
|
// 泳道之间的额外留白按半份归属本道,保证指针落在空隙中也能命中相邻泳道。
|
|
|
|
|
|
final double boundary =
|
|
|
|
|
|
top + h + (_grouped && li < lanes.length - 1 ? laneExtraGap * 0.5 : 0);
|
|
|
|
|
|
if (y < boundary || li == lanes.length - 1) {
|
|
|
|
|
|
final int slot = ((y - top) / pitch).round().clamp(0, s - 1);
|
|
|
|
|
|
return (lane, slot);
|
|
|
|
|
|
}
|
|
|
|
|
|
top += h + (_grouped ? laneExtraGap : 0);
|
|
|
|
|
|
}
|
|
|
|
|
|
return (lanes.last, slotsOf(lanes.last) - 1);
|
2026-09-18 17:54:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 相位角(度)转横坐标;角度先钳制到 `[0, 360]`。
|
|
|
|
|
|
double xFor(double deg) => plotLeft + (deg.clamp(0, 360) / 360) * span;
|
|
|
|
|
|
|
|
|
|
|
|
/// 计算动作 [a] 的条形矩形(按所属泳道的 y)。
|
2026-09-18 16:50:12 +08:00
|
|
|
|
///
|
|
|
|
|
|
/// 宽度至少为 3px,避免极短动作不可见;跨圈部分会被截断到 360°。
|
|
|
|
|
|
Rect rectFor(Action a) {
|
2026-10-10 09:21:45 +08:00
|
|
|
|
final double y = yForSlot(a.layer, slotOf(a));
|
2026-09-18 16:50:12 +08:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 命中测试:返回坐标 [p] 处最上层的动作,未命中返回 null。
|
|
|
|
|
|
///
|
2026-10-07 22:44:28 +08:00
|
|
|
|
/// 从后往前遍历(后绘制的在上层),并按 [slop] 向外膨胀矩形提升可点性
|
|
|
|
|
|
/// (触屏平台可传入更大的容差)。
|
|
|
|
|
|
Action? hitTest(Offset p, {double slop = 4}) {
|
2026-09-18 16:50:12 +08:00
|
|
|
|
for (int i = ordered.length - 1; i >= 0; i--) {
|
|
|
|
|
|
final Action a = ordered[i];
|
2026-10-07 22:44:28 +08:00
|
|
|
|
final Rect r = rectFor(a).inflate(slop);
|
2026-09-18 16:50:12 +08:00
|
|
|
|
if (r.contains(p)) return a;
|
|
|
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 横坐标反算相位角(度),结果钳制在 `[0, 360]`。
|
|
|
|
|
|
double angleAt(double x) => ((x - plotLeft) / span * 360).clamp(0, 360);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 圆周(环形)视图布局。
|
|
|
|
|
|
///
|
|
|
|
|
|
/// 每个动作的层 [Action.layer] 决定其所在同心环的半径,角度区间决定环上的弧段。
|
|
|
|
|
|
class CircularLayout {
|
|
|
|
|
|
/// 构造圆周布局并计算圆心、环间距与环宽。
|
|
|
|
|
|
///
|
|
|
|
|
|
/// 参数 [project]:项目数据;[size]:可用画布尺寸。
|
|
|
|
|
|
/// 副作用:初始化 [center]、[pitch]、[ringWidth]。
|
|
|
|
|
|
CircularLayout(this.project, this.size) {
|
|
|
|
|
|
// 圆心略微下移 18px 以平衡顶部刻度标签的视觉重心。
|
|
|
|
|
|
center = Offset(size.width / 2, size.height / 2 + 18);
|
2026-10-10 09:21:45 +08:00
|
|
|
|
// 子环总数 = Σ 各轨道槽位数(泳道加宽后同一轨道可有多条并行子环);
|
|
|
|
|
|
// 单槽时与旧版(按轨道数)完全一致。
|
|
|
|
|
|
final int rings = subRingCount;
|
|
|
|
|
|
final int laneN = lanes.length;
|
|
|
|
|
|
// 多槽位工程采用「组内紧凑 + 轨道间拉开」:同一轨道的子环更紧、
|
|
|
|
|
|
// 轨道之间额外留白,一眼能看出哪些子环属于同一轨道。
|
|
|
|
|
|
final double extraTotal = _grouped && laneN > 1 ? (laneN - 1) * ringExtraGap : 0;
|
2026-09-18 16:50:12 +08:00
|
|
|
|
// 最大半径受较短边约束,减去 74px 留给刻度与边距。
|
|
|
|
|
|
final double maxR = math.min(size.width, size.height) / 2 - 74;
|
2026-10-10 09:21:45 +08:00
|
|
|
|
// 在 r0 与 maxR 之间均分各子环,环间距夹取(多槽位时允许更紧)。
|
|
|
|
|
|
pitch = rings <= 1
|
|
|
|
|
|
? 46
|
|
|
|
|
|
: ((maxR - r0 - extraTotal) / (rings - 1)).clamp(_grouped ? 24 : 32, 56);
|
|
|
|
|
|
// 动作环带宽度:只求容纳文字;多槽位分组时子环更窄,轨道间靠留白区分。
|
|
|
|
|
|
ringWidth = (pitch * 0.42).clamp(_grouped ? 10 : 14, 17);
|
2026-09-18 16:50:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-10 09:21:45 +08:00
|
|
|
|
/// 是否启用「组内紧凑 + 轨道间拉开」的间隔分组(仅多槽位工程)。
|
|
|
|
|
|
bool get _grouped => project.hasMultiSlotLanes;
|
|
|
|
|
|
|
|
|
|
|
|
/// 轨道之间的额外径向留白(仅多槽位分组时生效,像素)。
|
|
|
|
|
|
static const double ringExtraGap = 18;
|
|
|
|
|
|
|
2026-09-18 16:50:12 +08:00
|
|
|
|
/// 布局所依据的项目数据。
|
|
|
|
|
|
final Project project;
|
|
|
|
|
|
|
|
|
|
|
|
/// 可用画布尺寸。
|
|
|
|
|
|
final Size size;
|
|
|
|
|
|
|
|
|
|
|
|
/// 最内层(第 0 层)半径(像素)。
|
|
|
|
|
|
static const double r0 = 58;
|
|
|
|
|
|
|
|
|
|
|
|
/// 圆心坐标。
|
|
|
|
|
|
late final Offset center;
|
|
|
|
|
|
|
|
|
|
|
|
/// 相邻层之间的半径差(像素)。
|
|
|
|
|
|
late final double pitch;
|
|
|
|
|
|
|
|
|
|
|
|
/// 环的视觉宽度(像素)。
|
|
|
|
|
|
late final double ringWidth;
|
|
|
|
|
|
|
2026-10-10 09:21:45 +08:00
|
|
|
|
/// 轨道编号(= 泳道):来自 [Project.renderLanes](含保留的空轨道)。
|
|
|
|
|
|
List<int> get lanes => project.renderLanes;
|
2026-09-18 17:54:35 +08:00
|
|
|
|
|
|
|
|
|
|
/// 轨道号在 [lanes] 中的序号。
|
|
|
|
|
|
int laneIndexOf(int lane) => lanes.indexOf(lane);
|
|
|
|
|
|
|
2026-10-10 09:21:45 +08:00
|
|
|
|
/// 轨道 [lane] 的槽位数(1–5)。
|
|
|
|
|
|
int slotsOf(int lane) => project.slotsOf(lane);
|
|
|
|
|
|
|
|
|
|
|
|
/// 动作 [a] 的实际槽位(钳制;1 槽轨道恒为 0)。
|
|
|
|
|
|
int slotOf(Action a) => effectiveSlot(project, a);
|
|
|
|
|
|
|
|
|
|
|
|
/// 全部轨道的子环总数(Σ 槽位)。
|
|
|
|
|
|
int get subRingCount {
|
|
|
|
|
|
int n = 0;
|
|
|
|
|
|
for (final int lane in lanes) {
|
|
|
|
|
|
n += slotsOf(lane);
|
|
|
|
|
|
}
|
|
|
|
|
|
return n;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 轨道 [lane] 之前(不含)的子环数。
|
|
|
|
|
|
int subRingIndexOfLane(int lane) {
|
|
|
|
|
|
int n = 0;
|
|
|
|
|
|
for (final int l in lanes) {
|
|
|
|
|
|
if (l == lane) break;
|
|
|
|
|
|
n += slotsOf(l);
|
|
|
|
|
|
}
|
|
|
|
|
|
return n;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-18 17:54:35 +08:00
|
|
|
|
/// 由半径反查最近的轨道号(用于拖拽换轨)。
|
2026-10-10 09:21:45 +08:00
|
|
|
|
int laneAtRadius(double r) => laneSlotAtRadius(r).$1;
|
|
|
|
|
|
|
|
|
|
|
|
/// 由半径反查(轨道号, 槽位)——拖拽换轨 / 换子环时使用。
|
|
|
|
|
|
(int, int) laneSlotAtRadius(double r) {
|
|
|
|
|
|
if (lanes.isEmpty) return (0, 0);
|
|
|
|
|
|
double base = r0;
|
|
|
|
|
|
for (int li = 0; li < lanes.length; li++) {
|
|
|
|
|
|
final int lane = lanes[li];
|
|
|
|
|
|
final int s = slotsOf(lane);
|
|
|
|
|
|
final double h = s * pitch;
|
|
|
|
|
|
// 轨道之间的额外留白按半份归属本道,落在空隙中也能命中相邻轨道。
|
|
|
|
|
|
final double boundary =
|
|
|
|
|
|
base + h + (_grouped && li < lanes.length - 1 ? ringExtraGap * 0.5 : 0);
|
|
|
|
|
|
if (r < boundary || li == lanes.length - 1) {
|
|
|
|
|
|
final int slot = ((r - base) / pitch).round().clamp(0, s - 1);
|
|
|
|
|
|
return (lane, slot);
|
|
|
|
|
|
}
|
|
|
|
|
|
base += h + (_grouped ? ringExtraGap : 0);
|
|
|
|
|
|
}
|
|
|
|
|
|
return (lanes.last, slotsOf(lanes.last) - 1);
|
2026-09-18 17:54:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-10 09:21:45 +08:00
|
|
|
|
/// 轨道 [lane] 第 [slot] 条子环的半径(未知轨道回退到最内圈)。
|
|
|
|
|
|
double radiusFor(int lane, [int slot = 0]) {
|
2026-09-18 17:54:35 +08:00
|
|
|
|
final int i = laneIndexOf(lane);
|
2026-10-10 09:21:45 +08:00
|
|
|
|
if (i < 0) return r0;
|
|
|
|
|
|
final int s = slotsOf(lane);
|
|
|
|
|
|
final int k = slot < 0 ? 0 : (slot >= s ? s - 1 : slot);
|
|
|
|
|
|
return r0 +
|
|
|
|
|
|
(subRingIndexOfLane(lane) + k) * pitch +
|
|
|
|
|
|
(_grouped ? i * ringExtraGap : 0);
|
2026-09-18 17:54:35 +08:00
|
|
|
|
}
|
2026-09-18 16:50:12 +08:00
|
|
|
|
|
|
|
|
|
|
/// 最外层半径。
|
2026-10-10 09:21:45 +08:00
|
|
|
|
double get rMax {
|
|
|
|
|
|
if (lanes.isEmpty) return r0;
|
|
|
|
|
|
final double extra = _grouped && lanes.length > 1 ? (lanes.length - 1) * ringExtraGap : 0;
|
|
|
|
|
|
return r0 + (subRingCount - 1) * pitch + extra;
|
|
|
|
|
|
}
|
2026-09-18 16:50:12 +08:00
|
|
|
|
|
|
|
|
|
|
/// 刻度标签所在的半径(最外层再向外偏移)。
|
|
|
|
|
|
/// 刻度环半径:在外层动作环之外留出足够间距,
|
|
|
|
|
|
/// 供相位分区带独立占位(不与动作环挤在一起)。
|
|
|
|
|
|
double get tickR => rMax + ringWidth / 2 + 38;
|
|
|
|
|
|
|
|
|
|
|
|
/// 环宽别名,供绘制代码使用。
|
|
|
|
|
|
double get th => ringWidth;
|
|
|
|
|
|
|
|
|
|
|
|
/// 命中测试:返回坐标 [p] 命中的动作,未命中返回 null。
|
|
|
|
|
|
///
|
|
|
|
|
|
/// 算法:计算点相对圆心的距离 [d] 与角度 [a];遍历所有动作,先按径向
|
2026-10-07 22:44:28 +08:00
|
|
|
|
/// 距离过滤(与所在环的偏差不超过半个环宽 + [slop]),再判断角度是否落在
|
|
|
|
|
|
/// 该动作弧段内;在候选中取径向偏差最小者(触屏平台可传入更大的容差)。
|
|
|
|
|
|
Action? hitTest(Offset p, {double slop = 3}) {
|
2026-09-18 16:50:12 +08:00
|
|
|
|
final double d = (p - center).distance;
|
|
|
|
|
|
final double a = angleOf(center, p);
|
|
|
|
|
|
Action? best;
|
|
|
|
|
|
double bestDelta = double.infinity;
|
|
|
|
|
|
for (final Action a2 in project.actions) {
|
2026-10-10 09:21:45 +08:00
|
|
|
|
final double r = radiusFor(a2.layer, slotOf(a2));
|
2026-10-07 22:44:28 +08:00
|
|
|
|
if ((d - r).abs() > ringWidth / 2 + slop) continue;
|
2026-09-18 16:50:12 +08:00
|
|
|
|
if (_inArc(a2, a)) {
|
|
|
|
|
|
final double delta = (d - r).abs();
|
|
|
|
|
|
if (delta < bestDelta) {
|
|
|
|
|
|
bestDelta = delta;
|
|
|
|
|
|
best = a2;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return best;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 判断角度 [deg] 是否落在动作 [a] 的弧段内(含 0.5° 容差)。
|
|
|
|
|
|
///
|
|
|
|
|
|
/// 当动作不跨圈(endAngle <= 360)时为普通区间判断;跨圈时区间回绕,
|
|
|
|
|
|
/// 条件变为 `n >= start-0.5 或 n <= (end-360)+0.5`。
|
|
|
|
|
|
bool _inArc(Action a, double deg) {
|
|
|
|
|
|
final double e = a.endAngle;
|
|
|
|
|
|
final double n = deg % 360;
|
|
|
|
|
|
if (e <= 360) {
|
|
|
|
|
|
return n >= a.startAngle - 0.5 && n <= e + 0.5;
|
|
|
|
|
|
}
|
|
|
|
|
|
return n >= a.startAngle - 0.5 || n <= (e - 360) + 0.5;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|