From 29e61a235bc14a8cec715766315e615825867172 Mon Sep 17 00:00:00 2001 From: Paula Date: Sun, 27 Sep 2026 14:52:30 -0300 Subject: [PATCH] Change the gui --- MODS.md | 2 +- assets/ASSETS.md | 31 ++++- assets/sprites/gui.png | Bin 0 -> 2306 bytes assets/sprites/ui.png | Bin 3656 -> 0 bytes client/gui/gui_atlas.ts | 29 ++++ client/gui/gui_container.ts | 43 ++---- client/gui/gui_player_inventory.ts | 39 ++---- client/gui/gui_screen.ts | 37 +++-- client/gui/gui_sprites.ts | 39 ++++++ client/gui/hud.ts | 153 ++++++++++---------- client/gui/mod_graphics.ts | 16 ++- client/gui/tooltip.ts | 12 +- client/gui/widgets.ts | 18 ++- client/main.ts | 2 +- client/rendering/render_utils.ts | 199 ++++----------------------- common/mod_api/client.ts | 2 +- mods/recipe_viewer/scripts/client.ts | 9 +- tests/bmod_test.ts | 2 +- tests/mod_tools_test.ts | 2 +- tools/pack_gui.ts | 175 +++++++++++++++++++++++ 20 files changed, 451 insertions(+), 359 deletions(-) create mode 100644 assets/sprites/gui.png delete mode 100644 assets/sprites/ui.png create mode 100644 client/gui/gui_atlas.ts create mode 100644 client/gui/gui_sprites.ts create mode 100644 tools/pack_gui.ts diff --git a/MODS.md b/MODS.md index 908938f..a358282 100644 --- a/MODS.md +++ b/MODS.md @@ -1125,7 +1125,7 @@ interface ModScreen { on_escape?(): boolean; } -// immediate mode, like the debug ui, styled with assets/sprites/ui.png +// immediate mode, like the debug ui, styled with the gui sprites (assets/sprites/gui.png) interface Graphics { readonly width: number; readonly height: number; diff --git a/assets/ASSETS.md b/assets/ASSETS.md index fabd085..68efa66 100644 --- a/assets/ASSETS.md +++ b/assets/ASSETS.md @@ -14,9 +14,36 @@ license: [Creative Commons CC0](https://creativecommons.org/publicdomain/zero/1. # UI -https://kenney.nl/assets/ui-pack-pixel-adventure +Catppuccin UI (Mocha, pink accent), with AppleSkin's saturation icons from the same pack. Packed into `sprites/gui.png` +by `tools/pack_gui.ts` -license: [Creative Commons CC0](https://creativecommons.org/publicdomain/zero/1.0/) +https://github.com/catppuccin/minecraft + +license: + +``` +MIT License + +Copyright (c) 2022 Catppuccin + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +``` # m6x11.ttf diff --git a/assets/sprites/gui.png b/assets/sprites/gui.png new file mode 100644 index 0000000000000000000000000000000000000000..41f0fdb39177f314370917b611136a3ebe3553d0 GIT binary patch literal 2306 zcmcIldpw(I7Jd^!5Tu>dwFp(!s=Cx=x=Nz7Xi=udOevzQOKDM-s!KvqouG!LjCJYM z^rGTgw~i2|b-!zg*}9dV#x;~lLXi0q-JR|Jv%7!nAK&-A?>Xmpp68tByzh6#XbC2eBG7wCZz2^kVErvD974acIs<@QmKT8#O#_JIQYchIPaviPIzkEK1+`g$>W|gt>rR8!3SX4h#?~BTXm%#9}K9>#0DNf z|J+zygy^V+b`sy=168oDM^7En6YdacxcEd|IpWMU#RUQ0F}n{1L9W9fG#F&$q!mZRY%{eD1f2HkVD>ugN% z6Uq-S-PpF@Lf08ZyE3;u@hUaWb{HRKuXcAt*0X1k3NjUlcz?e~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p=Q&n?GG(e(AJ_yr>;q7Ija;p;M8t?mYSz!xL6(?#*-=H?CFzpHPxg|x=4Z-#p` zxZ$cwJ>_NBr5p_Ib(M|?;3g5Wq^&vpbuC4QCnWJH>>0&FiBCmbiQ+otFnA&6D z_*S#NhWpE6pzU;VBfk&^?rI5`0RJGYKj00m0v;(|;dL;f;uS|JPpEixPQX8Gy&ir$ z8StUYv*!T(_Jh~MrvyIi1jJbYAJm7p0zUi{TL3s6G$~LzNY=Oez{#K~yR-J$TKDV2 zTMe310H zQJ0N(aOmRk4h~&B-oa59A37}L0Gyw20BV8D+4{`A@r=BgOsBiY`~jYHDPpepZ!yQl z|1QlGo6us0P3v9O!X|PlX9m1}(1aO!F=wnpf2vVEJZV!6nr8((85YW(ELK}>RL}M` zs)r|Ss<3cu;3V;ZP5neF@sYk;FK*kIl~+iwTBr6?BtD}9N3|1ES~10yRBGQ~ZQ%u^ z_TvMuY9|_aRUpT}XX=LS71dM58SJVJ3d^N$4!BJ{6TYyN#vgE;U4h%dib1Z=_UJ?4eOr_O0000("bworld:ui"); - - draw_nine_slice( - ui, - 160, - 0, - 16, - 16, - 4, - 4, - 4, - 4, - this.x, - this.y, - this.inventory_width, - this.inventory_height, - ); + draw_gui_sprite("panel", this.x, this.y, this.inventory_width, this.inventory_height); this.draw_bars(); @@ -66,11 +53,9 @@ export class GuiContainer extends GuiInventoryScreen { draw_bars() { const atlas = AssetManager.instance.get("bworld:textures"); - const area_y = PADDING * 3 + SLOT_SIZE * 4; - for (const bar of this.layout.bars) { - const x = this.x + PADDING + bar.x * SLOT_SIZE; - const y = this.y + area_y + bar.y * SLOT_SIZE; + const x = this.x + MARGIN + bar.x * SLOT_SIZE; + const y = this.y + AREA_Y + bar.y * SLOT_SIZE; const max = this.properties[bar.max] ?? 0; const pct = max > 0 ? Math.max(0, Math.min(1, (this.properties[bar.value] ?? 0) / max)) : 0; diff --git a/client/gui/gui_player_inventory.ts b/client/gui/gui_player_inventory.ts index 54eb06b..9dac726 100644 --- a/client/gui/gui_player_inventory.ts +++ b/client/gui/gui_player_inventory.ts @@ -1,26 +1,27 @@ import { add_player_hotbar, add_player_inventory, GuiInventoryScreen, Slot } from "./gui_screen.ts"; -import { canvas, draw_rect, Texture } from "$/client/renderer/mod.ts"; -import { AssetManager } from "../assets.ts"; +import { canvas, draw_rect } from "$/client/renderer/mod.ts"; import { SLOT_SIZE } from "../../common/constants.ts"; -import { draw_nine_slice } from "../rendering/render_utils.ts"; +import { draw_gui_sprite, GUI_SCALE } from "./gui_sprites.ts"; import { ClientInventories } from "../inventory.ts"; import { ClientMessage, CRAFTING_RESULT_SLOT } from "$/common/protocol.ts"; -const PADDING = 10; +// around the slots, and between the hotbar, the rest of the inventory and what's below, like minecraft's +const MARGIN = 7 * GUI_SCALE; +const GAP = 4 * GUI_SCALE; // the recipes live on the server, which fills in the result slot export class GuiPlayerInventory extends GuiInventoryScreen { - override inventory_width = PADDING * 2 + SLOT_SIZE * 9; - override inventory_height = PADDING * 2 + SLOT_SIZE * 4 + PADDING + SLOT_SIZE * 3 + PADDING; + override inventory_width = MARGIN * 2 + SLOT_SIZE * 9; + override inventory_height = MARGIN * 2 + GAP * 2 + SLOT_SIZE * 7; constructor(inventories: ClientInventories, send: (message: ClientMessage) => void) { super(inventories, send); - add_player_hotbar(this, PADDING, PADDING); - add_player_inventory(this, PADDING, PADDING * 2 + SLOT_SIZE); + add_player_hotbar(this, MARGIN, MARGIN); + add_player_inventory(this, MARGIN, MARGIN + GAP + SLOT_SIZE); - const crafting_x = PADDING; - const crafting_y = PADDING * 3 + SLOT_SIZE * 4; + const crafting_x = MARGIN; + const crafting_y = MARGIN + GAP * 2 + SLOT_SIZE * 4; for (let i = 0; i < 3; i += 1) { for (let j = 0; j < 3; j += 1) { @@ -38,23 +39,7 @@ export class GuiPlayerInventory extends GuiInventoryScreen { override on_render(): void { draw_rect(0, 0, canvas.width, canvas.height, [0, 0, 0, 0.8]); - const ui = AssetManager.instance.get("bworld:ui"); - - draw_nine_slice( - ui, - 160, - 0, - 16, - 16, - 4, - 4, - 4, - 4, - this.x, - this.y, - this.inventory_width, - this.inventory_height, - ); + draw_gui_sprite("panel", this.x, this.y, this.inventory_width, this.inventory_height); super.on_render(); } diff --git a/client/gui/gui_screen.ts b/client/gui/gui_screen.ts index 4ca1028..3205997 100644 --- a/client/gui/gui_screen.ts +++ b/client/gui/gui_screen.ts @@ -1,16 +1,19 @@ import { SLOT_SIZE } from "$/common/constants.ts"; +import { draw_gui_sprite, GUI_SCALE } from "./gui_sprites.ts"; import { click_slot, Container } from "$/common/inventory.ts"; import { ClientMessage, ContainerKey, CRAFTING_RESULT_SLOT } from "$/common/protocol.ts"; import { point_inside_rec } from "../../common/utils.ts"; -import { AssetManager } from "../assets.ts"; import { InputManager } from "../input_manager.ts"; import { ClientInventories } from "../inventory.ts"; -import { canvas, Texture } from "../renderer/mod.ts"; -import { draw_item, draw_nine_slice } from "../rendering/render_utils.ts"; +import { canvas } from "../renderer/mod.ts"; +import { draw_item } from "../rendering/render_utils.ts"; import { draw_tooltip, item_tooltip } from "./tooltip.ts"; import { ModGraphics } from "./mod_graphics.ts"; import { inventory_overlays } from "../mods.ts"; +// how far past the slot's edge its highlight goes +const HIGHLIGHT_OVERHANG = 3 * GUI_SCALE; + export class Slot { container: ContainerKey; index: number; @@ -83,33 +86,25 @@ export class GuiInventoryScreen extends GuiScreen { } on_render(): void { - const ui = AssetManager.instance.get("bworld:ui"); - for (const slot of this.slots) { - const hovered = slot === this.hovering; - draw_nine_slice( - ui, - hovered ? 19 * 16 : 160 + 32, - hovered ? 16 : 0, - 16, - 16, - 4, - 4, - 4, - 4, - this.x + slot.x, - this.y + slot.y, - SLOT_SIZE, - SLOT_SIZE, - ); + draw_gui_sprite("slot", this.x + slot.x, this.y + slot.y); } + // like minecraft's, the highlight is a bit bigger than the slot, part of it under the item and part over + const highlight = (sprite: "slot_highlight_back" | "slot_highlight_front") => { + if (this.hovering) { + const x = this.x + this.hovering.x - HIGHLIGHT_OVERHANG; + draw_gui_sprite(sprite, x, this.y + this.hovering.y - HIGHLIGHT_OVERHANG); + } + }; + highlight("slot_highlight_back"); for (const slot of this.slots) { const item = this.get_container(slot.container)?.get_item(slot.index); if (item) { draw_item(item, this.x + slot.x, this.y + slot.y); } } + highlight("slot_highlight_front"); // mods' overlays go on top and get the mouse first, then the slots get what they left this.#render_overlays(); diff --git a/client/gui/gui_sprites.ts b/client/gui/gui_sprites.ts new file mode 100644 index 0000000..0e4023e --- /dev/null +++ b/client/gui/gui_sprites.ts @@ -0,0 +1,39 @@ +import { AssetManager } from "$/client/assets.ts"; +import type { Texture } from "$/client/renderer/mod.ts"; +import { draw_nine_slice } from "$/client/rendering/render_utils.ts"; +import { GUI_ATLAS, type GuiSprite } from "./gui_atlas.ts"; + +// gui sprites are drawn 3 times bigger, like minecraft at gui scale 3. SLOT_SIZE is its 18 pixel slot at this scale +export const GUI_SCALE = 3; + +// a sprite from assets/sprites/gui.png, see tools/pack_gui.ts. nine slice ones stretch to the size given with their +// borders kept at GUI_SCALE, the rest are drawn at their size times GUI_SCALE unless given one +export function draw_gui_sprite( + name: GuiSprite, + x: number, + y: number, + width?: number, + height?: number, + color = [1, 1, 1, 1], +) { + const texture = AssetManager.instance.get("bworld:gui"); + const sprite = GUI_ATLAS[name]; + const [left, top, right, bottom] = sprite.border; + draw_nine_slice( + texture, + sprite.x, + sprite.y, + sprite.width, + sprite.height, + left, + right, + top, + bottom, + x, + y, + width ?? sprite.width * GUI_SCALE, + height ?? sprite.height * GUI_SCALE, + color, + GUI_SCALE, + ); +} diff --git a/client/gui/hud.ts b/client/gui/hud.ts index 1935648..7d70044 100644 --- a/client/gui/hud.ts +++ b/client/gui/hud.ts @@ -1,28 +1,29 @@ -import { SLOT_SIZE } from "$/common/constants.ts"; -import { AssetManager } from "$/client/assets.ts"; import type { Client } from "$/client/client.ts"; import type { ClientInventories } from "$/client/inventory.ts"; -import { draw_item, draw_nine_slice } from "$/client/rendering/render_utils.ts"; -import { canvas, draw_rect, draw_rect_stroke, draw_text, measure_text, Texture } from "$/client/renderer/mod.ts"; +import { draw_item } from "$/client/rendering/render_utils.ts"; +import { canvas, draw_text, measure_text } from "$/client/renderer/mod.ts"; import { display_name } from "$/common/utils.ts"; import { TEXT_HEIGHT, TEXT_SCALE } from "./widgets.ts"; +import { draw_gui_sprite, GUI_SCALE } from "./gui_sprites.ts"; -const PADDING = 10; -const CROSSHAIR_SIZE = 8; +// sizes and gaps in minecraft's gui pixels, drawn GUI_SCALE times bigger +const HOTBAR_WIDTH = 182; +const HOTBAR_HEIGHT = 22; +// each slot is 20 across, the item sits 3 in from its corner and the selection 1 out +const HOTBAR_CELL = 20; +const HOTBAR_ITEM_INSET = 3; +// hearts and hunger: 10 icons each, 8 apart, above the hotbar's top +const ICON_SIZE = 9; +const ICON_SPACING = 8; +const ICONS_ABOVE_HOTBAR = 17; +const MAX_VITAL = 20; // how long the held item's name shows above the hotbar after it changes, and the last part of that it fades out in const HELD_NAME_MS = 2000; const HELD_NAME_FADE_MS = 500; const HELD_NAME_GAP = 12; -// the health and hunger bars, side by side just above the hotbar -const BAR_HEIGHT = 14; -const BAR_GAP = 8; -const BAR_BORDER = 2; -const MAX_VITAL = 20; -const HEALTH_COLOR: [number, number, number, number] = [0.84, 0.12, 0.12, 1]; -const FOOD_COLOR: [number, number, number, number] = [0.55, 0.33, 0.14, 1]; -const SATURATION_COLOR: [number, number, number, number] = [1, 0.84, 0.25, 1]; -const BAR_BACKGROUND: [number, number, number, number] = [0.1, 0.08, 0.08, 0.75]; -const BAR_OUTLINE: [number, number, number, number] = [0, 0, 0, 0.8]; +const CROSSHAIR_SIZE = 15; +// under the crosshair so it shows on bright sky and snow, minecraft inverts the colors behind it instead +const CROSSHAIR_SHADOW: [number, number, number, number] = [0, 0, 0, 0.4]; // what's drawn over the world while playing, like minecraft's Gui: hotbar, crosshair and chat export class Hud { @@ -35,43 +36,22 @@ export class Hud { this.#render_vitals(client); this.#render_held_name(client.player.inventories); this.#render_crosshair(); - client.chat.render(false); + client.chat.render(false, icons_y() - HELD_NAME_GAP); } #render_hotbar(inventories: ClientInventories) { - const ui = AssetManager.instance.get("bworld:ui"); + const { x, y } = hotbar_position(); + draw_gui_sprite("hotbar", x, y); - const hotbar_width = PADDING * 2 + SLOT_SIZE * 9; - const hotbar_height = PADDING * 2 + SLOT_SIZE; - - const x = canvas.width / 2 - hotbar_width / 2; - const y = canvas.height - hotbar_height; - - draw_nine_slice(ui, 160, 0, 16, 16, 4, 4, 4, 4, x, y, hotbar_width, hotbar_height); - - for (let index = 0; index < 9; index += 1) { - const selected = inventories.hotbar_selected === index; - draw_nine_slice( - ui, - selected ? 19 * 16 : 160 + 32, - selected ? 16 : 0, - 16, - 16, - 4, - 4, - 4, - 4, - x + PADDING + index * SLOT_SIZE, - y + PADDING, - SLOT_SIZE, - SLOT_SIZE, - ); - } + const selected_x = x + (inventories.hotbar_selected * HOTBAR_CELL - 1) * GUI_SCALE; + draw_gui_sprite("hotbar_selection", selected_x, y - GUI_SCALE); for (let index = 0; index < 9; index += 1) { const item = inventories.inventory.get_item(index); if (item) { - draw_item(item, x + PADDING + index * SLOT_SIZE, y + PADDING); + // draw_item takes a slot's corner, which is a pixel out from the item + const item_x = x + (index * HOTBAR_CELL + HOTBAR_ITEM_INSET - 1) * GUI_SCALE; + draw_item(item, item_x, y + (HOTBAR_ITEM_INSET - 1) * GUI_SCALE); } } } @@ -92,50 +72,71 @@ export class Hud { } const alpha = Math.min(1, (HELD_NAME_MS - shown_for) / HELD_NAME_FADE_MS); const name = display_name(item.type_id); - const bars_top = canvas.height - (PADDING * 2 + SLOT_SIZE) - BAR_GAP - BAR_HEIGHT; const x = (canvas.width - measure_text(name, TEXT_SCALE)) / 2; - const y = bars_top - HELD_NAME_GAP - TEXT_HEIGHT * TEXT_SCALE; + const y = icons_y() - HELD_NAME_GAP - TEXT_HEIGHT * TEXT_SCALE; // a shadow so it reads over bright sky and snow draw_text(name, x + TEXT_SCALE, y + TEXT_SCALE, TEXT_SCALE, [0.15, 0.15, 0.15, alpha]); draw_text(name, x, y, TEXT_SCALE, [1, 1, 1, alpha]); } - // health on the left, hunger on the right, like minecraft's hearts and drumsticks. saturation is a gold border - // around as much of the hunger bar as there's saturation, it's never more than the hunger + // hearts on the left and hunger on the right like minecraft, each icon 2 points. saturation is appleskin's outline + // over the hunger icons, it's never more than the hunger #render_vitals(client: Client) { const player = client.player; - const hotbar_width = PADDING * 2 + SLOT_SIZE * 9; - const bar_width = (hotbar_width - BAR_GAP) / 2; - const left = canvas.width / 2 - hotbar_width / 2; - const y = canvas.height - (PADDING * 2 + SLOT_SIZE) - BAR_GAP - BAR_HEIGHT; + const { x: left } = hotbar_position(); + const right = left + HOTBAR_WIDTH * GUI_SCALE; + const y = icons_y(); + const health = Math.ceil(player.health); + const food = Math.ceil(player.food); + const saturation = Math.min(player.saturation, player.food); - this.#bar(left, y, bar_width, player.health / MAX_VITAL, HEALTH_COLOR); + for (let i = 0; i < MAX_VITAL / 2; i++) { + const heart_x = left + i * ICON_SPACING * GUI_SCALE; + draw_gui_sprite("heart_container", heart_x, y); + const heart = icon_fill(health, i); + if (heart) { + draw_gui_sprite(heart === "full" ? "heart_full" : "heart_half", heart_x, y); + } - const food_x = left + bar_width + BAR_GAP; - this.#bar(food_x, y, bar_width, player.food / MAX_VITAL, FOOD_COLOR); - const saturation = Math.min(player.saturation, player.food) / MAX_VITAL; - if (saturation > 0) { - const width = Math.max(BAR_BORDER * 2, bar_width * saturation); - draw_rect(food_x, y, width, BAR_BORDER, SATURATION_COLOR); - draw_rect(food_x, y + BAR_HEIGHT - BAR_BORDER, width, BAR_BORDER, SATURATION_COLOR); - draw_rect(food_x, y, BAR_BORDER, BAR_HEIGHT, SATURATION_COLOR); - draw_rect(food_x + width - BAR_BORDER, y, BAR_BORDER, BAR_HEIGHT, SATURATION_COLOR); + // hunger goes right to left + const food_x = right - (i * ICON_SPACING + ICON_SIZE) * GUI_SCALE; + draw_gui_sprite("food_empty", food_x, y); + const fill = icon_fill(food, i); + if (fill) { + draw_gui_sprite(fill === "full" ? "food_full" : "food_half", food_x, y); + } + const saturated = Math.max(0, Math.min(1, (saturation - i * 2) / 2)); + if (saturated > 0) { + const quarters = saturated >= 1 ? 4 : saturated > 0.5 ? 3 : saturated > 0.25 ? 2 : 1; + draw_gui_sprite(`saturation_${quarters}`, food_x, y); + } } } - #bar(x: number, y: number, width: number, fraction: number, color: [number, number, number, number]) { - draw_rect(x - 1, y - 1, width + 2, BAR_HEIGHT + 2, BAR_OUTLINE); - draw_rect(x, y, width, BAR_HEIGHT, BAR_BACKGROUND); - draw_rect(x, y, width * Math.max(0, Math.min(1, fraction)), BAR_HEIGHT, color); - } - #render_crosshair() { - draw_rect_stroke( - (canvas.width - CROSSHAIR_SIZE) / 2, - (canvas.height - CROSSHAIR_SIZE) / 2, - CROSSHAIR_SIZE, - CROSSHAIR_SIZE, - [0, 0, 0, 0.6], - ); + const size = CROSSHAIR_SIZE * GUI_SCALE; + const x = Math.round((canvas.width - size) / 2); + const y = Math.round((canvas.height - size) / 2); + draw_gui_sprite("crosshair", x + GUI_SCALE, y + GUI_SCALE, undefined, undefined, CROSSHAIR_SHADOW); + draw_gui_sprite("crosshair", x, y); } } + +function hotbar_position() { + return { + x: Math.round((canvas.width - HOTBAR_WIDTH * GUI_SCALE) / 2), + y: canvas.height - HOTBAR_HEIGHT * GUI_SCALE, + }; +} + +// the top of the hearts and hunger +function icons_y() { + return hotbar_position().y - ICONS_ABOVE_HOTBAR * GUI_SCALE; +} + +// whether icon i of a 0 to 20 value is full, half or empty +function icon_fill(value: number, i: number): "full" | "half" | undefined { + if (value >= i * 2 + 2) return "full"; + if (value === i * 2 + 1) return "half"; + return undefined; +} diff --git a/client/gui/mod_graphics.ts b/client/gui/mod_graphics.ts index 0b090fa..5264808 100644 --- a/client/gui/mod_graphics.ts +++ b/client/gui/mod_graphics.ts @@ -18,14 +18,17 @@ import { measure_text, Texture, } from "$/client/renderer/mod.ts"; -import { draw_item, draw_nine_slice } from "$/client/rendering/render_utils.ts"; +import { draw_item } from "$/client/rendering/render_utils.ts"; import { get_sprite_region } from "$/client/sprites.ts"; import { draw_tooltip, item_tooltip, type TooltipLine } from "./tooltip.ts"; import { TEXT_HEIGHT, TEXT_SCALE, TextInput } from "./widgets.ts"; +import { GUI_ATLAS } from "./gui_atlas.ts"; +import { draw_gui_sprite, GUI_SCALE } from "./gui_sprites.ts"; const WHITE: Color = [1, 1, 1, 1]; const GREY: Color = [0.66, 0.66, 0.66, 1]; const INPUT_HEIGHT = 32; +const FIELD_BORDER = GUI_ATLAS.text_field.border[0] * GUI_SCALE; export class ModGraphics implements Graphics { // text inputs by id, and which one has the keyboard @@ -83,7 +86,7 @@ export class ModGraphics implements Graphics { } panel(x: number, y: number, w: number, h: number) { - draw_nine_slice(AssetManager.instance.get("bworld:ui"), 160, 0, 16, 16, 4, 4, 4, 4, x, y, w, h); + draw_gui_sprite("panel", x, y, w, h); } text(text: string, x: number, y: number, options: { scale?: number; color?: Color } = {}) { @@ -129,8 +132,7 @@ export class ModGraphics implements Graphics { button(label: string, x: number, y: number, w: number, h: number): boolean { const hovered = point_inside_rec(this.mouse.x, this.mouse.y, x, y, w, h); - const [sx, sy] = hovered ? [224, 16] : [176, 0]; - draw_nine_slice(AssetManager.instance.get("bworld:ui"), sx, sy, 16, 16, 4, 4, 4, 4, x, y, w, h); + draw_gui_sprite(hovered ? "button_highlighted" : "button", x, y, w, h); const text_width = measure_text(label, TEXT_SCALE); draw_text(label, x + (w - text_width) / 2, y + (h - TEXT_HEIGHT * TEXT_SCALE) / 2, TEXT_SCALE, WHITE); if (hovered && this.mouse.pressed) { @@ -167,9 +169,9 @@ export class ModGraphics implements Graphics { } } - draw_rect(x, y, w, INPUT_HEIGHT, focused ? WHITE : GREY); - draw_rect(x + 2, y + 2, w - 4, INPUT_HEIGHT - 4, [0, 0, 0, 1]); - this.clip(x + 2, y + 2, w - 4, INPUT_HEIGHT - 4, () => { + draw_gui_sprite(focused ? "text_field_highlighted" : "text_field", x, y, w, INPUT_HEIGHT); + // inside the field's border, so long text doesn't go over it + this.clip(x + FIELD_BORDER, y + 2, w - FIELD_BORDER * 2, INPUT_HEIGHT - 4, () => { if (focused) { input.draw_selection(x + 6, y + 6, INPUT_HEIGHT - 12); } diff --git a/client/gui/tooltip.ts b/client/gui/tooltip.ts index 8d51ecc..fc29a60 100644 --- a/client/gui/tooltip.ts +++ b/client/gui/tooltip.ts @@ -2,7 +2,8 @@ import { EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts"; import type { ItemStack } from "$/common/inventory.ts"; import { display_name } from "$/common/utils.ts"; -import { canvas, draw_rect, draw_text, measure_text } from "$/client/renderer/mod.ts"; +import { canvas, draw_text, measure_text } from "$/client/renderer/mod.ts"; +import { draw_gui_sprite, GUI_SCALE } from "./gui_sprites.ts"; import { TEXT_HEIGHT, TEXT_SCALE } from "./widgets.ts"; type Color = [number, number, number, number]; @@ -10,10 +11,8 @@ type Color = [number, number, number, number]; const NAME_COLOR: Color = [1, 1, 1, 1]; const LORE_COLOR: Color = [0.66, 0.66, 0.66, 1]; const FOOD_COLOR: Color = [0.85, 0.6, 0.35, 1]; -const BACKGROUND: Color = [0.06, 0.02, 0.1, 0.94]; -const BORDER: Color = [0.31, 0.1, 0.6, 1]; -const BORDER_WIDTH = 2; -const PADDING = 8; +// from the edge of the tooltip sprite to the text +const PADDING = 4 * GUI_SCALE; const LINE_GAP = 4; // from the mouse, so the cursor doesn't cover it const OFFSET = 16; @@ -52,8 +51,7 @@ export function draw_tooltip(lines: TooltipLine[], mouse_x: number, mouse_y: num x = Math.max(0, x); y = Math.max(0, y); - draw_rect(x, y, width, height, BORDER); - draw_rect(x + BORDER_WIDTH, y + BORDER_WIDTH, width - BORDER_WIDTH * 2, height - BORDER_WIDTH * 2, BACKGROUND); + draw_gui_sprite("tooltip", x, y, width, height); lines.forEach((line, i) => { draw_text(line.text, x + PADDING, y + PADDING + i * (line_height + LINE_GAP), TEXT_SCALE, line.color); }); diff --git a/client/gui/widgets.ts b/client/gui/widgets.ts index 6a9b615..579ab20 100644 --- a/client/gui/widgets.ts +++ b/client/gui/widgets.ts @@ -1,8 +1,7 @@ import { point_inside_rec } from "$/common/utils.ts"; -import { AssetManager } from "$/client/assets.ts"; import { InputManager, type KeyCode } from "$/client/input_manager.ts"; -import { draw_nine_slice } from "$/client/rendering/render_utils.ts"; -import { draw_rect, draw_text, measure_text, Texture } from "$/client/renderer/mod.ts"; +import { draw_rect, draw_text, measure_text } from "$/client/renderer/mod.ts"; +import { draw_gui_sprite } from "./gui_sprites.ts"; export const TEXT_SCALE = 2; // how tall the font is at scale 1 @@ -12,7 +11,8 @@ const REPEAT_DELAY_MS = 400; const REPEAT_RATE_MS = 40; const CARET_BLINK_MS = 500; const DOUBLE_CLICK_MS = 400; -const SELECTION_COLOR: [number, number, number, number] = [0.25, 0.45, 1, 0.6]; +// catppuccin mocha's pink, like the gui sprites' highlights +const SELECTION_COLOR: [number, number, number, number] = [0.96, 0.76, 0.91, 0.45]; // editing a line of text, like minecraft's EditBox without the drawing. typing, backspace and delete, arrows, home // and end. shift with those selects, ctrl (alt on macs) moves and deletes whole words, ctrl+a selects everything, and @@ -270,10 +270,8 @@ export class Button { } render() { - const ui = AssetManager.instance.get("bworld:ui"); - const [sx, sy] = this.#hovered ? [224, 16] : [176, 0]; - const tint = this.active ? [1, 1, 1, 1] : [0.6, 0.6, 0.6, 1]; - draw_nine_slice(ui, sx, sy, 16, 16, 4, 4, 4, 4, this.x, this.y, this.width, this.height, tint); + const sprite = !this.active ? "button_disabled" : this.#hovered ? "button_highlighted" : "button"; + draw_gui_sprite(sprite, this.x, this.y, this.width, this.height); const text_width = measure_text(this.label, TEXT_SCALE); draw_text( @@ -322,8 +320,8 @@ export class EditBox { } render() { - const ui = AssetManager.instance.get("bworld:ui"); - draw_nine_slice(ui, this.focused ? 320 : 304, 0, 16, 16, 4, 4, 4, 4, this.x, this.y, this.width, this.height); + const sprite = this.focused ? "text_field_highlighted" : "text_field"; + draw_gui_sprite(sprite, this.x, this.y, this.width, this.height); const text_x = this.#text_x; const text_y = this.y + (this.height - TEXT_HEIGHT * TEXT_SCALE) / 2; diff --git a/client/main.ts b/client/main.ts index b05b7ba..70cb81c 100644 --- a/client/main.ts +++ b/client/main.ts @@ -176,7 +176,7 @@ AssetManager.instance.load("bworld:assets_text", "/assets/ASSETS.md"); AssetManager.instance.load("bworld:player", "/assets/sprites/player.png"); AssetManager.instance.load("bworld:skin", "/assets/sprites/skin_template.png"); AssetManager.instance.load("bworld:roguelike", "/assets/sprites/roguelike.png"); -AssetManager.instance.load("bworld:ui", "/assets/sprites/ui.png"); +AssetManager.instance.load("bworld:gui", "/assets/sprites/gui.png"); AssetManager.instance.load("bworld:m6x11", "/assets/fonts/m6x11.png"); AssetManager.instance.load("bworld:m6x11_fnt", "/assets/fonts/m6x11.fnt"); diff --git a/client/rendering/render_utils.ts b/client/rendering/render_utils.ts index 1d096fb..e4a77e1 100644 --- a/client/rendering/render_utils.ts +++ b/client/rendering/render_utils.ts @@ -6,6 +6,8 @@ import { ItemStack } from "$/common/inventory.ts"; import { block_item_texture, cube_face_texture } from "$/common/block_models.ts"; import { draw_text, draw_texture_region, draw_texture_region_skewed, Texture } from "$/client/renderer/mod.ts"; +// the corners keep their size (times scale), the edges stretch along and the middle fills the rest, like minecraft's +// nine slice gui sprites. borders are in source pixels export function draw_nine_slice( image: Texture, source_x: number, @@ -21,183 +23,38 @@ export function draw_nine_slice( width: number, height: number, color = [1, 1, 1, 1], + scale = 1, ) { - const center_width = source_width - left - right; - const center_height = source_height - top - bottom; - - const dest_center_width = width - left - right; - const dest_center_height = height - top - bottom; - - // top left - draw_texture_region( - image, - source_x, - source_y, - left, - top, - x, - y, - left, - top, - false, - false, - color[0], - color[1], - color[2], - color[3], - ); - - // top right - draw_texture_region( - image, + // start and size of each column and row, in the source and on screen + const source_columns = [[source_x, left], [source_x + left, source_width - left - right], [ source_x + source_width - right, - source_y, right, - top, - x + width - right, - y, - right, - top, - false, - false, - color[0], - color[1], - color[2], - color[3], - ); - - // bottom left - draw_texture_region( - image, - source_x, + ]]; + const source_rows = [[source_y, top], [source_y + top, source_height - top - bottom], [ source_y + source_height - bottom, - left, bottom, - x, - y + height - bottom, - left, - bottom, - false, - false, - color[0], - color[1], - color[2], - color[3], - ); + ]]; + const columns = [[x, left * scale], [x + left * scale, width - (left + right) * scale], [ + x + width - right * scale, + right * scale, + ]]; + const rows = [[y, top * scale], [y + top * scale, height - (top + bottom) * scale], [ + y + height - bottom * scale, + bottom * scale, + ]]; - // bottom right - draw_texture_region( - image, - source_x + source_width - right, - source_y + source_height - bottom, - right, - bottom, - x + width - right, - y + height - bottom, - right, - bottom, - false, - false, - color[0], - color[1], - color[2], - color[3], - ); - - // top - draw_texture_region( - image, - source_x + left, - source_y, - center_width, - top, - x + left, - y, - dest_center_width, - top, - false, - false, - color[0], - color[1], - color[2], - color[3], - ); - - // bottom - draw_texture_region( - image, - source_x + left, - source_y + source_height - bottom, - center_width, - bottom, - x + left, - y + height - bottom, - dest_center_width, - bottom, - false, - false, - color[0], - color[1], - color[2], - color[3], - ); - - // left - draw_texture_region( - image, - source_x, - source_y + top, - left, - center_height, - x, - y + top, - left, - dest_center_height, - false, - false, - color[0], - color[1], - color[2], - color[3], - ); - - // right - draw_texture_region( - image, - source_x + source_width - right, - source_y + top, - right, - center_height, - x + width - right, - y + top, - right, - dest_center_height, - false, - false, - color[0], - color[1], - color[2], - color[3], - ); - - // center ! - draw_texture_region( - image, - source_x + left, - source_y + top, - center_width, - center_height, - x + left, - y + top, - dest_center_width, - dest_center_height, - false, - false, - color[0], - color[1], - color[2], - color[3], - ); + for (let row = 0; row < 3; row++) { + for (let column = 0; column < 3; column++) { + const [sx, sw] = source_columns[column]; + const [sy, sh] = source_rows[row]; + const [dx, dw] = columns[column]; + const [dy, dh] = rows[row]; + if (sw > 0 && sh > 0 && dw > 0 && dh > 0) { + const [r, g, b, a] = color; + draw_texture_region(image, sx, sy, sw, sh, dx, dy, dw, dh, false, false, r, g, b, a); + } + } + } } export function draw_item(item: ItemStack, x: number, y: number) { diff --git a/common/mod_api/client.ts b/common/mod_api/client.ts index b61247f..d0c4ddf 100644 --- a/common/mod_api/client.ts +++ b/common/mod_api/client.ts @@ -46,7 +46,7 @@ export interface ModScreen { export type Color = [number, number, number, number]; -// immediate mode drawing, like the debug ui, styled with assets/sprites/ui.png +// immediate mode drawing, like the debug ui, styled with the gui sprites (assets/sprites/gui.png) export interface Graphics { readonly width: number; readonly height: number; diff --git a/mods/recipe_viewer/scripts/client.ts b/mods/recipe_viewer/scripts/client.ts index 79421c6..131a02d 100644 --- a/mods/recipe_viewer/scripts/client.ts +++ b/mods/recipe_viewer/scripts/client.ts @@ -9,10 +9,11 @@ const GAP = 16; const TEXT = 22; const LINE = 30; const BUTTON = 32; -const SLOT_COLOR: Color = [0, 0, 0, 0.25]; -const HOVER_COLOR: Color = [1, 1, 1, 0.35]; -const LABEL_COLOR: Color = [0.25, 0.25, 0.25, 1]; -const MOD_COLOR: Color = [0.33, 0.33, 1, 1]; +// catppuccin mocha, like the engine's gui: crust, pink, subtext 0 and blue +const SLOT_COLOR: Color = [0.07, 0.07, 0.11, 0.6]; +const HOVER_COLOR: Color = [0.96, 0.76, 0.91, 0.35]; +const LABEL_COLOR: Color = [0.65, 0.68, 0.78, 1]; +const MOD_COLOR: Color = [0.54, 0.71, 0.98, 1]; // alternatives, like "any vegetable", take turns this often const CYCLE_MS = 1000; diff --git a/tests/bmod_test.ts b/tests/bmod_test.ts index e662e1b..9750622 100644 --- a/tests/bmod_test.ts +++ b/tests/bmod_test.ts @@ -80,7 +80,7 @@ Deno.test("broken .bmod files are turned away, saying why", () => { "scripts.server scripts/server.js isn't in the file", ); // a texture that isn't 16x16 - const ui = Deno.readFileSync("assets/sprites/ui.png"); + const ui = Deno.readFileSync("assets/sprites/gui.png"); assertThrows( () => read_bmod( diff --git a/tests/mod_tools_test.ts b/tests/mod_tools_test.ts index 7665d7e..43e5a72 100644 --- a/tests/mod_tools_test.ts +++ b/tests/mod_tools_test.ts @@ -92,7 +92,7 @@ Deno.test("check-mods finds broken mods", async () => { }, }); // wrong texture size - Deno.copyFileSync("assets/sprites/ui.png", `${mod}/textures/huge.png`); + Deno.copyFileSync("assets/sprites/gui.png", `${mod}/textures/huge.png`); // a script that doesn't typecheck Deno.writeTextFileSync(`${mod}/scripts/client.ts`, "export function setup(ctx: number) { ctx.nope(); }\n"); // a dependency that isn't installed, and no longer depending on bworld diff --git a/tools/pack_gui.ts b/tools/pack_gui.ts new file mode 100644 index 0000000..5604ad6 --- /dev/null +++ b/tools/pack_gui.ts @@ -0,0 +1,175 @@ +// deno run -A tools/pack_gui.ts +// 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; size: number; border: Border }>; +} + +const SOURCES: Record = { + 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; 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 "); + Deno.exit(1); + } + await main(Deno.args[0]); +}