mod file;
pub use file::*;
use bevy::prelude::Resource;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;
use crate::ui::{EditorTool, ToolMode};
pub const MAX_RECENT_PROJECTS: usize = 10;
#[derive(Debug, Clone, Serialize, Deserialize, Resource)]
pub struct EditorPreferences {
pub version: u32,
pub show_tree_view: bool,
pub show_inspector: bool,
pub tree_view_width: f32,
pub inspector_width: f32,
pub show_grid: bool,
pub show_collisions: bool,
pub snap_to_grid: bool,
pub zoom: f32,
pub default_tool: EditorTool,
pub default_tool_mode: ToolMode,
pub recent_projects: Vec<RecentProject>,
pub auto_open_last_project: bool,
pub keybindings: HashMap<String, String>,
pub theme: ThemeSettings,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RecentProject {
pub path: String,
pub name: String,
pub last_opened: u64, }
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ThemeSettings {
pub use_dark_theme: bool,
pub accent_color: Option<[u8; 3]>,
}
impl Default for EditorPreferences {
fn default() -> Self {
Self {
version: 1,
show_tree_view: true,
show_inspector: true,
tree_view_width: 200.0,
inspector_width: 250.0,
show_grid: true,
show_collisions: false,
snap_to_grid: true,
zoom: 1.0,
default_tool: EditorTool::Select,
default_tool_mode: ToolMode::Point,
recent_projects: Vec::new(),
auto_open_last_project: false,
keybindings: HashMap::new(),
theme: ThemeSettings::default(),
}
}
}
impl EditorPreferences {
pub fn add_recent_project(&mut self, path: PathBuf, name: String) {
use std::time::{SystemTime, UNIX_EPOCH};
let path_str = path.to_string_lossy().to_string();
self.recent_projects.retain(|p| p.path != path_str);
self.recent_projects.insert(
0,
RecentProject {
path: path_str,
name,
last_opened: SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0),
},
);
self.recent_projects.truncate(MAX_RECENT_PROJECTS);
}
pub fn remove_recent_project(&mut self, path: &str) {
self.recent_projects.retain(|p| p.path != path);
}
pub fn last_project(&self) -> Option<&RecentProject> {
self.recent_projects.first()
}
pub fn clear_recent_projects(&mut self) {
self.recent_projects.clear();
}
}