From 0d2eb1d9832dfd684b69060a3a3abcbf96d70ee0 Mon Sep 17 00:00:00 2001 From: Greenlamp Date: Thu, 9 May 2024 23:23:09 +0200 Subject: [PATCH] working button detection in menu --- src/inputs-controller.ts | 11 ++++ src/system/settings-gamepad.ts | 44 ++++++++++---- src/ui/gamepad-binding-ui-handler.ts | 87 +++++++++++++++++++++++++++ src/ui/settings-gamepad-ui-handler.ts | 17 +++--- src/ui/ui.ts | 5 ++ 5 files changed, 145 insertions(+), 19 deletions(-) create mode 100644 src/ui/gamepad-binding-ui-handler.ts diff --git a/src/inputs-controller.ts b/src/inputs-controller.ts index 9c24c3cfd..940464975 100644 --- a/src/inputs-controller.ts +++ b/src/inputs-controller.ts @@ -8,6 +8,7 @@ import pad_dualshock from "./configs/pad_dualshock"; import {Button} from "./enums/buttons"; import {Mode} from "./ui/ui"; import SettingsGamepadUiHandler from "./ui/settings-gamepad-ui-handler"; +import {SettingGamepad} from "./system/settings-gamepad"; export interface GamepadMapping { [key: string]: number; @@ -252,6 +253,12 @@ export class InputsController { return gamepadMapping; } + getButtonLabel(button: Phaser.Input.Gamepad.Button) { + const mapping = this.player[this.chosenGamepad]['mapping']; + console.log('mapping', mapping); + return Object.keys(mapping).find(key => mapping[key] === button.index); + } + gamepadButtonDown(pad: Phaser.Input.Gamepad.Gamepad, button: Phaser.Input.Gamepad.Button, value: number): void { if (!this.chosenGamepad) // at the very first input, if we have not yet a chosen gamepad, we set it this.setChosenGamepad(pad.id); @@ -411,4 +418,8 @@ export class InputsController { if (this.buttonLock === button) this.buttonLock = null; else if (this.buttonLock2 === button) this.buttonLock2 = null; } + + setBind(setting: SettingGamepad, button: Button) { + console.log('button,', button); + } } \ No newline at end of file diff --git a/src/system/settings-gamepad.ts b/src/system/settings-gamepad.ts index a9ccc6090..ce4fa77ea 100644 --- a/src/system/settings-gamepad.ts +++ b/src/system/settings-gamepad.ts @@ -1,15 +1,16 @@ import BattleScene from "../battle-scene"; -import {SettingDefaults, SettingOptions} from "#app/system/settings"; -import SettingsGamepadUiHandler from "#app/ui/settings-gamepad-ui-handler"; +import {SettingDefaults, SettingOptions} from "./settings"; +import SettingsGamepadUiHandler from "../ui/settings-gamepad-ui-handler"; import {Mode} from "#app/ui/ui"; import {truncateString} from "../utils"; +import {Button} from "../enums/buttons"; export enum SettingGamepad { Default_Controller = "DEFAULT_CONTROLLER", Gamepad_Support = "GAMEPAD_SUPPORT", Swap_A_and_B = "SWAP_A_B", // Swaps which gamepad button handles ACTION and CANCEL - // Button_Action = "BUTTON_ACTION", - // Button_Cancel = "BUTTON_CANCEL", + Button_Action = "BUTTON_ACTION", + Button_Cancel = "BUTTON_CANCEL", // Button_Menu = "BUTTON_MENU", // Button_Stats = "BUTTON_STATS", // Button_Cycle_Shiny = "BUTTON_CYCLE_SHINY", @@ -26,8 +27,8 @@ export const settingGamepadOptions: SettingOptions = { [SettingGamepad.Default_Controller]: [ 'Default', 'Change' ], [SettingGamepad.Gamepad_Support]: [ 'Auto', 'Disabled' ], [SettingGamepad.Swap_A_and_B]: [ 'Enabled', 'Disabled' ], - // [SettingGamepad.Button_Action]: [`KEY ${Button.ACTION.toString()}`, 'Change'], - // [SettingGamepad.Button_Cancel]: [`KEY ${Button.CANCEL.toString()}`, 'Change'], + [SettingGamepad.Button_Action]: [`KEY ${Button.ACTION.toString()}`, 'Change'], + [SettingGamepad.Button_Cancel]: [`KEY ${Button.CANCEL.toString()}`, 'Change'], // [SettingGamepad.Button_Menu]: [`KEY ${Button.MENU.toString()}`, 'Change'], // [SettingGamepad.Button_Stats]: [`KEY ${Button.STATS.toString()}`, 'Change'], // [SettingGamepad.Button_Cycle_Shiny]: [`KEY ${Button.RB.toString()}`, 'Change'], @@ -44,8 +45,8 @@ export const settingGamepadDefaults: SettingDefaults = { [SettingGamepad.Default_Controller]: 0, [SettingGamepad.Gamepad_Support]: 0, [SettingGamepad.Swap_A_and_B]: 1, // Set to 'Disabled' by default - // [SettingGamepad.Button_Action]: Button.ACTION, - // [SettingGamepad.Button_Cancel]: Button.CANCEL, + [SettingGamepad.Button_Action]: 0, + [SettingGamepad.Button_Cancel]: 0, // [SettingGamepad.Button_Menu]: Button.MENU, // [SettingGamepad.Button_Stats]: Button.STATS, // [SettingGamepad.Button_Cycle_Shiny]: Button.RB, @@ -58,6 +59,11 @@ export const settingGamepadDefaults: SettingDefaults = { // [SettingGamepad.Button_Slow_Down]: Button.SLOW_DOWN, }; +export const noOptionsCursors : Array = [ + SettingGamepad.Button_Action, + SettingGamepad.Button_Cancel, +]; + export function setSettingGamepad(scene: BattleScene, setting: SettingGamepad, value: integer): boolean { switch (setting) { case SettingGamepad.Gamepad_Support: @@ -68,8 +74,8 @@ export function setSettingGamepad(scene: BattleScene, setting: SettingGamepad, v case SettingGamepad.Swap_A_and_B: scene.abSwapped = settingGamepadOptions[setting][value] !== 'Disabled'; break; - // case SettingGamepad.Button_Action: - // case SettingGamepad.Button_Cancel: + case SettingGamepad.Button_Action: + case SettingGamepad.Button_Cancel: // case SettingGamepad.Button_Menu: // case SettingGamepad.Button_Stats: // case SettingGamepad.Button_Cycle_Shiny: @@ -80,8 +86,22 @@ export function setSettingGamepad(scene: BattleScene, setting: SettingGamepad, v // case SettingGamepad.Button_Cycle_Variant: // case SettingGamepad.Button_Speed_Up: // case SettingGamepad.Button_Slow_Down: - // scene.inputController.customGamepadMapping[setting] = value; - // break; + if (value) { + if (scene.ui) { + const cancelHandler = () => { + scene.ui.revertMode(); + (scene.ui.getHandler() as SettingsGamepadUiHandler).setOptionCursor(Object.values(SettingGamepad).indexOf(setting), 0, true); + return false; + }; + scene.ui.setOverlayMode(Mode.GAMEPAD_BINDING, { + options: [{ + label: 'Press a button on your gamepad', + handler: cancelHandler, + }] + }); + } + } + break; case SettingGamepad.Default_Controller: if (value) { const gp = scene.inputController.getGamepadsName(); diff --git a/src/ui/gamepad-binding-ui-handler.ts b/src/ui/gamepad-binding-ui-handler.ts new file mode 100644 index 000000000..6f1422e67 --- /dev/null +++ b/src/ui/gamepad-binding-ui-handler.ts @@ -0,0 +1,87 @@ +import UiHandler from "./ui-handler"; +import BattleScene from "#app/battle-scene"; +import {Mode} from "./ui"; +import {Button} from "../enums/buttons"; +import {addWindow} from "./ui-theme"; +import {addTextObject, TextStyle} from "#app/ui/text"; +import Phaser from "phaser"; + + +export default class GamepadBindingUiHandler extends UiHandler { + protected optionSelectContainer: Phaser.GameObjects.Container; + protected optionSelectBg: Phaser.GameObjects.NineSlice; + private unlockText: Phaser.GameObjects.Text; + private keyPressed: Phaser.GameObjects.Text; + private listening: boolean = false; + private buttonPressed = ''; + + constructor(scene: BattleScene, mode: Mode = Mode.GAMEPAD_BINDING) { + super(scene, mode); + scene.input.gamepad.on('down', this.gamepadButtonDown, this); + } + + // const loadSessionBg = this.scene.add.rectangle(this.scene.game.canvas.width / 24, -this.scene.game.canvas.height / 24, this.scene.game.canvas.width / 12, -this.scene.game.canvas.height / 12, 0x006860); + // loadSessionBg.setOrigin(0, 0); + // this.optionSelectContainer.add(loadSessionBg); + setup() { + const ui = this.getUi(); + this.optionSelectContainer = this.scene.add.container(0, 0); + this.optionSelectContainer.setVisible(false); + ui.add(this.optionSelectContainer); + + // this.optionSelectBg = addWindow(this.scene, this.scene.game.canvas.width / 12, -this.scene.game.canvas.height / 12, this.getWindowWidth(), -this.getWindowHeight()); + 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); + + this.unlockText = addTextObject(this.scene, 0, 0, 'Press a button...', TextStyle.WINDOW); + this.unlockText.setOrigin(0, 0); + this.unlockText.setPositionRelative(this.optionSelectBg, 36, 4); + + this.keyPressed = addTextObject(this.scene, 0, 0, '', TextStyle.WINDOW); + this.keyPressed.setOrigin(0, 0); + this.keyPressed.setPositionRelative(this.unlockText, 0, 12); + this.keyPressed.setVisible(false); + + + this.optionSelectContainer.add(this.unlockText); + this.optionSelectContainer.add(this.keyPressed); + } + + gamepadButtonDown(pad: Phaser.Input.Gamepad.Gamepad, button: Phaser.Input.Gamepad.Button, value: number): void { + if (!this.listening) return; + this.buttonPressed = button.index; + const buttonLabel = this.scene.inputController.getButtonLabel(button); + this.keyPressed.setText(buttonLabel); + this.keyPressed.setVisible(true); + } + + show(args: any[]): boolean { + console.log('args', args); + super.show(args); + + this.getUi().bringToTop(this.optionSelectContainer); + + this.optionSelectContainer.setVisible(true); + setTimeout(() => this.listening = true, 150); + return true; + } + + getWindowWidth(): number { + return 160; + } + + getWindowHeight(): number { + return 64; + } + + processInput(button: Button): boolean { + const ui = this.getUi(); + return true; + } + + clear() { + super.clear(); + } + +} \ No newline at end of file diff --git a/src/ui/settings-gamepad-ui-handler.ts b/src/ui/settings-gamepad-ui-handler.ts index 77caa34f3..b044dd6f7 100644 --- a/src/ui/settings-gamepad-ui-handler.ts +++ b/src/ui/settings-gamepad-ui-handler.ts @@ -6,6 +6,7 @@ import UiHandler from "./ui-handler"; import { addWindow } from "./ui-theme"; import {Button} from "../enums/buttons"; import { + noOptionsCursors, SettingGamepad, settingGamepadDefaults, settingGamepadOptions @@ -210,14 +211,16 @@ export default class SettingsGamepadUiHandler extends UiHandler { const lastCursor = this.optionCursors[settingIndex]; - const lastValueLabel = this.optionValueLabels[settingIndex][lastCursor]; - lastValueLabel.setColor(this.getTextColor(TextStyle.WINDOW)); - lastValueLabel.setShadowColor(this.getTextColor(TextStyle.WINDOW, true)); + if (!noOptionsCursors.includes(setting)) { + const lastValueLabel = this.optionValueLabels[settingIndex][lastCursor]; + lastValueLabel.setColor(this.getTextColor(TextStyle.WINDOW)); + lastValueLabel.setShadowColor(this.getTextColor(TextStyle.WINDOW, true)); - this.optionCursors[settingIndex] = cursor; - const newValueLabel = this.optionValueLabels[settingIndex][cursor]; - newValueLabel.setColor(this.getTextColor(TextStyle.SETTINGS_SELECTED)); - newValueLabel.setShadowColor(this.getTextColor(TextStyle.SETTINGS_SELECTED, true)); + this.optionCursors[settingIndex] = cursor; + const newValueLabel = this.optionValueLabels[settingIndex][cursor]; + newValueLabel.setColor(this.getTextColor(TextStyle.SETTINGS_SELECTED)); + newValueLabel.setShadowColor(this.getTextColor(TextStyle.SETTINGS_SELECTED, true)); + } if (save) { if (SettingGamepad[setting] !== SettingGamepad.Default_Controller) diff --git a/src/ui/ui.ts b/src/ui/ui.ts index 2b2c8f756..fa2090f4a 100644 --- a/src/ui/ui.ts +++ b/src/ui/ui.ts @@ -37,6 +37,7 @@ import UnavailableModalUiHandler from './unavailable-modal-ui-handler'; import OutdatedModalUiHandler from './outdated-modal-ui-handler'; import SessionReloadModalUiHandler from './session-reload-modal-ui-handler'; import {Button} from "../enums/buttons"; +import GamepadBindingUiHandler from "./ui/gamepad-binding-ui-handler"; export enum Mode { MESSAGE, @@ -58,6 +59,7 @@ export enum Mode { MENU_OPTION_SELECT, SETTINGS, SETTINGS_GAMEPAD, + GAMEPAD_BINDING, ACHIEVEMENTS, GAME_STATS, VOUCHERS, @@ -88,7 +90,9 @@ const noTransitionModes = [ Mode.OPTION_SELECT, Mode.MENU, Mode.MENU_OPTION_SELECT, + Mode.GAMEPAD_BINDING, Mode.SETTINGS, + Mode.SETTINGS_GAMEPAD, Mode.ACHIEVEMENTS, Mode.GAME_STATS, Mode.VOUCHERS, @@ -140,6 +144,7 @@ export default class UI extends Phaser.GameObjects.Container { new OptionSelectUiHandler(scene, Mode.MENU_OPTION_SELECT), new SettingsUiHandler(scene), new SettingsGamepadUiHandler(scene), + new GamepadBindingUiHandler(scene), new AchvsUiHandler(scene), new GameStatsUiHandler(scene), new VouchersUiHandler(scene),