Change the gui

This commit is contained in:
2026-09-27 14:52:30 -03:00
parent d0cbec1846
commit 29e61a235b
20 changed files with 451 additions and 359 deletions
+1 -1
View File
@@ -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;
+29 -2
View File
@@ -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
Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.6 KiB

+29
View File
@@ -0,0 +1,29 @@
// made by tools/pack_gui.ts, where each gui sprite is in assets/sprites/gui.png. border is the nine slice border as
// left, top, right, bottom, all 0 for sprites drawn whole
export const GUI_ATLAS = {
button: { x: 0, y: 108, width: 200, height: 20, border: [3, 3, 3, 3] },
button_highlighted: { x: 0, y: 129, width: 200, height: 20, border: [3, 3, 3, 3] },
button_disabled: { x: 0, y: 150, width: 200, height: 20, border: [3, 3, 3, 3] },
text_field: { x: 0, y: 171, width: 200, height: 20, border: [2, 2, 2, 2] },
text_field_highlighted: { x: 0, y: 192, width: 200, height: 20, border: [2, 2, 2, 2] },
panel: { x: 236, y: 192, width: 12, height: 12, border: [4, 4, 4, 4] },
tooltip: { x: 0, y: 0, width: 84, height: 84, border: [3, 3, 3, 3] },
slot: { x: 201, y: 192, width: 18, height: 18, border: [0, 0, 0, 0] },
slot_highlight_back: { x: 85, y: 0, width: 24, height: 24, border: [4, 4, 4, 4] },
slot_highlight_front: { x: 110, y: 0, width: 24, height: 24, border: [4, 4, 4, 4] },
hotbar: { x: 0, y: 85, width: 182, height: 22, border: [0, 0, 0, 0] },
hotbar_selection: { x: 135, y: 0, width: 24, height: 23, border: [0, 0, 0, 0] },
crosshair: { x: 220, y: 192, width: 15, height: 15, border: [0, 0, 0, 0] },
heart_container: { x: 0, y: 213, width: 9, height: 9, border: [0, 0, 0, 0] },
heart_full: { x: 10, y: 213, width: 9, height: 9, border: [0, 0, 0, 0] },
heart_half: { x: 20, y: 213, width: 9, height: 9, border: [0, 0, 0, 0] },
food_empty: { x: 30, y: 213, width: 9, height: 9, border: [0, 0, 0, 0] },
food_full: { x: 40, y: 213, width: 9, height: 9, border: [0, 0, 0, 0] },
food_half: { x: 50, y: 213, width: 9, height: 9, border: [0, 0, 0, 0] },
saturation_1: { x: 60, y: 213, width: 9, height: 9, border: [0, 0, 0, 0] },
saturation_2: { x: 70, y: 213, width: 9, height: 9, border: [0, 0, 0, 0] },
saturation_3: { x: 80, y: 213, width: 9, height: 9, border: [0, 0, 0, 0] },
saturation_4: { x: 90, y: 213, width: 9, height: 9, border: [0, 0, 0, 0] },
} as const;
export type GuiSprite = keyof typeof GUI_ATLAS;
+14 -29
View File
@@ -3,11 +3,15 @@ import { canvas, draw_rect, draw_texture_region, Texture } from "$/client/render
import { AssetManager } from "../assets.ts";
import { SLOT_SIZE, TEXTURE_SIZE } from "$/common/constants.ts";
import { ClientMessage, ScreenLayout } from "$/common/protocol.ts";
import { draw_nine_slice } from "../rendering/render_utils.ts";
import { draw_gui_sprite, GUI_SCALE } from "./gui_sprites.ts";
import { get_sprite_region } from "$/client/sprites.ts";
import { ClientInventories } from "../inventory.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;
// where the screen's own slots start, below the player's
const AREA_Y = MARGIN + GAP * 2 + SLOT_SIZE * 4;
// a screen opened by the server (chest, furnace, ...), drawn from the layout it sent
export class GuiContainer extends GuiInventoryScreen {
@@ -24,16 +28,15 @@ export class GuiContainer extends GuiInventoryScreen {
this.layout = layout;
this.properties = properties;
this.inventory_width = PADDING * 2 + SLOT_SIZE * 9;
this.inventory_height = PADDING * 4 + SLOT_SIZE * (4 + layout.rows);
this.inventory_width = MARGIN * 2 + SLOT_SIZE * 9;
this.inventory_height = MARGIN * 2 + GAP * 2 + SLOT_SIZE * (4 + layout.rows);
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 area_y = PADDING * 3 + SLOT_SIZE * 4;
for (const slot of layout.slots) {
this.slots.push(
new Slot("screen", slot.index, PADDING + slot.x * SLOT_SIZE, area_y + slot.y * SLOT_SIZE, slot.output),
new Slot("screen", slot.index, MARGIN + slot.x * SLOT_SIZE, AREA_Y + slot.y * SLOT_SIZE, slot.output),
);
}
}
@@ -41,23 +44,7 @@ export class GuiContainer extends GuiInventoryScreen {
override on_render(): void {
draw_rect(0, 0, canvas.width, canvas.height, [0, 0, 0, 0.8]);
const ui = AssetManager.instance.get<Texture>("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<Texture>("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;
+12 -27
View File
@@ -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<Texture>("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();
}
+16 -21
View File
@@ -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<Texture>("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();
+39
View File
@@ -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<Texture>("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,
);
}
+77 -76
View File
@@ -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<Texture>("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;
}
+9 -7
View File
@@ -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<Texture>("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<Texture>("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);
}
+5 -7
View File
@@ -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);
});
+8 -10
View File
@@ -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<Texture>("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<Texture>("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;
+1 -1
View File
@@ -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");
+28 -171
View File
@@ -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) {
+1 -1
View File
@@ -46,7 +46,7 @@ export interface ModScreen<Props = unknown> {
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;
+5 -4
View File
@@ -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;
+1 -1
View File
@@ -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(
+1 -1
View File
@@ -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
+175
View File
@@ -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]);
}