Refactoring round 9 covers everything since round 8 (14972d0): 62 commits with about 29 000 new lines in src, api, tools and tests. Behaviour is unchanged. The one fix is in a check: e2e 04 counted 25 asset types, and the shader function from #303 makes 26, so the run ofa526e1bfailed there. Duplicate code (jscpd over src, 50 tokens, without the two licence texts: 35 clones / 289 lines before, 29 / 232 after): - ShaderPicture (new, viewport/shaderPicture.ts) is the picture a camera's shader and a particle field's shader are laid over. It keeps the render texture while its size holds and frees the replaced one once the shader holds the new one. CameraShade and ParticleField.shadeField drew it the same way, line for line. - CameraShade builds its mesh on the first draw. Before, every redraw of a view without a camera shader built a mesh and threw it away. - emptyHost in inspector/host.ts builds the empty host the Inspector, the Variables tab and the Tilemap panel start from. - paddedQuad(null) is the plain unit quad. symbols.ts and shaderPasses.ts each wrote it out by hand. - In graph/nested.ts, calls of shader functions and On behavior nodes are followed by one walk, `reaches`, over ids `namedBy` a node type. Dead code: - ShaderEditor.colorFieldOf and the getter CurveCanvas.snapping had no reader. - 22 values used only in their own file lose their `export`. Types and the node catalogues of shaders and particles stay exported. - Kept on purpose: PhysicsSystem.holdsJoint is read by tests only, but it is their one view of whether a joint holds. - A pass with stricter lint rules over every file changed since round 8 found no bug. PixelArtEditor.started is now readonly. Comments and docs: - Ten comments told history ("since #190", "used to", "any more"). They now describe what is. The bundle is md5-identical before and after, and the four e2e files minify to the same bytes. - README: joint and shader function in the asset list, joints on the Physics row, what the Shaders page covers, two CI runners. - e2e 04 keeps the number of asset types once, as ASSET_TYPE_COUNT. - screenshot.png in the README is a new picture of the editor (3840x2160 instead of 1825x1140), taken by the user. Tests: - Gate on melektaus-atx: 229 files, 3514 tests, CHECK EXIT 0 (+3 for emptyHost, +1 for the unit quad). - e2e 1-4, 31, 63 on melektaus-atx: ALL PASSED, 195 checks. That is the + menu of 04, the field shader of 31 and the camera shader of 63, both through ShaderPicture. - No new e2e check, so the sum stays 1728. - Counter-checks, each put back and compared with cmp: reaches that never goes on fails 1 test, listensTo reading the wrong parameter fails 1, emptyHost dropping what a panel hands in fails 1, folding without a relayout fails 1. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
104 lines
3.8 KiB
TypeScript
104 lines
3.8 KiB
TypeScript
/** Which link a click or a rubber band means, kept out of the canvas so tests can reach it (#289). */
|
||
|
||
import type { Band } from '../framing';
|
||
import { curvePoint } from './beads';
|
||
import { type Loop, loopPoint, type Point, type Rect } from './transitions';
|
||
|
||
/** One link as the last draw laid it out, in graph units. */
|
||
export type EdgePath = { edgeId: string } & (
|
||
| { kind: 'curve'; start: Point; end: Point }
|
||
| { kind: 'line'; start: Point; end: Point }
|
||
| { kind: 'loop'; loop: Loop }
|
||
);
|
||
|
||
/** A transition's name plate: clicking the name means its arrow. */
|
||
export type EdgePlate = Rect & { edgeId: string };
|
||
|
||
/** Straight pieces a curve is tested as; at 24 the error stays far below a pixel of tolerance. */
|
||
const CURVE_STEPS = 24;
|
||
|
||
function pathPoints(path: EdgePath): Point[] {
|
||
if (path.kind === 'line') return [path.start, path.end];
|
||
const at = (t: number) =>
|
||
path.kind === 'loop'
|
||
? loopPoint(path.loop, t)
|
||
: curvePoint(path.start.x, path.start.y, path.end.x, path.end.y, t);
|
||
return Array.from({ length: CURVE_STEPS + 1 }, (_, i) => at(i / CURVE_STEPS));
|
||
}
|
||
|
||
/** The point halfway along a link – where a check clicks it. */
|
||
export function pathMiddle(path: EdgePath): Point {
|
||
if (path.kind === 'line') return { x: (path.start.x + path.end.x) / 2, y: (path.start.y + path.end.y) / 2 };
|
||
return path.kind === 'loop'
|
||
? loopPoint(path.loop, 0.5)
|
||
: curvePoint(path.start.x, path.start.y, path.end.x, path.end.y, 0.5);
|
||
}
|
||
|
||
function distanceToSegment(p: Point, a: Point, b: Point): number {
|
||
const [dx, dy] = [b.x - a.x, b.y - a.y];
|
||
const length = dx * dx + dy * dy;
|
||
const t = length === 0 ? 0 : Math.min(1, Math.max(0, ((p.x - a.x) * dx + (p.y - a.y) * dy) / length));
|
||
return Math.hypot(p.x - (a.x + dx * t), p.y - (a.y + dy * t));
|
||
}
|
||
|
||
export function distanceToPath(path: EdgePath, point: Point): number {
|
||
const points = pathPoints(path);
|
||
let nearest = Infinity;
|
||
for (let i = 1; i < points.length; i++)
|
||
nearest = Math.min(nearest, distanceToSegment(point, points[i - 1]!, points[i]!));
|
||
return nearest;
|
||
}
|
||
|
||
/**
|
||
* The link under a point, or null. A name plate wins over any line, and of several lines within
|
||
* `tolerance` the one drawn last does – it is the one on top.
|
||
*/
|
||
export function edgeAt(
|
||
paths: readonly EdgePath[],
|
||
point: Point,
|
||
tolerance: number,
|
||
plates: readonly EdgePlate[] = [],
|
||
): string | null {
|
||
for (let i = plates.length - 1; i >= 0; i--) {
|
||
const p = plates[i]!;
|
||
if (point.x >= p.x && point.x <= p.x + p.w && point.y >= p.y && point.y <= p.y + p.h) return p.edgeId;
|
||
}
|
||
for (let i = paths.length - 1; i >= 0; i--)
|
||
if (distanceToPath(paths[i]!, point) <= tolerance) return paths[i]!.edgeId;
|
||
return null;
|
||
}
|
||
|
||
/** Whether the segment a-b has any point inside the rectangle (Liang-Barsky). */
|
||
function segmentInRect(a: Point, b: Point, minX: number, minY: number, maxX: number, maxY: number): boolean {
|
||
let [t0, t1] = [0, 1];
|
||
const [dx, dy] = [b.x - a.x, b.y - a.y];
|
||
for (const [p, q] of [
|
||
[-dx, a.x - minX],
|
||
[dx, maxX - a.x],
|
||
[-dy, a.y - minY],
|
||
[dy, maxY - a.y],
|
||
] as const) {
|
||
if (p === 0) {
|
||
if (q < 0) return false;
|
||
continue;
|
||
}
|
||
const r = q / p;
|
||
if (p < 0) t0 = Math.max(t0, r);
|
||
else t1 = Math.min(t1, r);
|
||
if (t0 > t1) return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
/** Ids of every link the band touches, in drawing order: a line is thin, so crossing it counts. */
|
||
export function edgesInBand(paths: readonly EdgePath[], band: Band): string[] {
|
||
const [minX, maxX] = [Math.min(band.x1, band.x2), Math.max(band.x1, band.x2)];
|
||
const [minY, maxY] = [Math.min(band.y1, band.y2), Math.max(band.y1, band.y2)];
|
||
return paths
|
||
.filter((path) => {
|
||
const points = pathPoints(path);
|
||
return points.some((p, i) => i > 0 && segmentInRect(points[i - 1]!, p, minX, minY, maxX, maxY));
|
||
})
|
||
.map((path) => path.edgeId);
|
||
}
|