Files
EasyDesignCollection/lib/layout/layout.dart
T

321 lines
12 KiB
Dart
Raw Normal View History

2026-09-18 16:50:12 +08:00
/// 几何布局层:把领域模型 [Project] 映射为屏幕坐标,供绘制与命中测试使用。
///
/// 本层不持有任何可变状态、不修改模型,属于纯计算:
/// - 顶层的极坐标/角度/圆弧工具函数服务于圆周视图;
/// - [BarLayout] 负责条形(甘特)视图的行与列定位;
/// - [CircularLayout] 负责圆周视图的同心环定位与命中测试。
/// 视图层根据这些坐标进行绘制与交互命中判断。
library;
import 'dart:math' as math;
import 'dart:ui';
import '../model/models.dart';
/// 极坐标转直角坐标:以 [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],
];
}
/// 计算动作在条形视图中的行顺序。
///
/// **泳道模型**:`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) {
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;
}
/// 判断两条角度区间是否重叠(用于同一泳道内“动作不可重叠”的约束)。
///
/// 参数均为角度区间 `[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-09-18 16:50:12 +08:00
/// 条形(甘特)视图布局。
///
/// 横轴为相位角(0°–360°),纵轴为动作行。行顺序由 [ordered] 决定,
/// 行高 [pitch] 会根据可用高度自适应(限制在 46–66 之间)。
class BarLayout {
/// 构造条形布局并计算行高。
///
/// 参数 [project]:项目数据;[size]:可用画布尺寸。
/// 副作用:初始化 [pitch]。
BarLayout(this.project, this.size) {
// 可用高度 = 总高 - 顶部留白 - 底部预留;按**泳道数**均分并夹取到 [44, 58],
// 让相邻泳道之间的空隙能容纳依赖连线及其标签(上限定得高一些,
// 泳道少时获得更多垂直间隙,减少标签相互压盖)。
final int n = lanes.length;
2026-09-18 16:50:12 +08:00
final double usable = size.height - topPad - bottomReserve;
pitch = n == 0 ? 58 : (usable / n).clamp(44, 58);
2026-09-18 16:50:12 +08:00
}
/// 布局所依据的项目数据。
final Project project;
/// 可用画布尺寸。
final Size size;
/// 左侧标签栏宽度(像素):紧贴图表,避免动作名离图太远。
static const double leftGutter = 210;
/// 顶部留白(为标题/刻度等预留)。
static const double topPad = 210;
/// 单个动作条的高度(像素):单行文字即可,条块更苗条(留出更多泳道间隙放依赖标签)。
static const double barH = 18;
2026-09-18 16:50:12 +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);
/// 泳道编号(= 物理执行器轨道):所有动作 `layer` 的去重升序列表。
List<int> get lanes {
final Set<int> s = <int>{for (final Action a in project.actions) a.layer};
final List<int> l = s.toList()..sort();
return l;
}
/// 泳道号在 [lanes] 中的序号。
int laneIndexOf(int lane) => lanes.indexOf(lane);
2026-09-18 16:50:12 +08:00
/// 泳道内包含的动作(按起始角排序)。
List<Action> actionsInLane(int lane) =>
ordered.where((Action a) => a.layer == lane).toList();
/// 泳道号转纵坐标(该泳道顶部 y)。
double yForLane(int lane) => topPad + laneIndexOf(lane) * pitch;
/// 行索引转纵坐标(按泳道序号)。
2026-09-18 16:50:12 +08:00
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;
/// 计算动作 [a] 的条形矩形(按所属泳道的 y)。
2026-09-18 16:50:12 +08:00
///
/// 宽度至少为 3px,避免极短动作不可见;跨圈部分会被截断到 360°。
Rect rectFor(Action a) {
final double y = yForLane(a.layer);
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。
///
/// 从后往前遍历(后绘制的在上层),并按 [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];
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);
// 轨道数 = 泳道数(layer 去重),同一轨道可放多个互不重叠的动作。
final int layers = lanes.length;
2026-09-18 16:50:12 +08:00
// 最大半径受较短边约束,减去 74px 留给刻度与边距。
final double maxR = math.min(size.width, size.height) / 2 - 74;
// 在 r0 与 maxR 之间均分各轨道,环间距夹取到 [32, 52]:
// 让相邻环之间留出足够空隙放置依赖箭头与图注。
pitch = layers <= 1 ? 46 : ((maxR - r0) / (layers - 1)).clamp(32, 56);
// 动作环带宽度:只求容纳文字(约 15–18px),环更窄、环间空隙更宽,
// 便于在环间放置依赖标签(减少与动作名、其它标签的压盖)。
ringWidth = (pitch * 0.42).clamp(14, 17);
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;
/// 轨道编号(= 泳道):所有动作 `layer` 的去重升序列表。
List<int> get lanes {
final Set<int> s = <int>{for (final Action a in project.actions) a.layer};
final List<int> 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;
}
2026-09-18 16:50:12 +08:00
/// 最外层半径。
double get rMax => lanes.isEmpty ? r0 : radiusFor(lanes.last);
2026-09-18 16:50:12 +08:00
/// 刻度标签所在的半径(最外层再向外偏移)。
/// 刻度环半径:在外层动作环之外留出足够间距,
/// 供相位分区带独立占位(不与动作环挤在一起)。
double get tickR => rMax + ringWidth / 2 + 38;
/// 环宽别名,供绘制代码使用。
double get th => ringWidth;
/// 命中测试:返回坐标 [p] 命中的动作,未命中返回 null。
///
/// 算法:计算点相对圆心的距离 [d] 与角度 [a];遍历所有动作,先按径向
/// 距离过滤(与所在环的偏差不超过半个环宽 + [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) {
final double r = radiusFor(a2.layer);
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;
}
}