pokerogue/src/ui/settings/settings-gamepad-ui-handler.ts

128 lines
4.7 KiB
TypeScript
Raw Normal View History

import BattleScene from "../../battle-scene";
import {addTextObject, TextStyle} from "../text";
import {Mode} from "../ui";
import {
setSettingGamepad,
SettingGamepad,
settingGamepadBlackList,
settingGamepadDefaults,
settingGamepadOptions
2024-06-03 19:57:47 -04:00
} from "../../system/settings/settings-gamepad";
import pad_xbox360 from "#app/configs/inputs/pad_xbox360";
import pad_dualshock from "#app/configs/inputs/pad_dualshock";
import pad_unlicensedSNES from "#app/configs/inputs/pad_unlicensedSNES";
import {InterfaceConfig} from "#app/inputs-controller";
2024-06-03 19:57:47 -04:00
import AbstractControlSettingsUiHandler from "#app/ui/settings/abstract-control-settings-ui-handler.js";
import {Device} from "#enums/devices";
import {truncateString} from "#app/utils";
import i18next from "i18next";
/**
* Class representing the settings UI handler for gamepads.
*
2024-06-03 19:57:47 -04:00
* @extends AbstractControlSettingsUiHandler
*/
2024-06-03 19:57:47 -04:00
export default class SettingsGamepadUiHandler extends AbstractControlSettingsUiHandler {
/**
* Creates an instance of SettingsGamepadUiHandler.
*
* @param scene - The BattleScene instance.
* @param mode - The UI mode, optional.
*/
constructor(scene: BattleScene, mode?: Mode) {
super(scene, mode);
this.titleSelected = "Gamepad";
2024-06-03 19:57:47 -04:00
this.setting = SettingGamepad;
this.settingDeviceDefaults = settingGamepadDefaults;
this.settingDeviceOptions = settingGamepadOptions;
this.configs = [pad_xbox360, pad_dualshock, pad_unlicensedSNES];
this.commonSettingsCount = 2;
this.localStoragePropertyName = "settingsGamepad";
this.settingBlacklisted = settingGamepadBlackList;
2024-06-03 19:57:47 -04:00
this.device = Device.GAMEPAD;
}
2024-06-03 19:57:47 -04:00
setSetting = setSettingGamepad;
/**
* Setup UI elements.
*/
setup() {
super.setup();
// If no gamepads are detected, set up a default UI prompt in the settings container.
this.layout["noGamepads"] = new Map();
const optionsContainer = this.scene.add.container(0, 0);
optionsContainer.setVisible(false); // Initially hide the container as no gamepads are connected.
const label = addTextObject(this.scene, 8, 28, i18next.t("settings:gamepadPleasePlug"), TextStyle.SETTINGS_LABEL);
label.setOrigin(0, 0);
optionsContainer.add(label);
this.settingsContainer.add(optionsContainer);
// Map the 'noGamepads' layout options for easy access.
this.layout["noGamepads"].optionsContainer = optionsContainer;
this.layout["noGamepads"].label = label;
}
/**
* Set the layout for the active configuration.
*
* @param activeConfig - The active gamepad configuration.
* @returns `true` if the layout was successfully applied, otherwise `false`.
*/
setLayout(activeConfig: InterfaceConfig): boolean {
// Check if there is no active configuration (e.g., no gamepad connected).
if (!activeConfig) {
// Retrieve the layout for when no gamepads are connected.
const layout = this.layout["noGamepads"];
// Make the options container visible to show message.
layout.optionsContainer.setVisible(true);
// Return false indicating the layout application was not successful due to lack of gamepad.
return false;
}
return super.setLayout(activeConfig);
}
/**
* Update the display of the chosen gamepad.
*/
updateChosenGamepadDisplay(): void {
// Update any bindings that might have changed since the last update.
this.updateBindings();
this.resetScroll();
// Iterate over the keys in the settingDevice enumeration.
2024-06-03 19:57:47 -04:00
for (const [index, key] of Object.keys(this.setting).entries()) {
const setting = this.setting[key]; // Get the actual setting value using the key.
// Check if the current setting corresponds to the controller setting.
2024-06-03 19:57:47 -04:00
if (setting === this.setting.Controller) {
// Iterate over all layouts excluding the 'noGamepads' special case.
for (const _key of Object.keys(this.layout)) {
if (_key === "noGamepads") {
continue;
} // Skip updating the no gamepad layout.
// Update the text of the first option label under the current setting to the name of the chosen gamepad,
// truncating the name to 30 characters if necessary.
this.layout[_key].optionValueLabels[index][0].setText(truncateString(this.scene.inputController.selectedDevice[Device.GAMEPAD], 20));
}
}
}
}
/**
* Save the setting to local storage.
*
2024-06-03 19:57:47 -04:00
* @param settingName - The setting to save.
* @param cursor - The cursor position to save.
*/
2024-06-03 19:57:47 -04:00
saveSettingToLocalStorage(settingName, cursor): void {
if (this.setting[settingName] !== this.setting.Controller) {
this.scene.gameData.saveControlSetting(this.device, this.localStoragePropertyName, settingName, this.settingDeviceDefaults, cursor);
}
}
}