diff options
Diffstat (limited to 'apps/fparkan-game/src/launch_menu')
| -rw-r--r-- | apps/fparkan-game/src/launch_menu/sound.rs | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/apps/fparkan-game/src/launch_menu/sound.rs b/apps/fparkan-game/src/launch_menu/sound.rs new file mode 100644 index 00000000..56fc1103 --- /dev/null +++ b/apps/fparkan-game/src/launch_menu/sound.rs @@ -0,0 +1,110 @@ +//! Draft and persistence for the menu's sound controls. + +use fparkan_assets::game_settings::GameSettings; +use fparkan_vfs::DirectoryVfs; +use std::{fs, io, io::Write, path::Path}; + +#[derive(Clone, Copy)] +pub(super) enum Volume { + Effects, + Music, +} + +#[derive(Clone, Copy)] +struct SoundValues { + effects: u8, + music: u8, + play_music: bool, +} + +pub(super) struct SoundSettings { + saved: SoundValues, + draft: SoundValues, + pub(super) status: &'static str, + pub(super) drag: Option<Volume>, +} + +impl SoundSettings { + pub(super) fn open(root: &Path) -> io::Result<Self> { + let profile = GameSettings::load(&DirectoryVfs::new(root)).map_err(io::Error::other)?; + let audio = profile.audio(); + let percent = |gain: Option<f32>| (gain.unwrap_or(1.0) * 100.0).round() as u8; + let values = SoundValues { + effects: percent(audio.effects_gain), + music: percent(audio.cd_gain), + play_music: audio.play_cd, + }; + Ok(Self { + saved: values, + draft: values, + status: "", + drag: None, + }) + } + + pub(super) fn volume(&self, volume: Volume) -> u8 { + match volume { + Volume::Effects => self.draft.effects, + Volume::Music => self.draft.music, + } + } + + pub(super) fn set_volume(&mut self, volume: Volume, value: u8) { + match volume { + Volume::Effects => self.draft.effects = value.min(100), + Volume::Music => self.draft.music = value.min(100), + } + self.status = ""; + } + + pub(super) fn music_enabled(&self) -> bool { + self.draft.play_music + } + + pub(super) fn toggle_music(&mut self) { + self.draft.play_music = !self.draft.play_music; + self.status = ""; + } + + pub(super) fn apply(&mut self, root: &Path) -> io::Result<()> { + let updates: Vec<_> = [ + ( + "SFX_VOLUME", + i32::from(self.saved.effects), + i32::from(self.draft.effects), + ), + ( + "CD_VOLUME", + i32::from(self.saved.music), + i32::from(self.draft.music), + ), + ( + "PLAY_CD_MUSIC", + i32::from(self.saved.play_music), + i32::from(self.draft.play_music), + ), + ] + .into_iter() + .filter_map(|(key, previous, next)| (previous != next).then_some((key, next))) + .collect(); + if !updates.is_empty() { + let profile = GameSettings::load(&DirectoryVfs::new(root)).map_err(io::Error::other)?; + let updated = profile.with_cs_integers(&updates); + let path = root.join("Iron_3D.ini"); + let temporary = root.join(format!("Iron_3D.ini.fparkan-{}.tmp", std::process::id())); + let mut file = fs::OpenOptions::new() + .write(true) + .create_new(true) + .open(&temporary)?; + let written = file.write_all(&updated).and_then(|()| file.sync_all()); + drop(file); + if let Err(error) = written.and_then(|()| fs::rename(&temporary, path)) { + let _ = fs::remove_file(&temporary); + return Err(error); + } + } + self.saved = self.draft; + self.status = "Сохранено"; + Ok(()) + } +} |
