Skip to main content

paiagram_core/
settings.rs

1//! # Settings
2//! Module for user preferences and project settings.
3
4use crate::{i18n::Language, units::time::Duration};
5use bevy::prelude::*;
6
7#[derive(Reflect, Copy, Clone, Debug, PartialEq, Eq)]
8pub enum AntialiasingMode {
9    On,
10    Off,
11}
12
13impl Default for AntialiasingMode {
14    fn default() -> Self {
15        if cfg!(target_arch = "wasm32") {
16            Self::Off
17        } else {
18            Self::On
19        }
20    }
21}
22
23#[derive(Reflect, Copy, Clone, Debug, PartialEq, Eq)]
24pub enum LevelOfDetailMode {
25    Off,
26    Lod2,
27    Lod4,
28}
29
30impl LevelOfDetailMode {
31    pub fn as_u8(self) -> u8 {
32        match self {
33            Self::Off => 1,
34            Self::Lod2 => 2,
35            Self::Lod4 => 4,
36        }
37    }
38}
39
40impl Default for LevelOfDetailMode {
41    fn default() -> Self {
42        if cfg!(target_arch = "wasm32") {
43            Self::Lod4
44        } else {
45            Self::Lod2
46        }
47    }
48}
49
50pub struct SettingsPlugin;
51impl Plugin for SettingsPlugin {
52    fn build(&self, app: &mut App) {
53        app.init_resource::<UserPreferences>()
54            .init_resource::<ProjectSettings>()
55            .add_systems(
56                Update,
57                sync_preferences.run_if(resource_changed::<UserPreferences>),
58            );
59    }
60}
61
62#[derive(Reflect, Resource)]
63#[reflect(Resource)]
64pub struct UserPreferences {
65    pub lang: Language,
66    pub dark_mode: bool,
67    pub developer_mode: bool,
68    pub antialiasing_mode: AntialiasingMode,
69    pub level_of_detail_mode: LevelOfDetailMode,
70}
71
72impl Default for UserPreferences {
73    fn default() -> Self {
74        Self {
75            lang: Language::EnCA,
76            dark_mode: false,
77            developer_mode: cfg!(debug_assertions),
78            antialiasing_mode: AntialiasingMode::default(),
79            level_of_detail_mode: LevelOfDetailMode::default(),
80        }
81    }
82}
83
84/// Only run when the preferences change
85fn sync_preferences(preferences: Res<UserPreferences>) {
86    egui_i18n::set_language(preferences.lang.identifier());
87}
88
89#[derive(Reflect, Resource)]
90#[reflect(Resource)]
91pub struct ProjectSettings {
92    pub remarks: String,
93    pub authors: Vec<String>,
94    pub repeat_frequency: Duration,
95}
96
97impl Default for ProjectSettings {
98    fn default() -> Self {
99        Self {
100            remarks: String::new(),
101            authors: Vec::new(),
102            repeat_frequency: Duration::from_secs(86400),
103        }
104    }
105}