Added ability to import / export to dcb-card-editor.

This commit is contained in:
Filipe Rodrigues 2021-08-04 19:04:47 +01:00
parent c18c3d7b62
commit cdb619fd1a
2 changed files with 68 additions and 0 deletions

View File

@ -32,4 +32,5 @@ strum = {version = "0.20.0", features = ["derive"]}
zutil = {git = "https://github.com/Zenithsiz/zutil", rev = "8c9cb2d", features = ["gui", "alert"]}
# Serde
serde_json = "1.0.66"
serde_yaml = "0.8.17"

View File

@ -41,6 +41,7 @@ use overview_screen::OverviewScreen;
use ref_cast::RefCast;
use replace_str_screen::ReplaceStrScreen;
use std::{
fs,
lazy::SyncLazy,
path::Path,
sync::Mutex,
@ -129,6 +130,26 @@ impl epi::App for CardEditor {
}
}
if ui.button("Export").clicked() {
match loaded_game {
Some(loaded_game) => match self::on_export(loaded_game) {
Ok(()) => alert::info!("Successfully exported!"),
Err(err) => alert::error!("Unable to export file: {err:?}"),
},
None => alert::warn!("You must first open a game file to export"),
}
}
if ui.button("Import").clicked() {
match loaded_game {
Some(loaded_game) => match self::on_import(loaded_game) {
Ok(()) => alert::info!("Successfully imported!"),
Err(err) => alert::error!("Unable to import file: {err:?}"),
},
None => alert::warn!("You must first open a game file to import"),
}
}
if ui.button("Quit").clicked() && alert::warn_confirm!("Are you sure you want to quit?") {
frame.quit();
}
@ -1042,6 +1063,52 @@ fn on_open(loaded_game: &mut Option<LoadedGame>) -> Result<(), anyhow::Error> {
Ok(())
}
/// On 'File > Export'.
fn on_export(loaded_game: &mut LoadedGame) -> Result<(), anyhow::Error> {
// Ask for the file path
let cur_dir_path = std::env::current_dir().expect("Unable to get current directory path");
let file_path = FileDialog::new()
.set_location(&cur_dir_path)
.add_filter("Exported file", &["json"])
.show_save_single_file()
.expect("Unable to ask user for file");
let file_path = match file_path {
Some(path) => path,
None => return Ok(()),
};
let file = fs::File::create(file_path).context("Unable to create file")?;
serde_json::to_writer_pretty(file, &loaded_game.card_table).context("Unable to write to file")
}
/// On 'File > Import'.
fn on_import(loaded_game: &mut LoadedGame) -> Result<(), anyhow::Error> {
// If we have a loaded game and it's modified, ask the user if they want to override it
if loaded_game.modified() && !alert::warn_confirm!("Do you want to discard the changes to the current file") {
return Ok(());
}
// Else ask for the file path
let cur_dir_path = std::env::current_dir().expect("Unable to get current directory path");
let file_path = FileDialog::new()
.set_location(&cur_dir_path)
.add_filter("Exported file", &["json"])
.show_open_single_file()
.expect("Unable to ask user for file");
let file_path = match file_path {
Some(path) => path,
None => return Ok(()),
};
// And try to load it and parse it
let file = fs::File::open(file_path).context("Unable to open file")?;
let card_table = serde_json::from_reader(file).context("Unable to parse file")?;
loaded_game.card_table = card_table;
Ok(())
}
/// On 'View > Diff'.
fn on_diff(diff_screen: &mut Option<DiffScreen>) -> Result<(), anyhow::Error> {
// Ask for the file path