diff options
Diffstat (limited to 'apps/fparkan-game/src/launch_menu')
| -rw-r--r-- | apps/fparkan-game/src/launch_menu/graphics.rs | 163 | ||||
| -rw-r--r-- | apps/fparkan-game/src/launch_menu/profile.rs | 24 | ||||
| -rw-r--r-- | apps/fparkan-game/src/launch_menu/sound.rs | 20 |
3 files changed, 190 insertions, 17 deletions
diff --git a/apps/fparkan-game/src/launch_menu/graphics.rs b/apps/fparkan-game/src/launch_menu/graphics.rs new file mode 100644 index 00000000..97c8aecd --- /dev/null +++ b/apps/fparkan-game/src/launch_menu/graphics.rs @@ -0,0 +1,163 @@ +//! Draft values for the graphics menu and their application to the current window. + +use super::profile; +use crate::mission_loading::fullscreen_mode; +use fparkan_assets::game_settings::{DisplaySettings, GameSettings}; +use fparkan_vfs::DirectoryVfs; +use std::path::Path; +use winit::{dpi::PhysicalSize, window::Window}; + +#[derive(Clone, Copy)] +pub(super) enum GraphicsControl { + Resolution, + WindowMode, + Reflections, +} + +pub(super) struct GraphicsSettings { + display: DisplaySettings, + saved: Option<DisplaySettings>, + resolutions: Vec<[u32; 2]>, + reflections: bool, + saved_reflections: bool, + pub(super) status: &'static str, +} + +impl GraphicsSettings { + pub(super) fn open(root: &Path, window: &Window) -> Result<Self, String> { + let profile = GameSettings::load(&DirectoryVfs::new(root)).map_err(|e| e.to_string())?; + let saved = profile.display(); + let size = window.inner_size(); + let display = saved.unwrap_or(DisplaySettings { + extent: [size.width.max(1), size.height.max(1)], + bit_depth: 32, + windowed: window.fullscreen().is_none(), + }); + let monitor = window + .current_monitor() + .or_else(|| window.primary_monitor()); + let mut resolutions = vec![display.extent]; + if let Some(monitor) = monitor { + resolutions.extend(monitor.video_modes().map(|mode| { + let size = mode.size(); + [size.width, size.height] + })); + } + resolutions.sort_unstable(); + resolutions.dedup(); + let reflections = profile.water_reflections(); + Ok(Self { + display, + saved, + resolutions, + reflections, + saved_reflections: reflections, + status: "", + }) + } + + pub(super) fn value(&self, control: GraphicsControl) -> String { + match control { + GraphicsControl::Resolution => { + let [width, height] = self.display.extent; + format!("{width}x{height}") + } + GraphicsControl::WindowMode if self.display.windowed => "Оконный".into(), + GraphicsControl::WindowMode => "Полноэкранный".into(), + GraphicsControl::Reflections if self.reflections => "Вкл".into(), + GraphicsControl::Reflections => "Выкл".into(), + } + } + + pub(super) fn change(&mut self, control: GraphicsControl, forward: bool) { + match control { + GraphicsControl::Resolution => { + let index = self + .resolutions + .binary_search(&self.display.extent) + .expect("current resolution is listed"); + let len = self.resolutions.len(); + let step = if forward { 1 } else { len - 1 }; + self.display.extent = self.resolutions[(index + step) % len]; + } + GraphicsControl::WindowMode => self.display.windowed = !self.display.windowed, + GraphicsControl::Reflections => self.reflections = !self.reflections, + } + self.status = ""; + } + + pub(super) fn apply(&mut self, root: &Path, window: &Window) -> Result<(), String> { + let mut updates = Vec::new(); + let changed_extent = self + .saved + .is_none_or(|saved| saved.extent != self.display.extent); + let changed_mode = self + .saved + .is_none_or(|saved| saved.windowed != self.display.windowed); + if changed_extent { + let [width, height] = self.display.extent; + updates.push(( + "DISPLAY_WIDTH", + i32::try_from(width).map_err(|e| e.to_string())?, + )); + updates.push(( + "DISPLAY_HEIGHT", + i32::try_from(height).map_err(|e| e.to_string())?, + )); + } + if changed_mode { + updates.push(("WINDOW_MODE", i32::from(self.display.windowed))); + } + if self.reflections != self.saved_reflections { + updates.push(("REFLECTIONS", i32::from(self.reflections))); + } + let fullscreen = if changed_extent || changed_mode { + let monitor = window + .current_monitor() + .or_else(|| window.primary_monitor()); + fullscreen_mode(self.display, monitor)? + } else { + None + }; + profile::save_integers(root, &updates).map_err(|e| e.to_string())?; + if changed_extent || changed_mode { + window.set_fullscreen(fullscreen); + if self.display.windowed { + let [width, height] = self.display.extent; + let _ = window.request_inner_size(PhysicalSize::new(width, height)); + } + } + self.saved = Some(self.display); + self.saved_reflections = self.reflections; + self.status = "Сохранено"; + Ok(()) + } +} + +#[cfg(test)] +#[test] +fn selection_wraps_without_changing_the_saved_display() { + let display = DisplaySettings { + extent: [800, 600], + bit_depth: 32, + windowed: true, + }; + let mut settings = GraphicsSettings { + display, + saved: Some(display), + resolutions: vec![[800, 600], [1024, 768]], + reflections: true, + saved_reflections: true, + status: "", + }; + settings.change(GraphicsControl::Resolution, false); + assert_eq!(settings.display.extent, [1024, 768]); + settings.change(GraphicsControl::Resolution, true); + settings.change(GraphicsControl::WindowMode, true); + settings.change(GraphicsControl::Reflections, true); + assert_eq!(settings.display.extent, [800, 600]); + assert!(!settings.display.windowed); + assert!(!settings.reflections); + assert_eq!(settings.saved, Some(display)); + assert!(settings.saved_reflections); +} diff --git a/apps/fparkan-game/src/launch_menu/profile.rs b/apps/fparkan-game/src/launch_menu/profile.rs new file mode 100644 index 00000000..366f499f --- /dev/null +++ b/apps/fparkan-game/src/launch_menu/profile.rs @@ -0,0 +1,24 @@ +use fparkan_assets::game_settings::GameSettings; +use fparkan_vfs::DirectoryVfs; +use std::{fs, io, io::Write, path::Path}; + +pub(super) fn save_integers(root: &Path, updates: &[(&str, i32)]) -> io::Result<()> { + if updates.is_empty() { + return Ok(()); + } + 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); + } + Ok(()) +} diff --git a/apps/fparkan-game/src/launch_menu/sound.rs b/apps/fparkan-game/src/launch_menu/sound.rs index 56fc1103..33df5088 100644 --- a/apps/fparkan-game/src/launch_menu/sound.rs +++ b/apps/fparkan-game/src/launch_menu/sound.rs @@ -1,8 +1,9 @@ //! Draft and persistence for the menu's sound controls. +use super::profile; use fparkan_assets::game_settings::GameSettings; use fparkan_vfs::DirectoryVfs; -use std::{fs, io, io::Write, path::Path}; +use std::{io, path::Path}; #[derive(Clone, Copy)] pub(super) enum Volume { @@ -87,22 +88,7 @@ impl SoundSettings { .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); - } - } + profile::save_integers(root, &updates)?; self.saved = self.draft; self.status = "Сохранено"; Ok(()) |
