Create relative pathes for saving projects

This commit is contained in:
Jaby 2025-04-02 20:47:50 +02:00
parent 5f93549a05
commit a89699334e
4 changed files with 41 additions and 6 deletions

View File

@ -3150,6 +3150,12 @@ version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
[[package]]
name = "pathdiff"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df94ce210e5bc13cb6651479fa48d14f601d9858cfe0467f43ae157023b938d3"
[[package]] [[package]]
name = "percent-encoding" name = "percent-encoding"
version = "2.3.1" version = "2.3.1"
@ -4111,6 +4117,7 @@ dependencies = [
name = "tim_tool" name = "tim_tool"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"pathdiff",
"png", "png",
"rfd", "rfd",
"serde", "serde",

View File

@ -7,12 +7,13 @@ edition = "2021"
panic = "abort" panic = "abort"
[dependencies] [dependencies]
pathdiff = "0.2.3"
png = "0.17.16" png = "0.17.16"
rfd = "0.15.2" rfd = "0.15.2"
slint = "1.9.2" slint = "1.9.2"
tiny-skia = "0.11.4"
serde = {version = "1.0.140", features = ["derive"]} serde = {version = "1.0.140", features = ["derive"]}
serde_json = "1.0" serde_json = "1.0"
tiny-skia = "0.11.4"
tool_helper = {path = "../tool_helper"} tool_helper = {path = "../tool_helper"}
[build-dependencies] [build-dependencies]

View File

@ -98,6 +98,7 @@ pub(super) fn on_save_project_clicked(tim_manager: MutexTIMManager) -> impl FnMu
let tim_info = tim_manager.lock().expect("VRAM already locked").clone_added_tims(); let tim_info = tim_manager.lock().expect("VRAM already locked").clone_added_tims();
let vram_info = main_tab.clone_vram_info(); let vram_info = main_tab.clone_vram_info();
let use_rel_path = main_window.get_project_widget_use_rel_paths();
if tim_info.len() != vram_info.len() { if tim_info.len() != vram_info.len() {
return Err(Error::from_str("TIM and VRAM info not in sync! Please close program")); return Err(Error::from_str("TIM and VRAM info not in sync! Please close program"));
@ -105,7 +106,12 @@ pub(super) fn on_save_project_clicked(tim_manager: MutexTIMManager) -> impl FnMu
let mut cur_job:Option<Job> = None; let mut cur_job:Option<Job> = None;
let mut cur_palette:Option<PaletteRect> = None; let mut cur_palette:Option<PaletteRect> = None;
let mut project = Project::new(tim_info.into_iter().zip(vram_info.into_iter()).filter_map(|(tim, (file_name, vram))| { let mut map_result = Ok(());
let mut project = Project::new(tim_info.into_iter().zip(vram_info.into_iter()).filter_map(|(tim, (name, vram))| {
if map_result.is_err() {
return None;
}
if vram.is_palette { if vram.is_palette {
let cur_palette = std::mem::replace(&mut cur_palette, None); let cur_palette = std::mem::replace(&mut cur_palette, None);
if let Some(mut cur_palette) = cur_palette { if let Some(mut cur_palette) = cur_palette {
@ -125,11 +131,22 @@ pub(super) fn on_save_project_clicked(tim_manager: MutexTIMManager) -> impl FnMu
if let Some((width, height)) = tim.get_palette_size() { if let Some((width, height)) = tim.get_palette_size() {
cur_palette = Some(PaletteRect::new(0, 0, width, height)); cur_palette = Some(PaletteRect::new(0, 0, width, height));
} }
cur_job = Some(Job::new(file_name, tim.get_path(), (vram.x as u16, vram.y as u16), Encoding::from_str(vram.encoding_str.as_str()))); let file_path = if use_rel_path {
match tim.get_path_rel_to(PathBuf::from(save_location.as_str())) {
Ok(file_path) => file_path,
Err(error) => {
map_result = Err(error);
PathBuf::new()
}
}
} else {tim.get_path()};
cur_job = Some(Job::new(name, file_path, (vram.x as u16, vram.y as u16), Encoding::from_str(vram.encoding_str.as_str())));
} }
prev_job prev_job
} }
}).collect()); }).collect());
map_result?;
if let Some(cur_job) = cur_job { if let Some(cur_job) = cur_job {
project.jobs.push(cur_job); project.jobs.push(cur_job);
} }

View File

@ -1,5 +1,6 @@
pub mod types; pub mod types;
use pathdiff::diff_paths;
use std::{fs::File, path::PathBuf}; use std::{fs::File, path::PathBuf};
use slint::{Rgba8Pixel, SharedPixelBuffer}; use slint::{Rgba8Pixel, SharedPixelBuffer};
use tool_helper::Error; use tool_helper::Error;
@ -132,6 +133,15 @@ impl TIMInfo {
pub fn get_path(&self) -> std::path::PathBuf { pub fn get_path(&self) -> std::path::PathBuf {
self.path.clone() self.path.clone()
} }
pub fn get_path_rel_to(&self, mut reference_path: std::path::PathBuf) -> Result<std::path::PathBuf, Error> {
let file_path = self.path.clone();
reference_path.pop();
diff_paths(&file_path, &reference_path).ok_or_else(|| {
Error::from_text(format!("Can not create relative path from {} to {}", file_path.to_string_lossy().as_ref(), reference_path.to_string_lossy().as_ref()))
})
}
} }
#[derive(Clone)] #[derive(Clone)]