pokerogue/src/configs/gamepad-utils.ts

60 lines
2.4 KiB
TypeScript

import {GamepadConfig} from "../inputs-controller";
import {SettingGamepad} from "#app/system/settings-gamepad";
import {Button} from "#app/enums/buttons";
// Given a button index from an input event, return its naming from the mapping config
export function getKeyFromInputIndex(config: GamepadConfig, index: number): String | null {
for (const key of Object.keys(config.gamepadMapping)) {
if (config.gamepadMapping[key] === index) return key;
}
return null;
}
export function getKeyFromKeyboardKey(config: GamepadConfig, key): String | null {
for (const _key of Object.keys(config.gamepadMapping)) {
if (config.gamepadMapping[_key] === key) return _key;
}
return null;
}
// Given a setting name, return the key assigned to it from the config file
export function getKeyForSettingName(config: GamepadConfig, settingName: string): String | null {
for (const key of Object.keys(config.setting)) {
if (config.setting[key] === settingName) return key;
}
return null;
}
// Given a Button, return the custom key assigned to it from the config file
export function getCurrenlyAssignedKeyToAction(config: GamepadConfig, action: Button): String | null {
for (const key of Object.keys(config.custom)) {
if (config.custom[key] === action) return key;
}
return null;
}
// Given a setting name, return the custom key for the default action from the config file
export function getCurrentlyAssignedToSettingName(config: GamepadConfig, settingName: string): String {
const oldKey = getKeyForSettingName(config, settingName)
const action = config.default[oldKey];
const key = getCurrenlyAssignedKeyToAction(config, action);
return key;
}
// Given a button index from an input event, return its icon from the config file
export function getCurrenlyAssignedIconFromInputIndex(config: GamepadConfig, index: number): String {
const key = getKeyFromInputIndex(config, index);
return config.icons[key];
}
export function getCurrenlyAssignedIconFromKeyboardKey(config: GamepadConfig, key): String {
const _key = getKeyFromKeyboardKey(config, key);
return config.icons[_key];
}
// Given a setting name, return the icon currently assigned to this setting name
export function getCurrentlyAssignedIconToSettingName(config: GamepadConfig, settingName: string): string {
const key = getCurrentlyAssignedToSettingName(config, settingName);
return config.icons[key];
}