added setting selector for controller like lang + populate the setting with detected devices
parent
d4e1834e7c
commit
b35af93ecb
|
@ -39,6 +39,7 @@ export class InputsController {
|
|||
private gamepadSupport: boolean = true;
|
||||
|
||||
public customGamepadMapping = new Map();
|
||||
public chosenGamepad;
|
||||
|
||||
constructor(scene: Phaser.Scene) {
|
||||
this.scene = scene;
|
||||
|
@ -60,6 +61,10 @@ export class InputsController {
|
|||
|
||||
init(): void {
|
||||
this.events = new Phaser.Events.EventEmitter();
|
||||
|
||||
if (localStorage.hasOwnProperty('chosenGamepad')) {
|
||||
this.chosenGamepad = localStorage.getItem('chosenGamepad');
|
||||
}
|
||||
// Handle the game losing focus
|
||||
this.scene.game.events.on(Phaser.Core.Events.BLUR, () => {
|
||||
this.loseFocus()
|
||||
|
@ -69,6 +74,7 @@ export class InputsController {
|
|||
this.scene.input.gamepad.on('connected', function (thisGamepad) {
|
||||
this.refreshGamepads();
|
||||
this.setupGamepad(thisGamepad);
|
||||
this.populateSetting();
|
||||
}, this);
|
||||
|
||||
// Check to see if the gamepad has already been setup by the browser
|
||||
|
@ -101,6 +107,10 @@ export class InputsController {
|
|||
}
|
||||
}
|
||||
|
||||
setChosenGamepad(gamepad: String): void {
|
||||
this.chosenGamepad = gamepad;
|
||||
}
|
||||
|
||||
update(): void {
|
||||
// reversed to let the cancel button have a kinda priority on the action button
|
||||
for (const b of Utils.getEnumValues(Button).reverse()) {
|
||||
|
@ -122,6 +132,13 @@ export class InputsController {
|
|||
}
|
||||
}
|
||||
|
||||
populateSetting(): void {
|
||||
const gamepadsName = this.gamepads.map(g => g.id);
|
||||
localStorage.setItem('gamepadsConnected', JSON.stringify(gamepadsName));
|
||||
if (!this.chosenGamepad) this.chosenGamepad = gamepadsName[0];
|
||||
localStorage.setItem('chosenGamepad', this.chosenGamepad);
|
||||
}
|
||||
|
||||
setupGamepad(thisGamepad: Phaser.Input.Gamepad.Gamepad): void {
|
||||
let gamepadID = thisGamepad.id.toLowerCase();
|
||||
const mappedPad = this.mapGamepad(gamepadID);
|
||||
|
|
|
@ -226,6 +226,7 @@ export class GameData {
|
|||
constructor(scene: BattleScene) {
|
||||
this.scene = scene;
|
||||
this.loadSettings();
|
||||
this.loadGamepadSettings();
|
||||
this.trainerId = Utils.randInt(65536);
|
||||
this.secretId = Utils.randInt(65536);
|
||||
this.starterData = {};
|
||||
|
@ -465,48 +466,66 @@ export class GameData {
|
|||
|
||||
public saveSetting(setting: Setting, valueIndex: integer): boolean {
|
||||
let settings: object = {};
|
||||
let settingsGamepad: object = {};
|
||||
if (localStorage.hasOwnProperty('settings'))
|
||||
settings = JSON.parse(localStorage.getItem('settings'));
|
||||
if (localStorage.hasOwnProperty('settingsGamepad'))
|
||||
settingsGamepad = JSON.parse(localStorage.getItem('settingsGamepad'));
|
||||
|
||||
setSetting(this.scene, setting as Setting, valueIndex);
|
||||
setSettingGamepad(this.scene, settingsGamepad as SettingGamepad, valueIndex);
|
||||
|
||||
Object.keys(settingDefaults).forEach(s => {
|
||||
if (s === setting)
|
||||
settings[s] = valueIndex;
|
||||
});
|
||||
|
||||
|
||||
localStorage.setItem('settings', JSON.stringify(settings));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public saveGamepadSetting(setting: SettingGamepad, valueIndex: integer): boolean {
|
||||
let settingsGamepad: object = {};
|
||||
if (localStorage.hasOwnProperty('settingsGamepad'))
|
||||
settingsGamepad = JSON.parse(localStorage.getItem('settingsGamepad'));
|
||||
|
||||
let gamepadsConnected = null;
|
||||
if (localStorage.hasOwnProperty('gamepadsConnected')) {
|
||||
gamepadsConnected = JSON.parse(localStorage.getItem('gamepadsConnected'));
|
||||
}
|
||||
setSettingGamepad(this.scene, setting as SettingGamepad, valueIndex, gamepadsConnected);
|
||||
Object.keys(settingGamepadDefaults).forEach(s => {
|
||||
if (s === setting)
|
||||
settingsGamepad[s] = valueIndex;
|
||||
});
|
||||
|
||||
localStorage.setItem('settings', JSON.stringify(settings));
|
||||
localStorage.setItem('settingsGamepad', JSON.stringify(settingsGamepad));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private loadSettings(): boolean {
|
||||
Object.values(Setting).map(setting => setting as Setting).forEach(setting => setSetting(this.scene, setting, settingDefaults[setting]));
|
||||
Object.values(SettingGamepad).map(setting => setting as SettingGamepad).forEach(setting => setSettingGamepad(this.scene, setting, settingGamepadDefaults[setting]));
|
||||
|
||||
if (!localStorage.hasOwnProperty('settings'))
|
||||
return false;
|
||||
if (!localStorage.hasOwnProperty('settingsGamepad'))
|
||||
return false;
|
||||
|
||||
const settings = JSON.parse(localStorage.getItem('settings'));
|
||||
const settingsGamepad = JSON.parse(localStorage.getItem('settingsGamepad'));
|
||||
|
||||
for (let setting of Object.keys(settings))
|
||||
setSetting(this.scene, setting as Setting, settings[setting]);
|
||||
}
|
||||
|
||||
private loadGamepadSettings(): boolean {
|
||||
Object.values(SettingGamepad).map(setting => setting as SettingGamepad).forEach(setting => setSettingGamepad(this.scene, setting, settingGamepadDefaults[setting]));
|
||||
|
||||
if (!localStorage.hasOwnProperty('settingsGamepad'))
|
||||
return false;
|
||||
|
||||
let gamepadConnected = null;
|
||||
if (localStorage.hasOwnProperty('gamepadConnected')) {
|
||||
gamepadConnected = JSON.parse(localStorage.getItem('gamepadConnected'));
|
||||
}
|
||||
const settingsGamepad = JSON.parse(localStorage.getItem('settingsGamepad'));
|
||||
|
||||
for (let setting of Object.keys(settingsGamepad))
|
||||
setSettingGamepad(this.scene, setting as SettingGamepad, settingGamepadDefaults[setting]);
|
||||
setSettingGamepad(this.scene, setting as SettingGamepad, settingsGamepad[setting], gamepadConnected);
|
||||
}
|
||||
|
||||
public saveTutorialFlag(tutorial: Tutorial, flag: boolean): boolean {
|
||||
|
|
|
@ -1,79 +1,118 @@
|
|||
import BattleScene from "../battle-scene";
|
||||
import {SettingDefaults, SettingOptions} from "#app/system/settings";
|
||||
import {Button} from "../enums/buttons";
|
||||
import SettingsGamepadUiHandler from "#app/ui/settings-gamepad-ui-handler";
|
||||
import {Mode} from "#app/ui/ui";
|
||||
|
||||
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_Menu = "BUTTON_MENU",
|
||||
Button_Stats = "BUTTON_STATS",
|
||||
Button_Cycle_Shiny = "BUTTON_CYCLE_SHINY",
|
||||
Button_Cycle_Form = "BUTTON_CYCLE_FORM",
|
||||
Button_Cycle_Gender = "BUTTON_CYCLE_GENDER",
|
||||
Button_Cycle_Ability = "BUTTON_CYCLE_ABILITY",
|
||||
Button_Cycle_Nature = "BUTTON_CYCLE_NATURE",
|
||||
Button_Cycle_Variant = "BUTTON_CYCLE_VARIANT",
|
||||
Button_Speed_Up = "BUTTON_SPEED_UP",
|
||||
Button_Slow_Down = "BUTTON_SLOW_DOWN",
|
||||
// Button_Action = "BUTTON_ACTION",
|
||||
// Button_Cancel = "BUTTON_CANCEL",
|
||||
// Button_Menu = "BUTTON_MENU",
|
||||
// Button_Stats = "BUTTON_STATS",
|
||||
// Button_Cycle_Shiny = "BUTTON_CYCLE_SHINY",
|
||||
// Button_Cycle_Form = "BUTTON_CYCLE_FORM",
|
||||
// Button_Cycle_Gender = "BUTTON_CYCLE_GENDER",
|
||||
// Button_Cycle_Ability = "BUTTON_CYCLE_ABILITY",
|
||||
// Button_Cycle_Nature = "BUTTON_CYCLE_NATURE",
|
||||
// Button_Cycle_Variant = "BUTTON_CYCLE_VARIANT",
|
||||
// Button_Speed_Up = "BUTTON_SPEED_UP",
|
||||
// Button_Slow_Down = "BUTTON_SLOW_DOWN",
|
||||
}
|
||||
|
||||
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_Menu]: [`KEY ${Button.MENU.toString()}`, 'Change'],
|
||||
[SettingGamepad.Button_Stats]: [`KEY ${Button.STATS.toString()}`, 'Change'],
|
||||
[SettingGamepad.Button_Cycle_Shiny]: [`KEY ${Button.RB.toString()}`, 'Change'],
|
||||
[SettingGamepad.Button_Cycle_Form]: [`KEY ${Button.LB.toString()}`, 'Change'],
|
||||
[SettingGamepad.Button_Cycle_Gender]: [`KEY ${Button.CYCLE_GENDER.toString()}`, 'Change'],
|
||||
[SettingGamepad.Button_Cycle_Ability]: [`KEY ${Button.CYCLE_ABILITY.toString()}`, 'Change'],
|
||||
[SettingGamepad.Button_Cycle_Nature]: [`KEY ${Button.CYCLE_NATURE.toString()}`, 'Change'],
|
||||
[SettingGamepad.Button_Cycle_Variant]: [`KEY ${Button.CYCLE_VARIANT.toString()}`, 'Change'],
|
||||
[SettingGamepad.Button_Speed_Up]: [`KEY ${Button.SPEED_UP.toString()}`, 'Change'],
|
||||
[SettingGamepad.Button_Slow_Down]: [`KEY ${Button.SLOW_DOWN.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'],
|
||||
// [SettingGamepad.Button_Cycle_Form]: [`KEY ${Button.LB.toString()}`, 'Change'],
|
||||
// [SettingGamepad.Button_Cycle_Gender]: [`KEY ${Button.CYCLE_GENDER.toString()}`, 'Change'],
|
||||
// [SettingGamepad.Button_Cycle_Ability]: [`KEY ${Button.CYCLE_ABILITY.toString()}`, 'Change'],
|
||||
// [SettingGamepad.Button_Cycle_Nature]: [`KEY ${Button.CYCLE_NATURE.toString()}`, 'Change'],
|
||||
// [SettingGamepad.Button_Cycle_Variant]: [`KEY ${Button.CYCLE_VARIANT.toString()}`, 'Change'],
|
||||
// [SettingGamepad.Button_Speed_Up]: [`KEY ${Button.SPEED_UP.toString()}`, 'Change'],
|
||||
// [SettingGamepad.Button_Slow_Down]: [`KEY ${Button.SLOW_DOWN.toString()}`, 'Change']
|
||||
};
|
||||
|
||||
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_Menu]: Button.MENU,
|
||||
[SettingGamepad.Button_Stats]: Button.STATS,
|
||||
[SettingGamepad.Button_Cycle_Shiny]: Button.RB,
|
||||
[SettingGamepad.Button_Cycle_Form]: Button.LB,
|
||||
[SettingGamepad.Button_Cycle_Gender]: Button.CYCLE_GENDER,
|
||||
[SettingGamepad.Button_Cycle_Ability]: Button.CYCLE_ABILITY,
|
||||
[SettingGamepad.Button_Cycle_Nature]: Button.CYCLE_NATURE,
|
||||
[SettingGamepad.Button_Cycle_Variant]: Button.CYCLE_VARIANT,
|
||||
[SettingGamepad.Button_Speed_Up]: Button.SPEED_UP,
|
||||
[SettingGamepad.Button_Slow_Down]: Button.SLOW_DOWN,
|
||||
// [SettingGamepad.Button_Action]: Button.ACTION,
|
||||
// [SettingGamepad.Button_Cancel]: Button.CANCEL,
|
||||
// [SettingGamepad.Button_Menu]: Button.MENU,
|
||||
// [SettingGamepad.Button_Stats]: Button.STATS,
|
||||
// [SettingGamepad.Button_Cycle_Shiny]: Button.RB,
|
||||
// [SettingGamepad.Button_Cycle_Form]: Button.LB,
|
||||
// [SettingGamepad.Button_Cycle_Gender]: Button.CYCLE_GENDER,
|
||||
// [SettingGamepad.Button_Cycle_Ability]: Button.CYCLE_ABILITY,
|
||||
// [SettingGamepad.Button_Cycle_Nature]: Button.CYCLE_NATURE,
|
||||
// [SettingGamepad.Button_Cycle_Variant]: Button.CYCLE_VARIANT,
|
||||
// [SettingGamepad.Button_Speed_Up]: Button.SPEED_UP,
|
||||
// [SettingGamepad.Button_Slow_Down]: Button.SLOW_DOWN,
|
||||
};
|
||||
|
||||
export function setSettingGamepad(scene: BattleScene, setting: SettingGamepad, value: integer): boolean {
|
||||
function truncateString(str: String, maxLength: number = 10) {
|
||||
if (str.length > maxLength) {
|
||||
return str.slice(0, maxLength - 3) + "..."; // Subtract 3 to accommodate the ellipsis
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
export function setSettingGamepad(scene: BattleScene, setting: SettingGamepad, value: integer, gamepads?: Array<String>): boolean {
|
||||
switch (setting) {
|
||||
case SettingGamepad.Gamepad_Support:
|
||||
scene.gamepadSupport = settingGamepadOptions[setting][value] !== 'Disabled';
|
||||
console.log('setting:', setting, settingGamepadOptions[setting][value]);
|
||||
scene.inputController.setGamepadSupport(settingGamepadOptions[setting][value] !== 'Disabled');
|
||||
break;
|
||||
case SettingGamepad.Swap_A_and_B:
|
||||
console.log('settingGamepadOptions[setting][value]:', settingGamepadOptions[setting][value]);
|
||||
console.log('settingGamepadOptions[setting]:', settingGamepadOptions[setting]);
|
||||
console.log('value:', value);
|
||||
scene.abSwapped = settingGamepadOptions[setting][value] !== 'Disabled';
|
||||
break;
|
||||
case SettingGamepad.Button_Action:
|
||||
case SettingGamepad.Button_Cancel:
|
||||
case SettingGamepad.Button_Menu:
|
||||
case SettingGamepad.Button_Stats:
|
||||
case SettingGamepad.Button_Cycle_Shiny:
|
||||
case SettingGamepad.Button_Cycle_Form:
|
||||
case SettingGamepad.Button_Cycle_Gender:
|
||||
case SettingGamepad.Button_Cycle_Ability:
|
||||
case SettingGamepad.Button_Cycle_Nature:
|
||||
case SettingGamepad.Button_Cycle_Variant:
|
||||
case SettingGamepad.Button_Speed_Up:
|
||||
case SettingGamepad.Button_Slow_Down:
|
||||
scene.inputController.customGamepadMapping[setting] = value;
|
||||
// case SettingGamepad.Button_Action:
|
||||
// case SettingGamepad.Button_Cancel:
|
||||
// case SettingGamepad.Button_Menu:
|
||||
// case SettingGamepad.Button_Stats:
|
||||
// case SettingGamepad.Button_Cycle_Shiny:
|
||||
// case SettingGamepad.Button_Cycle_Form:
|
||||
// case SettingGamepad.Button_Cycle_Gender:
|
||||
// case SettingGamepad.Button_Cycle_Ability:
|
||||
// case SettingGamepad.Button_Cycle_Nature:
|
||||
// case SettingGamepad.Button_Cycle_Variant:
|
||||
// case SettingGamepad.Button_Speed_Up:
|
||||
// case SettingGamepad.Button_Slow_Down:
|
||||
// scene.inputController.customGamepadMapping[setting] = value;
|
||||
// break;
|
||||
case SettingGamepad.Default_Controller:
|
||||
if (value) {
|
||||
if (scene.ui && gamepads) {
|
||||
const cancelHandler = () => {
|
||||
scene.ui.revertMode();
|
||||
(scene.ui.getHandler() as SettingsGamepadUiHandler).setOptionCursor(Object.values(SettingGamepad).indexOf(SettingGamepad.Default_Controller), 0, true);
|
||||
return false;
|
||||
};
|
||||
const changeGamepadHandler = (gamepad: string) => {
|
||||
scene.inputController.setChosenGamepad(gamepad);
|
||||
localStorage.setItem('chosenGamepad', gamepad);
|
||||
cancelHandler();
|
||||
return true;
|
||||
};
|
||||
scene.ui.setOverlayMode(Mode.OPTION_SELECT, {
|
||||
options: [...gamepads.map((g) => ({label: truncateString(g, 30), handler: () => changeGamepadHandler(g)})), {
|
||||
label: 'Cancel',
|
||||
handler: cancelHandler,
|
||||
}]
|
||||
});
|
||||
return false;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
|
@ -142,12 +142,6 @@ export function setSetting(scene: BattleScene, setting: Setting, value: integer)
|
|||
} else
|
||||
return false;
|
||||
break;
|
||||
case Setting.Gamepad_Support:
|
||||
scene.inputController.setGamepadSupport(settingOptions[setting][value] !== 'Disabled');
|
||||
break;
|
||||
case Setting.Swap_A_and_B:
|
||||
scene.abSwapped = settingOptions[setting][value] !== 'Disabled';
|
||||
break;
|
||||
case Setting.Touch_Controls:
|
||||
scene.enableTouchControls = settingOptions[setting][value] !== 'Disabled' && hasTouchscreen();
|
||||
const touchControls = document.getElementById('touchControls');
|
||||
|
|
|
@ -5,7 +5,11 @@ import { Mode } from "./ui";
|
|||
import UiHandler from "./ui-handler";
|
||||
import { addWindow } from "./ui-theme";
|
||||
import {Button} from "../enums/buttons";
|
||||
import {SettingGamepad, settingGamepadDefaults, settingGamepadOptions} from "../system/settings-gamepad";
|
||||
import {
|
||||
SettingGamepad,
|
||||
settingGamepadDefaults,
|
||||
settingGamepadOptions
|
||||
} from "../system/settings-gamepad";
|
||||
|
||||
export default class SettingsGamepadUiHandler extends UiHandler {
|
||||
private settingsContainer: Phaser.GameObjects.Container;
|
||||
|
@ -24,12 +28,14 @@ export default class SettingsGamepadUiHandler extends UiHandler {
|
|||
|
||||
private reloadRequired: boolean;
|
||||
private reloadI18n: boolean;
|
||||
private gamepads: Array<String>;
|
||||
|
||||
constructor(scene: BattleScene, mode?: Mode) {
|
||||
super(scene, mode);
|
||||
|
||||
this.reloadRequired = false;
|
||||
this.reloadI18n = false;
|
||||
this.gamepads = null;
|
||||
}
|
||||
|
||||
setup() {
|
||||
|
@ -109,8 +115,8 @@ export default class SettingsGamepadUiHandler extends UiHandler {
|
|||
show(args: any[]): boolean {
|
||||
super.show(args);
|
||||
|
||||
const settings: object = localStorage.hasOwnProperty('settings') ? JSON.parse(localStorage.getItem('settings')) : {};
|
||||
|
||||
const settings: object = localStorage.hasOwnProperty('settingsGamepad') ? JSON.parse(localStorage.getItem('settingsGamepad')) : {};
|
||||
console.log('from here');
|
||||
Object.keys(settingGamepadDefaults).forEach((setting, s) => this.setOptionCursor(s, settings.hasOwnProperty(setting) ? settings[setting] : settingGamepadDefaults[setting]));
|
||||
|
||||
this.settingsContainer.setVisible(true);
|
||||
|
@ -194,17 +200,17 @@ 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));
|
||||
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));
|
||||
newValueLabel.setColor(this.getTextColor(TextStyle.SETTINGS_SELECTED));
|
||||
newValueLabel.setShadowColor(this.getTextColor(TextStyle.SETTINGS_SELECTED, true));
|
||||
|
||||
if (save) {
|
||||
this.scene.gameData.saveSetting(setting, cursor)
|
||||
if (SettingGamepad[setting] !== SettingGamepad.Default_Controller)
|
||||
this.scene.gameData.saveGamepadSetting(setting, cursor)
|
||||
}
|
||||
|
||||
return true;
|
||||
|
|
Loading…
Reference in New Issue