@benev/nubs
Version:
user-input system for web games
39 lines • 1.72 kB
JavaScript
import { compareStringArrays } from "../../../../../tools/compare-string-arrays.js";
export function controlKeybindAssignments({ getMode, getWaiting, setWaiting, getShowDialog, setShowDialog, setKeysPressed, getKeysPressed, getBindingsDraft, setBindingsDraft, }) {
const key_set = new Set();
return (event) => {
const waiting = getWaiting();
const is_dialog_shown = getShowDialog();
if (waiting && event.detail.kind === "key") {
const { cause, pressed } = event.detail;
const { effect, keyIndex } = waiting;
if (pressed) {
const isEscapeKey = cause === "Escape";
if (is_dialog_shown) {
if (isEscapeKey) {
setShowDialog(false);
setWaiting(undefined);
key_set.clear();
}
else {
key_set.add(cause);
setKeysPressed(Array.from(key_set));
}
}
const mode = getMode();
const bindings = getBindingsDraft();
const combo_cause = getKeysPressed();
const keybinds = bindings.modes[mode].key[effect];
const redundant = keybinds.some(c => compareStringArrays(c, combo_cause));
if (!redundant) {
if (isEscapeKey)
keybinds.splice(keyIndex, 1);
else
keybinds[keyIndex] = combo_cause;
setBindingsDraft(bindings);
}
}
}
};
}
//# sourceMappingURL=control-keybind-assignments.js.map