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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user