remove localStorage for gamepads connected + fix lost focus effect on controller change

pull/685/head
Greenlamp 2024-05-09 12:55:00 +02:00
parent e162edf6f5
commit b58a444f80
4 changed files with 24 additions and 30 deletions

View File

@ -39,7 +39,7 @@ export class InputsController {
private gamepadSupport: boolean = true;
public customGamepadMapping = new Map();
public chosenGamepad;
public chosenGamepad: String;
constructor(scene: Phaser.Scene) {
this.scene = scene;
@ -74,7 +74,6 @@ export class InputsController {
this.scene.input.gamepad.on('connected', function (thisGamepad) {
this.refreshGamepads();
this.setupGamepad(thisGamepad);
this.populateSetting();
}, this);
// Check to see if the gamepad has already been setup by the browser
@ -108,6 +107,7 @@ export class InputsController {
}
setChosenGamepad(gamepad: String): void {
this.deactivatePressedKey();
this.chosenGamepad = gamepad;
}
@ -119,7 +119,10 @@ export class InputsController {
this.repeatInputDurationJustPassed(b) &&
this.interactions[b].isPressed
) {
if (!this.gamepadSupport && this.interactions[b].source === 'gamepad') {
if (
(!this.gamepadSupport && this.interactions[b].source === 'gamepad') ||
(this.interactions[b].sourceName !== null && this.interactions[b].sourceName !== this.chosenGamepad)
) {
this.delLastProcessedMovementTime(b);
return;
}
@ -127,22 +130,23 @@ export class InputsController {
controller_type: this.interactions[b].source,
button: b,
});
this.setLastProcessedMovementTime(b, this.interactions[b].source);
this.setLastProcessedMovementTime(b, this.interactions[b].source, this.interactions[b].sourceName);
}
}
}
populateSetting(): void {
const gamepadsName = this.gamepads.map(g => g.id);
localStorage.setItem('gamepadsConnected', JSON.stringify(gamepadsName));
if (!this.chosenGamepad) this.chosenGamepad = gamepadsName[0];
localStorage.setItem('chosenGamepad', this.chosenGamepad);
getGamepadsName(): Array<String> {
return this.gamepads.map(g => g.id);
}
setupGamepad(thisGamepad: Phaser.Input.Gamepad.Gamepad): void {
let gamepadID = this.chosenGamepad?.toLowerCase() || thisGamepad.id.toLowerCase();
const mappedPad = this.mapGamepad(gamepadID);
this.player['mapping'] = mappedPad.gamepadMapping;
if (!this.chosenGamepad) {
this.chosenGamepad = thisGamepad.id;
localStorage.setItem('chosenGamepad', this.chosenGamepad);
}
}
refreshGamepads(): void {
@ -189,7 +193,7 @@ export class InputsController {
controller_type: 'gamepad',
button: buttonDown,
});
this.setLastProcessedMovementTime(buttonDown, 'gamepad');
this.setLastProcessedMovementTime(buttonDown, 'gamepad', pad.id);
}
}
@ -289,12 +293,13 @@ export class InputsController {
}
}
setLastProcessedMovementTime(button: Button, source: String = 'keyboard'): void {
setLastProcessedMovementTime(button: Button, source: String = 'keyboard', sourceName: String): void {
if (!this.interactions.hasOwnProperty(button)) return;
this.setButtonLock(button);
this.interactions[button].pressTime = this.time.now;
this.interactions[button].isPressed = true;
this.interactions[button].source = source;
this.interactions[button].sourceName = sourceName;
}
delLastProcessedMovementTime(button: Button): void {
@ -303,6 +308,7 @@ export class InputsController {
this.interactions[button].pressTime = null;
this.interactions[button].isPressed = false;
this.interactions[button].source = null;
this.interactions[button].sourceName = null;
}
deactivatePressedKey(): void {
@ -313,6 +319,7 @@ export class InputsController {
this.interactions[b].pressTime = null;
this.interactions[b].isPressed = false;
this.interactions[b].source = null;
this.interactions[b].sourceName = null;
}
}
}

View File

@ -487,11 +487,7 @@ export class GameData {
if (localStorage.hasOwnProperty('settingsGamepad'))
settingsGamepad = JSON.parse(localStorage.getItem('settingsGamepad'));
let gamepadsConnected = null;
if (localStorage.hasOwnProperty('gamepadsConnected')) {
gamepadsConnected = JSON.parse(localStorage.getItem('gamepadsConnected'));
}
setSettingGamepad(this.scene, setting as SettingGamepad, valueIndex, gamepadsConnected);
setSettingGamepad(this.scene, setting as SettingGamepad, valueIndex);
Object.keys(settingGamepadDefaults).forEach(s => {
if (s === setting)
settingsGamepad[s] = valueIndex;
@ -517,15 +513,10 @@ export class GameData {
if (!localStorage.hasOwnProperty('settingsGamepad'))
return false;
let gamepadConnected = null;
if (localStorage.hasOwnProperty('gamepadConnected')) {
gamepadConnected = JSON.parse(localStorage.getItem('gamepadConnected'));
}
const settingsGamepad = JSON.parse(localStorage.getItem('settingsGamepad'));
for (let setting of Object.keys(settingsGamepad))
setSettingGamepad(this.scene, setting as SettingGamepad, settingsGamepad[setting], gamepadConnected);
setSettingGamepad(this.scene, setting as SettingGamepad, settingsGamepad[setting]);
}
public saveTutorialFlag(tutorial: Tutorial, flag: boolean): boolean {

View File

@ -64,16 +64,12 @@ function truncateString(str: String, maxLength: number = 10) {
return str;
}
export function setSettingGamepad(scene: BattleScene, setting: SettingGamepad, value: integer, gamepads?: Array<String>): boolean {
export function setSettingGamepad(scene: BattleScene, setting: SettingGamepad, value: integer): boolean {
switch (setting) {
case SettingGamepad.Gamepad_Support:
console.log('setting:', setting, settingGamepadOptions[setting][value]);
scene.inputController.setGamepadSupport(settingGamepadOptions[setting][value] !== 'Disabled');
break;
case SettingGamepad.Swap_A_and_B:
console.log('settingGamepadOptions[setting][value]:', settingGamepadOptions[setting][value]);
console.log('settingGamepadOptions[setting]:', settingGamepadOptions[setting]);
console.log('value:', value);
scene.abSwapped = settingGamepadOptions[setting][value] !== 'Disabled';
break;
// case SettingGamepad.Button_Action:
@ -92,7 +88,8 @@ export function setSettingGamepad(scene: BattleScene, setting: SettingGamepad, v
// break;
case SettingGamepad.Default_Controller:
if (value) {
if (scene.ui && gamepads) {
const gp = scene.inputController.getGamepadsName();
if (scene.ui && gp) {
const cancelHandler = () => {
scene.ui.revertMode();
(scene.ui.getHandler() as SettingsGamepadUiHandler).setOptionCursor(Object.values(SettingGamepad).indexOf(SettingGamepad.Default_Controller), 0, true);
@ -105,7 +102,7 @@ export function setSettingGamepad(scene: BattleScene, setting: SettingGamepad, v
return true;
};
scene.ui.setOverlayMode(Mode.OPTION_SELECT, {
options: [...gamepads.map((g) => ({label: truncateString(g, 30), handler: () => changeGamepadHandler(g)})), {
options: [...gp.map((g) => ({label: truncateString(g, 30), handler: () => changeGamepadHandler(g)})), {
label: 'Cancel',
handler: cancelHandler,
}]

View File

@ -116,7 +116,6 @@ export default class SettingsGamepadUiHandler extends UiHandler {
super.show(args);
const settings: object = localStorage.hasOwnProperty('settingsGamepad') ? JSON.parse(localStorage.getItem('settingsGamepad')) : {};
console.log('from here');
Object.keys(settingGamepadDefaults).forEach((setting, s) => this.setOptionCursor(s, settings.hasOwnProperty(setting) ? settings[setting] : settingGamepadDefaults[setting]));
this.settingsContainer.setVisible(true);