//! 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, 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 { 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); }