From 45e40f66ce7bc822d416c95a204633b77f0c44e6 Mon Sep 17 00:00:00 2001 From: Greenlamp Date: Wed, 8 May 2024 23:07:17 +0200 Subject: [PATCH] fix "lost focus behaviour" when disabling gamepad and thus repeating RIGHT key --- src/battle-scene.ts | 1 - src/inputs-controller.ts | 47 ++++++++++++++++++++++++++++++---------- src/system/settings.ts | 2 +- 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/battle-scene.ts b/src/battle-scene.ts index f117615e5..14fddfd36 100644 --- a/src/battle-scene.ts +++ b/src/battle-scene.ts @@ -103,7 +103,6 @@ export default class BattleScene extends SceneBase { public expGainsSpeed: integer = 0; public hpBarSpeed: integer = 0; public fusionPaletteSwaps: boolean = true; - public gamepadSupport: boolean = true; public enableTouchControls: boolean = false; public enableVibration: boolean = false; public abSwapped: boolean = false; diff --git a/src/inputs-controller.ts b/src/inputs-controller.ts index 5f5615358..6b3e6182a 100644 --- a/src/inputs-controller.ts +++ b/src/inputs-controller.ts @@ -36,6 +36,8 @@ export class InputsController { private time: Time; private player: Map = new Map(); + private gamepadSupport: boolean = true; + constructor(scene: Phaser.Scene) { this.scene = scene; this.time = this.scene.time; @@ -45,6 +47,7 @@ export class InputsController { this.interactions[b] = { pressTime: false, isPressed: false, + source: null, } } // We don't want the menu key to be repeated @@ -87,16 +90,32 @@ export class InputsController { this.deactivatePressedKey(); } + setGamepadSupport(value: boolean): void { + if (value) { + this.gamepadSupport = true; + } else { + this.gamepadSupport = false; + this.deactivatePressedKey(); + } + } + update(): void { // reversed to let the cancel button have a kinda priority on the action button for (const b of Utils.getEnumValues(Button).reverse()) { - if (!this.interactions.hasOwnProperty(b)) continue; - if (this.repeatInputDurationJustPassed(b) && this.interactions[b].isPressed) { + if ( + this.interactions.hasOwnProperty(b) && + this.repeatInputDurationJustPassed(b) && + this.interactions[b].isPressed + ) { + if (!this.gamepadSupport && this.interactions[b].source === 'gamepad') { + this.delLastProcessedMovementTime(b); + return; + } this.events.emit('input_down', { - controller_type: 'repeated', + controller_type: this.interactions[b].source, button: b, }); - this.setLastProcessedMovementTime(b); + this.setLastProcessedMovementTime(b, this.interactions[b].source); } } } @@ -143,7 +162,7 @@ export class InputsController { } gamepadButtonDown(pad: Phaser.Input.Gamepad.Gamepad, button: Phaser.Input.Gamepad.Button, value: number): void { - if (!this.scene.gamepadSupport) return; + if (!this.gamepadSupport) return; const actionMapping = this.getActionGamepadMapping(); const buttonDown = actionMapping.hasOwnProperty(button.index) && actionMapping[button.index]; if (buttonDown !== undefined) { @@ -151,12 +170,12 @@ export class InputsController { controller_type: 'gamepad', button: buttonDown, }); - this.setLastProcessedMovementTime(buttonDown); + this.setLastProcessedMovementTime(buttonDown, 'gamepad'); } } gamepadButtonUp(pad: Phaser.Input.Gamepad.Gamepad, button: Phaser.Input.Gamepad.Button, value: number): void { - if (!this.scene.gamepadSupport) return; + if (!this.gamepadSupport) return; const actionMapping = this.getActionGamepadMapping(); const buttonUp = actionMapping.hasOwnProperty(button.index) && actionMapping[button.index]; if (buttonUp !== undefined) { @@ -212,7 +231,7 @@ export class InputsController { controller_type: 'keyboard', button: index, }); - this.setLastProcessedMovementTime(index); + this.setLastProcessedMovementTime(index, 'keyboard'); }); key.on('up', () => { this.events.emit('input_up', { @@ -251,11 +270,12 @@ export class InputsController { } } - setLastProcessedMovementTime(button: Button): void { + setLastProcessedMovementTime(button: Button, source: String = 'keyboard'): 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; } delLastProcessedMovementTime(button: Button): void { @@ -263,15 +283,18 @@ export class InputsController { this.releaseButtonLock(button); this.interactions[button].pressTime = null; this.interactions[button].isPressed = false; + this.interactions[button].source = null; } deactivatePressedKey(): void { this.releaseButtonLock(this.buttonLock); this.releaseButtonLock(this.buttonLock2); for (const b of Utils.getEnumValues(Button)) { - if (!this.interactions.hasOwnProperty(b)) return; - this.interactions[b].pressTime = null; - this.interactions[b].isPressed = false; + if (this.interactions.hasOwnProperty(b)) { + this.interactions[b].pressTime = null; + this.interactions[b].isPressed = false; + this.interactions[b].source = null; + } } } diff --git a/src/system/settings.ts b/src/system/settings.ts index 054bdc9fc..b0c61fabf 100644 --- a/src/system/settings.ts +++ b/src/system/settings.ts @@ -149,7 +149,7 @@ export function setSetting(scene: BattleScene, setting: Setting, value: integer) return false; break; case Setting.Gamepad_Support: - scene.gamepadSupport = settingOptions[setting][value] !== 'Disabled'; + scene.inputController.setGamepadSupport(settingOptions[setting][value] !== 'Disabled'); break; case Setting.Swap_A_and_B: scene.abSwapped = settingOptions[setting][value] !== 'Disabled';