made settings persistant
This commit is contained in:
+10
-1
@@ -1,4 +1,5 @@
|
||||
mod layout;
|
||||
mod persistence;
|
||||
mod seed;
|
||||
mod ui;
|
||||
|
||||
@@ -26,7 +27,7 @@ struct DungeonApp {
|
||||
|
||||
impl Default for DungeonApp {
|
||||
fn default() -> Self {
|
||||
let settings = UiSettings::default();
|
||||
let settings = persistence::load_settings().unwrap_or_default();
|
||||
let layout = generate_layout(
|
||||
settings.cols,
|
||||
settings.rows,
|
||||
@@ -43,6 +44,14 @@ impl Default for DungeonApp {
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for DungeonApp {
|
||||
fn drop(&mut self) {
|
||||
if let Err(err) = persistence::save_settings(&self.settings) {
|
||||
eprintln!("Failed to save settings: {err}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl eframe::App for DungeonApp {
|
||||
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
||||
let panel_result = draw_side_panel(ctx, &mut self.settings);
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::ui::UiSettings;
|
||||
|
||||
const APP_DIR_NAME: &str = "desktop_dungeon_generator";
|
||||
const SETTINGS_FILE_NAME: &str = "settings.json";
|
||||
|
||||
pub fn load_settings() -> Option<UiSettings> {
|
||||
let path = settings_path()?;
|
||||
let content = fs::read_to_string(path).ok()?;
|
||||
serde_json::from_str::<UiSettings>(&content).ok()
|
||||
}
|
||||
|
||||
pub fn save_settings(settings: &UiSettings) -> io::Result<()> {
|
||||
let path = settings_path().ok_or_else(|| {
|
||||
io::Error::new(
|
||||
io::ErrorKind::NotFound,
|
||||
"No OS application data directory found",
|
||||
)
|
||||
})?;
|
||||
|
||||
if let Some(parent) = path.parent() {
|
||||
fs::create_dir_all(parent)?;
|
||||
}
|
||||
|
||||
let json = serde_json::to_string_pretty(settings)
|
||||
.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?;
|
||||
fs::write(path, json)
|
||||
}
|
||||
|
||||
fn settings_path() -> Option<PathBuf> {
|
||||
let base = dirs::data_local_dir().or_else(dirs::data_dir)?;
|
||||
Some(base.join(APP_DIR_NAME).join(SETTINGS_FILE_NAME))
|
||||
}
|
||||
@@ -1,14 +1,22 @@
|
||||
use crate::seed;
|
||||
use eframe::egui;
|
||||
use egui::RichText;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
enum Tab {
|
||||
Generate,
|
||||
Layout,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
impl Default for Tab {
|
||||
fn default() -> Self {
|
||||
Self::Generate
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(default)]
|
||||
pub struct UiSettings {
|
||||
pub seed: u64,
|
||||
pub cols: usize,
|
||||
|
||||
Reference in New Issue
Block a user