Allow muting sounds and music

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

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

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

@ -53,6 +53,12 @@ export class HUDGameMenu extends BaseHUDPart {
this.musicButton = makeDiv(menuButtons, null, ["button", "music"]); this.musicButton = makeDiv(menuButtons, null, ["button", "music"]);
this.sfxButton = makeDiv(menuButtons, null, ["button", "sfx"]); this.sfxButton = makeDiv(menuButtons, null, ["button", "sfx"]);
this.settingsButton = makeDiv(menuButtons, null, ["button", "settings"]); 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() { 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 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 // GAME
]; ];
@ -79,6 +97,9 @@ class SettingsStorage {
constructor() { constructor() {
this.uiScale = "regular"; this.uiScale = "regular";
this.fullscreen = G_IS_STANDALONE; this.fullscreen = G_IS_STANDALONE;
this.soundsMuted = false;
this.musicMuted = false;
} }
} }
@ -143,9 +164,20 @@ export class ApplicationSettings extends ReadWriteProxy {
* @param {string|boolean} value * @param {string|boolean} value
*/ */
updateSetting(key, value) { updateSetting(key, value) {
assert(this.getAllSettings().hasOwnProperty(key), "Setting not known: " + key); for (let i = 0; i < allApplicationSettings.length; ++i) {
this.getAllSettings()[key] = value; const setting = allApplicationSettings[i];
return this.writeAsync(); 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 // RW Proxy impl
@ -176,14 +208,14 @@ export class ApplicationSettings extends ReadWriteProxy {
} }
getCurrentVersion() { getCurrentVersion() {
return 1; return 2;
} }
migrate(data) { migrate(data) {
// Simply reset // Simply reset
if (data.version < 1) { if (data.version < this.getCurrentVersion()) {
data.settings = new SettingsStorage(); data.settings = new SettingsStorage();
data.version = 1; data.version = this.getCurrentVersion();
} }
return ExplainedResult.good(); return ExplainedResult.good();

Loading…
Cancel
Save