Skip to content

Generate clean 2D game sprites & animation atlases — component-row pipeline: state rows, alpha cleanup, frame extraction, runtime atlases. Codex/Claude skill.

docs(ui-kit): the drift search, the accepted range and the two new refusals

Soohong Kim9b9af2c4f874
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 032b51a..51320ae 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,8 @@ All notable public changes to `sprite-gen` are recorded here. Versions track the
## Unreleased
+- **Fix: a drifted UI kit sheet with missing elements cuts each element under its own name instead of a neighbour's.** When a sheet was drawn shorter than its guide with elements left out, a drawn element's centroid could lie in an empty neighbouring widened box. The fit previously started from that wrong assignment and was kept only when it left fewer boxes empty; because the right assignment also left those boxes empty, the fit was dropped and the wrong assignment stood, naming a drawn part missing and an undrawn part present, and cutting a neighbour under a missing element's name. When the layout's own boxes leave a box empty, or a box holds parts whose bounding box is centred outside the box, `ui-kit` now searches a grid of 81 drifts along x and 81 along y (9 scales from 0.9 to 1.1 by 9 offsets from -96 to 96 px at 1024), fits each assignment, and scores it by how close each box's parts are to its centre. A box that holds a neighbour's element adds nothing, so the assignment that gives every drawing its own box scores highest. Repeating the fit from that best assignment until it converges finds the drift. The fit is kept unless the layout's own boxes assign every part as the fitted boxes do and each box's parts are centred in it. Along each axis, a cut accepts a scale from 0.9 to 1.1 and an offset of at most 96 px at 1024; a fit more than a grid step outside that range (a scale below 0.875 or above 1.125, or an offset beyond 120 px at 1024), or a box whose parts are centred in the gap beside it or further, refuses the sheet ([ui-kit](docs/ui-kit.md#a-drawing-that-drifted-from-the-layout)). A sheet whose every box holds parts centred in it is cut exactly as before; a sheet whose every box held parts but some of them centred outside their boxes is now fitted, with the same elements, and its report now has `layout_fit`.
+- **Tests** — synthetic sheets cover the fix: the reported 2:3 sheet (512 × 768 px, 6 % shorter, `hp_bar_fill`, `pip_off`, `joystick_knob` left out), tested through the CLI with the report outside `--out-dir` and through `game-pack`; the same three elements left out under squeezes from the top of 0.90 to 0.99; each of the 256 sets of band elements that can be left out, on a sheet drawn under a drift picked at random (seeded by the set) within the accepted range (a scale and an offset along x and along y), verifying that exactly the elements not drawn are missing, every drawn element is cut as its own drawing to within its 2 px edge, and a kept fit is the drift the sheet was drawn with to within a hundredth in scale and 4 px in offset; each frame left out under a random accepted drift is refused by its name; the lit dot drawn so far below its box that its centre lies in the gap (`pip_off` left out) is refused by the off-centre reason, naming `pip_on` when the centre is nearer the lit dot's box and `pip_off` when nearer the unlit dot's; a sheet drawn 20 % shorter is refused by the drift reason; and the constants match the documented range.
- **Fix: the test suite runs on Windows.** `pyproject.toml` named pytest-timeout's `signal` method, and Windows has no SIGALRM, so pytest stopped with an internal error before its first test unless `-o timeout_method=thread` was given. The method is now left to pytest-timeout, which takes `signal` where SIGALRM exists (Linux, macOS: unchanged) and `thread` on Windows. Test: `tests/packaging/test_project_metadata.py`. `tests/video/test_one_step_cut.py` skips where img2webp is missing, as the other loop tests do, instead of failing all twelve of its tests.
- **Fix: `sprite-gen prepare --request` keeps the request's `style`.** The subcommand declared its own copy of prepare's options, with `--style` defaulting to the default style sentence and `--cell-size` to 256, so a flag nobody gave counted as given: the request's `style` was replaced by the default sentence under a note that `--style, --cell-size` were given both ways (`scripts/prepare_sprite_run.py` and `prepare.run` were right). The subcommand now takes prepare's own declaration (`prepare.add_arguments`), which also gives it `--directions` and `--mirror`, missing until now. A request with no style and no cell still gets the default sentence and 256. Test: `tests/gen/test_flags_over_spec.py` (through `sprite-gen prepare`: a request alone keeps its style and cell with no note, a given `--style` wins and says so, the subcommand takes every option prepare declares).
- **Behavior change: `pixel-draw` runs on your Claude subscription first, and on `ANTHROPIC_API_KEY` only without one** — the order `codex` and `grok` keep. When `claude auth status` says Claude Code is signed in with a subscription (`claude.ai` or `oauth_token`), every call — the drawings, the row repairs, the pick — goes through the unmodified `claude` binary in print mode, from an empty directory, with no tool, no session written and no CLAUDE.md, hook, skill, plugin or MCP server loaded, and costs nothing per call: `report.json` says `auth_source: claude-login`, each call's `mode` is `subscription` and its `cost_usd` `0`, its usage kept. The child never sees `ANTHROPIC_API_KEY`, which print mode would use instead of the login. Without a subscription login the run uses the key as before, billed per call and said on stderr; with neither it is refused naming both, before the run folder is made. A login that refuses a call, has expired or has reached its usage limit fails the call; the run never switches to the key on its own. `--batch` is the API's Message Batches and runs on the key even when signed in. An answer the binary continues past the output cap is read whole from every message, not from its `result`, which holds only the last part; one it cannot finish is read as `stop_reason: max_tokens`, so the one redraw one effort lower happens on either route. One call may take up to `SPRITE_GEN_CLAUDE_CALL_TIMEOUT_SECONDS` (1800). When candidates ask at once, none of their requests leaves before the billing line has been printed. `report.json`'s `auth_source` is what the calls ran on by their records, `mixed` (with `auth_sources`) for a run resumed under the other credential. A `--resume` that reads every call back no longer prints the billing line ([pixel-draw](docs/pixel-draw.md#which-credential-a-call-runs-on)).
diff --git a/docs/ui-kit.md b/docs/ui-kit.md
index cc23730..eabb875 100644
--- a/docs/ui-kit.md
+++ b/docs/ui-kit.md
@@ -71,24 +71,35 @@ The steps of the cut:
1. **Alpha cleanup.** Faint haze between elements would join them, so it is cleared first (below).
2. **Parts.** Pixels with alpha above zero form 8-connected parts. A part smaller than 16 pixels is dropped and counted.
-3. **Assignment.** Each part goes to the box that contains its centroid, with every box widened by half the gap (24 px at 1024). When that leaves a box empty, the layout is [fitted to the drawing](#a-drawing-that-drifted-from-the-layout) and the parts are assigned again. A part whose centroid lies in no widened box is reported under `unassigned`; it makes the report `needs-review` when it has at least 16 pixels.
+3. **Assignment.** Each part goes to the box that contains its centroid, with every box widened by half the gap (24 px at 1024). When that leaves a box empty, or a box holds parts whose bounding box is centred outside the box, the layout is [fitted to the drawing](#a-drawing-that-drifted-from-the-layout) and the parts are assigned again. A part whose centroid lies in no widened box is reported under `unassigned`; it makes the report `needs-review` when it has at least 16 pixels.
4. **Crop.** An element is the bounding box of its box's parts, so ornaments that reach past the box stay whole. A box with no part fails the command when its element is required (the sheet has to be generated again), and leaves its element out when it is optional ([missing elements](#missing-elements)).
5. **Insets** and **padding** are measured on the cropped element (below).
### A drawing that drifted from the layout
-An image model may draw the layout a little off. The whole drawing can come out a few percent shorter than the guide, and then the lower an element is, the farther it sits above its box, until its centroid lies in the widened box above its own: that box holds two elements, and the element's own box none. So, when a box is left empty:
+An image model may draw the layout a little off. The whole drawing can come out a few percent shorter than the guide, and then the lower an element is, the farther it sits above its box, until its centroid lies in the widened box above its own: that box holds two elements, and the element's own box none. So, when the layout's own boxes leave a box empty, or a box holds parts whose bounding box is centred outside the box:
-1. **Fit.** Along x and along y, take the scale and offset that move the layout's box centres closest (least squares) to the centres of what the boxes hold (the bounding box of each box's parts). Along an axis on which every box that holds parts is centred on one line, take an offset alone.
-2. **Assign again.** Move every box by the fit, its position and its size, and its widening by half the gap with it, and assign the parts to the moved boxes.
-3. Repeat from the new assignment until it comes out the same, at most 8 rounds. Fewer than 3 boxes that hold parts, or a scale that is not positive, end it without a fit.
-4. **Keep it or not.** The fitted assignment replaces the layout's own when it leaves fewer required boxes empty, or as many and fewer boxes in all.
+1. **Search.** The layout's boxes are moved by each drift of a grid over the range a cut accepts. Along each axis the grid has 9 scales, from 0.9 to 1.1, by 9 offsets, from -96 to 96 px at 1024 (81 drifts along x times 81 along y). For each drift the parts are assigned to the moved boxes as in step 3 of the cut (by centroid, each box widened by half the gap, the widening scaled with the box). Each such assignment is fitted (as in the Fit step) and scored: each box that holds parts adds 1 - d, where d is how far the centre of its parts' bounding box lies from the centre of its fitted box, as a share of half the box's width or height (the larger of the two shares), and nothing when that centre lies on the box's edge or outside it. A box that holds a neighbour's element so adds nothing, and the assignment that gives every drawing its own box scores highest. An assignment that holds fewer than 3 boxes is not scored.
+2. **Fit.** Along x and along y, take the scale and offset that move the layout's box centres closest (least squares) to the centres of what the boxes hold (the bounding box of each box's parts). Along an axis on which every box that holds parts is centred on one line, take an offset alone.
+3. **Assign again.** Move every box by the fit, its position and its size, and its widening by half the gap with it, and assign the parts to the moved boxes.
+4. Repeat from the Fit, starting from the search's best assignment, until the assignment comes out the same, at most 8 rounds. Fewer than 3 boxes that hold parts, or a scale that is not positive, end it without a fit. (If no drift of the grid gives an assignment of 3 boxes, there is no fit; then so many frames are empty that the sheet is refused for its empty frames.)
+5. **Keep it or not.** The layout's own boxes stand when they assign every part as the fitted boxes do and each box's parts are centred in it (then the report has no `layout_fit` and each `box` is the layout's). Otherwise the fit is kept: the report has `layout_fit` and each `box` is the fitted box.
-A sheet whose every box holds its element is never fitted, so it is cut exactly as before. A kept fit is in the report as `layout_fit`, and each element's `box` is then its fitted box. The tests draw a 2:3 sheet 7 % shorter than its guide, from the top edge down. Assigned to the layout's own boxes, three boxes are left empty (`badge`, `pip_off` and `joystick_knob`), two hold two elements each, and the upper fill lies in no box; the fitted boxes find all fifteen elements whole.
+Along each axis, the drift a cut accepts is a scale from 0.9 to 1.1 and an offset of at most 96 px at 1024 (the offset is in the sheet's pixels: 96 px on a 1024 px wide sheet, 48 px on a 512 px wide one). A drawing drawn at the edge of that range, with its elements a little off their boxes' centres, is fitted a little to either side of it, so a kept fit is refused only when it lies more than one step of the grid outside the range: a scale below 0.875 or above 1.125, or an offset beyond 120 px at 1024, along either axis.
+
+Refusals happen in this order:
+
+* The drift refusal above: `the drawing drifted further from the layout than a cut accepts: fitted to it, the layout is scaled 1.000 along x and 0.800 along y and offset 0.0, 0.0 px, where a cut accepts a scale from 0.9 to 1.1 and an offset of at most 96 px along each axis; generate the sheet again`.
+* The required-frame refusal: `<element>: its box holds no element; generate the sheet again`.
+* A box whose parts' bounding box is centred outside the box, that is in the gap beside it or further, where the drawing does not say which element they are: `pip_on: its box holds a part centred outside it (in the gap beside it or further, so the drawing does not say which element it is); generate the sheet again`, and for several boxes `pip_on, badge: their boxes hold parts centred outside them (in the gap beside them or further, so the drawing does not say which elements they are); generate the sheet again`.
+
+In every case `ui-kit` writes nothing and fails.
+
+A sheet whose every box holds parts centred in it is never fitted, so it is cut exactly as before. A sheet whose every box held parts but some of them centred outside their boxes (the lower elements drawn higher than their boxes, each still in its own widened box) is now fitted: its elements are the same, and its report now has `layout_fit`. The tests draw a 2:3 sheet 7 % shorter than its guide, from the top edge down: the fitted boxes find all fifteen elements whole. Each of the 256 sets of band elements that can be left out, on a sheet drawn under a drift picked at random within the accepted range, names exactly the elements not drawn as missing and cuts every drawn element as itself.
## Missing elements
-The seven frames are the kit, and each is required: `panel`, `button`, `button_pressed`, `card`, `hp_bar_frame`, `xp_bar_frame` and `timer_badge`. When a frame's box is empty, after the fit, `ui-kit` writes nothing and fails with `<element>: its box holds no element; generate the sheet again`, or, for several, `panel, card: their boxes hold no element; generate the sheet again`.
+The seven frames are the kit, and each is required: `panel`, `button`, `button_pressed`, `card`, `hp_bar_frame`, `xp_bar_frame` and `timer_badge`. When a frame's box is empty, after the fit, `ui-kit` writes nothing and fails with `<element>: its box holds no element; generate the sheet again`, or, for several, `panel, card: their boxes hold no element; generate the sheet again`. When the layout is [fitted](#a-drawing-that-drifted-from-the-layout), a box that holds a neighbour's element is never taken for the missing element.
The eight elements of the 2:3 band are optional: the two fills, the slot, the badge, the two dots and the joystick base and knob. When one's box is empty, the kit is made without it: