working button detection in menu

pull/685/head
Greenlamp 2024-05-09 23:23:09 +02:00
parent 59832c6bef
commit 0d2eb1d983
5 changed files with 145 additions and 19 deletions

View File

@ -8,6 +8,7 @@ import pad_dualshock from "./configs/pad_dualshock";
import {Button} from "./enums/buttons"; import {Button} from "./enums/buttons";
import {Mode} from "./ui/ui"; import {Mode} from "./ui/ui";
import SettingsGamepadUiHandler from "./ui/settings-gamepad-ui-handler"; import SettingsGamepadUiHandler from "./ui/settings-gamepad-ui-handler";
import {SettingGamepad} from "./system/settings-gamepad";
export interface GamepadMapping { export interface GamepadMapping {
[key: string]: number; [key: string]: number;
@ -252,6 +253,12 @@ export class InputsController {
return gamepadMapping; 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 { 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 if (!this.chosenGamepad) // at the very first input, if we have not yet a chosen gamepad, we set it
this.setChosenGamepad(pad.id); this.setChosenGamepad(pad.id);
@ -411,4 +418,8 @@ export class InputsController {
if (this.buttonLock === button) this.buttonLock = null; if (this.buttonLock === button) this.buttonLock = null;
else if (this.buttonLock2 === button) this.buttonLock2 = null; else if (this.buttonLock2 === button) this.buttonLock2 = null;
} }
setBind(setting: SettingGamepad, button: Button) {
console.log('button,', button);
}
} }

View File

@ -1,15 +1,16 @@
import BattleScene from "../battle-scene"; import BattleScene from "../battle-scene";
import {SettingDefaults, SettingOptions} from "#app/system/settings"; import {SettingDefaults, SettingOptions} from "./settings";
import SettingsGamepadUiHandler from "#app/ui/settings-gamepad-ui-handler"; import SettingsGamepadUiHandler from "../ui/settings-gamepad-ui-handler";
import {Mode} from "#app/ui/ui"; import {Mode} from "#app/ui/ui";
import {truncateString} from "../utils"; import {truncateString} from "../utils";
import {Button} from "../enums/buttons";
export enum SettingGamepad { export enum SettingGamepad {
Default_Controller = "DEFAULT_CONTROLLER", Default_Controller = "DEFAULT_CONTROLLER",
Gamepad_Support = "GAMEPAD_SUPPORT", Gamepad_Support = "GAMEPAD_SUPPORT",
Swap_A_and_B = "SWAP_A_B", // Swaps which gamepad button handles ACTION and CANCEL Swap_A_and_B = "SWAP_A_B", // Swaps which gamepad button handles ACTION and CANCEL
// Button_Action = "BUTTON_ACTION", Button_Action = "BUTTON_ACTION",
// Button_Cancel = "BUTTON_CANCEL", Button_Cancel = "BUTTON_CANCEL",
// Button_Menu = "BUTTON_MENU", // Button_Menu = "BUTTON_MENU",
// Button_Stats = "BUTTON_STATS", // Button_Stats = "BUTTON_STATS",
// Button_Cycle_Shiny = "BUTTON_CYCLE_SHINY", // Button_Cycle_Shiny = "BUTTON_CYCLE_SHINY",
@ -26,8 +27,8 @@ export const settingGamepadOptions: SettingOptions = {
[SettingGamepad.Default_Controller]: [ 'Default', 'Change' ], [SettingGamepad.Default_Controller]: [ 'Default', 'Change' ],
[SettingGamepad.Gamepad_Support]: [ 'Auto', 'Disabled' ], [SettingGamepad.Gamepad_Support]: [ 'Auto', 'Disabled' ],
[SettingGamepad.Swap_A_and_B]: [ 'Enabled', 'Disabled' ], [SettingGamepad.Swap_A_and_B]: [ 'Enabled', 'Disabled' ],
// [SettingGamepad.Button_Action]: [`KEY ${Button.ACTION.toString()}`, 'Change'], [SettingGamepad.Button_Action]: [`KEY ${Button.ACTION.toString()}`, 'Change'],
// [SettingGamepad.Button_Cancel]: [`KEY ${Button.CANCEL.toString()}`, 'Change'], [SettingGamepad.Button_Cancel]: [`KEY ${Button.CANCEL.toString()}`, 'Change'],
// [SettingGamepad.Button_Menu]: [`KEY ${Button.MENU.toString()}`, 'Change'], // [SettingGamepad.Button_Menu]: [`KEY ${Button.MENU.toString()}`, 'Change'],
// [SettingGamepad.Button_Stats]: [`KEY ${Button.STATS.toString()}`, 'Change'], // [SettingGamepad.Button_Stats]: [`KEY ${Button.STATS.toString()}`, 'Change'],
// [SettingGamepad.Button_Cycle_Shiny]: [`KEY ${Button.RB.toString()}`, 'Change'], // [SettingGamepad.Button_Cycle_Shiny]: [`KEY ${Button.RB.toString()}`, 'Change'],
@ -44,8 +45,8 @@ export const settingGamepadDefaults: SettingDefaults = {
[SettingGamepad.Default_Controller]: 0, [SettingGamepad.Default_Controller]: 0,
[SettingGamepad.Gamepad_Support]: 0, [SettingGamepad.Gamepad_Support]: 0,
[SettingGamepad.Swap_A_and_B]: 1, // Set to 'Disabled' by default [SettingGamepad.Swap_A_and_B]: 1, // Set to 'Disabled' by default
// [SettingGamepad.Button_Action]: Button.ACTION, [SettingGamepad.Button_Action]: 0,
// [SettingGamepad.Button_Cancel]: Button.CANCEL, [SettingGamepad.Button_Cancel]: 0,
// [SettingGamepad.Button_Menu]: Button.MENU, // [SettingGamepad.Button_Menu]: Button.MENU,
// [SettingGamepad.Button_Stats]: Button.STATS, // [SettingGamepad.Button_Stats]: Button.STATS,
// [SettingGamepad.Button_Cycle_Shiny]: Button.RB, // [SettingGamepad.Button_Cycle_Shiny]: Button.RB,
@ -58,6 +59,11 @@ export const settingGamepadDefaults: SettingDefaults = {
// [SettingGamepad.Button_Slow_Down]: Button.SLOW_DOWN, // [SettingGamepad.Button_Slow_Down]: Button.SLOW_DOWN,
}; };
export const noOptionsCursors : Array<SettingGamepad> = [
SettingGamepad.Button_Action,
SettingGamepad.Button_Cancel,
];
export function setSettingGamepad(scene: BattleScene, setting: SettingGamepad, value: integer): boolean { export function setSettingGamepad(scene: BattleScene, setting: SettingGamepad, value: integer): boolean {
switch (setting) { switch (setting) {
case SettingGamepad.Gamepad_Support: case SettingGamepad.Gamepad_Support:
@ -68,8 +74,8 @@ export function setSettingGamepad(scene: BattleScene, setting: SettingGamepad, v
case SettingGamepad.Swap_A_and_B: case SettingGamepad.Swap_A_and_B:
scene.abSwapped = settingGamepadOptions[setting][value] !== 'Disabled'; scene.abSwapped = settingGamepadOptions[setting][value] !== 'Disabled';
break; break;
// case SettingGamepad.Button_Action: case SettingGamepad.Button_Action:
// case SettingGamepad.Button_Cancel: case SettingGamepad.Button_Cancel:
// case SettingGamepad.Button_Menu: // case SettingGamepad.Button_Menu:
// case SettingGamepad.Button_Stats: // case SettingGamepad.Button_Stats:
// case SettingGamepad.Button_Cycle_Shiny: // 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_Cycle_Variant:
// case SettingGamepad.Button_Speed_Up: // case SettingGamepad.Button_Speed_Up:
// case SettingGamepad.Button_Slow_Down: // case SettingGamepad.Button_Slow_Down:
// scene.inputController.customGamepadMapping[setting] = value; if (value) {
// break; 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: case SettingGamepad.Default_Controller:
if (value) { if (value) {
const gp = scene.inputController.getGamepadsName(); const gp = scene.inputController.getGamepadsName();

View File

@ -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();
}
}

View File

@ -6,6 +6,7 @@ import UiHandler from "./ui-handler";
import { addWindow } from "./ui-theme"; import { addWindow } from "./ui-theme";
import {Button} from "../enums/buttons"; import {Button} from "../enums/buttons";
import { import {
noOptionsCursors,
SettingGamepad, SettingGamepad,
settingGamepadDefaults, settingGamepadDefaults,
settingGamepadOptions settingGamepadOptions
@ -210,6 +211,7 @@ export default class SettingsGamepadUiHandler extends UiHandler {
const lastCursor = this.optionCursors[settingIndex]; const lastCursor = this.optionCursors[settingIndex];
if (!noOptionsCursors.includes(setting)) {
const lastValueLabel = this.optionValueLabels[settingIndex][lastCursor]; const lastValueLabel = this.optionValueLabels[settingIndex][lastCursor];
lastValueLabel.setColor(this.getTextColor(TextStyle.WINDOW)); lastValueLabel.setColor(this.getTextColor(TextStyle.WINDOW));
lastValueLabel.setShadowColor(this.getTextColor(TextStyle.WINDOW, true)); lastValueLabel.setShadowColor(this.getTextColor(TextStyle.WINDOW, true));
@ -218,6 +220,7 @@ export default class SettingsGamepadUiHandler extends UiHandler {
const newValueLabel = this.optionValueLabels[settingIndex][cursor]; const newValueLabel = this.optionValueLabels[settingIndex][cursor];
newValueLabel.setColor(this.getTextColor(TextStyle.SETTINGS_SELECTED)); newValueLabel.setColor(this.getTextColor(TextStyle.SETTINGS_SELECTED));
newValueLabel.setShadowColor(this.getTextColor(TextStyle.SETTINGS_SELECTED, true)); newValueLabel.setShadowColor(this.getTextColor(TextStyle.SETTINGS_SELECTED, true));
}
if (save) { if (save) {
if (SettingGamepad[setting] !== SettingGamepad.Default_Controller) if (SettingGamepad[setting] !== SettingGamepad.Default_Controller)

View File

@ -37,6 +37,7 @@ import UnavailableModalUiHandler from './unavailable-modal-ui-handler';
import OutdatedModalUiHandler from './outdated-modal-ui-handler'; import OutdatedModalUiHandler from './outdated-modal-ui-handler';
import SessionReloadModalUiHandler from './session-reload-modal-ui-handler'; import SessionReloadModalUiHandler from './session-reload-modal-ui-handler';
import {Button} from "../enums/buttons"; import {Button} from "../enums/buttons";
import GamepadBindingUiHandler from "./ui/gamepad-binding-ui-handler";
export enum Mode { export enum Mode {
MESSAGE, MESSAGE,
@ -58,6 +59,7 @@ export enum Mode {
MENU_OPTION_SELECT, MENU_OPTION_SELECT,
SETTINGS, SETTINGS,
SETTINGS_GAMEPAD, SETTINGS_GAMEPAD,
GAMEPAD_BINDING,
ACHIEVEMENTS, ACHIEVEMENTS,
GAME_STATS, GAME_STATS,
VOUCHERS, VOUCHERS,
@ -88,7 +90,9 @@ const noTransitionModes = [
Mode.OPTION_SELECT, Mode.OPTION_SELECT,
Mode.MENU, Mode.MENU,
Mode.MENU_OPTION_SELECT, Mode.MENU_OPTION_SELECT,
Mode.GAMEPAD_BINDING,
Mode.SETTINGS, Mode.SETTINGS,
Mode.SETTINGS_GAMEPAD,
Mode.ACHIEVEMENTS, Mode.ACHIEVEMENTS,
Mode.GAME_STATS, Mode.GAME_STATS,
Mode.VOUCHERS, Mode.VOUCHERS,
@ -140,6 +144,7 @@ export default class UI extends Phaser.GameObjects.Container {
new OptionSelectUiHandler(scene, Mode.MENU_OPTION_SELECT), new OptionSelectUiHandler(scene, Mode.MENU_OPTION_SELECT),
new SettingsUiHandler(scene), new SettingsUiHandler(scene),
new SettingsGamepadUiHandler(scene), new SettingsGamepadUiHandler(scene),
new GamepadBindingUiHandler(scene),
new AchvsUiHandler(scene), new AchvsUiHandler(scene),
new GameStatsUiHandler(scene), new GameStatsUiHandler(scene),
new VouchersUiHandler(scene), new VouchersUiHandler(scene),