From d08b0284981d982195943d14454bedd419cc3801 Mon Sep 17 00:00:00 2001 From: Greenlamp Date: Sun, 19 May 2024 11:00:08 +0200 Subject: [PATCH] added a timeout to the binding windows + fix update layout on new gamepad --- src/inputs-controller.ts | 2 ++ src/system/settings-gamepad.ts | 2 +- src/ui/settings/abrast-binding-ui-handler.ts | 29 ++++++++++++++++++- .../settings/abstract-settings-ui-handler.ts | 12 ++++++-- .../settings/settings-gamepad-ui-handler.ts | 1 + 5 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/inputs-controller.ts b/src/inputs-controller.ts index 77a7cc88817..b9649657a89 100644 --- a/src/inputs-controller.ts +++ b/src/inputs-controller.ts @@ -331,6 +331,8 @@ export class InputsController { this.scene.gameData?.saveMappingConfigs(gamepadID, this.configs[gamepadID]); } this.lastSource = 'gamepad'; + const handler = this.scene.ui?.handlers[Mode.SETTINGS_GAMEPAD] as SettingsGamepadUiHandler; + handler && handler.updateChosenGamepadDisplay() } /** diff --git a/src/system/settings-gamepad.ts b/src/system/settings-gamepad.ts index 34ec7d192e0..428d149841a 100644 --- a/src/system/settings-gamepad.ts +++ b/src/system/settings-gamepad.ts @@ -129,7 +129,7 @@ export function setSettingGamepad(scene: BattleScene, setting: SettingGamepad, v }; scene.ui.setOverlayMode(Mode.OPTION_SELECT, { options: [...gp.map((g) => ({ - label: truncateString(g, 22), // Truncate the gamepad name for display + label: truncateString(g, 30), // Truncate the gamepad name for display handler: () => changeGamepadHandler(g) })), { label: 'Cancel', diff --git a/src/ui/settings/abrast-binding-ui-handler.ts b/src/ui/settings/abrast-binding-ui-handler.ts index 954ac1c755b..e5e317ca4bc 100644 --- a/src/ui/settings/abrast-binding-ui-handler.ts +++ b/src/ui/settings/abrast-binding-ui-handler.ts @@ -21,6 +21,7 @@ export default abstract class AbstractBindingUiHandler extends UiHandler { // Text elements for displaying instructions and actions. protected unlockText: Phaser.GameObjects.Text; + protected timerText: Phaser.GameObjects.Text; protected swapText: Phaser.GameObjects.Text; protected actionLabel: Phaser.GameObjects.Text; protected cancelLabel: Phaser.GameObjects.Text; @@ -37,6 +38,8 @@ export default abstract class AbstractBindingUiHandler extends UiHandler { protected swapAction: () => boolean; protected confirmText: string; + protected timeLeftAutoClose: number = 5; + protected countdownTimer; // The specific setting being modified. protected target; @@ -81,6 +84,11 @@ export default abstract class AbstractBindingUiHandler extends UiHandler { this.unlockText.setPositionRelative(this.titleBg, 36, 4); this.optionSelectContainer.add(this.unlockText); + this.timerText = addTextObject(this.scene, 0, 0, '(5)', TextStyle.WINDOW); + this.timerText.setOrigin(0, 0); + this.timerText.setPositionRelative(this.unlockText, (this.unlockText.width/6) + 5, 0); + this.optionSelectContainer.add(this.timerText); + this.optionSelectBg = addWindow(this.scene, (this.scene.game.canvas.width / 6) - this.getWindowWidth(), -(this.scene.game.canvas.height / 6) + this.getWindowHeight() + 28, this.getWindowWidth(), this.getWindowHeight()); this.optionSelectBg.setOrigin(0.5); this.optionSelectContainer.add(this.optionSelectBg); @@ -91,6 +99,18 @@ export default abstract class AbstractBindingUiHandler extends UiHandler { this.actionsContainer.add(this.cancelLabel); } + manageAutoCloseTimer(){ + clearTimeout(this.countdownTimer); + this.countdownTimer = setTimeout(() => { + this.timeLeftAutoClose -= 1; + this.timerText.setText(`(${this.timeLeftAutoClose})`); + if (this.timeLeftAutoClose >= 0) + this.manageAutoCloseTimer(); + else + this.cancelFn(); + }, 1000); + } + /** * Show the UI with the provided arguments. * @@ -100,6 +120,7 @@ export default abstract class AbstractBindingUiHandler extends UiHandler { show(args: any[]): boolean { super.show(args); this.buttonPressed = null; + this.timeLeftAutoClose = 5; this.cancelFn = args[0].cancelHandler; this.target = args[0].target; @@ -108,7 +129,10 @@ export default abstract class AbstractBindingUiHandler extends UiHandler { this.getUi().bringToTop(this.actionsContainer); this.optionSelectContainer.setVisible(true); - setTimeout(() => this.listening = true, 100); + setTimeout(() => { + this.listening = true; + this.manageAutoCloseTimer(); + }, 100); return true; } @@ -194,6 +218,9 @@ export default abstract class AbstractBindingUiHandler extends UiHandler { */ clear() { super.clear(); + clearTimeout(this.countdownTimer); + this.timerText.setText('(5)'); + this.timeLeftAutoClose = 5; this.listening = false; this.target = null; this.cancelFn = null; diff --git a/src/ui/settings/abstract-settings-ui-handler.ts b/src/ui/settings/abstract-settings-ui-handler.ts index c810a095cc9..3c34cdf0662 100644 --- a/src/ui/settings/abstract-settings-ui-handler.ts +++ b/src/ui/settings/abstract-settings-ui-handler.ts @@ -373,8 +373,7 @@ export default abstract class AbstractSettingsUiUiHandler extends UiHandler { // Make the settings container visible to the user. this.settingsContainer.setVisible(true); // Reset the scroll cursor to the top of the settings container. - this.setCursor(0); - this.setScrollCursor(0); + this.resetScroll(); // Move the settings container to the end of the UI stack to ensure it is displayed on top. this.getUi().moveTo(this.settingsContainer, this.getUi().length - 1); @@ -510,6 +509,15 @@ export default abstract class AbstractSettingsUiUiHandler extends UiHandler { return success; // Return whether the input resulted in a successful action. } + resetScroll() { + this.cursorObj?.destroy(); + this.cursorObj = null; + this.cursor = null; + this.setCursor(0); + this.setScrollCursor(0); + this.updateSettingsScroll(); + } + /** * Set the cursor to the specified position. * diff --git a/src/ui/settings/settings-gamepad-ui-handler.ts b/src/ui/settings/settings-gamepad-ui-handler.ts index 1a7e3b396d9..3d59b090591 100644 --- a/src/ui/settings/settings-gamepad-ui-handler.ts +++ b/src/ui/settings/settings-gamepad-ui-handler.ts @@ -133,6 +133,7 @@ export default class SettingsGamepadUiHandler extends AbstractSettingsUiUiHandle 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. for (const [index, key] of Object.keys(this.settingDevice).entries()) {