Files
healer-man/src/game/keybindPreferences.ts
T
2026-09-11 14:26:15 -04:00

28 lines
1.5 KiB
TypeScript

import { type InputBindings, type InputDevice, INPUT_ACTIONS, replaceInputBinding } from "../input/keybinds";
import { type UiPreferences } from "./uiLayout";
/** PC bar keys live with their UI layout so loading a template also loads its keys. */
export function profileInputBindings(preferences: UiPreferences, device: InputDevice): InputBindings {
if (device === "controller") return { ...preferences.keybinds.controller };
const result = { ...preferences.keybinds.pc };
preferences.current.pc.bars.forEach((bar, b) => bar.slots.forEach((slot, s) => { result[`bar:${b}:${s}`] = slot.key; }));
return result;
}
export function withProfileInputBindings(preferences: UiPreferences, device: InputDevice, bindings: InputBindings): UiPreferences {
const layout = preferences.current.pc;
return {
...preferences,
keybinds: { ...preferences.keybinds, [device]: { ...bindings } },
current: device !== "pc" ? preferences.current : { ...preferences.current, pc: {
...layout, bars: layout.bars.map((bar, b) => ({ ...bar, slots: bar.slots.map((slot, s) => ({ ...slot, key: bindings[`bar:${b}:${s}`] ?? null })) })),
} },
};
}
export function restoreMovementBindings(preferences: UiPreferences, device: InputDevice): UiPreferences {
let bindings = profileInputBindings(preferences, device);
for (const entry of INPUT_ACTIONS.filter((entry) => entry.id.startsWith("move-") || entry.id === "jump")) {
bindings = replaceInputBinding(bindings, device, entry.id, entry[device]);
}
return withProfileInputBindings(preferences, device, bindings);
}