Remove duplicate code
This commit is contained in:
+12
-97
@@ -3,7 +3,8 @@
|
||||
|
||||
import { point_inside_rec } from "$/common/utils.ts";
|
||||
import { InputManager } from "$/client/input_manager.ts";
|
||||
import { begin_clip, draw_rect, draw_rect_stroke, draw_text, end_clip, measure_text } from "./renderer/mod.ts";
|
||||
import { TextInput } from "$/client/gui/widgets.ts";
|
||||
import { begin_clip, draw_rect, draw_rect_stroke, draw_text, end_clip } from "./renderer/mod.ts";
|
||||
|
||||
const HEADER_HEIGHT = 24;
|
||||
const RESIZE_SIZE = 12;
|
||||
@@ -21,20 +22,11 @@ interface WindowState {
|
||||
content_height: number;
|
||||
}
|
||||
|
||||
interface TextInputState {
|
||||
value: string;
|
||||
caret: number;
|
||||
focused: boolean;
|
||||
last_blink: number;
|
||||
show_caret: boolean;
|
||||
key_repeat_timer: number;
|
||||
}
|
||||
|
||||
export class DebugUI {
|
||||
static windows = new Map<string, WindowState>();
|
||||
static current_window: WindowState | undefined = undefined;
|
||||
|
||||
static text_inputs = new Map<string, TextInputState>();
|
||||
static text_inputs = new Map<string, TextInput>();
|
||||
static active_text_input: string | undefined = undefined;
|
||||
|
||||
static open_sections = new Map<string, boolean>();
|
||||
@@ -324,15 +316,7 @@ export class DebugUI {
|
||||
|
||||
let state = this.text_inputs.get(id);
|
||||
if (!state) {
|
||||
// initial state
|
||||
state = {
|
||||
value,
|
||||
caret: value.length,
|
||||
focused: false,
|
||||
last_blink: performance.now(),
|
||||
show_caret: true,
|
||||
key_repeat_timer: 0,
|
||||
};
|
||||
state = new TextInput(value, Infinity);
|
||||
this.text_inputs.set(id, state);
|
||||
}
|
||||
// update on every call
|
||||
@@ -350,11 +334,9 @@ export class DebugUI {
|
||||
if (hovered && InputManager.is_mouse_pressed(0)) {
|
||||
InputManager.consume_mouse(0);
|
||||
this.active_text_input = id;
|
||||
state.focused = true;
|
||||
state.caret = value.length;
|
||||
state.move_caret(value.length);
|
||||
} else if (InputManager.is_mouse_pressed(0) && !hovered) {
|
||||
if (this.active_text_input === id) {
|
||||
state.focused = false;
|
||||
this.active_text_input = undefined;
|
||||
}
|
||||
}
|
||||
@@ -362,18 +344,7 @@ export class DebugUI {
|
||||
const is_active = this.active_text_input === id;
|
||||
|
||||
if (is_active) {
|
||||
this.#handle_text_editing(state);
|
||||
}
|
||||
|
||||
// overkill?
|
||||
if (is_active) {
|
||||
const now = performance.now();
|
||||
if (now - state.last_blink > 500) {
|
||||
state.show_caret = !state.show_caret;
|
||||
state.last_blink = now;
|
||||
}
|
||||
} else {
|
||||
state.show_caret = false;
|
||||
state.handle_keys();
|
||||
}
|
||||
|
||||
// draw box
|
||||
@@ -396,14 +367,14 @@ export class DebugUI {
|
||||
|
||||
const text_x = x + 6;
|
||||
|
||||
draw_text(state.value, x + 6, y + 4);
|
||||
if (is_active) {
|
||||
state.draw_selection(text_x, y + 4, height - 8, 1);
|
||||
}
|
||||
draw_text(state.value, text_x, y + 4);
|
||||
|
||||
// caret !
|
||||
if (is_active && state.show_caret) {
|
||||
const before_text = state.value.substring(0, state.caret);
|
||||
const caret_x = text_x + measure_text(before_text);
|
||||
|
||||
draw_rect(caret_x, y + 4, 1, height - 4);
|
||||
if (is_active && TextInput.caret_visible()) {
|
||||
draw_rect(state.caret_x(text_x, 1), y + 4, 1, height - 4);
|
||||
}
|
||||
|
||||
this.cursor_y += height + PADDING;
|
||||
@@ -459,60 +430,4 @@ export class DebugUI {
|
||||
// thumb
|
||||
draw_rect(x, thumb_y, scrollbar_width, thumb_height, [0x88 / 255, 0x88 / 255, 0x88 / 255, 1]);
|
||||
}
|
||||
|
||||
static #handle_text_editing(state: TextInputState) {
|
||||
const now = performance.now();
|
||||
const repeat_delay = 400;
|
||||
const repeat_rate = 40;
|
||||
|
||||
function allow_repeat(): boolean {
|
||||
if (InputManager.is_key_pressed("Backspace")) {
|
||||
state.key_repeat_timer = now;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (InputManager.is_key_down("Backspace")) {
|
||||
if (now - state.key_repeat_timer > repeat_delay) {
|
||||
state.key_repeat_timer = now - (repeat_delay - repeat_rate);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (allow_repeat()) {
|
||||
if (state.caret > 0) {
|
||||
state.value = state.value.slice(0, state.caret - 1) + state.value.slice(state.caret);
|
||||
state.caret -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (InputManager.is_key_pressed("Delete")) {
|
||||
state.value = state.value.slice(0, state.caret) + state.value.slice(state.caret + 1);
|
||||
}
|
||||
|
||||
if (InputManager.is_key_pressed("ArrowLeft")) {
|
||||
state.caret = Math.max(0, state.caret - 1);
|
||||
}
|
||||
|
||||
if (InputManager.is_key_pressed("ArrowRight")) {
|
||||
state.caret = Math.min(state.value.length, state.caret + 1);
|
||||
}
|
||||
|
||||
if (InputManager.is_key_pressed("Home")) {
|
||||
state.caret = 0;
|
||||
}
|
||||
|
||||
if (InputManager.is_key_pressed("End")) {
|
||||
state.caret = state.value.length;
|
||||
}
|
||||
|
||||
const typed = InputManager.get_typed_characters();
|
||||
|
||||
for (const char of typed) {
|
||||
state.value = state.value.slice(0, state.caret) + char + state.value.slice(state.caret);
|
||||
state.caret += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
import type { EntityData, EntityEvent, EntityInfo } from "$/common/protocol.ts";
|
||||
import type { EntityJson } from "$/common/mod_data.ts";
|
||||
import { EverythingRegistry } from "$/common/everything_registry.ts";
|
||||
import { wrap_angle } from "$/common/utils.ts";
|
||||
import type { ClientLevel } from "../level/client_level.ts";
|
||||
import { Entity } from "./entity.ts";
|
||||
|
||||
@@ -88,10 +89,3 @@ export class RemoteEntity extends Entity {
|
||||
return this.swing_ticks === undefined ? 0 : Math.min(1, (this.swing_ticks + partial_tick) / SWING_TICKS);
|
||||
}
|
||||
}
|
||||
|
||||
function wrap_angle(angle: number) {
|
||||
angle %= Math.PI * 2;
|
||||
if (angle >= Math.PI) angle -= Math.PI * 2;
|
||||
if (angle < -Math.PI) angle += Math.PI * 2;
|
||||
return angle;
|
||||
}
|
||||
|
||||
@@ -137,6 +137,26 @@ export class ModGraphics implements Graphics {
|
||||
);
|
||||
}
|
||||
|
||||
progress(
|
||||
empty: string,
|
||||
full: string,
|
||||
x: number,
|
||||
y: number,
|
||||
w: number,
|
||||
h: number,
|
||||
filled: number,
|
||||
direction: "right" | "up" = "right",
|
||||
) {
|
||||
// nothing filled for NaN too, like progress / max before there's a max
|
||||
const done = filled > 0 ? Math.min(1, filled) : 0;
|
||||
this.texture(empty, x, y, w, h);
|
||||
if (direction === "right") {
|
||||
this.texture(full, x, y, w * done, h, [0, 0, done, 1]);
|
||||
} else {
|
||||
this.texture(full, x, y + h * (1 - done), w, h * done, [0, 1 - done, 1, done]);
|
||||
}
|
||||
}
|
||||
|
||||
item(id: string, x: number, y: number, count = 1) {
|
||||
if (!EverythingRegistry.get<ItemRegistry>("items", id)) throw new Error(`unknown item ${id}`);
|
||||
const stack = new ItemStack(id, count);
|
||||
|
||||
@@ -567,6 +567,14 @@ export function set_current_texture(texture: GPUTexture) {
|
||||
current_texture = texture;
|
||||
}
|
||||
|
||||
// draws with texture from now on, flushing what was batched with another one first
|
||||
export function use_texture(texture: GPUTexture) {
|
||||
if (current_texture !== texture) {
|
||||
flush_batch();
|
||||
current_texture = texture;
|
||||
}
|
||||
}
|
||||
|
||||
export function create_texture(width: number, height: number): GPUTexture {
|
||||
return device.createTexture({
|
||||
size: [width, height],
|
||||
|
||||
@@ -5,5 +5,4 @@ export * from "./core.ts";
|
||||
export * from "./shapes.ts";
|
||||
export * from "./textures.ts";
|
||||
export * from "./text.ts";
|
||||
export * from "./models.ts";
|
||||
export * from "./types.ts";
|
||||
|
||||
@@ -1,262 +0,0 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import { push_vertex } from "./core.ts";
|
||||
import type { Texture } from "./types.ts";
|
||||
|
||||
const pad = 0.5;
|
||||
|
||||
export function push_front_face(
|
||||
texture: Texture,
|
||||
x: number,
|
||||
y: number,
|
||||
z: number,
|
||||
sx: number,
|
||||
sy: number,
|
||||
sw: number,
|
||||
sh: number,
|
||||
r = 1,
|
||||
g = 1,
|
||||
b = 1,
|
||||
a = 1,
|
||||
) {
|
||||
const x2 = x + 1;
|
||||
const y2 = y + 1;
|
||||
const z2 = z + 1;
|
||||
|
||||
const u0 = (sx + pad) / texture.width;
|
||||
const v0 = (sy + pad) / texture.height;
|
||||
const u1 = (sx + sw - pad) / texture.width;
|
||||
const v1 = (sy + sh - pad) / texture.height;
|
||||
|
||||
push_vertex(x, y, z2, u0, v1, r, g, b, a);
|
||||
push_vertex(x2, y, z2, u1, v1, r, g, b, a);
|
||||
push_vertex(x2, y2, z2, u1, v0, r, g, b, a);
|
||||
|
||||
push_vertex(x, y, z2, u0, v1, r, g, b, a);
|
||||
push_vertex(x2, y2, z2, u1, v0, r, g, b, a);
|
||||
push_vertex(x, y2, z2, u0, v0, r, g, b, a);
|
||||
}
|
||||
|
||||
export function push_back_face(
|
||||
texture: Texture,
|
||||
x: number,
|
||||
y: number,
|
||||
z: number,
|
||||
sx: number,
|
||||
sy: number,
|
||||
sw: number,
|
||||
sh: number,
|
||||
r = 1,
|
||||
g = 1,
|
||||
b = 1,
|
||||
a = 1,
|
||||
) {
|
||||
const x2 = x + 1;
|
||||
const y2 = y + 1;
|
||||
|
||||
const u0 = (sx + pad) / texture.width;
|
||||
const v0 = (sy + pad) / texture.height;
|
||||
const u1 = (sx + sw - pad) / texture.width;
|
||||
const v1 = (sy + sh - pad) / texture.height;
|
||||
|
||||
push_vertex(x2, y, z, u0, v1, r, g, b, a);
|
||||
push_vertex(x, y, z, u1, v1, r, g, b, a);
|
||||
push_vertex(x, y2, z, u1, v0, r, g, b, a);
|
||||
|
||||
push_vertex(x2, y, z, u0, v1, r, g, b, a);
|
||||
push_vertex(x, y2, z, u1, v0, r, g, b, a);
|
||||
push_vertex(x2, y2, z, u0, v0, r, g, b, a);
|
||||
}
|
||||
|
||||
export function push_left_face(
|
||||
texture: Texture,
|
||||
x: number,
|
||||
y: number,
|
||||
z: number,
|
||||
sx: number,
|
||||
sy: number,
|
||||
sw: number,
|
||||
sh: number,
|
||||
r = 1,
|
||||
g = 1,
|
||||
b = 1,
|
||||
a = 1,
|
||||
) {
|
||||
const y2 = y + 1;
|
||||
const z2 = z + 1;
|
||||
|
||||
const u0 = (sx + pad) / texture.width;
|
||||
const v0 = (sy + pad) / texture.height;
|
||||
const u1 = (sx + sw - pad) / texture.width;
|
||||
const v1 = (sy + sh - pad) / texture.height;
|
||||
|
||||
push_vertex(x, y, z, u0, v1, r, g, b, a);
|
||||
push_vertex(x, y, z2, u1, v1, r, g, b, a);
|
||||
push_vertex(x, y2, z2, u1, v0, r, g, b, a);
|
||||
|
||||
push_vertex(x, y, z, u0, v1, r, g, b, a);
|
||||
push_vertex(x, y2, z2, u1, v0, r, g, b, a);
|
||||
push_vertex(x, y2, z, u0, v0, r, g, b, a);
|
||||
}
|
||||
|
||||
export function push_right_face(
|
||||
texture: Texture,
|
||||
x: number,
|
||||
y: number,
|
||||
z: number,
|
||||
sx: number,
|
||||
sy: number,
|
||||
sw: number,
|
||||
sh: number,
|
||||
r = 1,
|
||||
g = 1,
|
||||
b = 1,
|
||||
a = 1,
|
||||
) {
|
||||
const x2 = x + 1;
|
||||
const y2 = y + 1;
|
||||
const z2 = z + 1;
|
||||
|
||||
const u0 = (sx + pad) / texture.width;
|
||||
const v0 = (sy + pad) / texture.height;
|
||||
const u1 = (sx + sw - pad) / texture.width;
|
||||
const v1 = (sy + sh - pad) / texture.height;
|
||||
|
||||
push_vertex(x2, y, z2, u0, v1, r, g, b, a);
|
||||
push_vertex(x2, y, z, u1, v1, r, g, b, a);
|
||||
push_vertex(x2, y2, z, u1, v0, r, g, b, a);
|
||||
|
||||
push_vertex(x2, y, z2, u0, v1, r, g, b, a);
|
||||
push_vertex(x2, y2, z, u1, v0, r, g, b, a);
|
||||
push_vertex(x2, y2, z2, u0, v0, r, g, b, a);
|
||||
}
|
||||
|
||||
export function push_top_face(
|
||||
texture: Texture,
|
||||
x: number,
|
||||
y: number,
|
||||
z: number,
|
||||
sx: number,
|
||||
sy: number,
|
||||
sw: number,
|
||||
sh: number,
|
||||
r = 1,
|
||||
g = 1,
|
||||
b = 1,
|
||||
a = 1,
|
||||
) {
|
||||
const x2 = x + 1;
|
||||
const z2 = z + 1;
|
||||
const y2 = y + 1;
|
||||
|
||||
const u0 = (sx + pad) / texture.width;
|
||||
const v0 = (sy + pad) / texture.height;
|
||||
const u1 = (sx + sw - pad) / texture.width;
|
||||
const v1 = (sy + sh - pad) / texture.height;
|
||||
|
||||
push_vertex(x, y2, z2, u0, v1, r, g, b, a);
|
||||
push_vertex(x2, y2, z2, u1, v1, r, g, b, a);
|
||||
push_vertex(x2, y2, z, u1, v0, r, g, b, a);
|
||||
|
||||
push_vertex(x, y2, z2, u0, v1, r, g, b, a);
|
||||
push_vertex(x2, y2, z, u1, v0, r, g, b, a);
|
||||
push_vertex(x, y2, z, u0, v0, r, g, b, a);
|
||||
}
|
||||
|
||||
export function push_bottom_face(
|
||||
texture: Texture,
|
||||
x: number,
|
||||
y: number,
|
||||
z: number,
|
||||
sx: number,
|
||||
sy: number,
|
||||
sw: number,
|
||||
sh: number,
|
||||
r = 1,
|
||||
g = 1,
|
||||
b = 1,
|
||||
a = 1,
|
||||
) {
|
||||
const x2 = x + 1;
|
||||
const z2 = z + 1;
|
||||
|
||||
const u0 = (sx + pad) / texture.width;
|
||||
const v0 = (sy + pad) / texture.height;
|
||||
const u1 = (sx + sw - pad) / texture.width;
|
||||
const v1 = (sy + sh - pad) / texture.height;
|
||||
|
||||
push_vertex(x, y, z, u0, v1, r, g, b, a);
|
||||
push_vertex(x2, y, z, u1, v1, r, g, b, a);
|
||||
push_vertex(x2, y, z2, u1, v0, r, g, b, a);
|
||||
|
||||
push_vertex(x, y, z, u0, v1, r, g, b, a);
|
||||
push_vertex(x2, y, z2, u1, v0, r, g, b, a);
|
||||
push_vertex(x, y, z2, u0, v0, r, g, b, a);
|
||||
}
|
||||
|
||||
// solid colored box, draw it with white_tex as the current texture
|
||||
export function push_box(
|
||||
x: number,
|
||||
y: number,
|
||||
z: number,
|
||||
width: number,
|
||||
height: number,
|
||||
depth: number,
|
||||
r = 1,
|
||||
g = 1,
|
||||
b = 1,
|
||||
a = 1,
|
||||
) {
|
||||
const x2 = x + width;
|
||||
const y2 = y + height;
|
||||
const z2 = z + depth;
|
||||
|
||||
// front
|
||||
push_vertex(x, y, z2, 0, 1, r, g, b, a);
|
||||
push_vertex(x2, y, z2, 1, 1, r, g, b, a);
|
||||
push_vertex(x2, y2, z2, 1, 0, r, g, b, a);
|
||||
push_vertex(x, y, z2, 0, 1, r, g, b, a);
|
||||
push_vertex(x2, y2, z2, 1, 0, r, g, b, a);
|
||||
push_vertex(x, y2, z2, 0, 0, r, g, b, a);
|
||||
|
||||
// back
|
||||
push_vertex(x2, y, z, 0, 1, r * 0.8, g * 0.8, b * 0.8, a);
|
||||
push_vertex(x, y, z, 1, 1, r * 0.8, g * 0.8, b * 0.8, a);
|
||||
push_vertex(x, y2, z, 1, 0, r * 0.8, g * 0.8, b * 0.8, a);
|
||||
push_vertex(x2, y, z, 0, 1, r * 0.8, g * 0.8, b * 0.8, a);
|
||||
push_vertex(x, y2, z, 1, 0, r * 0.8, g * 0.8, b * 0.8, a);
|
||||
push_vertex(x2, y2, z, 0, 0, r * 0.8, g * 0.8, b * 0.8, a);
|
||||
|
||||
// left
|
||||
push_vertex(x, y, z, 0, 1, r * 0.9, g * 0.9, b * 0.9, a);
|
||||
push_vertex(x, y, z2, 1, 1, r * 0.9, g * 0.9, b * 0.9, a);
|
||||
push_vertex(x, y2, z2, 1, 0, r * 0.9, g * 0.9, b * 0.9, a);
|
||||
push_vertex(x, y, z, 0, 1, r * 0.9, g * 0.9, b * 0.9, a);
|
||||
push_vertex(x, y2, z2, 1, 0, r * 0.9, g * 0.9, b * 0.9, a);
|
||||
push_vertex(x, y2, z, 0, 0, r * 0.9, g * 0.9, b * 0.9, a);
|
||||
|
||||
// right
|
||||
push_vertex(x2, y, z2, 0, 1, r * 0.9, g * 0.9, b * 0.9, a);
|
||||
push_vertex(x2, y, z, 1, 1, r * 0.9, g * 0.9, b * 0.9, a);
|
||||
push_vertex(x2, y2, z, 1, 0, r * 0.9, g * 0.9, b * 0.9, a);
|
||||
push_vertex(x2, y, z2, 0, 1, r * 0.9, g * 0.9, b * 0.9, a);
|
||||
push_vertex(x2, y2, z, 1, 0, r * 0.9, g * 0.9, b * 0.9, a);
|
||||
push_vertex(x2, y2, z2, 0, 0, r * 0.9, g * 0.9, b * 0.9, a);
|
||||
|
||||
// top
|
||||
push_vertex(x, y2, z2, 0, 1, r, g, b, a);
|
||||
push_vertex(x2, y2, z2, 1, 1, r, g, b, a);
|
||||
push_vertex(x2, y2, z, 1, 0, r, g, b, a);
|
||||
push_vertex(x, y2, z2, 0, 1, r, g, b, a);
|
||||
push_vertex(x2, y2, z, 1, 0, r, g, b, a);
|
||||
push_vertex(x, y2, z, 0, 0, r, g, b, a);
|
||||
|
||||
// bottom
|
||||
push_vertex(x, y, z, 0, 1, r * 0.6, g * 0.6, b * 0.6, a);
|
||||
push_vertex(x2, y, z, 1, 1, r * 0.6, g * 0.6, b * 0.6, a);
|
||||
push_vertex(x2, y, z2, 1, 0, r * 0.6, g * 0.6, b * 0.6, a);
|
||||
push_vertex(x, y, z, 0, 1, r * 0.6, g * 0.6, b * 0.6, a);
|
||||
push_vertex(x2, y, z2, 1, 0, r * 0.6, g * 0.6, b * 0.6, a);
|
||||
push_vertex(x, y, z2, 0, 0, r * 0.6, g * 0.6, b * 0.6, a);
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import { flush_batch, get_current_texture, push_quad, push_vertex, set_current_texture, white_tex } from "./core.ts";
|
||||
import { push_quad, push_vertex, use_texture, white_tex } from "./core.ts";
|
||||
|
||||
export function draw_rect(
|
||||
x: number,
|
||||
@@ -10,10 +10,7 @@ export function draw_rect(
|
||||
h: number,
|
||||
color: [number, number, number, number] = [1, 1, 1, 1],
|
||||
) {
|
||||
if (get_current_texture() !== white_tex) {
|
||||
flush_batch();
|
||||
set_current_texture(white_tex!);
|
||||
}
|
||||
use_texture(white_tex!);
|
||||
|
||||
push_quad(x, y, w, h, 0, 0, 1, 1, color[0], color[1], color[2], color[3]);
|
||||
}
|
||||
@@ -89,9 +86,6 @@ export function draw_rect_3d(
|
||||
h: number,
|
||||
color: readonly number[] = [1, 1, 1, 1],
|
||||
) {
|
||||
if (get_current_texture() !== white_tex) {
|
||||
flush_batch();
|
||||
set_current_texture(white_tex!);
|
||||
}
|
||||
use_texture(white_tex!);
|
||||
push_plane_quad(plane, x, y, w, h, 0, 0, 1, 1, color);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import { AssetManager } from "../assets.ts";
|
||||
import { flush_batch, get_current_texture, push_quad, set_current_texture } from "./core.ts";
|
||||
import { push_quad, use_texture } from "./core.ts";
|
||||
import { type Plane3d, push_plane_quad } from "./shapes.ts";
|
||||
import { FntChar, FntFont, Texture } from "./types.ts";
|
||||
|
||||
@@ -79,10 +79,7 @@ export function draw_text(str: string, x: number, y: number, scale: number = 1,
|
||||
return;
|
||||
}
|
||||
|
||||
if (get_current_texture() !== font_texture.tex) {
|
||||
flush_batch();
|
||||
set_current_texture(font_texture.tex);
|
||||
}
|
||||
use_texture(font_texture.tex);
|
||||
|
||||
for (const ch of str) {
|
||||
const glyph = font_fnt.chars[ch.charCodeAt(0)];
|
||||
@@ -120,10 +117,7 @@ export function draw_text_3d(
|
||||
return;
|
||||
}
|
||||
|
||||
if (get_current_texture() !== font_texture.tex) {
|
||||
flush_batch();
|
||||
set_current_texture(font_texture.tex);
|
||||
}
|
||||
use_texture(font_texture.tex);
|
||||
|
||||
for (const ch of str) {
|
||||
const glyph = font_fnt.chars[ch.charCodeAt(0)];
|
||||
|
||||
+23
-57
@@ -1,15 +1,7 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {
|
||||
create_texture,
|
||||
device,
|
||||
flush_batch,
|
||||
get_current_texture,
|
||||
push_quad,
|
||||
push_quad_vertices,
|
||||
set_current_texture,
|
||||
} from "./core.ts";
|
||||
import { create_texture, device, push_quad, push_quad_vertices, use_texture } from "./core.ts";
|
||||
import { Texture } from "./types.ts";
|
||||
|
||||
export function load_texture(image: HTMLImageElement | ImageBitmap): Texture {
|
||||
@@ -32,21 +24,7 @@ export function draw_texture(
|
||||
flip_x = false,
|
||||
flip_y = false,
|
||||
) {
|
||||
if (get_current_texture() !== texture.tex) {
|
||||
flush_batch();
|
||||
set_current_texture(texture.tex);
|
||||
}
|
||||
|
||||
let u0 = 0, v0 = 0, u1 = 1, v1 = 1;
|
||||
|
||||
if (flip_x) {
|
||||
[u0, u1] = [u1, u0];
|
||||
}
|
||||
if (flip_y) {
|
||||
[v0, v1] = [v1, v0];
|
||||
}
|
||||
|
||||
push_quad(x, y, w, h, u0, v0, u1, v1);
|
||||
draw_texture_region(texture, 0, 0, texture.width, texture.height, x, y, w, h, flip_x, flip_y);
|
||||
}
|
||||
|
||||
export function draw_texture_region(
|
||||
@@ -66,23 +44,8 @@ export function draw_texture_region(
|
||||
b = 1,
|
||||
a = 1,
|
||||
) {
|
||||
if (get_current_texture() !== texture.tex) {
|
||||
flush_batch();
|
||||
set_current_texture(texture.tex);
|
||||
}
|
||||
|
||||
let u0 = sx / texture.width;
|
||||
let v0 = sy / texture.height;
|
||||
let u1 = (sx + sw) / texture.width;
|
||||
let v1 = (sy + sh) / texture.height;
|
||||
|
||||
if (flip_x) {
|
||||
[u0, u1] = [u1, u0];
|
||||
}
|
||||
if (flip_y) {
|
||||
[v0, v1] = [v1, v0];
|
||||
}
|
||||
|
||||
use_texture(texture.tex);
|
||||
const [u0, v0, u1, v1] = region_uvs(texture, sx, sy, sw, sh, flip_x, flip_y);
|
||||
push_quad(dx, dy, dw, dh, u0, v0, u1, v1, r, g, b, a);
|
||||
}
|
||||
|
||||
@@ -105,22 +68,8 @@ export function draw_texture_region_skewed(
|
||||
flip_x = false,
|
||||
flip_y = false,
|
||||
) {
|
||||
if (get_current_texture() !== texture.tex) {
|
||||
flush_batch();
|
||||
set_current_texture(texture.tex);
|
||||
}
|
||||
|
||||
let u0 = sx / texture.width;
|
||||
let v0 = sy / texture.height;
|
||||
let u1 = (sx + sw) / texture.width;
|
||||
let v1 = (sy + sh) / texture.height;
|
||||
|
||||
if (flip_x) {
|
||||
[u0, u1] = [u1, u0];
|
||||
}
|
||||
if (flip_y) {
|
||||
[v0, v1] = [v1, v0];
|
||||
}
|
||||
use_texture(texture.tex);
|
||||
const [u0, v0, u1, v1] = region_uvs(texture, sx, sy, sw, sh, flip_x, flip_y);
|
||||
|
||||
const p0 = points[0];
|
||||
const p1 = points[1];
|
||||
@@ -146,3 +95,20 @@ export function draw_texture_region_skewed(
|
||||
a,
|
||||
);
|
||||
}
|
||||
|
||||
// the uvs of a region of texture given in pixels, swapped to flip it
|
||||
function region_uvs(
|
||||
texture: Texture,
|
||||
sx: number,
|
||||
sy: number,
|
||||
sw: number,
|
||||
sh: number,
|
||||
flip_x: boolean,
|
||||
flip_y: boolean,
|
||||
) {
|
||||
const u = [sx / texture.width, (sx + sw) / texture.width];
|
||||
const v = [sy / texture.height, (sy + sh) / texture.height];
|
||||
if (flip_x) u.reverse();
|
||||
if (flip_y) v.reverse();
|
||||
return [u[0], v[0], u[1], v[1]];
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
// facing out of the screen, the body turned a little toward the mouse and the head turned and tilted more, with what
|
||||
// they wear, like their curios. the gui has no depth buffer, so the faces are drawn back to front with the ones facing
|
||||
// away left out, like 3d items in slots
|
||||
import { flush_batch, get_current_texture, push_vertex, set_current_texture, type Texture } from "../renderer/mod.ts";
|
||||
import { push_vertex, type Texture, use_texture } from "../renderer/mod.ts";
|
||||
import type { ItemStack } from "$/common/inventory.ts";
|
||||
import { AssetManager } from "../assets.ts";
|
||||
import { rotation_around } from "./model_math.ts";
|
||||
@@ -102,10 +102,7 @@ export function draw_gui_player(
|
||||
// blocks to skin pixels to the screen, y down
|
||||
const scale = 16 * pixel;
|
||||
for (const { vertices, texture } of faces) {
|
||||
if (get_current_texture() !== texture.tex) {
|
||||
flush_batch();
|
||||
set_current_texture(texture.tex);
|
||||
}
|
||||
use_texture(texture.tex);
|
||||
for (const [vx, vy, , u, v, r, g, b] of vertices) {
|
||||
push_vertex(x + vx * scale, feet_y - vy * scale, 0, u, v, r, g, b, 1);
|
||||
}
|
||||
|
||||
@@ -8,7 +8,14 @@ import { mat4, vec3 } from "gl-matrix";
|
||||
import type { ItemStack } from "$/common/inventory.ts";
|
||||
import { AssetManager } from "$/client/assets.ts";
|
||||
import type { LocalPlayer } from "$/client/entity/local_player.ts";
|
||||
import { begin_view_model, end_mode_3d, flush_batch, set_current_texture, Texture } from "$/client/renderer/mod.ts";
|
||||
import {
|
||||
begin_view_model,
|
||||
end_mode_3d,
|
||||
flush_batch,
|
||||
set_current_texture,
|
||||
Texture,
|
||||
use_texture,
|
||||
} from "$/client/renderer/mod.ts";
|
||||
import { item_block, push_item } from "./item_renderer.ts";
|
||||
import { push_cube, type Vec3 } from "./player_renderer.ts";
|
||||
import { right_arm } from "./player_model.ts";
|
||||
@@ -69,8 +76,7 @@ export class HandRenderer {
|
||||
push_cube(skin, arm, arm.overlay, arm.inflate, transform, tint);
|
||||
// what's worn on it goes over it, from the atlas
|
||||
if (player.worn.length > 0) {
|
||||
flush_batch();
|
||||
set_current_texture(AssetManager.instance.get<Texture>("bworld:textures").tex);
|
||||
use_texture(AssetManager.instance.get<Texture>("bworld:textures").tex);
|
||||
render_worn_arm(player, transform, tint);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ import {
|
||||
push_vertex,
|
||||
set_current_texture,
|
||||
Texture,
|
||||
use_texture,
|
||||
view_projection,
|
||||
white_tex,
|
||||
} from "$/client/renderer/mod.ts";
|
||||
@@ -123,10 +124,7 @@ export class LevelRenderer {
|
||||
for (const entity of level.entities.values()) {
|
||||
if (entity !== camera_entity && entity instanceof RemotePlayer) {
|
||||
const skin = entity.skin ?? default_skin;
|
||||
if (get_current_texture() !== skin.tex) {
|
||||
flush_batch();
|
||||
set_current_texture(skin.tex);
|
||||
}
|
||||
use_texture(skin.tex);
|
||||
const tint = entity_tint(entity, partial_tick, entity.hurt_time > 0 ? HURT_TINT : undefined);
|
||||
const pose = player_pose(entity, partial_tick);
|
||||
render_player(skin, pose, tint);
|
||||
|
||||
@@ -21,11 +21,9 @@ import {
|
||||
draw_text,
|
||||
draw_texture_region,
|
||||
draw_texture_region_skewed,
|
||||
flush_batch,
|
||||
get_current_texture,
|
||||
push_vertex,
|
||||
set_current_texture,
|
||||
Texture,
|
||||
use_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
|
||||
@@ -163,10 +161,7 @@ const ICON_VIEW = [-1, 1, -1];
|
||||
// a block's item_model in a slot: its faces that face the viewer, back to front
|
||||
function draw_item_model(block_info: BlockRegistry, quads: BakedQuad[], x: number, y: number) {
|
||||
const atlas = AssetManager.instance.get<Texture>("bworld:textures");
|
||||
if (get_current_texture() !== atlas.tex) {
|
||||
flush_batch();
|
||||
set_current_texture(atlas.tex);
|
||||
}
|
||||
use_texture(atlas.tex);
|
||||
const project = item_projection(x, y);
|
||||
const toward_viewer = (p: number[]) => p[0] * ICON_VIEW[0] + p[1] * ICON_VIEW[1] + p[2] * ICON_VIEW[2];
|
||||
const visible = quads
|
||||
|
||||
Reference in New Issue
Block a user