Skip to content

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

test: ui-kit tall 2:3 sheet cuts bar fills, a slot, a badge, pips and a joystick; fills fit their frame's content area in the report and the pack; square sheets frozen from before (red)

Soohong Kim32261bd840c6
diff --git a/tests/compose/test_game_pack_v2.py b/tests/compose/test_game_pack_v2.py
index 0f34b8d..8d489e6 100644
--- a/tests/compose/test_game_pack_v2.py
+++ b/tests/compose/test_game_pack_v2.py
@@ -9,6 +9,7 @@ import hashlib
import json
import math
+import numpy as np
import pytest
from PIL import Image, ImageDraw
@@ -273,6 +274,98 @@ def test_a_ui_kit_report_must_be_a_ui_kit_report(tmp_path):
game_pack.pack(request, tmp_path / "out")
+def fill_strip(path, size, cap):
+ """A bar fill: a green strip with a lighter end cap at each end."""
+ image = Image.new("RGBA", size, (40, 180, 90, 255))
+ draw = ImageDraw.Draw(image)
+ draw.rectangle((0, 0, cap - 1, size[1] - 1), fill=(200, 250, 210, 255))
+ draw.rectangle((size[0] - cap, 0, size[0] - 1, size[1] - 1), fill=(200, 250, 210, 255))
+ image.save(path)
+ return image
+
+
+def fill_kit(tmp_path, *, scale=1, inside="hp_bar_frame", stretch="x", cap=3, size=(40, 8)):
+ """A report with a bar frame (content area 32 x 6 at scale 1) and a fill drawn inside it."""
+ ui_kit_report(tmp_path / "ui")
+ report = json.loads((tmp_path / "ui" / "ui-kit.json").read_text())
+ fill_strip(tmp_path / "ui" / "hp_bar_fill.png", size, cap)
+ record = {"file": "hp_bar_fill.png", "box": {"x": 0, "y": 0, "w": 64, "h": 16},
+ "rect": {"x": 1, "y": 1, "w": size[0], "h": size[1]}, "size": {"w": size[0], "h": size[1]},
+ "stretch": stretch, "insets": {"left": cap + 2, "right": cap + 2, "top": 0, "bottom": 0},
+ "padding": {"left": 0, "right": 0, "top": 0, "bottom": 0}, "parts": 1}
+ if inside is not None:
+ record["inside"] = {"element": inside, "rect": {"x": 10, "y": 3, "w": 32, "h": 6}, "scale": 0.75}
+ report["elements"]["hp_bar_fill"] = record
+ write_json(tmp_path / "ui" / "ui-kit.json", report)
+ return write_json(tmp_path / "game.json", {"version": 2, "ui": {"kit": "ui/ui-kit.json", "scale": scale}})
+
+
[email protected]("scale", [1, 0.5, 2])
+def test_a_fill_is_stored_at_its_frames_content_area(tmp_path, scale):
+ out = tmp_path / "out"
+ result = game_pack.pack(fill_kit(tmp_path, scale=scale), out)
+ nine = result["ui"]["nine_slice"]
+ frame, fill = nine["hp_bar_frame"], nine["hp_bar_fill"]
+ # the frame is packed as before; the fill's content rectangle is the frame's, in the frame's stored pixels
+ assert set(frame) == {"frame", "size", "stretch", "insets", "padding"}
+ pad = frame["padding"]
+ rect = {"x": pad["left"], "y": pad["top"], "w": frame["size"]["w"] - pad["left"] - pad["right"],
+ "h": frame["size"]["h"] - pad["top"] - pad["bottom"]}
+ assert set(fill) == {"frame", "size", "stretch", "insets", "padding", "inside"}
+ assert fill["frame"] == "ui/hp_bar_fill" and fill["stretch"] == "x"
+ assert fill["inside"] == {"element": "hp_bar_frame", "rect": rect}
+ assert fill["size"] == {"w": rect["w"], "h": rect["h"]}
+ assert fill["padding"] == {"left": 0, "right": 0, "top": 0, "bottom": 0}
+ # its ends are the fill's own, resized by the one factor that makes it as tall as the content area
+ factor = rect["h"] / 8
+ resized = resize_cell(Image.open(tmp_path / "ui" / "hp_bar_fill.png").convert("RGBA"),
+ (round(40 * factor), rect["h"]))
+ measured, _ = measure_insets(resized, "x")
+ insets = {side: max(math.ceil(value * factor), measured[side])
+ for side, value in {"left": 5, "right": 5, "top": 0, "bottom": 0}.items()}
+ assert fill["insets"] == insets
+ image, record = crop(out, result, "ui/hp_bar_fill")
+ assert image.size == (rect["w"], rect["h"]) and record["pivot"] == {"x": 0.5, "y": 0.5}
+ left, right = insets["left"], insets["right"]
+ stored, source = np.asarray(image), np.asarray(resized)
+ assert np.array_equal(stored[:, :left], source[:, :left])
+ assert np.array_equal(stored[:, rect["w"] - right:], source[:, resized.width - right:])
+ # the middle is the fill's own middle columns, repeated or dropped, never blended
+ middle = {tuple(map(tuple, source[:, x])) for x in range(left, resized.width - right)}
+ assert all(tuple(map(tuple, stored[:, x])) in middle for x in range(left, rect["w"] - right))
+
+
+def test_a_report_without_fills_packs_as_before(tmp_path):
+ ui_kit_report(tmp_path / "ui")
+ request = write_json(tmp_path / "game.json", {"version": 2, "ui": {"kit": "ui/ui-kit.json", "scale": 0.5}})
+ result = game_pack.pack(request, tmp_path / "out")
+ assert all(set(entry) == {"frame", "size", "stretch", "insets", "padding"}
+ for entry in result["ui"]["nine_slice"].values())
+
+
[email protected]("change,message", [
+ ({"inside": "mana_bar_frame"}, "hp_bar_fill: inside names mana_bar_frame, which is not a frame of this report"),
+ ({"inside": "hp_bar_fill"}, "hp_bar_fill: inside names hp_bar_fill, which is not a frame of this report"),
+ ({"stretch": "xy"}, "hp_bar_fill: a fill stretches in width only"),
+ # 4 px tall: scaled 1.5x to the 6 px content area, its 11 px ends are 17 px each, 34 of the 32 px it has
+ ({"cap": 9, "size": (40, 4)}, "hp_bar_fill: scaled to the height of hp_bar_frame's content area, its fixed ends"),
+])
+def test_bad_fills_are_refused(tmp_path, change, message):
+ request = fill_kit(tmp_path, **change)
+ with pytest.raises(ValueError, match=message):
+ game_pack.pack(request, tmp_path / "out")
+ assert not (tmp_path / "out").exists()
+
+
+def test_a_fill_must_name_its_frame(tmp_path):
+ request = fill_kit(tmp_path)
+ report = json.loads((tmp_path / "ui" / "ui-kit.json").read_text())
+ report["elements"]["hp_bar_fill"]["inside"] = {"rect": {"x": 10, "y": 3, "w": 32, "h": 6}}
+ write_json(tmp_path / "ui" / "ui-kit.json", report)
+ with pytest.raises(ValueError, match="hp_bar_fill: inside must name the element"):
+ game_pack.pack(request, tmp_path / "out")
+
+
def test_version_1_refuses_the_version_2_sections(tmp_path):
tile(tmp_path / "ground.png")
for section, value in (("images", []), ("tiles", []), ("ui", {})):
diff --git a/tests/frames/test_ui_kit.py b/tests/frames/test_ui_kit.py
index 919f0e3..96e4e64 100644
--- a/tests/frames/test_ui_kit.py
+++ b/tests/frames/test_ui_kit.py
@@ -6,8 +6,16 @@ ornaments (one with a long thin flourish along its top edge), a bar frame with a
taller than the bar, a round badge, light colour noise, a faint haze ring around every
element and loose specks. Insets are checked to the pixel: every ornament column and row
lies inside a fixed slice, and nothing more than the 2 px margin does.
+
+A tall (2:3) sheet holds the same seven frames at the same places and, in the band below them,
+two bar fills with end caps of their own colour and a top-to-bottom shade, a slot and a counter
+plate with corner ornaments, two pips and a joystick base and knob drawn as discs. A fill fits
+its bar frame's content area exactly, in the report and in the pack; round parts never stretch.
+A square sheet's report, element pixels and packs are frozen from the splitter before the tall
+sheet existed.
"""
+import hashlib
import json
import math
@@ -18,14 +26,21 @@ from PIL import Image
from sprite_gen import cli
from sprite_gen.compose import game_pack
from sprite_gen.frames import ui_kit
+from sprite_gen.util.resample import resize_cell
SIZE = 512
+TALL = (SIZE, SIZE * 3 // 2)
BORDER = (200, 200, 220, 255)
ORNAMENT = (120, 220, 200, 255)
FLOURISH = (255, 180, 60, 255)
INTERIOR = (20, 30, 60, 210)
TRACK = (10, 10, 20, 180)
GEM = (220, 40, 80, 255)
+FILL = (60, 200, 120, 255)
+FILL_CAP = (200, 255, 220, 255)
+PIP_ON = (255, 220, 90, 255)
+PIP_OFF = (70, 60, 50, 255)
+KNOB = (180, 190, 210, 255)
def fill(pixels, x0, y0, x1, y1, color):
@@ -59,25 +74,52 @@ def draw_badge(pixels, x, y, d):
region[r <= d / 2 - 3] = INTERIOR
+def draw_fill(pixels, x, y, w, h, cap):
+ """A bar fill: one colour along its length, shaded from top to bottom, with an end cap of its own colour."""
+ shade = np.linspace(1.0, 0.6, h)[:, None]
+ region = pixels[y:y + h, x:x + w]
+ region[..., 3] = 255
+ for channel in range(3):
+ region[..., channel] = (FILL[channel] * shade).round().astype(np.uint8)
+ fill(pixels, x, y, x + cap, y + h, FILL_CAP)
+ fill(pixels, x + w - cap, y, x + w, y + h, FILL_CAP)
+
+
+def draw_disc(pixels, x, y, d, color, ring=0):
+ yy, xx = np.indices((d, d))
+ r = np.hypot(xx - d / 2 + 0.5, yy - d / 2 + 0.5)
+ region = pixels[y:y + d, x:x + d]
+ region[r <= d / 2] = BORDER if ring else color
+ region[r <= d / 2 - ring] = color
+
+
# element -> (offset inside its box, drawing parameters); every element sits 4 px inside its box
PLAN = {"panel": dict(k=18, flourish=60), "button": dict(k=12), "button_pressed": dict(k=12),
"card": dict(k=16), "hp_bar_frame": dict(radius=20, cap=10), "xp_bar_frame": dict(radius=20, cap=10),
- "timer_badge": dict()}
+ "timer_badge": dict(), "hp_bar_fill": dict(cap=6), "xp_bar_fill": dict(cap=6),
+ "slot": dict(k=14), "badge": dict(k=10), "pip_on": dict(color=PIP_ON, ring=3),
+ "pip_off": dict(color=PIP_OFF, ring=3), "joystick_base": dict(color=INTERIOR, ring=6),
+ "joystick_knob": dict(color=KNOB)}
-def synthetic_sheet(*, haze=True, noise=True, stray=False, missing=None, crest=False):
- pixels = np.zeros((SIZE, SIZE, 4), dtype=np.uint8)
+def synthetic_sheet(*, size=(SIZE, SIZE), haze=True, noise=True, stray=False, missing=None, crest=False):
+ pixels = np.zeros((size[1], size[0], 4), dtype=np.uint8)
drawn = {}
- for name, (bx, by, bw, bh) in ui_kit.boxes((SIZE, SIZE)).items():
+ for name, (bx, by, bw, bh) in ui_kit.boxes(size).items():
if name == missing:
continue
x, y, w, h = bx + 4, by + 4, bw - 8, bh - 8
params = PLAN[name]
if name.endswith("bar_frame"):
draw_bar(pixels, x, y, w, h, **params)
+ elif name.endswith("bar_fill"):
+ draw_fill(pixels, x, y, w, h, **params)
elif name == "timer_badge":
w = h = min(w, h)
draw_badge(pixels, x, y, w)
+ elif name.startswith(("pip_", "joystick_")):
+ w = h = min(w, h)
+ draw_disc(pixels, x, y, w, **params)
else:
draw_frame(pixels, x, y, w, h, **params)
if crest and name == "card":
@@ -114,24 +156,45 @@ def offset(report, drawn, name):
return x - rect["x"], y - rect["y"], rect["x"] + rect["w"] - (x + w), rect["y"] + rect["h"] - (y + h)
+FRAMES = ["panel", "button", "button_pressed", "card", "hp_bar_frame", "xp_bar_frame", "timer_badge"]
+BAND = ["hp_bar_fill", "xp_bar_fill", "slot", "badge", "pip_on", "pip_off", "joystick_base", "joystick_knob"]
+
+
def test_layout_is_the_documented_table_and_scales_with_the_sheet():
- assert [entry.name for entry in ui_kit.LAYOUT] == [
- "panel", "button", "button_pressed", "card", "hp_bar_frame", "xp_bar_frame", "timer_badge"]
+ assert [entry.name for entry in ui_kit.LAYOUT] == FRAMES + BAND
full = ui_kit.boxes((1024, 1024))
+ assert list(full) == FRAMES
assert full["panel"] == (48, 48, 576, 400)
assert full["card"] == (704, 400, 272, 576)
assert full["timer_badge"] == (48, 816, 160, 160)
assert ui_kit.boxes((512, 512))["card"] == (352, 200, 136, 288)
assert {e.name: e.stretch for e in ui_kit.LAYOUT} == {
"panel": "xy", "button": "xy", "button_pressed": "xy", "card": "xy",
- "hp_bar_frame": "x", "xp_bar_frame": "x", "timer_badge": "none"}
- # boxes never touch: 48 px apart at 1024
- names = list(full)
- for i, a in enumerate(names):
- for b in names[i + 1:]:
- ax, ay, aw, ah = full[a]
- bx, by, bw, bh = full[b]
- assert ax + aw + 48 <= bx or bx + bw + 48 <= ax or ay + ah + 48 <= by or by + bh + 48 <= ay
+ "hp_bar_frame": "x", "xp_bar_frame": "x", "timer_badge": "none",
+ "hp_bar_fill": "x", "xp_bar_fill": "x", "slot": "xy", "badge": "xy",
+ "pip_on": "none", "pip_off": "none", "joystick_base": "none", "joystick_knob": "none"}
+ # the 2:3 sheet keeps the seven frames where the square sheet has them and adds the band below
+ tall = ui_kit.boxes((1024, 1536))
+ assert list(tall) == FRAMES + BAND
+ assert {name: tall[name] for name in FRAMES} == full
+ assert tall["hp_bar_fill"] == (48, 1024, 608, 64)
+ assert tall["xp_bar_fill"] == (48, 1136, 608, 64)
+ assert tall["slot"] == (48, 1248, 192, 192)
+ assert tall["badge"] == (288, 1248, 176, 112)
+ assert tall["pip_on"] == (512, 1248, 96, 96)
+ assert tall["pip_off"] == (512, 1392, 96, 96)
+ assert tall["joystick_base"] == (704, 1024, 272, 272)
+ assert tall["joystick_knob"] == (768, 1344, 144, 144)
+ assert ui_kit.boxes(TALL)["joystick_base"] == (352, 512, 136, 136)
+ # boxes never touch: 48 px apart at 1024, and 48 px in from the sheet's edges
+ for layout, (width, height) in ((full, (1024, 1024)), (tall, (1024, 1536))):
+ names = list(layout)
+ for i, a in enumerate(names):
+ ax, ay, aw, ah = layout[a]
+ assert ax >= 48 and ay >= 48 and ax + aw + 48 <= width and ay + ah + 48 <= height, a
+ for b in names[i + 1:]:
+ bx, by, bw, bh = layout[b]
+ assert ax + aw + 48 <= bx or bx + bw + 48 <= ax or ay + ah + 48 <= by or by + bh + 48 <= ay
def test_frame_insets_cover_every_corner_ornament_and_the_long_flourish():
@@ -222,9 +285,10 @@ def test_an_empty_box_fails():
ui_kit.split_ui_kit(image)
-def test_a_sheet_that_is_not_square_fails():
- with pytest.raises(ValueError, match="square"):
- ui_kit.split_ui_kit(Image.new("RGBA", (512, 400)))
+def test_a_sheet_that_is_neither_square_nor_2_3_fails():
+ for size in ((512, 400), (512, 900), (768, 512)):
+ with pytest.raises(ValueError, match=r"square \(1:1.*2:3"):
+ ui_kit.split_ui_kit(Image.new("RGBA", size))
def test_cli_writes_element_pngs_and_a_report_the_pack_reads(tmp_path):
@@ -238,8 +302,8 @@ def test_cli_writes_element_pngs_and_a_report_the_pack_reads(tmp_path):
report = json.loads((out / "ui-kit.json").read_text())
assert report["kind"] == "sprite-gen-ui-kit" and report["version"] == 1
assert report["sheet"] == str(sheet.resolve()) and report["sheet_size"] == [SIZE, SIZE]
- assert list(report["elements"]) == [entry.name for entry in ui_kit.LAYOUT]
- assert sorted(p.name for p in out.iterdir()) == sorted([f"{e.name}.png" for e in ui_kit.LAYOUT] + ["ui-kit.json"])
+ assert list(report["elements"]) == FRAMES
+ assert sorted(p.name for p in out.iterdir()) == sorted([f"{name}.png" for name in FRAMES] + ["ui-kit.json"])
cleaned, _ = ui_kit.clear_haze(image.convert("RGBA"), floor=64, edge=2)
for name, entry in report["elements"].items():
assert entry["file"] == f"{name}.png"
@@ -296,7 +360,7 @@ def test_a_report_anywhere_names_the_written_pngs_and_packs_the_same(tmp_path):
request.write_text(json.dumps({"version": 2, "ui": {"kit": report_path.relative_to(root).as_posix()}}))
assert cli.main(["game-pack", "--request", str(request), "--out-dir", str(root / "pack")]) == 0
packs.append({path.name: path.read_bytes() for path in (root / "pack").iterdir()})
- assert len(json.loads(packs[0]["game-pack.json"])["ui"]["nine_slice"]) == len(ui_kit.LAYOUT)
+ assert len(json.loads(packs[0]["game-pack.json"])["ui"]["nine_slice"]) == len(FRAMES)
assert packs == [packs[0]] * len(REPORT_PLACES)
@@ -324,15 +388,206 @@ def test_guide_draws_every_box_and_the_text_names_every_element(tmp_path):
assert cell == {"kind": "ui-kit", "width": 1024, "height": 1024,
"boxes": {name: dict(zip("xywh", box)) for name, box in ui_kit.boxes((1024, 1024)).items()}}
text = ui_kit.guide_text()
- positions = [text.index(entry.what) for entry in ui_kit.LAYOUT]
+ assert ui_kit.guide_text((1024, 1024)) == text
+ positions = [text.index(entry.what) for entry in ui_kit.LAYOUT[:len(FRAMES)]]
assert positions == sorted(positions)
assert "last attached image is a layout guide" in text
assert "no text" in text
+ assert not any(entry.what in text for entry in ui_kit.LAYOUT[len(FRAMES):])
+
+
+def test_tall_guide_draws_the_square_boxes_and_the_band_and_names_every_element(tmp_path):
+ path = tmp_path / "guide.png"
+ cell = ui_kit.draw_guide(path, (683, 1024))
+ with Image.open(path) as guide:
+ guide = guide.convert("RGB")
+ assert guide.size == (683, 1024)
+ for x, y, w, h in ui_kit.boxes((683, 1024)).values():
+ assert guide.getpixel((x + w // 2, y)) == ui_kit.GUIDE_BOX
+ assert guide.getpixel((x, y + h // 2)) == ui_kit.GUIDE_BOX
+ assert guide.getpixel((x + w // 2, y + h // 2)) == ui_kit.GUIDE_BACKGROUND
+ assert cell == {"kind": "ui-kit", "width": 683, "height": 1024,
+ "boxes": {name: dict(zip("xywh", box)) for name, box in ui_kit.boxes((683, 1024)).items()}}
+ assert list(cell["boxes"]) == FRAMES + BAND
+ text = ui_kit.guide_text((683, 1024))
+ assert text == ui_kit.guide_text((1024, 1536))
+ positions = [text.index(entry.what) for entry in ui_kit.LAYOUT]
+ assert positions == sorted(positions)
+ assert "15 blue boxes" in text and "last attached image is a layout guide" in text
+ assert "no text" in text and "round" in text
+
+
+def tall_split(**kwargs):
+ return split(size=TALL, **kwargs)
+
+
+def test_tall_sheet_cuts_every_element_of_the_whole_kit():
+ _, drawn, elements, report = tall_split()
+ assert report["status"] == "pass", report["review_reasons"]
+ assert report["sheet_size"] == list(TALL)
+ assert list(report["elements"]) == list(elements) == FRAMES + BAND
+ assert report["unassigned"] == [] and report["dropped_parts"] == 0
+ for name in FRAMES + BAND:
+ entry = report["elements"][name]
+ assert entry["file"] == f"{name}.png" and entry["parts"] == 1
+ assert entry["box"] == dict(zip("xywh", ui_kit.boxes(TALL)[name]))
+ assert elements[name].size == (entry["size"]["w"], entry["size"]["h"])
+ # the cut is the drawn element and at most its kept two-pixel haze edge
+ assert all(0 <= value <= 2 for value in offset(report, drawn, name)), name
+def test_tall_sheet_cuts_the_seven_frames_as_the_square_sheet_does():
+ _, _, square_elements, square = split(noise=False)
+ _, _, tall_elements, tall = tall_split(noise=False)
+ for name in FRAMES:
+ assert tall["elements"][name] == square["elements"][name]
+ assert tall_elements[name].tobytes() == square_elements[name].tobytes()
+
+
+def test_fills_stretch_in_width_and_fit_the_content_area_of_their_bar_frame():
+ _, drawn, elements, report = tall_split()
+ for fill, frame in (("hp_bar_fill", "hp_bar_frame"), ("xp_bar_fill", "xp_bar_frame")):
+ entry, bar = report["elements"][fill], report["elements"][frame]
+ left, top, right, bottom = offset(report, drawn, fill)
+ cap = PLAN[fill]["cap"]
+ assert entry["stretch"] == "x"
+ assert entry["insets"] == {"left": left + cap + 2, "right": right + cap + 2, "top": 0, "bottom": 0}
+ # a fill holds no content: its padding is 0, as a frame's insets are 0 along an axis it keeps
+ assert entry["padding"] == {"left": 0, "right": 0, "top": 0, "bottom": 0}
+ pad, size = bar["padding"], bar["size"]
+ rect = {"x": pad["left"], "y": pad["top"], "w": size["w"] - pad["left"] - pad["right"],
+ "h": size["h"] - pad["top"] - pad["bottom"]}
+ assert entry["inside"] == {"element": frame, "rect": rect, "scale": round(rect["h"] / entry["size"]["h"], 6)}
+ # scaled by `scale`, the fill is as tall as the content area to the pixel and its two ends fit inside it
+ assert abs(entry["size"]["h"] * entry["inside"]["scale"] - rect["h"]) < 0.01
+ assert (entry["insets"]["left"] + entry["insets"]["right"]) * entry["inside"]["scale"] < rect["w"]
+ for name in FRAMES + BAND:
+ if name not in ("hp_bar_fill", "xp_bar_fill"):
+ assert "inside" not in report["elements"][name], name
+
+
+def test_pips_and_joystick_parts_never_stretch_and_keep_their_round_shape():
+ _, drawn, elements, report = tall_split()
+ for name in ("pip_on", "pip_off", "joystick_base", "joystick_knob"):
+ entry = report["elements"][name]
+ assert entry["stretch"] == "none"
+ assert entry["insets"] == entry["padding"] == {"left": 0, "right": 0, "top": 0, "bottom": 0}
+ d = drawn[name][2]
+ assert d == drawn[name][3]
+ assert d <= entry["size"]["w"] <= d + 4 and d <= entry["size"]["h"] <= d + 4
+ assert abs(entry["size"]["w"] - entry["size"]["h"]) <= 2, name
+ # the two pips are drawn in boxes of one size, so one dot swaps for the other in place
+ assert ui_kit.boxes(TALL)["pip_on"][2:] == ui_kit.boxes(TALL)["pip_off"][2:]
+
+
+def test_slot_and_badge_insets_cover_their_corner_ornaments_and_padding_keeps_content_off_them():
+ _, drawn, elements, report = tall_split()
+ for name in ("slot", "badge"):
+ entry = report["elements"][name]
+ left, top, right, bottom = offset(report, drawn, name)
+ k = PLAN[name]["k"]
+ assert entry["stretch"] == "xy"
+ assert entry["insets"] == {"left": left + k + 2, "right": right + k + 2,
+ "top": top + k + 2, "bottom": bottom + k + 2}, name
+ for side, edge in (("left", left), ("right", right), ("top", top), ("bottom", bottom)):
+ assert edge + k <= entry["padding"][side] <= edge + k + 4, (name, side, entry["padding"])
+
+
+def test_an_empty_box_of_the_band_fails():
+ image, _ = synthetic_sheet(size=TALL, missing="joystick_knob")
+ with pytest.raises(ValueError, match="joystick_knob"):
+ ui_kit.split_ui_kit(image)
+
+
+def test_a_fill_whose_ends_do_not_fit_its_bar_frame_is_flagged():
+ image, drawn = synthetic_sheet(size=TALL, noise=False, haze=False)
+ pixels = np.asarray(image).copy()
+ # end caps 112 px long on a 296 px fill, short of its middle fifth: scaled to the 26 px track's height
+ # (the fill is 24 px tall), the two fixed ends are 247 px wide, and the track is 242 px long
+ x, y, w, h = drawn["xp_bar_fill"]
+ fill(pixels, x, y, x + 112, y + h, FILL_CAP)
+ fill(pixels, x + w - 112, y, x + w, y + h, FILL_CAP)
+ _, report = ui_kit.split_ui_kit(Image.fromarray(pixels))
+ entry = report["elements"]["xp_bar_fill"]
+ assert entry["insets"]["left"] == entry["insets"]["right"] == 114
+ rect = entry["inside"]["rect"]
+ assert (entry["insets"]["left"] + entry["insets"]["right"]) * entry["inside"]["scale"] >= rect["w"]
+ assert report["status"] == "needs-review"
+ assert [reason for reason in report["review_reasons"] if reason.startswith("xp_bar_fill:")] == [
+ "xp_bar_fill: scaled to the height of xp_bar_frame's content area, its fixed ends are wider than that "
+ "area; a full bar would overflow it"]
+
+
+def pack_tall_kit(tmp_path, scale, **kwargs):
+ image, drawn = synthetic_sheet(size=TALL, **kwargs)
+ sheet = tmp_path / "uikit.png"
+ image.save(sheet)
+ assert ui_kit.main(["--sheet", str(sheet), "--out-dir", str(tmp_path / "ui")]) == 0
+ request = tmp_path / "game.json"
+ request.write_text(json.dumps({"version": 2, "ui": {"kit": "ui/ui-kit.json", "scale": scale}}))
+ out = tmp_path / "pack"
+ return game_pack.pack(request, out), out
+
+
+def stored_frame(out, result, frame):
+ for page in result["textures"]:
+ if frame in page["frames"]:
+ box = page["frames"][frame]["frame"]
+ with Image.open(out / page["image"]) as atlas:
+ return atlas.convert("RGBA").crop((box["x"], box["y"], box["x"] + box["w"], box["y"] + box["h"]))
+ raise AssertionError(frame)
+
+
[email protected]("scale", [1, 0.5, 0.37])
+def test_a_packed_fill_is_stored_as_its_bar_frames_content_area(tmp_path, scale):
+ result, out = pack_tall_kit(tmp_path, scale)
+ nine = result["ui"]["nine_slice"]
+ assert list(nine) == sorted(FRAMES + BAND)
+ for fill_name, frame in (("hp_bar_fill", "hp_bar_frame"), ("xp_bar_fill", "xp_bar_frame")):
+ entry, bar = nine[fill_name], nine[frame]
+ assert set(entry) == {"frame", "size", "stretch", "insets", "padding", "inside"}
+ assert all("inside" not in nine[name] for name in nine if not name.endswith("_fill"))
+ pad = bar["padding"]
+ rect = {"x": pad["left"], "y": pad["top"], "w": bar["size"]["w"] - pad["left"] - pad["right"],
+ "h": bar["size"]["h"] - pad["top"] - pad["bottom"]}
+ # the fill is stored at the frame's content area: at full width it covers it to the pixel, never past it
+ assert entry["inside"] == {"element": frame, "rect": rect}
+ assert entry["size"] == {"w": rect["w"], "h": rect["h"]}
+ assert rect["x"] + rect["w"] + pad["right"] == bar["size"]["w"]
+ assert rect["y"] + rect["h"] + pad["bottom"] == bar["size"]["h"]
+ assert entry["stretch"] == "x" and entry["insets"]["top"] == entry["insets"]["bottom"] == 0
+ assert entry["insets"]["left"] + entry["insets"]["right"] < rect["w"]
+ assert entry["padding"] == {"left": 0, "right": 0, "top": 0, "bottom": 0}
+ stored = stored_frame(out, result, entry["frame"])
+ assert stored.size == (rect["w"], rect["h"])
+ # its ends are the fill's own ends resized by one factor, never stretched: the factor makes it as tall
+ # as the content area
+ with Image.open(tmp_path / "ui" / f"{fill_name}.png") as source:
+ source = source.convert("RGBA")
+ factor = rect["h"] / source.height
+ resized = resize_cell(source, (max(1, round(source.width * factor)), rect["h"]))
+ left, right = entry["insets"]["left"], entry["insets"]["right"]
+ cells = np.asarray(stored)
+ ends = np.asarray(resized)
+ assert np.array_equal(cells[:, :left], ends[:, :left])
+ assert np.array_equal(cells[:, rect["w"] - right:], ends[:, resized.width - right:])
+
+
[email protected]("scale", [1, 0.5, 0.37])
+def test_packed_round_parts_keep_their_aspect(tmp_path, scale):
+ result, out = pack_tall_kit(tmp_path, scale)
+ report = json.loads((tmp_path / "ui" / "ui-kit.json").read_text())
+ for name in ("pip_on", "pip_off", "joystick_base", "joystick_knob"):
+ entry, source = result["ui"]["nine_slice"][name], report["elements"][name]["size"]
+ assert entry["stretch"] == "none" and set(entry["insets"].values()) == {0}
+ assert entry["size"] == {"w": max(1, round(source["w"] * scale)), "h": max(1, round(source["h"] * scale))}
+ assert stored_frame(out, result, entry["frame"]).size == (entry["size"]["w"], entry["size"]["h"])
+
+
[email protected]("size", [(SIZE, SIZE), TALL])
@pytest.mark.parametrize("scale", [0.75, 0.5, 0.37, 0.25])
-def test_packed_kit_keeps_ornament_fringes_out_of_the_stretched_middle(tmp_path, scale):
- image, _ = synthetic_sheet(noise=False, haze=False)
+def test_packed_kit_keeps_ornament_fringes_out_of_the_stretched_middle(tmp_path, scale, size):
+ image, _ = synthetic_sheet(size=size, noise=False, haze=False)
sheet = tmp_path / "uikit.png"
image.save(sheet)
assert ui_kit.main(["--sheet", str(sheet), "--out-dir", str(tmp_path / "ui")]) == 0
@@ -372,3 +627,53 @@ def test_padding_follows_a_smooth_gradient_face_to_its_border():
padding, notes = ui_kit.measure_padding(Image.fromarray(pixels))
assert padding == {"left": 6, "right": 6, "top": 6, "bottom": 6}
assert notes == []
+
+
+# A square sheet, split and packed by `ui-kit` and `game-pack` before the 2:3 sheet existed (base bfcab36):
+# the guide, the report (sheet path aside) and every element's pixels, and the pack's manifest bytes and page
+# pixels at two scales. Encoded PNG bytes depend on the zlib build, so images are compared as pixels.
+SQUARE_FROZEN = {
+ "guide_text": "6dccc85d1b063e3d7714435ca629ad0ade3b42b720e01ae2b63bd74723383b43",
+ "guide_image": "66fe2622be0544604774c577bf1eb0619abd48f430a0eafe983a4f9132acaf91",
+ "report": "56d2b589e613f436fd18c295d74e6031505ffb5114397384929cca9f1e959888",
+ "elements": {
+ "button.png": "080fcf70864cb593dc41bbce0ca25f53aa98dc0046b376f2244970e130ff3d97",
+ "button_pressed.png": "cd789c638d312ad99406db8886c892d3caeb9a2754b31a9a4eb2af7a03f735a2",
+ "card.png": "79e53083c231e2cffabad9f96204d907c9fc7e9e0e32ba631aa19a6c14e34b67",
+ "hp_bar_frame.png": "d79732398bf77c5cc378a050d4340e7824f8353539a03cc6d5116fb914267f51",
+ "panel.png": "e1cb7ffd6c22205d8de77417ceb8d4193a76cb30e06ac2b7186cd61a2e45048d",
+ "timer_badge.png": "18e772b599a5c07004eab4c06cf43650b8617690ee151e265d0de9b32d4e9588",
+ "xp_bar_frame.png": "9209c935bc75c4dfb465c9060b7f3b8783186f2f69a0ebb85b7ecd161ef9bc00",
+ },
+ "pack": {
+ 0.5: ("1c1e4f1f8a844c8ab9eafd2ebbe0adea3a62b79d960d1518f2c55b6f62576a86",
+ ["8c107995d96e71ba7693c30a763b8b47df5c497a5041506ec5152fb465e9e640"]),
+ 0.37: ("698822abe6d887c04cc54e3ed40526366da298f164bda81c77980574c2c94445",
+ ["a819f45d7b812e5c420a3f9908dd7bb48e80ba8bac94354859ffd2af09bb40d2"]),
+ },
+}
+
+
+def pixel_digest(path):
+ with Image.open(path) as image:
+ return hashlib.sha256(image.convert("RGBA").tobytes()).hexdigest()
+
+
+def test_a_square_sheet_is_split_and_packed_as_before_the_tall_sheet(tmp_path):
+ ui_kit.draw_guide(tmp_path / "guide.png", (1024, 1024))
+ assert hashlib.sha256(ui_kit.guide_text().encode()).hexdigest() == SQUARE_FROZEN["guide_text"]
+ assert pixel_digest(tmp_path / "guide.png") == SQUARE_FROZEN["guide_image"]
+ image, _ = synthetic_sheet()
+ sheet = tmp_path / "uikit.png"
+ image.save(sheet)
+ assert ui_kit.main(["--sheet", str(sheet), "--out-dir", str(tmp_path / "ui")]) == 0
+ text = (tmp_path / "ui" / "ui-kit.json").read_text().replace(json.dumps(str(sheet.resolve())), '"<sheet>"')
+ assert hashlib.sha256(text.encode()).hexdigest() == SQUARE_FROZEN["report"]
+ assert {png.name: pixel_digest(png) for png in sorted((tmp_path / "ui").glob("*.png"))} == SQUARE_FROZEN["elements"]
+ for scale, (manifest, pages) in SQUARE_FROZEN["pack"].items():
+ request = tmp_path / f"game-{scale}.json"
+ request.write_text(json.dumps({"version": 2, "ui": {"kit": "ui/ui-kit.json", "scale": scale}}))
+ out = tmp_path / f"pack-{scale}"
+ result = game_pack.pack(request, out)
+ assert hashlib.sha256((out / "game-pack.json").read_bytes()).hexdigest() == manifest, scale
+ assert [pixel_digest(out / page["image"]) for page in result["textures"]] == pages, scale
diff --git a/tests/gen/test_layout_guide.py b/tests/gen/test_layout_guide.py
index 8521a65..c7413c5 100644
--- a/tests/gen/test_layout_guide.py
+++ b/tests/gen/test_layout_guide.py
@@ -139,13 +139,36 @@ def test_ui_kit_guide_attaches_the_kit_layout_and_names_every_element(tmp_path,
assert report["refs"] == []
-def test_ui_kit_guide_is_square(tmp_path, monkeypatch):
- with pytest.raises(SystemExit, match="square"):
- _run(tmp_path, monkeypatch, layout_guide="ui-kit", aspect_ratio="16:9")
+def test_ui_kit_guide_is_square_or_2_3(tmp_path, monkeypatch):
+ for ratio in ("16:9", "3:4", "9:16"):
+ with pytest.raises(SystemExit, match=r"square \(1:1.*2:3"):
+ _run(tmp_path, monkeypatch, layout_guide="ui-kit", aspect_ratio=ratio)
fake, _ = _run(tmp_path, monkeypatch, layout_guide="ui-kit")
assert fake.attached[0].size == (1024, 1024)
+def test_ui_kit_guide_at_2_3_is_the_whole_kit(tmp_path, monkeypatch):
+ from sprite_gen.frames import ui_kit
+
+ fake, report = _run(tmp_path, monkeypatch, layout_guide="ui-kit", aspect_ratio="2:3")
+
+ guide = fake.attached[0].convert("RGB")
+ assert guide.size == (683, 1024)
+ boxes = ui_kit.boxes((683, 1024))
+ assert list(boxes) == [slot.name for slot in ui_kit.LAYOUT]
+ for x, y, w, h in boxes.values():
+ assert guide.getpixel((x + w // 2, y)) == ui_kit.GUIDE_BOX
+ assert guide.getpixel((x + w // 2, y + h // 2)) == ui_kit.GUIDE_BACKGROUND
+ request = fake.requests[0]
+ assert request.aspect_ratio == "2:3"
+ assert request.prompt.endswith(ui_kit.guide_text((683, 1024)))
+ assert ui_kit.guide_text((683, 1024)) != ui_kit.guide_text()
+ cell = report["extra"]["layout_guide"]
+ assert cell["kind"] == "ui-kit" and (cell["width"], cell["height"]) == (683, 1024)
+ assert cell["boxes"]["joystick_knob"] == dict(zip("xywh", boxes["joystick_knob"]))
+ assert cell["boxes"]["panel"] == {"x": 32, "y": 32, "w": 384, "h": 267}
+
+
def test_the_flag_alone_is_still_the_figure_guide():
parser = gen._build_parser()
assert parser.parse_args(["--out", "x.png", "--layout-guide"]).layout_guide == "figure"