comment TSDOC for inputs-controller.ts

This commit is contained in:
Greenlamp 2024-05-16 01:44:00 +02:00
parent 13e47e82ae
commit 951422fb51

View File

@ -166,8 +166,6 @@ export class InputsController {
this.scene.input.keyboard.on('keyup', this.keyboardKeyUp, this); this.scene.input.keyboard.on('keyup', this.keyboardKeyUp, this);
} }
initTouchControls(this.events); initTouchControls(this.events);
// Keyboard
// this.setupKeyboardControls();
} }
/** /**
@ -193,7 +191,6 @@ export class InputsController {
this.gamepadSupport = true; this.gamepadSupport = true;
} else { } else {
this.gamepadSupport = false; this.gamepadSupport = false;
// if we disable the gamepad, we want to release every key pressed
this.deactivatePressedKey(); this.deactivatePressedKey();
} }
} }
@ -209,6 +206,11 @@ export class InputsController {
this.initChosenGamepad(gamepad) this.initChosenGamepad(gamepad)
} }
/**
* Sets the currently chosen keyboard layout and initializes related settings.
*
* @param layoutKeyboard - The identifier of the keyboard layout to set as chosen.
*/
setChosenKeyboardLayout(layoutKeyboard: String): void { setChosenKeyboardLayout(layoutKeyboard: String): void {
this.deactivatePressedKey(); this.deactivatePressedKey();
this.initChosenLayoutKeyboard(layoutKeyboard) this.initChosenLayoutKeyboard(layoutKeyboard)
@ -274,6 +276,11 @@ export class InputsController {
handler && handler.updateChosenGamepadDisplay() handler && handler.updateChosenGamepadDisplay()
} }
/**
* Initializes the chosen keyboard layout by setting its identifier in the local storage and updating the UI to reflect the chosen layout.
* If a layout name is provided, it uses that as the chosen layout; otherwise, it defaults to the currently chosen layout.
* @param layoutKeyboard Optional parameter to specify the name of the keyboard layout to initialize as chosen.
*/
initChosenLayoutKeyboard(layoutKeyboard?: String): void { initChosenLayoutKeyboard(layoutKeyboard?: String): void {
if (layoutKeyboard) if (layoutKeyboard)
this.selectedDevice[Device.KEYBOARD] = layoutKeyboard.toLowerCase(); this.selectedDevice[Device.KEYBOARD] = layoutKeyboard.toLowerCase();
@ -327,6 +334,9 @@ export class InputsController {
this.initChosenGamepad(this.selectedDevice[Device.GAMEPAD]) this.initChosenGamepad(this.selectedDevice[Device.GAMEPAD])
} }
/**
* Initializes or updates configurations for connected keyboards.
*/
setupKeyboard(): void { setupKeyboard(): void {
for (const layout of ['default']) { for (const layout of ['default']) {
const config = deepCopy(this.getConfigKeyboard(layout)); const config = deepCopy(this.getConfigKeyboard(layout));
@ -356,14 +366,23 @@ export class InputsController {
} }
} }
checkIfKeyboardIsInit(): void { /**
* Ensures the keyboard is initialized by checking if there is an active configuration for the keyboard.
* If not, it sets up the keyboard with default configurations.
*/
ensureKeyboardIsInit(): void {
if (!this.getActiveConfig(Device.KEYBOARD)?.padID) if (!this.getActiveConfig(Device.KEYBOARD)?.padID)
this.setupKeyboard(); this.setupKeyboard();
} }
/**
* Handles the keydown event for the keyboard.
*
* @param event The keyboard event.
*/
keyboardKeyDown(event): void { keyboardKeyDown(event): void {
const keyDown = event.keyCode; const keyDown = event.keyCode;
this.checkIfKeyboardIsInit(); this.ensureKeyboardIsInit();
if (this.keys.includes(keyDown)) return; if (this.keys.includes(keyDown)) return;
this.keys.push(keyDown); this.keys.push(keyDown);
const buttonDown = getButtonWithKeycode(this.getActiveConfig(Device.KEYBOARD), keyDown); const buttonDown = getButtonWithKeycode(this.getActiveConfig(Device.KEYBOARD), keyDown);
@ -377,10 +396,15 @@ export class InputsController {
} }
} }
/**
* Handles the keyup event for the keyboard.
*
* @param event The keyboard event.
*/
keyboardKeyUp(event): void { keyboardKeyUp(event): void {
const keyDown = event.keyCode; const keyDown = event.keyCode;
this.keys = this.keys.filter(k => k !== keyDown); this.keys = this.keys.filter(k => k !== keyDown);
this.checkIfKeyboardIsInit() this.ensureKeyboardIsInit()
const buttonUp = getButtonWithKeycode(this.getActiveConfig(Device.KEYBOARD), keyDown); const buttonUp = getButtonWithKeycode(this.getActiveConfig(Device.KEYBOARD), keyDown);
if (buttonUp !== undefined) { if (buttonUp !== undefined) {
this.events.emit('input_up', { this.events.emit('input_up', {
@ -462,6 +486,12 @@ export class InputsController {
return pad_generic; return pad_generic;
} }
/**
* Retrieves the configuration object for a keyboard layout based on its identifier.
*
* @param id The identifier string of the keyboard layout.
* @returns InterfaceConfig The configuration object corresponding to the identified keyboard layout.
*/
getConfigKeyboard(id: string): InterfaceConfig { getConfigKeyboard(id: string): InterfaceConfig {
if (id === 'default') if (id === 'default')
return cfg_keyboard_azerty; return cfg_keyboard_azerty;
@ -621,11 +651,11 @@ export class InputsController {
} }
/** /**
* Injects a custom mapping configuration into the gamepad configuration for a specific gamepad. * Injects a custom mapping configuration into the configuration for a specific gamepad.
* If the gamepad does not have an existing configuration, it initializes one first. * If the device does not have an existing configuration, it initializes one first.
* *
* @param gamepadName The identifier of the gamepad to configure. * @param selectedDevice The identifier of the device to configure.
* @param customMappings The custom mapping configuration to apply to the gamepad. * @param mappingConfigs The mapping configuration to apply to the device.
*/ */
injectConfig(selectedDevice: string, mappingConfigs): void { injectConfig(selectedDevice: string, mappingConfigs): void {
if (!this.configs[selectedDevice]) this.configs[selectedDevice] = {}; if (!this.configs[selectedDevice]) this.configs[selectedDevice] = {};
@ -635,6 +665,13 @@ export class InputsController {
regenerateIdentifiers(this.configs[selectedDevice]); regenerateIdentifiers(this.configs[selectedDevice]);
} }
/**
* Swaps a binding in the configuration.
*
* @param config The configuration object.
* @param settingName The name of the setting to swap.
* @param pressedButton The button that was pressed.
*/
swapBinding(config, settingName, pressedButton): void { swapBinding(config, settingName, pressedButton): void {
this.pauseUpdate = true; this.pauseUpdate = true;
swap(config, settingName, pressedButton); swap(config, settingName, pressedButton);