From ac9a011da44c3381c5ef209d432845edbeebf3de Mon Sep 17 00:00:00 2001 From: Greenlamp Date: Wed, 15 May 2024 00:32:38 +0200 Subject: [PATCH] added a message instead of nothing in the keyboard menu if playing on mobile --- src/inputs-controller.ts | 11 ++++--- .../settings/settings-keyboard-ui-handler.ts | 31 +++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/inputs-controller.ts b/src/inputs-controller.ts index ab85ad12b..4c6dcd928 100644 --- a/src/inputs-controller.ts +++ b/src/inputs-controller.ts @@ -389,10 +389,14 @@ export class InputsController { } } - keyboardKeyDown(event): void { - const keyDown = event.keyCode; + checkIfKeyboardIsInit(): void { if (!this.keyboardConfigs[this.chosenKeyboard]?.padID) this.setupKeyboard(); + } + + keyboardKeyDown(event): void { + const keyDown = event.keyCode; + this.checkIfKeyboardIsInit(); if (this.keys.includes(keyDown)) return; this.keys.push(keyDown); const key = getKeyFromMapping(this.keyboardConfigs[this.chosenKeyboard], keyDown); @@ -410,8 +414,7 @@ export class InputsController { keyboardKeyUp(event): void { const keyDown = event.keyCode; this.keys = this.keys.filter(k => k !== keyDown); - if (!this.keyboardConfigs[this.chosenKeyboard]?.padID) - this.setupKeyboard(); + this.checkIfKeyboardIsInit() const key = getKeyFromMapping(this.keyboardConfigs[this.chosenKeyboard], keyDown); const buttonUp = this.keyboardConfigs[this.chosenKeyboard].custom[key]; if (buttonUp !== undefined) { diff --git a/src/ui/settings/settings-keyboard-ui-handler.ts b/src/ui/settings/settings-keyboard-ui-handler.ts index ad2b04cf4..fd5a86876 100644 --- a/src/ui/settings/settings-keyboard-ui-handler.ts +++ b/src/ui/settings/settings-keyboard-ui-handler.ts @@ -6,6 +6,7 @@ import {reverseValueToKeySetting, truncateString} from "#app/utils"; import AbstractSettingsUiUiHandler from "#app/ui/settings/abstract-settings-ui-handler"; import {InterfaceConfig} from "#app/inputs-controller"; import {deleteBind} from "#app/configs/gamepad-utils"; +import {addTextObject, TextStyle} from "#app/ui/text"; export default class SettingsKeyboardUiHandler extends AbstractSettingsUiUiHandler { @@ -24,6 +25,22 @@ export default class SettingsKeyboardUiHandler extends AbstractSettingsUiUiHandl deleteEvent.on('up', this.onDeleteDown, this); } + setup() { + super.setup(); + // If no gamepads are detected, set up a default UI prompt in the settings container. + this.layout['noKeyboard'] = 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, 'Please press a key on your keyboard', TextStyle.SETTINGS_LABEL); + label.setOrigin(0, 0); + optionsContainer.add(label); + this.settingsContainer.add(optionsContainer); + + // Map the 'noKeyboard' layout options for easy access. + this.layout['noKeyboard'].optionsContainer = optionsContainer; + this.layout['noKeyboard'].label = label; + } + onDeleteDown(): void { const cursor = this.cursor + this.scrollCursor; // Calculate the absolute cursor position. const selection = this.settingLabels[cursor].text; @@ -45,6 +62,20 @@ export default class SettingsKeyboardUiHandler extends AbstractSettingsUiUiHandl return settings; } + 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['noKeyboard']; + // 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); + } + navigateMenuLeft(): boolean { this.scene.ui.setMode(Mode.SETTINGS_GAMEPAD) return true;