added setting selector for controller like lang + populate the setting with detected devices

pull/685/head
Greenlamp 2024-05-09 03:33:25 +02:00
parent d4e1834e7c
commit b35af93ecb
5 changed files with 374 additions and 299 deletions

View File

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

View File

@ -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 {

View File

@ -1,81 +1,120 @@
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 {
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",
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",
}
export const settingGamepadOptions: SettingOptions = {
[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.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']
};
export const settingGamepadDefaults: SettingDefaults = {
[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.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,
};
export function setSettingGamepad(scene: BattleScene, setting: SettingGamepad, value: integer): boolean {
switch (setting) {
case SettingGamepad.Gamepad_Support:
scene.gamepadSupport = settingGamepadOptions[setting][value] !== 'Disabled';
break;
case SettingGamepad.Swap_A_and_B:
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;
break;
}
return true;
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:
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;
// 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;
}
return true;
}

View File

@ -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');

View File

@ -5,248 +5,254 @@ 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;
private optionsContainer: Phaser.GameObjects.Container;
private settingsContainer: Phaser.GameObjects.Container;
private optionsContainer: Phaser.GameObjects.Container;
private scrollCursor: integer;
private scrollCursor: integer;
private optionsBg: Phaser.GameObjects.NineSlice;
private optionsBg: Phaser.GameObjects.NineSlice;
private optionCursors: integer[];
private optionCursors: integer[];
private settingLabels: Phaser.GameObjects.Text[];
private optionValueLabels: Phaser.GameObjects.Text[][];
private settingLabels: Phaser.GameObjects.Text[];
private optionValueLabels: Phaser.GameObjects.Text[][];
private cursorObj: Phaser.GameObjects.NineSlice;
private cursorObj: Phaser.GameObjects.NineSlice;
private reloadRequired: boolean;
private reloadI18n: boolean;
private reloadRequired: boolean;
private reloadI18n: boolean;
private gamepads: Array<String>;
constructor(scene: BattleScene, mode?: Mode) {
super(scene, mode);
constructor(scene: BattleScene, mode?: Mode) {
super(scene, mode);
this.reloadRequired = false;
this.reloadI18n = false;
}
setup() {
const ui = this.getUi();
this.settingsContainer = this.scene.add.container(1, -(this.scene.game.canvas.height / 6) + 1);
this.settingsContainer.setInteractive(new Phaser.Geom.Rectangle(0, 0, this.scene.game.canvas.width / 6, this.scene.game.canvas.height / 6), Phaser.Geom.Rectangle.Contains);
const headerBg = addWindow(this.scene, 0, 0, (this.scene.game.canvas.width / 6) - 2, 24);
headerBg.setOrigin(0, 0);
const headerText = addTextObject(this.scene, 0, 0, 'General', TextStyle.SETTINGS_LABEL);
headerText.setOrigin(0, 0);
headerText.setPositionRelative(headerBg, 8, 4);
const gamepadText = addTextObject(this.scene, 0, 0, 'Gamepad', TextStyle.SETTINGS_SELECTED);
gamepadText.setOrigin(0, 0);
gamepadText.setPositionRelative(headerBg, 50, 4);
this.optionsBg = addWindow(this.scene, 0, headerBg.height, (this.scene.game.canvas.width / 6) - 2, (this.scene.game.canvas.height / 6) - headerBg.height - 2);
this.optionsBg.setOrigin(0, 0);
this.optionsContainer = this.scene.add.container(0, 0);
this.settingLabels = [];
this.optionValueLabels = [];
Object.keys(SettingGamepad).forEach((setting, s) => {
let settingName = setting.replace(/\_/g, ' ');
this.settingLabels[s] = addTextObject(this.scene, 8, 28 + s * 16, settingName, TextStyle.SETTINGS_LABEL);
this.settingLabels[s].setOrigin(0, 0);
this.optionsContainer.add(this.settingLabels[s]);
this.optionValueLabels.push(settingGamepadOptions[SettingGamepad[setting]].map((option, o) => {
const valueLabel = addTextObject(this.scene, 0, 0, option, settingGamepadDefaults[SettingGamepad[setting]] === o ? TextStyle.SETTINGS_SELECTED : TextStyle.WINDOW);
valueLabel.setOrigin(0, 0);
this.optionsContainer.add(valueLabel);
return valueLabel;
}));
const totalWidth = this.optionValueLabels[s].map(o => o.width).reduce((total, width) => total += width, 0);
const labelWidth = Math.max(78, this.settingLabels[s].displayWidth + 8);
const totalSpace = (300 - labelWidth) - totalWidth / 6;
const optionSpacing = Math.floor(totalSpace / (this.optionValueLabels[s].length - 1));
let xOffset = 0;
for (let value of this.optionValueLabels[s]) {
value.setPositionRelative(this.settingLabels[s], labelWidth + xOffset, 0);
xOffset += value.width / 6 + optionSpacing;
}
});
this.optionCursors = Object.values(settingGamepadDefaults);
this.settingsContainer.add(headerBg);
this.settingsContainer.add(headerText);
this.settingsContainer.add(gamepadText);
this.settingsContainer.add(this.optionsBg);
this.settingsContainer.add(this.optionsContainer);
ui.add(this.settingsContainer);
this.setCursor(0);
this.setScrollCursor(0);
this.settingsContainer.setVisible(false);
}
show(args: any[]): boolean {
super.show(args);
const settings: object = localStorage.hasOwnProperty('settings') ? JSON.parse(localStorage.getItem('settings')) : {};
Object.keys(settingGamepadDefaults).forEach((setting, s) => this.setOptionCursor(s, settings.hasOwnProperty(setting) ? settings[setting] : settingGamepadDefaults[setting]));
this.settingsContainer.setVisible(true);
this.setCursor(0);
this.getUi().moveTo(this.settingsContainer, this.getUi().length - 1);
this.getUi().hideTooltip();
return true;
}
processInput(button: Button): boolean {
const ui = this.getUi();
let success = false;
if (button === Button.CANCEL) {
success = true;
this.scene.ui.revertMode();
} else {
const cursor = this.cursor + this.scrollCursor;
switch (button) {
case Button.UP:
if (cursor) {
if (this.cursor)
success = this.setCursor(this.cursor - 1);
else
success = this.setScrollCursor(this.scrollCursor - 1);
}
break;
case Button.DOWN:
if (cursor < this.optionValueLabels.length) {
if (this.cursor < 8)
success = this.setCursor(this.cursor + 1);
else if (this.scrollCursor < this.optionValueLabels.length - 9)
success = this.setScrollCursor(this.scrollCursor + 1);
}
break;
case Button.LEFT:
if (this.optionCursors[cursor])
success = this.setOptionCursor(cursor, this.optionCursors[cursor] - 1, true);
break;
case Button.RIGHT:
if (this.optionCursors[cursor] < this.optionValueLabels[cursor].length - 1)
success = this.setOptionCursor(cursor, this.optionCursors[cursor] + 1, true);
break;
case Button.LB:
this.scene.ui.setMode(Mode.SETTINGS)
success = true;
case Button.RB:
this.scene.ui.setMode(Mode.SETTINGS)
success = true;
break;
}
this.reloadRequired = false;
this.reloadI18n = false;
this.gamepads = null;
}
if (success)
ui.playSelect();
setup() {
const ui = this.getUi();
return success;
}
this.settingsContainer = this.scene.add.container(1, -(this.scene.game.canvas.height / 6) + 1);
setCursor(cursor: integer): boolean {
const ret = super.setCursor(cursor);
this.settingsContainer.setInteractive(new Phaser.Geom.Rectangle(0, 0, this.scene.game.canvas.width / 6, this.scene.game.canvas.height / 6), Phaser.Geom.Rectangle.Contains);
if (!this.cursorObj) {
this.cursorObj = this.scene.add.nineslice(0, 0, 'summary_moves_cursor', null, (this.scene.game.canvas.width / 6) - 10, 16, 1, 1, 1, 1);
this.cursorObj.setOrigin(0, 0);
this.optionsContainer.add(this.cursorObj);
const headerBg = addWindow(this.scene, 0, 0, (this.scene.game.canvas.width / 6) - 2, 24);
headerBg.setOrigin(0, 0);
const headerText = addTextObject(this.scene, 0, 0, 'General', TextStyle.SETTINGS_LABEL);
headerText.setOrigin(0, 0);
headerText.setPositionRelative(headerBg, 8, 4);
const gamepadText = addTextObject(this.scene, 0, 0, 'Gamepad', TextStyle.SETTINGS_SELECTED);
gamepadText.setOrigin(0, 0);
gamepadText.setPositionRelative(headerBg, 50, 4);
this.optionsBg = addWindow(this.scene, 0, headerBg.height, (this.scene.game.canvas.width / 6) - 2, (this.scene.game.canvas.height / 6) - headerBg.height - 2);
this.optionsBg.setOrigin(0, 0);
this.optionsContainer = this.scene.add.container(0, 0);
this.settingLabels = [];
this.optionValueLabels = [];
Object.keys(SettingGamepad).forEach((setting, s) => {
let settingName = setting.replace(/\_/g, ' ');
this.settingLabels[s] = addTextObject(this.scene, 8, 28 + s * 16, settingName, TextStyle.SETTINGS_LABEL);
this.settingLabels[s].setOrigin(0, 0);
this.optionsContainer.add(this.settingLabels[s]);
this.optionValueLabels.push(settingGamepadOptions[SettingGamepad[setting]].map((option, o) => {
const valueLabel = addTextObject(this.scene, 0, 0, option, settingGamepadDefaults[SettingGamepad[setting]] === o ? TextStyle.SETTINGS_SELECTED : TextStyle.WINDOW);
valueLabel.setOrigin(0, 0);
this.optionsContainer.add(valueLabel);
return valueLabel;
}));
const totalWidth = this.optionValueLabels[s].map(o => o.width).reduce((total, width) => total += width, 0);
const labelWidth = Math.max(78, this.settingLabels[s].displayWidth + 8);
const totalSpace = (300 - labelWidth) - totalWidth / 6;
const optionSpacing = Math.floor(totalSpace / (this.optionValueLabels[s].length - 1));
let xOffset = 0;
for (let value of this.optionValueLabels[s]) {
value.setPositionRelative(this.settingLabels[s], labelWidth + xOffset, 0);
xOffset += value.width / 6 + optionSpacing;
}
});
this.optionCursors = Object.values(settingGamepadDefaults);
this.settingsContainer.add(headerBg);
this.settingsContainer.add(headerText);
this.settingsContainer.add(gamepadText);
this.settingsContainer.add(this.optionsBg);
this.settingsContainer.add(this.optionsContainer);
ui.add(this.settingsContainer);
this.setCursor(0);
this.setScrollCursor(0);
this.settingsContainer.setVisible(false);
}
this.cursorObj.setPositionRelative(this.optionsBg, 4, 4 + (this.cursor + this.scrollCursor) * 16);
show(args: any[]): boolean {
super.show(args);
return ret;
}
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]));
setOptionCursor(settingIndex: integer, cursor: integer, save?: boolean): boolean {
const setting = SettingGamepad[Object.keys(SettingGamepad)[settingIndex]];
this.settingsContainer.setVisible(true);
this.setCursor(0);
const lastCursor = this.optionCursors[settingIndex];
this.getUi().moveTo(this.settingsContainer, this.getUi().length - 1);
const lastValueLabel = this.optionValueLabels[settingIndex][lastCursor];
lastValueLabel?.setColor(this.getTextColor(TextStyle.WINDOW));
lastValueLabel?.setShadowColor(this.getTextColor(TextStyle.WINDOW, true));
this.getUi().hideTooltip();
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) {
this.scene.gameData.saveSetting(setting, cursor)
return true;
}
return true;
}
processInput(button: Button): boolean {
const ui = this.getUi();
setScrollCursor(scrollCursor: integer): boolean {
if (scrollCursor === this.scrollCursor)
return false;
let success = false;
this.scrollCursor = scrollCursor;
if (button === Button.CANCEL) {
success = true;
this.scene.ui.revertMode();
} else {
const cursor = this.cursor + this.scrollCursor;
switch (button) {
case Button.UP:
if (cursor) {
if (this.cursor)
success = this.setCursor(this.cursor - 1);
else
success = this.setScrollCursor(this.scrollCursor - 1);
}
break;
case Button.DOWN:
if (cursor < this.optionValueLabels.length) {
if (this.cursor < 8)
success = this.setCursor(this.cursor + 1);
else if (this.scrollCursor < this.optionValueLabels.length - 9)
success = this.setScrollCursor(this.scrollCursor + 1);
}
break;
case Button.LEFT:
if (this.optionCursors[cursor])
success = this.setOptionCursor(cursor, this.optionCursors[cursor] - 1, true);
break;
case Button.RIGHT:
if (this.optionCursors[cursor] < this.optionValueLabels[cursor].length - 1)
success = this.setOptionCursor(cursor, this.optionCursors[cursor] + 1, true);
break;
case Button.LB:
this.scene.ui.setMode(Mode.SETTINGS)
success = true;
case Button.RB:
this.scene.ui.setMode(Mode.SETTINGS)
success = true;
break;
}
}
this.updateSettingsScroll();
if (success)
ui.playSelect();
this.setCursor(this.cursor);
return true;
}
updateSettingsScroll(): void {
this.optionsContainer.setY(-16 * this.scrollCursor);
for (let s = 0; s < this.settingLabels.length; s++) {
const visible = s >= this.scrollCursor && s < this.scrollCursor + 9;
this.settingLabels[s].setVisible(visible);
for (let option of this.optionValueLabels[s])
option.setVisible(visible);
return success;
}
}
clear() {
super.clear();
this.settingsContainer.setVisible(false);
this.eraseCursor();
if (this.reloadRequired) {
this.reloadRequired = false;
this.scene.reset(true, false, true);
}
}
setCursor(cursor: integer): boolean {
const ret = super.setCursor(cursor);
eraseCursor() {
if (this.cursorObj)
this.cursorObj.destroy();
this.cursorObj = null;
}
if (!this.cursorObj) {
this.cursorObj = this.scene.add.nineslice(0, 0, 'summary_moves_cursor', null, (this.scene.game.canvas.width / 6) - 10, 16, 1, 1, 1, 1);
this.cursorObj.setOrigin(0, 0);
this.optionsContainer.add(this.cursorObj);
}
this.cursorObj.setPositionRelative(this.optionsBg, 4, 4 + (this.cursor + this.scrollCursor) * 16);
return ret;
}
setOptionCursor(settingIndex: integer, cursor: integer, save?: boolean): boolean {
const setting = SettingGamepad[Object.keys(SettingGamepad)[settingIndex]];
const lastCursor = this.optionCursors[settingIndex];
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));
if (save) {
if (SettingGamepad[setting] !== SettingGamepad.Default_Controller)
this.scene.gameData.saveGamepadSetting(setting, cursor)
}
return true;
}
setScrollCursor(scrollCursor: integer): boolean {
if (scrollCursor === this.scrollCursor)
return false;
this.scrollCursor = scrollCursor;
this.updateSettingsScroll();
this.setCursor(this.cursor);
return true;
}
updateSettingsScroll(): void {
this.optionsContainer.setY(-16 * this.scrollCursor);
for (let s = 0; s < this.settingLabels.length; s++) {
const visible = s >= this.scrollCursor && s < this.scrollCursor + 9;
this.settingLabels[s].setVisible(visible);
for (let option of this.optionValueLabels[s])
option.setVisible(visible);
}
}
clear() {
super.clear();
this.settingsContainer.setVisible(false);
this.eraseCursor();
if (this.reloadRequired) {
this.reloadRequired = false;
this.scene.reset(true, false, true);
}
}
eraseCursor() {
if (this.cursorObj)
this.cursorObj.destroy();
this.cursorObj = null;
}
}