Allow muting sounds and music

pull/33/head
tobspr 4 years ago
parent af0f56b5e4
commit 23874c43dc

@ -27,14 +27,19 @@
&:hover {
opacity: 0.8;
transform: translateY(3px);
}
&.music {
background-image: uiResource("icons/music_on.png");
&.muted {
background-image: uiResource("icons/music_off.png");
}
}
&.sfx {
background-image: uiResource("icons/sound_on.png");
&.muted {
background-image: uiResource("icons/sound_off.png");
}
}
&.settings {

@ -71,7 +71,7 @@ export const globalConfig = {
debug: {
/* dev:start */
fastGameEnter: true,
// fastGameEnter: true,
noArtificialDelays: true,
disableSavegameWrite: false,
showEntityBounds: false,

@ -53,6 +53,12 @@ export class HUDGameMenu extends BaseHUDPart {
this.musicButton = makeDiv(menuButtons, null, ["button", "music"]);
this.sfxButton = makeDiv(menuButtons, null, ["button", "sfx"]);
this.settingsButton = makeDiv(menuButtons, null, ["button", "settings"]);
this.trackClicks(this.musicButton, this.toggleMusic);
this.trackClicks(this.sfxButton, this.toggleSfx);
this.musicButton.classList.toggle("muted", this.root.app.settings.getAllSettings().musicMuted);
this.sfxButton.classList.toggle("muted", this.root.app.settings.getAllSettings().musicMuted);
}
update() {
@ -68,4 +74,17 @@ export class HUDGameMenu extends BaseHUDPart {
}
}
}
toggleMusic() {
const newValue = !this.root.app.settings.getAllSettings().musicMuted;
this.root.app.settings.updateSetting("musicMuted", newValue);
this.musicButton.classList.toggle("muted", newValue);
}
toggleSfx() {
const newValue = !this.root.app.settings.getAllSettings().soundsMuted;
this.root.app.settings.updateSetting("soundsMuted", newValue);
this.sfxButton.classList.toggle("muted", newValue);
}
}

@ -67,6 +67,24 @@ export const allApplicationSettings = [
},
G_IS_STANDALONE
),
new BoolSetting(
"soundsMuted",
categoryApp,
/**
* @param {Application} app
*/
(app, value) => app.sound.setSoundsMuted(value),
false
),
new BoolSetting(
"musicMuted",
categoryApp,
/**
* @param {Application} app
*/
(app, value) => app.sound.setMusicMuted(value),
false
),
// GAME
];
@ -79,6 +97,9 @@ class SettingsStorage {
constructor() {
this.uiScale = "regular";
this.fullscreen = G_IS_STANDALONE;
this.soundsMuted = false;
this.musicMuted = false;
}
}
@ -143,9 +164,20 @@ export class ApplicationSettings extends ReadWriteProxy {
* @param {string|boolean} value
*/
updateSetting(key, value) {
assert(this.getAllSettings().hasOwnProperty(key), "Setting not known: " + key);
this.getAllSettings()[key] = value;
return this.writeAsync();
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);
}
// RW Proxy impl
@ -176,14 +208,14 @@ export class ApplicationSettings extends ReadWriteProxy {
}
getCurrentVersion() {
return 1;
return 2;
}
migrate(data) {
// Simply reset
if (data.version < 1) {
if (data.version < this.getCurrentVersion()) {
data.settings = new SettingsStorage();
data.version = 1;
data.version = this.getCurrentVersion();
}
return ExplainedResult.good();

Loading…
Cancel
Save