Awesome menu and saves
This commit is contained in:
+76
-1
@@ -3,7 +3,7 @@
|
||||
|
||||
import { point_inside_rec } from "@/common/utils.ts";
|
||||
import { InputManager, type KeyCode } from "@/client/input_manager.ts";
|
||||
import { draw_rect, draw_text, measure_text } from "@/client/renderer/mod.ts";
|
||||
import { draw_rect, draw_rect_stroke, draw_text, measure_text } from "@/client/renderer/mod.ts";
|
||||
import { draw_gui_sprite } from "./gui_sprites.ts";
|
||||
|
||||
export const TEXT_SCALE = 2;
|
||||
@@ -345,3 +345,78 @@ export class EditBox {
|
||||
return this.x + 10;
|
||||
}
|
||||
}
|
||||
|
||||
const LIST_ROW_HEIGHT = 40;
|
||||
const CHECK_SIZE = 20;
|
||||
|
||||
// rows of text that scroll with the mouse wheel, like minecraft's selection lists. a row can have a box ticked or not
|
||||
// in front, for lists of things to turn on and off
|
||||
export class ListBox {
|
||||
x = 0;
|
||||
y = 0;
|
||||
width: number;
|
||||
height: number;
|
||||
rows: { text: string; checked?: boolean }[] = [];
|
||||
selected = -1;
|
||||
// clicking a row, double is whether it was the second click of a double click
|
||||
on_click: (row: number, double: boolean) => void;
|
||||
#scroll = 0;
|
||||
#hovered = -1;
|
||||
#last_click = { row: -1, time: -Infinity };
|
||||
|
||||
constructor(width: number, height: number, on_click: (row: number, double: boolean) => void) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.on_click = on_click;
|
||||
}
|
||||
|
||||
get #visible() {
|
||||
return Math.max(1, Math.floor(this.height / LIST_ROW_HEIGHT));
|
||||
}
|
||||
|
||||
handle_input() {
|
||||
const mouse = InputManager.get_mouse_position();
|
||||
const inside = point_inside_rec(mouse.x, mouse.y, this.x, this.y, this.width, this.height);
|
||||
const max_scroll = Math.max(0, this.rows.length - this.#visible);
|
||||
if (inside) this.#scroll += Math.sign(InputManager.get_wheel_delta());
|
||||
this.#scroll = Math.max(0, Math.min(max_scroll, this.#scroll));
|
||||
|
||||
const row = this.#scroll + Math.floor((mouse.y - this.y) / LIST_ROW_HEIGHT);
|
||||
this.#hovered = inside && row < this.rows.length ? row : -1;
|
||||
if (this.#hovered >= 0 && InputManager.is_mouse_pressed(0)) {
|
||||
InputManager.consume_mouse(0);
|
||||
const now = performance.now();
|
||||
const double = this.#last_click.row === row && now - this.#last_click.time < DOUBLE_CLICK_MS;
|
||||
this.#last_click = double ? { row: -1, time: -Infinity } : { row, time: now };
|
||||
this.on_click(row, double);
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
draw_rect(this.x, this.y, this.width, this.height, [0, 0, 0, 0.5]);
|
||||
const shown = this.rows.slice(this.#scroll, this.#scroll + this.#visible);
|
||||
for (const [i, row] of shown.entries()) {
|
||||
const index = this.#scroll + i;
|
||||
const y = this.y + i * LIST_ROW_HEIGHT;
|
||||
if (index === this.selected) {
|
||||
draw_rect(this.x, y, this.width, LIST_ROW_HEIGHT, SELECTION_COLOR);
|
||||
} else if (index === this.#hovered) {
|
||||
draw_rect(this.x, y, this.width, LIST_ROW_HEIGHT, [1, 1, 1, 0.1]);
|
||||
}
|
||||
let text_x = this.x + 12;
|
||||
if (row.checked !== undefined) {
|
||||
const box_y = y + (LIST_ROW_HEIGHT - CHECK_SIZE) / 2;
|
||||
draw_rect_stroke(text_x, box_y, CHECK_SIZE, CHECK_SIZE, [1, 1, 1, 1], 2);
|
||||
if (row.checked) draw_rect(text_x + 5, box_y + 5, CHECK_SIZE - 10, CHECK_SIZE - 10);
|
||||
text_x += CHECK_SIZE + 12;
|
||||
}
|
||||
draw_text(row.text, text_x, y + (LIST_ROW_HEIGHT - TEXT_HEIGHT * TEXT_SCALE) / 2, TEXT_SCALE);
|
||||
}
|
||||
// how far down it's scrolled, when it doesn't all fit
|
||||
if (this.rows.length > this.#visible) {
|
||||
const bar = this.height * this.#visible / this.rows.length;
|
||||
const bar_y = this.y + (this.height - bar) * this.#scroll / (this.rows.length - this.#visible);
|
||||
draw_rect(this.x + this.width - 6, bar_y, 6, bar, [1, 1, 1, 0.5]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user