taloria/art/tools/ui_slice.py

115 lines
4.1 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
"""ui_slice.py - 9-Slice-Panel aus ui.py pruefen und als Blatt zeigen.
`tools/ui.py` zeichnet Panels prozedural (`panel()`). Fuer die Engine wird
dasselbe als **9-Slice-Textur** ausgeliefert
(`final/UI/ui_panel_9slice_12px.png`, Rand 2 px bei 1x), damit ein
`NinePatchRect` in Godot jedes Fenster ohne eigene Zeichenroutine fuellt.
Dieses Werkzeug beweist, dass die Textur stimmt: es setzt Panels aus den neun
Teilen zusammen und vergleicht sie Pixel fuer Pixel mit `panel()`.
python3 tools/ui_slice.py # pruefen + Blatt nach prep/ui-slice/
python3 tools/ui_slice.py --check # nur pruefen (Exit 1 bei Abweichung)
"""
from __future__ import annotations
import argparse
import sys
from pathlib import Path
from PIL import Image, ImageDraw
ART = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ART / "tools"))
import ui # noqa: E402
M = ui.PANEL_SLICE
# Groessen aus docs/ui.md: Dialog, Kampf-Log, Handelsfenster, Leiste, Minimum
GROESSEN = [(4, 4), (12, 12), (24, 16), (170, 132), (228, 56), (390, 71),
(400, 56)]
def neun(textur: Image.Image, w: int, h: int) -> Image.Image:
"""Panel aus neun Teilen bauen, wie ein NinePatchRect es tut (nearest)."""
s = textur.width
m = M
out = Image.new("RGBA", (w, h), (0, 0, 0, 0))
wm, hm = w - 2 * m, h - 2 * m
def stueck(box, ziel, ziel_box) -> None:
teil = textur.crop(box)
if teil.size != ziel:
teil = teil.resize(ziel, Image.NEAREST)
out.paste(teil, ziel_box)
# Ecken bleiben 1:1
out.paste(textur.crop((0, 0, m, m)), (0, 0))
out.paste(textur.crop((s - m, 0, s, m)), (w - m, 0))
out.paste(textur.crop((0, s - m, m, s)), (0, h - m))
out.paste(textur.crop((s - m, s - m, s, s)), (w - m, h - m))
if wm > 0:
stueck((m, 0, s - m, m), (wm, m), (m, 0))
stueck((m, s - m, s - m, s), (wm, m), (m, h - m))
if hm > 0:
stueck((0, m, m, s - m), (m, hm), (0, m))
stueck((s - m, m, s, s - m), (m, hm), (w - m, m))
if wm > 0 and hm > 0:
stueck((m, m, s - m, s - m), (wm, hm), (m, m))
return out
def soll(w: int, h: int) -> Image.Image:
im = Image.new("RGBA", (w, h), (0, 0, 0, 0))
ui.panel(ImageDraw.Draw(im), (0, 0, w - 1, h - 1))
return im
def main(argv: list[str] | None = None) -> int:
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
ap.add_argument("--check", action="store_true", help="nur pruefen, kein Blatt")
a = ap.parse_args(argv)
textur = ui.elem_panel_slice()
fehler = []
for w, h in GROESSEN:
ist, ref = neun(textur, w, h), soll(w, h)
gleich = ist.tobytes() == ref.tobytes()
print(f" {w:4d}x{h:<4d} {'pixelgleich' if gleich else 'ABWEICHUNG'}")
if not gleich:
fehler.append(f"{w}x{h}")
if not a.check:
blatt = Image.new("RGBA", (420, 212), (24, 20, 17, 255))
d = ImageDraw.Draw(blatt)
ui.draw_text(d, 6, 4, "9-SLICE PANEL - GEGENPROBE", ui.OCHRE)
# Reihe 1: die breite Dialogbox, Reihe 2: Log und Handelsfenster
ui.draw_text(d, 6, 16, "390x71", ui.CREAM_D)
blatt.alpha_composite(neun(textur, 390, 71), (6, 28))
ui.draw_text(d, 6, 103, "228x56", ui.CREAM_D)
blatt.alpha_composite(neun(textur, 228, 56), (6, 115))
ui.draw_text(d, 242, 103, "170x52", ui.CREAM_D)
blatt.alpha_composite(neun(textur, 170, 52), (242, 115))
ui.draw_text(d, 6, 178, f"TEXTUR {textur.width}x{textur.height}, RAND {M} PX (1X)",
ui.CREAM)
ui.draw_text(d, 6, 194, "GODOT: NINEPATCHRECT, MARGIN 2 (1X) / 4 (2X)",
ui.CREAM_D)
out = ART / "prep" / "ui-slice"
out.mkdir(parents=True, exist_ok=True)
blatt.save(out / "_panel_probe_420x212.png")
blatt.resize((840, 424), Image.NEAREST).save(
out / "_panel_probe_840x424.png")
print(f"{out}/_panel_probe_420x212.png (+2x)")
if fehler:
print(f"\n{len(fehler)} Abweichungen: {', '.join(fehler)}", file=sys.stderr)
return 1
print("\nAlle Groessen pixelgleich.")
return 0
if __name__ == "__main__":
sys.exit(main())