Recurring cleanup round. Behaviour is unchanged. Duplicated code in
src went from 27 clones with 299 lines to 6 clones with 56 lines
(jscpd, the two identical OFL licence texts not counted).
- SheetCanvas is the new base of the texture, pixel art and vector art
canvases. It holds background, mask, hint, panning, wheel zoom, fit,
view save and restore and the screen mapping. Pixel art keeps its
whole-pixel panning and fixed zoom steps.
- mixColor in src/core/util/color.ts replaces three copies in
animation, gradient and particles. It clamps t like the particle one.
- fingerprint now lives once in generated.ts, next to the revision
contract from #185.
- MoveCommand next to RenameCommand is the base of the three reorder
commands for tile pictures, tilemap layers and vector shapes.
- The two raycast nodes share inputs, settings and filter via rayOf.
- canvasRect() lives once in AssetTabEditor.
- Removed unused code: findPixelArt, isDrawableShape,
GeneratedPictures.has and SnapToggle.snapping. Tests now check the
same things through equivalent paths.
- 38 values used only in their own file lost their export.
- Three comments that told history now describe the current state.
The bundle stayed md5-identical for those edits.
- README lists vector art, pixel art and palette, and links the two
wiki pages.
Findings:
- PaintTilesCommand.changes is meant to prevent empty undo steps but
has not been called since #128. The rectangle tool therefore records
a no-op undo step when it paints the same tile over itself. Left as
is, because fixing it changes behaviour.
- No test covered middle-click panning in any sheet tab before. 13f
and 48a now check it.
- The default values of the ray inputs were untested. Length 1000
could become 10 without a red test. tests/core/raycast.test.ts now
covers it.
- CI measured 1308 editor checks at d7c0b7c, not the 1307 counted for
#164. The sum is now 1310 (1308 + 2), computed.
Tests:
- ./check.sh: 144 files, 2143 tests, exit 0 (before 2142).
- e2e 1-16,37,39-43,47,48 fresh: ALL PASSED, 729 checks.
- e2e 1-13,47,48 with the new checks: ALL PASSED, 518 checks.
- Counter-checks, each restored via cp and cmp: halved panning fails
both new pan checks; unclamped mix fails 1 test, no blue channel 3;
coarser hash 3; broken reorder undo 2; triggers setting forced on 2;
bent ray length 1.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
83 lines
3.0 KiB
TypeScript
83 lines
3.0 KiB
TypeScript
/**
|
||
* The picture list of a tile (#118): which blocks it is drawn from, and in which order – the
|
||
* order is the animation, so moving a picture is an edit of its own rather than a cosmetic sort.
|
||
* The kind, the rate and the playback are ordinary properties and go through `SetAssetPropertyCommand`.
|
||
*/
|
||
import type { Project } from '../model/Project';
|
||
import { resolvePicture } from '../model/texture';
|
||
import { moveItem } from '../util/list';
|
||
import { type Command, CommandError, MoveCommand } from './Command';
|
||
import { requireTile } from './require';
|
||
|
||
/** Appends a picture; `at` puts it before the picture with that index instead. */
|
||
export class AddTilePictureCommand implements Command {
|
||
readonly label = 'Add tile picture';
|
||
private index = -1;
|
||
|
||
constructor(
|
||
private readonly assetId: string,
|
||
private readonly pictureId: string,
|
||
private readonly at?: number,
|
||
) {}
|
||
|
||
execute(project: Project): void {
|
||
const asset = requireTile(project, this.assetId);
|
||
// A texture or one of its frames – anything else names no block, and a list of dead ids is
|
||
// the one thing the tab could not explain to whoever built it.
|
||
if (!resolvePicture(project, this.pictureId))
|
||
throw new CommandError(`${this.pictureId} is not a texture or a frame`);
|
||
this.index = this.at === undefined ? asset.pictures.length : clampIndex(this.at, asset.pictures.length);
|
||
asset.pictures.splice(this.index, 0, this.pictureId);
|
||
}
|
||
|
||
undo(project: Project): void {
|
||
requireTile(project, this.assetId).pictures.splice(this.index, 1);
|
||
}
|
||
}
|
||
|
||
export class RemoveTilePictureCommand implements Command {
|
||
readonly label = 'Remove tile picture';
|
||
private removed = '';
|
||
|
||
constructor(
|
||
private readonly assetId: string,
|
||
private readonly index: number,
|
||
) {}
|
||
|
||
execute(project: Project): void {
|
||
const asset = requireTile(project, this.assetId);
|
||
const picture = asset.pictures[this.index];
|
||
if (picture === undefined) throw new CommandError(`${asset.name} has no picture ${this.index + 1}`);
|
||
this.removed = picture;
|
||
asset.pictures.splice(this.index, 1);
|
||
}
|
||
|
||
undo(project: Project): void {
|
||
requireTile(project, this.assetId).pictures.splice(this.index, 0, this.removed);
|
||
}
|
||
}
|
||
|
||
/** Moves one picture to another place in the list; the step in between is what changes the animation. */
|
||
export class MoveTilePictureCommand extends MoveCommand {
|
||
constructor(
|
||
private readonly assetId: string,
|
||
from: number,
|
||
to: number,
|
||
) {
|
||
super('Reorder tile pictures', from, to);
|
||
}
|
||
|
||
protected move(project: Project, from: number, to: number): void {
|
||
const asset = requireTile(project, this.assetId);
|
||
if (asset.pictures[from] === undefined)
|
||
throw new CommandError(`${asset.name} has no picture ${from + 1}`);
|
||
if (!moveItem(asset.pictures, from, to))
|
||
throw new CommandError(`${asset.name} has no place ${to + 1} to move it to`);
|
||
}
|
||
}
|
||
|
||
function clampIndex(index: number, length: number): number {
|
||
if (!Number.isFinite(index)) return length;
|
||
return Math.max(0, Math.min(length, Math.floor(index)));
|
||
}
|