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

View File

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

View File

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

View File

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