Change the gui
This commit is contained in:
@@ -0,0 +1,175 @@
|
||||
// deno run -A tools/pack_gui.ts <resource pack assets folder>
|
||||
// packs the gui sprites the client uses out of a minecraft (1.20.2+) resource pack's assets folder, the one with
|
||||
// minecraft/ in it, into assets/sprites/gui.png, and lists where each is in client/gui/gui_atlas.ts. nine slice
|
||||
// borders come from the sprites' .mcmeta files. the saturation outlines are appleskin's, if the pack has them
|
||||
import { createCanvas, type Image, loadImage } from "@gfx/canvas-wasm";
|
||||
import { join } from "@std/path";
|
||||
|
||||
const OUTPUT_IMAGE = "assets/sprites/gui.png";
|
||||
const OUTPUT_TABLE = "client/gui/gui_atlas.ts";
|
||||
const ATLAS_WIDTH = 256;
|
||||
const GAP = 1;
|
||||
|
||||
type Border = [left: number, top: number, right: number, bottom: number];
|
||||
|
||||
interface Source {
|
||||
// from the pack's minecraft/textures/gui/sprites, unless it's made by `make`
|
||||
file?: string;
|
||||
// a part of the file
|
||||
crop?: [x: number, y: number, width: number, height: number];
|
||||
border?: Border;
|
||||
make?: (pack: string) => Promise<{ canvas: ReturnType<typeof createCanvas>; size: number; border: Border }>;
|
||||
}
|
||||
|
||||
const SOURCES: Record<string, Source> = {
|
||||
button: { file: "widget/button.png" },
|
||||
button_highlighted: { file: "widget/button_highlighted.png" },
|
||||
button_disabled: { file: "widget/button_disabled.png" },
|
||||
text_field: { file: "widget/text_field.png" },
|
||||
text_field_highlighted: { file: "widget/text_field_highlighted.png" },
|
||||
panel: { make: make_panel },
|
||||
tooltip: { make: make_tooltip },
|
||||
slot: { file: "container/slot.png" },
|
||||
slot_highlight_back: { file: "container/slot_highlight_back.png" },
|
||||
slot_highlight_front: { file: "container/slot_highlight_front.png" },
|
||||
hotbar: { file: "hud/hotbar.png" },
|
||||
hotbar_selection: { file: "hud/hotbar_selection.png" },
|
||||
crosshair: { file: "hud/crosshair.png" },
|
||||
heart_container: { file: "hud/heart/container.png" },
|
||||
heart_full: { file: "hud/heart/full.png" },
|
||||
heart_half: { file: "hud/heart/half.png" },
|
||||
food_empty: { file: "hud/food_empty.png" },
|
||||
food_full: { file: "hud/food_full.png" },
|
||||
food_half: { file: "hud/food_half.png" },
|
||||
saturation_1: { file: "../../../../appleskin/textures/icons.png", crop: [0, 0, 9, 9] },
|
||||
saturation_2: { file: "../../../../appleskin/textures/icons.png", crop: [9, 0, 9, 9] },
|
||||
saturation_3: { file: "../../../../appleskin/textures/icons.png", crop: [18, 0, 9, 9] },
|
||||
saturation_4: { file: "../../../../appleskin/textures/icons.png", crop: [27, 0, 9, 9] },
|
||||
};
|
||||
|
||||
function sprites_dir(pack: string) {
|
||||
return join(pack, "minecraft/textures/gui/sprites");
|
||||
}
|
||||
|
||||
// minecraft has no plain panel sprite, container screens are whole pictures. this takes the corners and edges of the
|
||||
// big chest's
|
||||
async function make_panel(pack: string) {
|
||||
const image = await loadImage(join(pack, "minecraft/textures/gui/container/generic_54.png"));
|
||||
// its outline, and a size of corner that fits the rounding
|
||||
const [left, top, right, bottom] = [2, 2, 174, 220];
|
||||
const corner = 4;
|
||||
const canvas = createCanvas(corner * 3, corner * 3);
|
||||
const ctx = canvas.getContext("2d");
|
||||
const columns = [left, left + corner, right - corner];
|
||||
const rows = [top, top + corner, bottom - corner];
|
||||
for (let row = 0; row < 3; row++) {
|
||||
for (let column = 0; column < 3; column++) {
|
||||
ctx.drawImage(
|
||||
image,
|
||||
columns[column],
|
||||
rows[row],
|
||||
corner,
|
||||
corner,
|
||||
column * corner,
|
||||
row * corner,
|
||||
corner,
|
||||
corner,
|
||||
);
|
||||
}
|
||||
}
|
||||
return { canvas, size: corner * 3, border: [corner, corner, corner, corner] as Border };
|
||||
}
|
||||
|
||||
// the background with the frame over it, without the empty space around them
|
||||
async function make_tooltip(pack: string) {
|
||||
const background = await loadImage(join(sprites_dir(pack), "tooltip/background.png"));
|
||||
const frame = await loadImage(join(sprites_dir(pack), "tooltip/frame.png"));
|
||||
const [margin, size] = [8, 84];
|
||||
const canvas = createCanvas(size, size);
|
||||
const ctx = canvas.getContext("2d");
|
||||
ctx.drawImage(background, margin, margin, size, size, 0, 0, size, size);
|
||||
ctx.drawImage(frame, margin, margin, size, size, 0, 0, size, size);
|
||||
return { canvas, size, border: [3, 3, 3, 3] as Border };
|
||||
}
|
||||
|
||||
function mcmeta_border(path: string): Border {
|
||||
let meta;
|
||||
try {
|
||||
meta = JSON.parse(Deno.readTextFileSync(`${path}.mcmeta`));
|
||||
} catch {
|
||||
return [0, 0, 0, 0];
|
||||
}
|
||||
const border = meta?.gui?.scaling?.type === "nine_slice" ? meta.gui.scaling.border : 0;
|
||||
if (typeof border === "number") {
|
||||
return [border, border, border, border];
|
||||
}
|
||||
return [border.left ?? 0, border.top ?? 0, border.right ?? 0, border.bottom ?? 0];
|
||||
}
|
||||
|
||||
async function main(pack: string) {
|
||||
const pieces: { name: string; image: Image | ReturnType<typeof createCanvas>; crop: number[]; border: Border }[] =
|
||||
[];
|
||||
for (const [name, source] of Object.entries(SOURCES)) {
|
||||
if (source.make) {
|
||||
const { canvas, size, border } = await source.make(pack);
|
||||
pieces.push({ name, image: canvas, crop: [0, 0, size, size], border });
|
||||
continue;
|
||||
}
|
||||
const path = join(sprites_dir(pack), source.file!);
|
||||
const image = await loadImage(path);
|
||||
const crop = source.crop ?? [0, 0, image.width(), image.height()];
|
||||
pieces.push({ name, image, crop, border: source.border ?? mcmeta_border(path) });
|
||||
}
|
||||
|
||||
// rows of pieces, tallest first
|
||||
const placed: { name: string; x: number; y: number; width: number; height: number; border: Border }[] = [];
|
||||
let x = 0;
|
||||
let y = 0;
|
||||
let row_height = 0;
|
||||
for (const piece of [...pieces].sort((a, b) => b.crop[3] - a.crop[3])) {
|
||||
const [, , width, height] = piece.crop;
|
||||
if (x + width > ATLAS_WIDTH) {
|
||||
x = 0;
|
||||
y += row_height + GAP;
|
||||
row_height = 0;
|
||||
}
|
||||
placed.push({ name: piece.name, x, y, width, height, border: piece.border });
|
||||
x += width + GAP;
|
||||
row_height = Math.max(row_height, height);
|
||||
}
|
||||
const height = y + row_height;
|
||||
|
||||
const canvas = createCanvas(ATLAS_WIDTH, height);
|
||||
const ctx = canvas.getContext("2d");
|
||||
for (const piece of pieces) {
|
||||
const at = placed.find((p) => p.name === piece.name)!;
|
||||
const [cx, cy, cw, ch] = piece.crop;
|
||||
// deno-lint-ignore no-explicit-any
|
||||
ctx.drawImage(piece.image as any, cx, cy, cw, ch, at.x, at.y, cw, ch);
|
||||
}
|
||||
Deno.writeFileSync(OUTPUT_IMAGE, canvas.toBuffer());
|
||||
|
||||
const lines = placed
|
||||
.sort((a, b) => Object.keys(SOURCES).indexOf(a.name) - Object.keys(SOURCES).indexOf(b.name))
|
||||
.map((p) =>
|
||||
`\t${p.name}: { x: ${p.x}, y: ${p.y}, width: ${p.width}, height: ${p.height}, border: [${
|
||||
p.border.join(", ")
|
||||
}] },`
|
||||
);
|
||||
Deno.writeTextFileSync(
|
||||
OUTPUT_TABLE,
|
||||
`// made by tools/pack_gui.ts, where each gui sprite is in ${OUTPUT_IMAGE}. border is the nine slice border as\n` +
|
||||
`// left, top, right, bottom, all 0 for sprites drawn whole\n` +
|
||||
`export const GUI_ATLAS = {\n${lines.join("\n")}\n} as const;\n\n` +
|
||||
`export type GuiSprite = keyof typeof GUI_ATLAS;\n`,
|
||||
);
|
||||
console.log(`Packed ${placed.length} sprites into ${OUTPUT_IMAGE} (${ATLAS_WIDTH}x${height})`);
|
||||
}
|
||||
|
||||
if (import.meta.main) {
|
||||
if (Deno.args.length !== 1) {
|
||||
console.error("usage: deno run -A tools/pack_gui.ts <resource pack assets folder>");
|
||||
Deno.exit(1);
|
||||
}
|
||||
await main(Deno.args[0]);
|
||||
}
|
||||
Reference in New Issue
Block a user