Files
EasyDesignCollection/lib/layout/layout.dart
T
呆头鹅 6cfa79507c feat: V1.0.0 发布(呆头鹅)
- 默认浅色白底主题;字体整数化 + Microsoft YaHei UI 提升清晰度
- 相位分区:圆周/条形显示、沿弧排列、拖拽边界(虚线参考+弱吸附)、增删改(对话框/右键/双击)
- 动作:Ctrl 多选批量对齐;上下拖拽自由换行;左右端拖拽改起止(虚线+弱吸附+左右箭头光标)
- 动作编辑支持角度与时间双向换算
- 圆周顺时针箭头移至刻度环外;条形左侧标题列收窄
- 侧栏可隐藏/钉住;适应视图按内容自适应;节拍统计改为按钮弹窗
- AI 生成时序图(DeepSeek,Flash/Pro,推理强度),基于 dart:io 无插件
- 窗口标题显示文件名/未命名文件;关于信息(设计人/版本/版权)
- 全量中文注释、docs/ARCHITECTURE.md、docs/HELP.md(按任务组织)
- 构建脚本输出到 dist/,并附带 dist 生成物
2026-09-18 16:50:12 +08:00

265 lines
9.6 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/// 几何布局层:把领域模型 [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],
];
}
/// 计算动作在条形视图中的行顺序。
///
/// 规则:
/// 1. 先按「工位 order → 起始角 → id」得到**自动顺序**;
/// 2. 若存在显式行序(`Action.row >= 0`),则显式行序优先,其余按自动顺序追加。
///
/// 这样一旦用户上下拖拽过行,行位置就固定下来,**不会再随相位变化而跳动**,
/// 依赖箭头也就不会在水平拖动时乱跳。
List<Action> orderActions(Project project) {
final List<Action> list = List<Action>.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);
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<Action> explicit = list.where((Action a) => a.row >= 0).toList()
..sort((Action a, Action b) => a.row.compareTo(b.row));
final List<Action> auto = list.where((Action a) => a.row < 0).toList();
list
..clear()
..addAll(explicit)
..addAll(auto);
}
return list;
}
/// 条形(甘特)视图布局。
///
/// 横轴为相位角(0°–360°),纵轴为动作行。行顺序由 [ordered] 决定,
/// 行高 [pitch] 会根据可用高度自适应(限制在 46–66 之间)。
class BarLayout {
/// 构造条形布局并计算行高。
///
/// 参数 [project]:项目数据;[size]:可用画布尺寸。
/// 副作用:初始化 [pitch]。
BarLayout(this.project, this.size) {
// 可用高度 = 总高 - 顶部留白 - 底部预留;按动作数均分并夹取到 [46, 66]。
final int n = project.actions.length;
final double usable = size.height - topPad - bottomReserve;
pitch = n == 0 ? 66 : (usable / n).clamp(46, 66);
}
/// 布局所依据的项目数据。
final Project project;
/// 可用画布尺寸。
final Size size;
/// 左侧标签栏宽度(像素):紧贴图表,避免动作名离图太远。
static const double leftGutter = 210;
/// 顶部留白(为标题/刻度等预留)。
static const double topPad = 210;
/// 单个动作条的高度(像素):与 11–13px 文字匹配,避免过厚。
static const double barH = 26;
/// 行的视觉参考高度(像素)。
static const double rowVisual = 40;
/// 底部预留空间(像素)。
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);
/// 相位角(度)转横坐标;角度先钳制到 `[0, 360]`。
double xFor(double deg) => plotLeft + (deg.clamp(0, 360) / 360) * span;
/// 行索引转纵坐标(该行顶部 y)。
double yFor(int index) => topPad + index * pitch;
/// 计算动作 [a] 的条形矩形。
///
/// 宽度至少为 3px,避免极短动作不可见;跨圈部分会被截断到 360°。
Rect rectFor(Action a) {
final int i = ordered.indexOf(a);
final double y = yFor(i);
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。
///
/// 从后往前遍历(后绘制的在上层),并向外膨胀 4px 提升可点性。
Action? hitTest(Offset p) {
for (int i = ordered.length - 1; i >= 0; i--) {
final Action a = ordered[i];
final Rect r = rectFor(a).inflate(4);
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);
final int layers = project.maxLayer + 1;
// 最大半径受较短边约束,减去 74px 留给刻度与边距。
final double maxR = math.min(size.width, size.height) / 2 - 74;
// 在 r0 与 maxR 之间均分各层,环间距夹取到 [24, 46]。
pitch = layers <= 1 ? 40 : ((maxR - r0) / (layers - 1)).clamp(24, 46);
// 动作环带宽度:相对行距收窄(约 0.56),并夹取到 [12, 26],
// 让环与环之间留出呼吸空间、文字仍能容纳。
ringWidth = (pitch * 0.56).clamp(12, 26);
}
/// 布局所依据的项目数据。
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] 层对应的环半径。
double radiusFor(int layer) => r0 + layer * pitch;
/// 最外层半径。
double get rMax => radiusFor(project.maxLayer);
/// 刻度标签所在的半径(最外层再向外偏移)。
/// 刻度环半径:在外层动作环之外留出足够间距,
/// 供相位分区带独立占位(不与动作环挤在一起)。
double get tickR => rMax + ringWidth / 2 + 38;
/// 环宽别名,供绘制代码使用。
double get th => ringWidth;
/// 命中测试:返回坐标 [p] 命中的动作,未命中返回 null。
///
/// 算法:计算点相对圆心的距离 [d] 与角度 [a];遍历所有动作,先按径向
/// 距离过滤(与所在环的偏差不超过半个环宽 + 3px),再判断角度是否落在
/// 该动作弧段内;在候选中取径向偏差最小者。
Action? hitTest(Offset p) {
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 + 3) continue;
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;
}
}