uuuaaahhg

This commit is contained in:
legop3
2025-11-17 23:24:49 -05:00
parent a038bf775a
commit 10e78757b9
3 changed files with 62 additions and 51 deletions
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -5,7 +5,7 @@
<link rel="icon" type="image/svg+xml" href="/vite.svg" /> <link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>webui</title> <title>webui</title>
<script type="module" crossorigin src="/assets/index-CiyUnwMv.js"></script> <script type="module" crossorigin src="/assets/index-DgBoZ7rI.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-BaCUS21F.css"> <link rel="stylesheet" crossorigin href="/assets/index-BaCUS21F.css">
</head> </head>
<body> <body>
@@ -17,19 +17,21 @@ const KEY_ALIASES = {
'_': '-', '_': '-',
'+': '=', '+': '=',
}; };
const CODE_ALIASES = { const BINDING_CODE_MAP = {
BracketLeft: '[', '[': 'BracketLeft',
BracketRight: ']', ']': 'BracketRight',
Backslash: '\\', '\\': 'Backslash',
Semicolon: ';', ';': 'Semicolon',
Quote: "'", "'": 'Quote',
Comma: ',', ',': 'Comma',
Period: '.', '.': 'Period',
Slash: '/', '/': 'Slash',
Minus: '-', '-': 'Minus',
Equal: '=', '=': 'Equal',
Backquote: '`', '`': 'Backquote',
}; };
const CODE_PREFIX = 'code:';
const KEY_PREFIX = 'key:';
function shouldIgnoreEvent(event) { function shouldIgnoreEvent(event) {
const target = event.target; const target = event.target;
@@ -49,28 +51,35 @@ function canonicalizeValue(value) {
return KEY_ALIASES[lower] ?? lower; return KEY_ALIASES[lower] ?? lower;
} }
function resolveCodeToken(code) { function deriveCodeForValue(value) {
if (!code || typeof code !== 'string') return ''; if (!value) return null;
if (CODE_ALIASES[code]) return CODE_ALIASES[code]; if (BINDING_CODE_MAP[value]) return BINDING_CODE_MAP[value];
if (code.startsWith('Key') && code.length === 4) { if (value.length === 1) {
return code.slice(3).toLowerCase(); if (/[a-z]/.test(value)) return `Key${value.toUpperCase()}`;
if (/[0-9]/.test(value)) return `Digit${value}`;
} }
if (code.startsWith('Digit') && code.length === 6) { return null;
return code.slice(5);
}
if (code.startsWith('Numpad') && code.length > 6) {
const suffix = code.slice(6);
if (/^[0-9]$/.test(suffix)) return suffix;
}
return '';
} }
function eventToTokens(event) { function makeKeyToken(value) {
const canonical = canonicalizeValue(value);
return canonical ? `${KEY_PREFIX}${canonical}` : null;
}
function makeCodeToken(value) {
const canonical = canonicalizeValue(value);
const code = deriveCodeForValue(canonical);
return code ? `${CODE_PREFIX}${code}` : null;
}
function tokensFromEvent(event) {
const tokens = new Set(); const tokens = new Set();
const keyToken = canonicalizeValue(event?.key ?? ''); const keyToken = makeKeyToken(event?.key ?? '');
if (keyToken) tokens.add(keyToken); if (keyToken) tokens.add(keyToken);
const codeToken = resolveCodeToken(event?.code ?? ''); const code = event?.code;
if (codeToken) tokens.add(codeToken); if (code) {
tokens.add(`${CODE_PREFIX}${code}`);
}
return Array.from(tokens); return Array.from(tokens);
} }
@@ -79,8 +88,10 @@ function normalizeKeymap(keymap = {}) {
const values = Array.isArray(bindings) ? bindings : [bindings]; const values = Array.isArray(bindings) ? bindings : [bindings];
const normalized = new Set(); const normalized = new Set();
values.forEach((value) => { values.forEach((value) => {
const token = canonicalizeValue(String(value)); const keyToken = makeKeyToken(String(value));
if (token) normalized.add(token); if (keyToken) normalized.add(keyToken);
const codeToken = makeCodeToken(String(value));
if (codeToken) normalized.add(codeToken);
}); });
return [action, normalized]; return [action, normalized];
}); });
@@ -169,15 +180,15 @@ export default function KeyboardInputManager() {
[state.camera?.config?.nudgeDegrees], [state.camera?.config?.nudgeDegrees],
); );
const activeKeysRef = useRef(new Set()); const activeTokensRef = useRef(new Set());
const lastVectorRef = useRef(ZERO_VECTOR); const lastVectorRef = useRef(ZERO_VECTOR);
const lastAuxRef = useRef(ZERO_AUX); const lastAuxRef = useRef(ZERO_AUX);
const servoIntervalRef = useRef(null); const servoIntervalRef = useRef(null);
const driveFromKeys = useCallback(() => { const driveFromKeys = useCallback(() => {
const keysSnapshot = new Set(activeKeysRef.current); const tokensSnapshot = new Set(activeTokensRef.current);
const vector = computeDriveVector(keysSnapshot, keymap); const vector = computeDriveVector(tokensSnapshot, keymap);
const aux = computeAuxMotors(keysSnapshot, keymap); const aux = computeAuxMotors(tokensSnapshot, keymap);
if ( if (
vector.x !== lastVectorRef.current.x || vector.x !== lastVectorRef.current.x ||
vector.y !== lastVectorRef.current.y || vector.y !== lastVectorRef.current.y ||
@@ -195,7 +206,7 @@ export default function KeyboardInputManager() {
setAuxMotors(aux); setAuxMotors(aux);
} }
registerInputState(SOURCE, { registerInputState(SOURCE, {
keys: Array.from(keysSnapshot), keys: Array.from(tokensSnapshot),
vector, vector,
aux, aux,
}); });
@@ -209,9 +220,9 @@ export default function KeyboardInputManager() {
}, []); }, []);
const computeServoDirection = useCallback(() => { const computeServoDirection = useCallback(() => {
const keysSnapshot = new Set(activeKeysRef.current); const tokensSnapshot = new Set(activeTokensRef.current);
const up = bindingActive(keymap.cameraUp, keysSnapshot); const up = bindingActive(keymap.cameraUp, tokensSnapshot);
const down = bindingActive(keymap.cameraDown, keysSnapshot); const down = bindingActive(keymap.cameraDown, tokensSnapshot);
return (up ? 1 : 0) - (down ? 1 : 0); return (up ? 1 : 0) - (down ? 1 : 0);
}, [keymap]); }, [keymap]);
@@ -237,7 +248,7 @@ export default function KeyboardInputManager() {
}, [computeServoDirection, nudgeServo, servoStep, stopServoLoop]); }, [computeServoDirection, nudgeServo, servoStep, stopServoLoop]);
const resetAll = useCallback(() => { const resetAll = useCallback(() => {
activeKeysRef.current.clear(); activeTokensRef.current.clear();
lastVectorRef.current = ZERO_VECTOR; lastVectorRef.current = ZERO_VECTOR;
lastAuxRef.current = ZERO_AUX; lastAuxRef.current = ZERO_AUX;
stopServoLoop(); stopServoLoop();
@@ -248,13 +259,13 @@ export default function KeyboardInputManager() {
useEffect(() => { useEffect(() => {
function handleKeyDown(event) { function handleKeyDown(event) {
if (shouldIgnoreEvent(event)) return; if (shouldIgnoreEvent(event)) return;
const tokens = eventToTokens(event); const tokens = tokensFromEvent(event);
if (tokens.length === 0) return; if (tokens.length === 0) return;
if (tokens.some((token) => actionTokens.has(token))) { if (tokens.some((token) => actionTokens.has(token))) {
event.preventDefault(); event.preventDefault();
} }
const newlyPressed = tokens.filter((token) => !activeKeysRef.current.has(token)); const newlyPressed = tokens.filter((token) => !activeTokensRef.current.has(token));
newlyPressed.forEach((token) => activeKeysRef.current.add(token)); newlyPressed.forEach((token) => activeTokensRef.current.add(token));
if (newlyPressed.length > 0) { if (newlyPressed.length > 0) {
if (newlyPressed.some((token) => keymap.driveMacro?.has(token))) { if (newlyPressed.some((token) => keymap.driveMacro?.has(token))) {
@@ -271,8 +282,8 @@ export default function KeyboardInputManager() {
} }
function handleKeyUp(event) { function handleKeyUp(event) {
const tokens = eventToTokens(event); const tokens = tokensFromEvent(event);
tokens.forEach((token) => activeKeysRef.current.delete(token)); tokens.forEach((token) => activeTokensRef.current.delete(token));
ensureServoLoop(); ensureServoLoop();
driveFromKeys(); driveFromKeys();
} }