swap is working + display current assignation on rebinding
parent
cfa919b46c
commit
5ff09389db
|
@ -2,15 +2,21 @@ import {GamepadConfig} from "../inputs-controller";
|
||||||
import {SettingGamepad} from "#app/system/settings-gamepad";
|
import {SettingGamepad} from "#app/system/settings-gamepad";
|
||||||
|
|
||||||
|
|
||||||
export function getKeyForButtonIndex(config: GamepadConfig, index: integer): String {
|
export function getKeyForButtonIndex(config: GamepadConfig, index: number): String {
|
||||||
for (const key of Object.keys(config.gamepadMapping)) {
|
for (const key of Object.keys(config.gamepadMapping)) {
|
||||||
const id = config.gamepadMapping[key];
|
const id = config.gamepadMapping[key];
|
||||||
if (id === index) return key;
|
if (id === index) return key;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
export function getButtonIndexForKey(config: GamepadConfig, _key: string): number {
|
||||||
|
for (const key of Object.keys(config.gamepadMapping)) {
|
||||||
|
if (key === _key) return config.gamepadMapping[key];
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
export function getIconForCustomIndex(config: GamepadConfig, index: integer): String {
|
export function getIconForCustomIndex(config: GamepadConfig, index: number): String {
|
||||||
const key = getKeyForButtonIndex(config, index);
|
const key = getKeyForButtonIndex(config, index);
|
||||||
return config.icons[key];
|
return config.icons[key];
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,12 @@ 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";
|
import {SettingGamepad} from "./system/settings-gamepad";
|
||||||
import {getIconForCustomIndex, getKeyForButtonIndex} from "#app/configs/gamepad-utils";
|
import {
|
||||||
|
getButtonIndexForKey,
|
||||||
|
getIconForCustomIndex,
|
||||||
|
getKeyForButtonIndex,
|
||||||
|
getKeyForSettingName
|
||||||
|
} from "./configs/gamepad-utils";
|
||||||
|
|
||||||
export interface GamepadMapping {
|
export interface GamepadMapping {
|
||||||
[key: string]: number;
|
[key: string]: number;
|
||||||
|
@ -33,6 +38,7 @@ export interface GamepadConfig {
|
||||||
icons: IconsMapping;
|
icons: IconsMapping;
|
||||||
setting: SettingMapping;
|
setting: SettingMapping;
|
||||||
default: DefaultMapping;
|
default: DefaultMapping;
|
||||||
|
custom: DefaultMapping;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ActionGamepadMapping {
|
export interface ActionGamepadMapping {
|
||||||
|
@ -323,10 +329,6 @@ export class InputsController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
getButtonLabel(button: Phaser.Input.Gamepad.Button) {
|
|
||||||
return getIconForCustomIndex(this.configs[this.chosenGamepad], button.index);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles the 'down' event for gamepad buttons, emitting appropriate events and updating the interaction state.
|
* Handles the 'down' event for gamepad buttons, emitting appropriate events and updating the interaction state.
|
||||||
*
|
*
|
||||||
|
@ -648,4 +650,24 @@ export class InputsController {
|
||||||
getActiveConfig() :GamepadConfig {
|
getActiveConfig() :GamepadConfig {
|
||||||
return this.configs[this.chosenGamepad] || pad_generic;
|
return this.configs[this.chosenGamepad] || pad_generic;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getPressedButtonLabel(button: Phaser.Input.Gamepad.Button) {
|
||||||
|
return [this.configs[this.chosenGamepad].padType, getIconForCustomIndex(this.configs[this.chosenGamepad], button.index)];
|
||||||
|
}
|
||||||
|
|
||||||
|
getCurrentButtonLabel(target: SettingGamepad) {
|
||||||
|
const key = getKeyForSettingName(this.configs[this.chosenGamepad], target);
|
||||||
|
const id = getButtonIndexForKey(this.configs[this.chosenGamepad], key);
|
||||||
|
return getIconForCustomIndex(this.configs[this.chosenGamepad], id);
|
||||||
|
}
|
||||||
|
|
||||||
|
swapBinding(target, newBinding) {
|
||||||
|
this.deactivatePressedKey();
|
||||||
|
const keyTarget = getKeyForSettingName(this.configs[this.chosenGamepad], target);
|
||||||
|
const keyNewBinding = getKeyForButtonIndex(this.configs[this.chosenGamepad], newBinding);
|
||||||
|
const previousActionForThisNewBinding = this.configs[this.chosenGamepad].custom[keyNewBinding];
|
||||||
|
const ActionForThisNewBinding = this.configs[this.chosenGamepad].custom[keyTarget];
|
||||||
|
this.configs[this.chosenGamepad].custom[keyTarget] = previousActionForThisNewBinding;
|
||||||
|
this.configs[this.chosenGamepad].custom[keyNewBinding] = ActionForThisNewBinding;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -102,9 +102,10 @@ export function setSettingGamepad(scene: BattleScene, setting: SettingGamepad, v
|
||||||
case SettingGamepad.Button_Slow_Down:
|
case SettingGamepad.Button_Slow_Down:
|
||||||
if (value) {
|
if (value) {
|
||||||
if (scene.ui) {
|
if (scene.ui) {
|
||||||
const cancelHandler = () => {
|
const cancelHandler = (success: boolean = false) => {
|
||||||
scene.ui.revertMode();
|
scene.ui.revertMode();
|
||||||
return false;
|
(scene.ui.getHandler() as SettingsGamepadUiHandler).updateBindings();
|
||||||
|
return success;
|
||||||
};
|
};
|
||||||
scene.ui.setOverlayMode(Mode.GAMEPAD_BINDING, {
|
scene.ui.setOverlayMode(Mode.GAMEPAD_BINDING, {
|
||||||
target: setting,
|
target: setting,
|
||||||
|
|
|
@ -5,6 +5,7 @@ import {Button} from "../enums/buttons";
|
||||||
import {addWindow} from "./ui-theme";
|
import {addWindow} from "./ui-theme";
|
||||||
import {addTextObject, TextStyle} from "#app/ui/text";
|
import {addTextObject, TextStyle} from "#app/ui/text";
|
||||||
import Phaser from "phaser";
|
import Phaser from "phaser";
|
||||||
|
import {SettingGamepad} from "../system/settings-gamepad";
|
||||||
|
|
||||||
|
|
||||||
export default class GamepadBindingUiHandler extends UiHandler {
|
export default class GamepadBindingUiHandler extends UiHandler {
|
||||||
|
@ -24,7 +25,7 @@ export default class GamepadBindingUiHandler extends UiHandler {
|
||||||
private previousIconXbox: Phaser.GameObjects.Sprite;
|
private previousIconXbox: Phaser.GameObjects.Sprite;
|
||||||
private iconDualshock: Phaser.GameObjects.Sprite;
|
private iconDualshock: Phaser.GameObjects.Sprite;
|
||||||
private cancelFn;
|
private cancelFn;
|
||||||
private target;
|
private target: SettingGamepad;
|
||||||
|
|
||||||
constructor(scene: BattleScene, mode: Mode = Mode.GAMEPAD_BINDING) {
|
constructor(scene: BattleScene, mode: Mode = Mode.GAMEPAD_BINDING) {
|
||||||
super(scene, mode);
|
super(scene, mode);
|
||||||
|
@ -104,7 +105,8 @@ export default class GamepadBindingUiHandler extends UiHandler {
|
||||||
const blacklist = [12, 13, 14, 15];
|
const blacklist = [12, 13, 14, 15];
|
||||||
if (!this.listening || pad.id !== this.scene.inputController?.chosenGamepad || blacklist.includes(button.index) || this.buttonPressed !== null) return;
|
if (!this.listening || pad.id !== this.scene.inputController?.chosenGamepad || blacklist.includes(button.index) || this.buttonPressed !== null) return;
|
||||||
this.buttonPressed = button.index;
|
this.buttonPressed = button.index;
|
||||||
const [type, buttonIcon] = this.scene.inputController.getButtonLabel(button);
|
const [type, buttonIcon] = this.scene.inputController.getPressedButtonLabel(button);
|
||||||
|
const assignedButtonIcon = this.scene.inputController.getCurrentButtonLabel(this.target);
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case 'dualshock':
|
case 'dualshock':
|
||||||
this.iconXbox.setVisible(false);
|
this.iconXbox.setVisible(false);
|
||||||
|
@ -118,6 +120,7 @@ export default class GamepadBindingUiHandler extends UiHandler {
|
||||||
this.iconDualshock.setVisible(false);
|
this.iconDualshock.setVisible(false);
|
||||||
this.iconXbox.setFrame(buttonIcon);
|
this.iconXbox.setFrame(buttonIcon);
|
||||||
this.iconXbox.setVisible(true);
|
this.iconXbox.setVisible(true);
|
||||||
|
this.previousIconXbox.setFrame(assignedButtonIcon);
|
||||||
this.previousIconXbox.setVisible(true);
|
this.previousIconXbox.setVisible(true);
|
||||||
this.alreadyAssignedText.setVisible(true);
|
this.alreadyAssignedText.setVisible(true);
|
||||||
break
|
break
|
||||||
|
@ -164,6 +167,10 @@ export default class GamepadBindingUiHandler extends UiHandler {
|
||||||
// Reverts UI to its previous state on cancel.
|
// Reverts UI to its previous state on cancel.
|
||||||
// this.scene.ui.revertMode();
|
// this.scene.ui.revertMode();
|
||||||
this.cancelFn();
|
this.cancelFn();
|
||||||
|
} else {
|
||||||
|
success = true;
|
||||||
|
this.scene.inputController.swapBinding(this.target, this.buttonPressed);
|
||||||
|
this.cancelFn(success);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -139,6 +139,12 @@ export default class SettingsGamepadUiHandler extends UiHandler {
|
||||||
this.settingsContainer.setVisible(false);
|
this.settingsContainer.setVisible(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updateBindings(): void {
|
||||||
|
// for (const elm of noOptionsCursors) {
|
||||||
|
// console.log('elm:', elm);
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
show(args: any[]): boolean {
|
show(args: any[]): boolean {
|
||||||
super.show(args);
|
super.show(args);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue