Files
windroost/tools/create_pixel_ui_frames.py
T
wushenghua e7b55e1c17 feat: align runtime with paper numbers and build the battle HUD carrier
Numbers. The paper runner (tools/mvp_validation_sim.py) and the Godot
runtime adapter were two independent rewrites of the same rules and had
drifted: seed derivation, damage order, the weakness rule and the shield
rebuild semantics all differed, so the runtime never actually reproduced
the paper numbers and P2-BAT-01 could never pass.

- add data/mvp_battle_rules.json as the single source of balance data and
  of the per-run seed derivation, read by both engines
- derive the efficiency factor from SHA-256 (48-bit prefix) so Python and
  GDScript land on the same float64
- port the paper semantics into battle_sim.gd and expose the simulation as
  a stepwise state machine so a live scene can drive it at 1x/2x/3x
- add tools/mvp_parity_gate.py: diffs 300 results field by field, event
  log by event log, and exits non-zero on any difference
- freeze data/mvp_validation_seed_reference.json so unapproved numeric
  drift fails the gate
- note that GDScript's roundf() is half-away-from-zero while Python's
  round() is banker's rounding; the runtime uses String.num instead

UI. The sample had no real UI carrier, no localization and no touch path,
so the NinePatch, dynamic text, safe area and input cases had nothing to
be proven against.

- add the battle HUD, laid out from the contract table rather than container
  stretch ratios, reproducing the 390x844 rectangles exactly and adapting
  360x800 / 430x932 with the stage kept as priority space
- add runtime text tables for zh_CN and en_US and a key validator that
  proves both locales carry the same keys and every content id has a string
- add NinePatch frames with a 6px border band and 6px cut corners
- add the live battle scene, the capture runner and the 1x/2x/3x
  equivalence test

tools/run_p2_checks.sh runs the nine gates in one command; all pass.
2026-11-16 22:22:55 +08:00

136 lines
4.5 KiB
Python

#!/usr/bin/env python3
"""Generate the runtime NinePatch frames for the Windroost UI.
The pixel UI component board (`pixel_ui_component_board.png`) is a style
reference only. The game needs individual frames with known border margins so
Godot's NinePatchRect can keep the pixel corners and the 6px border band
crisp while the interior stretches.
Frame geometry (shared with the component board):
* 6px accent border band
* 6px transparent pixel-cut corners
* flat interior
* patch margin = 6 on every side
Outputs land in PRODUCTION/source/pixel_pilot/ui/frames/ and are linked into
the Godot sample as symlinks; the board is never modified.
"""
from __future__ import annotations
import json
from pathlib import Path
from typing import Dict
from PIL import Image, ImageDraw
BG = (15, 25, 42)
PANEL = (28, 45, 68)
PANEL_INNER = (36, 58, 82)
BORDER = (232, 201, 120)
BORDER_DARK = (127, 91, 58)
TEXT = (255, 244, 205)
MINT = (111, 218, 178)
ORANGE = (225, 116, 72)
PURPLE = (126, 93, 154)
PURPLE_DARK = (86, 62, 110)
FRAME_SIZE = 48
BORDER_INSET = 6
OUT_DIR = Path("PRODUCTION/source/pixel_pilot/ui/frames")
# name -> (interior fill, accent colour, transparent background)
FRAMES: Dict[str, tuple] = {
"ui_frame_panel": (PANEL, BORDER, False),
"ui_frame_button": (PANEL, BORDER, False),
"ui_frame_button_pressed": (PURPLE, BORDER, False),
"ui_frame_tab": (PANEL, BORDER, False),
"ui_frame_tab_selected": (PURPLE_DARK, BORDER, False),
"ui_frame_tile": (PANEL, BORDER, False),
"ui_frame_stage": (PANEL_INNER, BORDER, False),
}
def draw_frame(size: int, fill, accent, transparent_bg: bool) -> Image.Image:
"""Draw one NinePatch frame using the component board's geometry."""
if transparent_bg:
image = Image.new("RGBA", (size, size), (0, 0, 0, 0))
else:
image = Image.new("RGBA", (size, size), BG + (255,))
draw = ImageDraw.Draw(image)
inset = BORDER_INSET
# Pixel-cut corners keep NinePatch corners stable when the center stretches.
draw.rectangle((inset, 0, size - inset, size), fill=accent + (255,))
draw.rectangle((0, inset, size, size - inset), fill=accent + (255,))
draw.rectangle((inset, inset, size - inset, size - inset), fill=fill + (255,))
# Punch the four corner notches back out so the panel reads as pixel cut.
for cx, cy in ((0, 0), (size - inset, 0), (0, size - inset), (size - inset, size - inset)):
draw.rectangle((cx, cy, cx + inset - 1, cy + inset - 1), fill=(0, 0, 0, 0))
return image
def draw_icon(size: int, kind: str, color) -> Image.Image:
image = Image.new("RGBA", (size, size), (0, 0, 0, 0))
draw = ImageDraw.Draw(image)
if kind == "sword":
draw.polygon([(20, 4), (30, 14), (14, 30), (4, 20)], fill=color + (255,))
draw.rectangle((2, 29, 31, 35), fill=BORDER + (255,))
draw.rectangle((13, 34, 20, 43), fill=BORDER_DARK + (255,))
elif kind == "bolt":
draw.polygon(
[(22, 2), (8, 24), (19, 24), (13, 44), (35, 17), (24, 17)], fill=color + (255,)
)
else:
draw.polygon(
[
(22, 2), (28, 16), (43, 18), (31, 28), (35, 43), (22, 34),
(9, 43), (13, 28), (1, 18), (16, 16),
],
fill=color + (255,),
)
return image
def main() -> int:
OUT_DIR.mkdir(parents=True, exist_ok=True)
manifest = {}
for name, (fill, accent, transparent) in FRAMES.items():
image = draw_frame(FRAME_SIZE, fill, accent, transparent)
path = OUT_DIR / f"{name}.png"
image.save(path)
manifest[name] = {
"file": path.name,
"width": FRAME_SIZE,
"height": FRAME_SIZE,
"patch_margin": BORDER_INSET,
"transparent_background": transparent,
}
for name, kind, color in (
("ui_icon_sword", "sword", ORANGE),
("ui_icon_bolt", "bolt", MINT),
("ui_icon_star", "star", BORDER),
):
image = draw_icon(48, kind, color)
path = OUT_DIR / f"{name}.png"
image.save(path)
manifest[name] = {
"file": path.name,
"width": 48,
"height": 48,
"patch_margin": 0,
"transparent_background": True,
}
manifest_path = OUT_DIR / "ui_frames_manifest.json"
manifest_path.write_text(
json.dumps({"schema": 1, "frames": manifest}, ensure_ascii=False, indent=2) + "\n",
encoding="utf-8",
)
print(f"wrote {len(manifest)} frames + manifest to {OUT_DIR}")
return 0
if __name__ == "__main__":
raise SystemExit(main())