aboutsummaryrefslogtreecommitdiff
path: root/apps/fparkan-game/src/launch_menu/graphics.rs
blob: 97c8aecd27f2ca73be182a1c1717d0933bdc74cf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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);
}