text input now passes actual constructors instead of strings
This commit is contained in:
parent
223eea72e3
commit
645de97db2
6 changed files with 46 additions and 38 deletions
51
src/ui.ts
51
src/ui.ts
|
|
@ -1,4 +1,4 @@
|
|||
import type { UIValue } from './types';
|
||||
import type { UIValue, Value } from './types';
|
||||
|
||||
type ClickRegion = {
|
||||
x: number;
|
||||
|
|
@ -13,14 +13,14 @@ type TextInputRegion = {
|
|||
y: number;
|
||||
width: number;
|
||||
height: number;
|
||||
inputId: string;
|
||||
submitId: string;
|
||||
inputConstructor: Value;
|
||||
submitConstructor: Value;
|
||||
}
|
||||
|
||||
let clickRegions: ClickRegion[] = [];
|
||||
let textInputRegions: TextInputRegion[] = [];
|
||||
let focusedInput: string | null = null;
|
||||
let focusedInputSubmit: string | null = null;
|
||||
let focusedInput: Value | null = null;
|
||||
let focusedInputSubmit: Value | null = null;
|
||||
|
||||
export function render(ui: UIValue, canvas: HTMLCanvasElement) {
|
||||
const ctx = canvas.getContext('2d');
|
||||
|
|
@ -102,11 +102,11 @@ function renderUI(ui: UIValue, ctx: CanvasRenderingContext2D, x: number, y: numb
|
|||
y: y + ui.y,
|
||||
width: ui.w,
|
||||
height: ui.h,
|
||||
inputId: ui.onInput,
|
||||
submitId: ui.onSubmit
|
||||
inputConstructor: ui.onInput,
|
||||
submitConstructor: ui.onSubmit
|
||||
});
|
||||
|
||||
if (ui.focused && ui.onInput === focusedInput) {
|
||||
if (ui.focused && focusedInput && (ui.onInput as any).name === (focusedInput as any).name) {
|
||||
currentInputValue = ui.value;
|
||||
}
|
||||
|
||||
|
|
@ -191,8 +191,8 @@ export function hitTestTextInput(x: number, y: number): boolean {
|
|||
for (const region of textInputRegions) {
|
||||
if (x >= region.x && x < region.x + region.width &&
|
||||
y >= region.y && y < region.y + region.height) {
|
||||
focusedInput = region.inputId;
|
||||
focusedInputSubmit = region.submitId;
|
||||
focusedInput = region.inputConstructor;
|
||||
focusedInputSubmit = region.submitConstructor;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -202,36 +202,41 @@ export function hitTestTextInput(x: number, y: number): boolean {
|
|||
return false;
|
||||
}
|
||||
|
||||
export function getFocusedInput(): string | null {
|
||||
export function getFocusedInput(): Value | null {
|
||||
return focusedInput;
|
||||
}
|
||||
|
||||
export function storeInputValue(inputId: string, value: string) {
|
||||
if (inputId === focusedInput) {
|
||||
currentInputValue = value;
|
||||
}
|
||||
}
|
||||
|
||||
export function handleKeyboard(key: string): { event: string, value: string } | null {
|
||||
export function handleKeyboard(key: string): Value | null {
|
||||
if (!focusedInput) return null;
|
||||
|
||||
if (key === 'Enter') {
|
||||
return focusedInputSubmit
|
||||
? { event: focusedInputSubmit, value: currentInputValue }
|
||||
: null;
|
||||
if (!focusedInputSubmit) return null;
|
||||
return {
|
||||
kind: 'constructor',
|
||||
name: (focusedInputSubmit as any).name,
|
||||
args: [{ kind: 'string', value: currentInputValue }]
|
||||
};
|
||||
}
|
||||
|
||||
if (key === 'Backspace') {
|
||||
const newValue = currentInputValue.slice(0, -1);
|
||||
currentInputValue = newValue;
|
||||
return { event: focusedInput, value: newValue };
|
||||
return {
|
||||
kind: 'constructor',
|
||||
name: (focusedInput as any).name,
|
||||
args: [{ kind: 'string', value: newValue }]
|
||||
};
|
||||
}
|
||||
|
||||
// Character
|
||||
if (key.length === 1) {
|
||||
const newValue = currentInputValue + key;
|
||||
currentInputValue = newValue;
|
||||
return { event: focusedInput, value: newValue };
|
||||
return {
|
||||
kind: 'constructor',
|
||||
name: (focusedInput as any).name,
|
||||
args: [{ kind: 'string', value: newValue }]
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue