Files
feuerwerk/src/editor/vectorart/shapeFrame.ts
T
adminandClaude Opus 5.5 808e1eefc2
game-maker / gate (push) Successful in 3m2s
game-maker / suite (push) Successful in 15m29s
Clean up duplicates and dead code, and fix the + menu check (#85)
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
of a526e1b failed 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>
2026-10-03 17:38:39 +02:00

121 lines
4.7 KiB
TypeScript
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.
/**
* The frame round the selected shapes of a vector drawing (#291), without Pixi: where it is drawn,
* which of its handles a press lands on and what a pull on that handle means. The handles are the
* scene view's (`sizeGizmo.ts`) – eight squares that size and a knob on a stem that turns.
*
* Unlike a scene object's, the frame stands a few pixels off what it frames. A shape's own points
* lie on its bounds, a square's exactly on the corners, and a handle drawn there would hide the
* point under it; off the bounds neither is ever in the other's reach.
*/
import { applyVector, type Mat } from '../../core/model/matrix';
import type { Vec2 } from '../../core/model/Properties';
import type { Extent } from '../../core/model/vectorPaint';
import {
extentCentre,
extentCorners,
type FrameSide,
frameScale,
rotationAbout,
} from '../../core/model/vectorTransform';
import { knobAt, type SizeGrip, sizeGripAt } from '../sizeGizmo';
/** Screen pixels between what is framed and the frame. More than a point's reach plus a handle's. */
export const FRAME_PAD = 15;
/** Screen pixels around a handle that grab it. */
const FRAME_GRIP = 6;
/** How far the knob stands out from the top edge, in screen pixels. */
export const FRAME_STEM = 26;
/** Degrees a turn snaps to while Shift is held. */
const TURN_SNAP = 15;
export interface FrameBox {
/** Top left, top right, bottom right, bottom left – in the pixels `toLocal` hands out. */
corners: Vec2[];
/** The middle of each edge: top, right, bottom, left. */
edges: Vec2[];
base: Vec2;
knob: Vec2;
}
/**
* The frame as it is drawn: the corners of `extent` taken through `m` – the map of a drag under way,
* so the frame turns with what it holds – then onto the screen and `FRAME_PAD` pixels outwards.
*/
export function frameBox(extent: Extent, m: Mat, toLocal: (x: number, y: number) => Vec2): FrameBox {
const origin = toLocal(0, 0);
const direction = (v: Vec2): Vec2 => {
const moved = applyVector(m, v);
const tip = toLocal(moved.x, moved.y);
const d = { x: tip.x - origin.x, y: tip.y - origin.y };
const length = Math.hypot(d.x, d.y);
return length < 1e-9 ? v : { x: d.x / length, y: d.y / length };
};
const right = direction({ x: 1, y: 0 });
const down = direction({ x: 0, y: 1 });
const out: [number, number][] = [
[-1, -1],
[1, -1],
[1, 1],
[-1, 1],
];
const corners = extentCorners(extent, m).map((corner, i) => {
const at = toLocal(corner.x, corner.y);
const [sx, sy] = out[i]!;
return {
x: at.x + FRAME_PAD * (sx * right.x + sy * down.x),
y: at.y + FRAME_PAD * (sx * right.y + sy * down.y),
};
});
const edges = corners.map((a, i) => {
const b = corners[(i + 1) % corners.length]!;
return { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 };
});
return { corners, edges, ...knobAt(corners, FRAME_STEM) };
}
/** The handle of the frame under `p`, the knob first; null beside all of them. */
export function frameGripAt(box: FrameBox, p: Vec2): SizeGrip | null {
return sizeGripAt(box.corners, box.knob, p, FRAME_GRIP, { sizable: true, corners: true });
}
/** The arrow a handle shows: along the direction it pulls in; the knob shows a hand. */
export function frameCursor(grip: SizeGrip): string {
if (grip.kind === 'knob') return 'grab';
const { sx, sy } = grip.side;
if (sx === 0) return 'ns-resize';
if (sy === 0) return 'ew-resize';
return sx === sy ? 'nwse-resize' : 'nesw-resize';
}
/** Where the handle on `side` sits on the framed extent itself, in canvas pixels. */
export function sidePoint(extent: Extent, side: FrameSide): Vec2 {
const centre = extentCentre(extent);
return {
x: side.sx === 0 ? centre.x : side.sx > 0 ? extent.x1 : extent.x0,
y: side.sy === 0 ? centre.y : side.sy > 0 ? extent.y1 : extent.y0,
};
}
/**
* The map a size handle has reached: the pointer went from `press` to `now` (canvas pixels), and the
* handle went the same way from where it sat – so a press beside the handle's middle does not jump.
* `snap` puts the place the handle is pulled to on the grid.
*/
export function frameSizing(
extent: Extent,
side: FrameSide,
press: Vec2,
now: Vec2,
options: { proportional: boolean; centred: boolean; snap?: (p: Vec2) => Vec2 },
): Mat {
const from = sidePoint(extent, side);
const to = { x: from.x + now.x - press.x, y: from.y + now.y - press.y };
return frameScale(extent, side, options.snap ? options.snap(to) : to, options);
}
/** The map of a turn by `sweep` degrees round the middle: to a tenth, or in steps of `TURN_SNAP`. */
export function frameTurning(extent: Extent, sweep: number, snap: boolean): Mat {
const degrees = snap ? Math.round(sweep / TURN_SNAP) * TURN_SNAP : Math.round(sweep * 10) / 10;
return rotationAbout(extentCentre(extent), degrees);
}