|
|
|
@ -1,4 +1,4 @@
|
|
|
|
import type { UIValue } from './types';
|
|
|
|
import type { UIValue, Value } from './types';
|
|
|
|
|
|
|
|
|
|
|
|
type ClickRegion = {
|
|
|
|
type ClickRegion = {
|
|
|
|
x: number;
|
|
|
|
x: number;
|
|
|
|
@ -13,14 +13,14 @@ type TextInputRegion = {
|
|
|
|
y: number;
|
|
|
|
y: number;
|
|
|
|
width: number;
|
|
|
|
width: number;
|
|
|
|
height: number;
|
|
|
|
height: number;
|
|
|
|
inputId: string;
|
|
|
|
inputConstructor: Value;
|
|
|
|
submitId: string;
|
|
|
|
submitConstructor: Value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let clickRegions: ClickRegion[] = [];
|
|
|
|
let clickRegions: ClickRegion[] = [];
|
|
|
|
let textInputRegions: TextInputRegion[] = [];
|
|
|
|
let textInputRegions: TextInputRegion[] = [];
|
|
|
|
let focusedInput: string | null = null;
|
|
|
|
let focusedInput: Value | null = null;
|
|
|
|
let focusedInputSubmit: string | null = null;
|
|
|
|
let focusedInputSubmit: Value | null = null;
|
|
|
|
|
|
|
|
|
|
|
|
export function render(ui: UIValue, canvas: HTMLCanvasElement) {
|
|
|
|
export function render(ui: UIValue, canvas: HTMLCanvasElement) {
|
|
|
|
const ctx = canvas.getContext('2d');
|
|
|
|
const ctx = canvas.getContext('2d');
|
|
|
|
@ -102,11 +102,11 @@ function renderUI(ui: UIValue, ctx: CanvasRenderingContext2D, x: number, y: numb
|
|
|
|
y: y + ui.y,
|
|
|
|
y: y + ui.y,
|
|
|
|
width: ui.w,
|
|
|
|
width: ui.w,
|
|
|
|
height: ui.h,
|
|
|
|
height: ui.h,
|
|
|
|
inputId: ui.onInput,
|
|
|
|
inputConstructor: ui.onInput,
|
|
|
|
submitId: ui.onSubmit
|
|
|
|
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;
|
|
|
|
currentInputValue = ui.value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -191,8 +191,8 @@ export function hitTestTextInput(x: number, y: number): boolean {
|
|
|
|
for (const region of textInputRegions) {
|
|
|
|
for (const region of textInputRegions) {
|
|
|
|
if (x >= region.x && x < region.x + region.width &&
|
|
|
|
if (x >= region.x && x < region.x + region.width &&
|
|
|
|
y >= region.y && y < region.y + region.height) {
|
|
|
|
y >= region.y && y < region.y + region.height) {
|
|
|
|
focusedInput = region.inputId;
|
|
|
|
focusedInput = region.inputConstructor;
|
|
|
|
focusedInputSubmit = region.submitId;
|
|
|
|
focusedInputSubmit = region.submitConstructor;
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -202,36 +202,41 @@ export function hitTestTextInput(x: number, y: number): boolean {
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function getFocusedInput(): string | null {
|
|
|
|
export function getFocusedInput(): Value | null {
|
|
|
|
return focusedInput;
|
|
|
|
return focusedInput;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function storeInputValue(inputId: string, value: string) {
|
|
|
|
export function handleKeyboard(key: string): Value | null {
|
|
|
|
if (inputId === focusedInput) {
|
|
|
|
|
|
|
|
currentInputValue = value;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function handleKeyboard(key: string): { event: string, value: string } | null {
|
|
|
|
|
|
|
|
if (!focusedInput) return null;
|
|
|
|
if (!focusedInput) return null;
|
|
|
|
|
|
|
|
|
|
|
|
if (key === 'Enter') {
|
|
|
|
if (key === 'Enter') {
|
|
|
|
return focusedInputSubmit
|
|
|
|
if (!focusedInputSubmit) return null;
|
|
|
|
? { event: focusedInputSubmit, value: currentInputValue }
|
|
|
|
return {
|
|
|
|
: null;
|
|
|
|
kind: 'constructor',
|
|
|
|
|
|
|
|
name: (focusedInputSubmit as any).name,
|
|
|
|
|
|
|
|
args: [{ kind: 'string', value: currentInputValue }]
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (key === 'Backspace') {
|
|
|
|
if (key === 'Backspace') {
|
|
|
|
const newValue = currentInputValue.slice(0, -1);
|
|
|
|
const newValue = currentInputValue.slice(0, -1);
|
|
|
|
currentInputValue = newValue;
|
|
|
|
currentInputValue = newValue;
|
|
|
|
return { event: focusedInput, value: newValue };
|
|
|
|
return {
|
|
|
|
|
|
|
|
kind: 'constructor',
|
|
|
|
|
|
|
|
name: (focusedInput as any).name,
|
|
|
|
|
|
|
|
args: [{ kind: 'string', value: newValue }]
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Character
|
|
|
|
// Character
|
|
|
|
if (key.length === 1) {
|
|
|
|
if (key.length === 1) {
|
|
|
|
const newValue = currentInputValue + key;
|
|
|
|
const newValue = currentInputValue + key;
|
|
|
|
currentInputValue = newValue;
|
|
|
|
currentInputValue = newValue;
|
|
|
|
return { event: focusedInput, value: newValue };
|
|
|
|
return {
|
|
|
|
|
|
|
|
kind: 'constructor',
|
|
|
|
|
|
|
|
name: (focusedInput as any).name,
|
|
|
|
|
|
|
|
args: [{ kind: 'string', value: newValue }]
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
|