2020-05-09 14:45:23 +00:00
|
|
|
/* typehints:start */
|
|
|
|
import { Application } from "../application";
|
|
|
|
/* typehints:end */
|
|
|
|
|
|
|
|
import { ReadWriteProxy } from "../core/read_write_proxy";
|
|
|
|
import { BoolSetting, EnumSetting, BaseSetting } from "./setting_types";
|
|
|
|
import { createLogger } from "../core/logging";
|
|
|
|
import { ExplainedResult } from "../core/explained_result";
|
2020-05-17 11:24:47 +00:00
|
|
|
import { THEMES, THEME, applyGameTheme } from "../game/theme";
|
2020-05-19 13:03:13 +00:00
|
|
|
import { IS_DEMO } from "../core/config";
|
2020-06-01 11:05:15 +00:00
|
|
|
import { T } from "../translations";
|
2020-06-10 10:13:38 +00:00
|
|
|
import { LANGUAGES } from "../languages";
|
2020-05-09 14:45:23 +00:00
|
|
|
|
|
|
|
const logger = createLogger("application_settings");
|
|
|
|
|
|
|
|
const categoryGame = "game";
|
|
|
|
const categoryApp = "app";
|
|
|
|
|
|
|
|
export const uiScales = [
|
|
|
|
{
|
|
|
|
id: "super_small",
|
|
|
|
size: 0.6,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "small",
|
|
|
|
size: 0.8,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "regular",
|
|
|
|
size: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "large",
|
2020-06-10 09:49:33 +00:00
|
|
|
size: 1.05,
|
2020-05-09 14:45:23 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "huge",
|
2020-06-10 09:49:33 +00:00
|
|
|
size: 1.1,
|
2020-06-01 11:05:15 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
export const scrollWheelSensitivities = [
|
|
|
|
{
|
|
|
|
id: "super_slow",
|
|
|
|
scale: 0.25,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "slow",
|
|
|
|
scale: 0.5,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "regular",
|
|
|
|
scale: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "fast",
|
|
|
|
scale: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "super_fast",
|
|
|
|
scale: 4,
|
2020-05-09 14:45:23 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2020-06-11 22:47:59 +00:00
|
|
|
export const movementSpeeds = [
|
|
|
|
{
|
|
|
|
id: "super_slow",
|
|
|
|
multiplier: 0.25,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "slow",
|
|
|
|
multiplier: 0.5,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "regular",
|
|
|
|
multiplier: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "fast",
|
|
|
|
multiplier: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "super_fast",
|
|
|
|
multiplier: 4,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "extremely_fast",
|
|
|
|
multiplier: 8,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2020-05-09 14:45:23 +00:00
|
|
|
/** @type {Array<BaseSetting>} */
|
|
|
|
export const allApplicationSettings = [
|
2020-06-10 10:13:38 +00:00
|
|
|
new EnumSetting("language", {
|
|
|
|
options: Object.keys(LANGUAGES),
|
|
|
|
valueGetter: key => key,
|
|
|
|
textGetter: key => LANGUAGES[key].name,
|
|
|
|
category: categoryApp,
|
|
|
|
restartRequired: true,
|
|
|
|
changeCb: (app, id) => null,
|
|
|
|
magicValue: "auto-detect",
|
|
|
|
}),
|
|
|
|
|
2020-05-09 14:45:23 +00:00
|
|
|
new EnumSetting("uiScale", {
|
|
|
|
options: uiScales.sort((a, b) => a.size - b.size),
|
|
|
|
valueGetter: scale => scale.id,
|
2020-06-01 11:05:15 +00:00
|
|
|
textGetter: scale => T.settings.labels.uiScale.scales[scale.id],
|
2020-05-09 14:45:23 +00:00
|
|
|
category: categoryApp,
|
|
|
|
restartRequired: false,
|
|
|
|
changeCb:
|
|
|
|
/**
|
|
|
|
* @param {Application} app
|
|
|
|
*/
|
|
|
|
(app, id) => app.updateAfterUiScaleChanged(),
|
|
|
|
}),
|
2020-06-01 11:05:15 +00:00
|
|
|
|
2020-05-09 14:45:23 +00:00
|
|
|
new BoolSetting(
|
|
|
|
"fullscreen",
|
|
|
|
categoryApp,
|
|
|
|
/**
|
|
|
|
* @param {Application} app
|
|
|
|
*/
|
|
|
|
(app, value) => {
|
|
|
|
if (app.platformWrapper.getSupportsFullscreen()) {
|
|
|
|
app.platformWrapper.setFullscreen(value);
|
|
|
|
}
|
|
|
|
},
|
2020-05-19 13:03:13 +00:00
|
|
|
!IS_DEMO
|
2020-05-09 14:45:23 +00:00
|
|
|
),
|
2020-05-17 11:24:47 +00:00
|
|
|
|
2020-05-14 17:29:21 +00:00
|
|
|
new BoolSetting(
|
|
|
|
"soundsMuted",
|
|
|
|
categoryApp,
|
|
|
|
/**
|
|
|
|
* @param {Application} app
|
|
|
|
*/
|
2020-05-17 11:24:47 +00:00
|
|
|
(app, value) => app.sound.setSoundsMuted(value)
|
2020-05-14 17:29:21 +00:00
|
|
|
),
|
|
|
|
new BoolSetting(
|
|
|
|
"musicMuted",
|
|
|
|
categoryApp,
|
|
|
|
/**
|
|
|
|
* @param {Application} app
|
|
|
|
*/
|
2020-05-17 11:24:47 +00:00
|
|
|
(app, value) => app.sound.setMusicMuted(value)
|
2020-05-14 17:29:21 +00:00
|
|
|
),
|
2020-05-09 14:45:23 +00:00
|
|
|
|
|
|
|
// GAME
|
2020-06-12 17:12:35 +00:00
|
|
|
|
2020-05-17 11:24:47 +00:00
|
|
|
new EnumSetting("theme", {
|
|
|
|
options: Object.keys(THEMES),
|
|
|
|
valueGetter: theme => theme,
|
2020-06-11 15:18:10 +00:00
|
|
|
textGetter: theme => T.settings.labels.theme.themes[theme],
|
2020-05-17 11:24:47 +00:00
|
|
|
category: categoryGame,
|
|
|
|
restartRequired: false,
|
|
|
|
changeCb:
|
|
|
|
/**
|
|
|
|
* @param {Application} app
|
|
|
|
*/
|
|
|
|
(app, id) => {
|
|
|
|
applyGameTheme(id);
|
|
|
|
document.body.setAttribute("data-theme", id);
|
|
|
|
},
|
2020-05-19 13:03:13 +00:00
|
|
|
enabled: !IS_DEMO,
|
2020-05-17 11:24:47 +00:00
|
|
|
}),
|
2020-05-18 15:40:20 +00:00
|
|
|
|
|
|
|
new EnumSetting("refreshRate", {
|
2020-06-13 09:13:28 +00:00
|
|
|
options: ["60", "100", "144", "165", "250", "500"],
|
2020-05-18 15:40:20 +00:00
|
|
|
valueGetter: rate => rate,
|
|
|
|
textGetter: rate => rate + " Hz",
|
|
|
|
category: categoryGame,
|
|
|
|
restartRequired: false,
|
2020-05-23 08:57:02 +00:00
|
|
|
changeCb: (app, id) => {},
|
2020-05-19 13:03:13 +00:00
|
|
|
enabled: !IS_DEMO,
|
2020-05-18 15:40:20 +00:00
|
|
|
}),
|
2020-05-23 08:57:02 +00:00
|
|
|
|
2020-06-12 17:12:35 +00:00
|
|
|
new EnumSetting("scrollWheelSensitivity", {
|
|
|
|
options: scrollWheelSensitivities.sort((a, b) => a.scale - b.scale),
|
|
|
|
valueGetter: scale => scale.id,
|
|
|
|
textGetter: scale => T.settings.labels.scrollWheelSensitivity.sensitivity[scale.id],
|
|
|
|
category: categoryGame,
|
|
|
|
restartRequired: false,
|
|
|
|
changeCb:
|
|
|
|
/**
|
|
|
|
* @param {Application} app
|
|
|
|
*/
|
|
|
|
(app, id) => app.updateAfterUiScaleChanged(),
|
|
|
|
}),
|
|
|
|
|
|
|
|
new EnumSetting("movementSpeed", {
|
|
|
|
options: movementSpeeds.sort((a, b) => a.multiplier - b.multiplier),
|
|
|
|
valueGetter: multiplier => multiplier.id,
|
|
|
|
textGetter: multiplier => T.settings.labels.movementSpeed.speeds[multiplier.id],
|
|
|
|
category: categoryGame,
|
|
|
|
restartRequired: false,
|
|
|
|
changeCb: (app, id) => {},
|
|
|
|
}),
|
|
|
|
|
2020-05-23 08:57:02 +00:00
|
|
|
new BoolSetting("alwaysMultiplace", categoryGame, (app, value) => {}),
|
2020-05-23 13:04:55 +00:00
|
|
|
new BoolSetting("offerHints", categoryGame, (app, value) => {}),
|
2020-05-09 14:45:23 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
export function getApplicationSettingById(id) {
|
|
|
|
return allApplicationSettings.find(setting => setting.id === id);
|
|
|
|
}
|
|
|
|
|
|
|
|
class SettingsStorage {
|
|
|
|
constructor() {
|
|
|
|
this.uiScale = "regular";
|
|
|
|
this.fullscreen = G_IS_STANDALONE;
|
2020-05-14 17:29:21 +00:00
|
|
|
|
|
|
|
this.soundsMuted = false;
|
|
|
|
this.musicMuted = false;
|
2020-05-16 07:49:00 +00:00
|
|
|
this.theme = "light";
|
2020-05-18 15:40:20 +00:00
|
|
|
this.refreshRate = "60";
|
2020-06-01 11:05:15 +00:00
|
|
|
this.scrollWheelSensitivity = "regular";
|
2020-06-11 22:47:59 +00:00
|
|
|
this.movementSpeed = "regular";
|
2020-06-10 10:13:38 +00:00
|
|
|
this.language = "auto-detect";
|
2020-05-19 07:14:40 +00:00
|
|
|
|
2020-05-23 08:57:02 +00:00
|
|
|
this.alwaysMultiplace = false;
|
2020-05-23 13:04:55 +00:00
|
|
|
this.offerHints = true;
|
2020-05-23 08:57:02 +00:00
|
|
|
|
2020-05-19 07:14:40 +00:00
|
|
|
/**
|
|
|
|
* @type {Object.<string, number>}
|
|
|
|
*/
|
|
|
|
this.keybindingOverrides = {};
|
2020-05-09 14:45:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ApplicationSettings extends ReadWriteProxy {
|
|
|
|
constructor(app) {
|
|
|
|
super(app, "app_settings.bin");
|
|
|
|
}
|
|
|
|
|
|
|
|
initialize() {
|
|
|
|
// Read and directly write latest data back
|
2020-05-16 07:49:00 +00:00
|
|
|
return this.readAsync()
|
|
|
|
.then(() => {
|
|
|
|
// Apply default setting callbacks
|
|
|
|
const settings = this.getAllSettings();
|
|
|
|
for (let i = 0; i < allApplicationSettings.length; ++i) {
|
|
|
|
const handle = allApplicationSettings[i];
|
|
|
|
handle.apply(this.app, settings[handle.id]);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
.then(() => this.writeAsync());
|
2020-05-09 14:45:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
save() {
|
|
|
|
return this.writeAsync();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Getters
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {SettingsStorage}
|
|
|
|
*/
|
|
|
|
getAllSettings() {
|
|
|
|
return this.getCurrentData().settings;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} key
|
|
|
|
*/
|
|
|
|
getSetting(key) {
|
|
|
|
assert(this.getAllSettings().hasOwnProperty(key), "Setting not known: " + key);
|
|
|
|
return this.getAllSettings()[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
getInterfaceScaleId() {
|
|
|
|
if (!this.currentData) {
|
|
|
|
// Not initialized yet
|
|
|
|
return "regular";
|
|
|
|
}
|
|
|
|
return this.getAllSettings().uiScale;
|
|
|
|
}
|
|
|
|
|
2020-05-18 15:40:20 +00:00
|
|
|
getDesiredFps() {
|
|
|
|
return parseInt(this.getAllSettings().refreshRate);
|
|
|
|
}
|
|
|
|
|
2020-05-09 14:45:23 +00:00
|
|
|
getInterfaceScaleValue() {
|
|
|
|
const id = this.getInterfaceScaleId();
|
|
|
|
for (let i = 0; i < uiScales.length; ++i) {
|
|
|
|
if (uiScales[i].id === id) {
|
|
|
|
return uiScales[i].size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logger.error("Unknown ui scale id:", id);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-06-01 11:05:15 +00:00
|
|
|
getScrollWheelSensitivity() {
|
|
|
|
const id = this.getAllSettings().scrollWheelSensitivity;
|
|
|
|
for (let i = 0; i < scrollWheelSensitivities.length; ++i) {
|
|
|
|
if (scrollWheelSensitivities[i].id === id) {
|
|
|
|
return scrollWheelSensitivities[i].scale;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logger.error("Unknown scroll wheel sensitivity id:", id);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-06-11 22:47:59 +00:00
|
|
|
getMovementSpeed() {
|
|
|
|
const id = this.getAllSettings().movementSpeed;
|
|
|
|
for (let i = 0; i < movementSpeeds.length; ++i) {
|
|
|
|
if (movementSpeeds[i].id === id) {
|
|
|
|
return movementSpeeds[i].multiplier;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logger.error("Unknown movement speed id:", id);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-05-09 14:45:23 +00:00
|
|
|
getIsFullScreen() {
|
|
|
|
return this.getAllSettings().fullscreen;
|
|
|
|
}
|
|
|
|
|
2020-05-19 07:14:40 +00:00
|
|
|
getKeybindingOverrides() {
|
|
|
|
return this.getAllSettings().keybindingOverrides;
|
|
|
|
}
|
|
|
|
|
2020-06-10 10:13:38 +00:00
|
|
|
getLanguage() {
|
|
|
|
return this.getAllSettings().language;
|
|
|
|
}
|
|
|
|
|
2020-05-09 14:45:23 +00:00
|
|
|
// Setters
|
|
|
|
|
2020-06-10 10:13:38 +00:00
|
|
|
updateLanguage(id) {
|
|
|
|
assert(LANGUAGES[id], "Language not known: " + id);
|
|
|
|
return this.updateSetting("language", id);
|
|
|
|
}
|
|
|
|
|
2020-05-09 14:45:23 +00:00
|
|
|
/**
|
|
|
|
* @param {string} key
|
|
|
|
* @param {string|boolean} value
|
|
|
|
*/
|
|
|
|
updateSetting(key, value) {
|
2020-05-14 17:29:21 +00:00
|
|
|
for (let i = 0; i < allApplicationSettings.length; ++i) {
|
|
|
|
const setting = allApplicationSettings[i];
|
|
|
|
if (setting.id === key) {
|
|
|
|
if (!setting.validate(value)) {
|
|
|
|
assertAlways(false, "Bad setting value: " + key);
|
|
|
|
}
|
|
|
|
this.getAllSettings()[key] = value;
|
|
|
|
if (setting.changeCb) {
|
|
|
|
setting.changeCb(this.app, value);
|
|
|
|
}
|
|
|
|
return this.writeAsync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assertAlways(false, "Unknown setting: " + key);
|
2020-05-09 14:45:23 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 07:14:40 +00:00
|
|
|
/**
|
|
|
|
* Sets a new keybinding override
|
|
|
|
* @param {string} keybindingId
|
|
|
|
* @param {number} keyCode
|
|
|
|
*/
|
|
|
|
updateKeybindingOverride(keybindingId, keyCode) {
|
|
|
|
assert(Number.isInteger(keyCode), "Not a valid key code: " + keyCode);
|
|
|
|
this.getAllSettings().keybindingOverrides[keybindingId] = keyCode;
|
|
|
|
return this.writeAsync();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets a given keybinding override
|
|
|
|
* @param {string} id
|
|
|
|
*/
|
|
|
|
resetKeybindingOverride(id) {
|
|
|
|
delete this.getAllSettings().keybindingOverrides[id];
|
|
|
|
return this.writeAsync();
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Resets all keybinding overrides
|
|
|
|
*/
|
|
|
|
resetKeybindingOverrides() {
|
|
|
|
this.getAllSettings().keybindingOverrides = {};
|
|
|
|
return this.writeAsync();
|
|
|
|
}
|
|
|
|
|
2020-05-09 14:45:23 +00:00
|
|
|
// RW Proxy impl
|
|
|
|
verify(data) {
|
|
|
|
if (!data.settings) {
|
|
|
|
return ExplainedResult.bad("missing key 'settings'");
|
|
|
|
}
|
|
|
|
if (typeof data.settings !== "object") {
|
|
|
|
return ExplainedResult.bad("Bad settings object");
|
|
|
|
}
|
|
|
|
|
|
|
|
const settings = data.settings;
|
|
|
|
for (let i = 0; i < allApplicationSettings.length; ++i) {
|
|
|
|
const setting = allApplicationSettings[i];
|
|
|
|
const storedValue = settings[setting.id];
|
|
|
|
if (!setting.validate(storedValue)) {
|
|
|
|
return ExplainedResult.bad("Bad setting value for " + setting.id + ": " + storedValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ExplainedResult.good();
|
|
|
|
}
|
|
|
|
|
|
|
|
getDefaultData() {
|
|
|
|
return {
|
|
|
|
version: this.getCurrentVersion(),
|
|
|
|
settings: new SettingsStorage(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
getCurrentVersion() {
|
2020-06-11 22:47:59 +00:00
|
|
|
return 10;
|
2020-05-09 14:45:23 +00:00
|
|
|
}
|
|
|
|
|
2020-05-23 13:04:55 +00:00
|
|
|
/** @param {{settings: SettingsStorage, version: number}} data */
|
2020-05-09 14:45:23 +00:00
|
|
|
migrate(data) {
|
2020-05-23 08:57:02 +00:00
|
|
|
// Simply reset before
|
|
|
|
if (data.version < 5) {
|
2020-05-09 14:45:23 +00:00
|
|
|
data.settings = new SettingsStorage();
|
2020-05-14 17:29:21 +00:00
|
|
|
data.version = this.getCurrentVersion();
|
2020-05-23 08:57:02 +00:00
|
|
|
return ExplainedResult.good();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.version < 6) {
|
2020-05-23 10:36:59 +00:00
|
|
|
data.settings.alwaysMultiplace = false;
|
2020-05-23 08:57:02 +00:00
|
|
|
data.version = 6;
|
2020-05-09 14:45:23 +00:00
|
|
|
}
|
|
|
|
|
2020-05-23 13:04:55 +00:00
|
|
|
if (data.version < 7) {
|
|
|
|
data.settings.offerHints = true;
|
|
|
|
data.version = 7;
|
|
|
|
}
|
|
|
|
|
2020-06-01 11:05:15 +00:00
|
|
|
if (data.version < 8) {
|
|
|
|
data.settings.scrollWheelSensitivity = "regular";
|
|
|
|
data.version = 8;
|
|
|
|
}
|
|
|
|
|
2020-06-10 10:13:38 +00:00
|
|
|
if (data.version < 9) {
|
|
|
|
data.settings.language = "auto-detect";
|
|
|
|
data.version = 9;
|
|
|
|
}
|
|
|
|
|
2020-06-11 22:47:59 +00:00
|
|
|
if (data.version < 10) {
|
|
|
|
data.settings.movementSpeed = "regular";
|
|
|
|
data.version = 10;
|
|
|
|
}
|
|
|
|
|
2020-05-09 14:45:23 +00:00
|
|
|
return ExplainedResult.good();
|
|
|
|
}
|
|
|
|
}
|