From a260c1ffb75fc9474c2a728a491641421283bd9c Mon Sep 17 00:00:00 2001 From: Jaby Date: Tue, 1 Apr 2025 15:24:41 +0200 Subject: [PATCH] Allow position information for newly added VRAM items --- .../tim_tool/src/gui/file_tab/callbacks.rs | 39 ++++++++++++------- src/Tools/tim_tool/src/gui/file_tab/mod.rs | 2 +- src/Tools/tim_tool/src/gui/main_tab/mod.rs | 29 ++++++++++---- src/Tools/tim_tool/src/logic/project/mod.rs | 28 +++---------- 4 files changed, 54 insertions(+), 44 deletions(-) diff --git a/src/Tools/tim_tool/src/gui/file_tab/callbacks.rs b/src/Tools/tim_tool/src/gui/file_tab/callbacks.rs index 2f3d1608..8567251d 100644 --- a/src/Tools/tim_tool/src/gui/file_tab/callbacks.rs +++ b/src/Tools/tim_tool/src/gui/file_tab/callbacks.rs @@ -1,6 +1,6 @@ use std::{fs::File, io::Write, path::PathBuf}; -use crate::{gui::{self, main_tab::MainTab, MutexTIMManager, VRAM_HEIGHT, VRAM_WIDTH}, MainWindow}; +use crate::{gui::{self, main_tab::{MainTab, NewVRAMImageInfo}, MutexTIMManager, VRAM_HEIGHT, VRAM_WIDTH}, MainWindow}; use super::FileTab; use rfd::FileDialog; use slint::SharedString; @@ -45,15 +45,8 @@ pub(super) fn on_add_image(tim_manager: MutexTIMManager) -> impl FnMut(&mut File return move |file_tab, main_tab, main_window| { let file_name = file_tab.get_file_name(); let encoding = file_tab.get_encoding()?; - let mut tim_mgr = tim_manager.lock().expect("VRAM already locked"); - let (image, palette_image) = tim_mgr.get_converted_unadded_tim_image(encoding)?; - let (full_image, _) = tim_mgr.get_converted_unadded_tim_image(Encoding::FullColor)?; - let images_created = main_tab.add_new_vram_file(&file_name, full_image, image, encoding, palette_image); - if let Err(error) = tim_mgr.add_unadded_tim(images_created) { - main_tab.pop_vram_files(images_created); - return Err(error); - } + add_unadded_tim(main_tab, tim_manager.clone(), &file_name, encoding)?; file_tab.clear_load(); main_window.invoke_change_to_main(); @@ -61,7 +54,7 @@ pub(super) fn on_add_image(tim_manager: MutexTIMManager) -> impl FnMut(&mut File }; } -pub(super) fn on_load_project_clicked() -> impl FnMut(&mut FileTab, &MainWindow) -> Result<(), Error> + 'static { +pub(super) fn on_load_project_clicked(tim_manager: MutexTIMManager) -> impl FnMut(&mut FileTab, &MainWindow) -> Result<(), Error> + 'static { move |_file_tab, main_window| { let open_location = main_window.get_project_widget_open_project_path(); if open_location.is_empty() { @@ -69,13 +62,17 @@ pub(super) fn on_load_project_clicked() -> impl FnMut(&mut FileTab, &MainWindow) } let file_content = tool_helper::read_file_to_string(&PathBuf::from(open_location.as_str()))?; - let _new_project = match serde_json::from_str::(&file_content) { + let new_project = match serde_json::from_str::(&file_content) { Ok(project) => project, Err(error) => { return Err(Error::from_error(error)); } }; + for job in new_project.jobs { + let (_, _) = tim_manager.lock().expect("VRAM already locked").load_unadded_tim(&job.file_path)?; + } + Ok(()) } } @@ -102,7 +99,7 @@ pub(super) fn on_save_project_clicked(tim_manager: MutexTIMManager) -> impl FnMu if let Some(mut cur_palette) = cur_palette { cur_palette.pos = ImagePosition::new(vram.x as u16, vram.y as u16); if let Some(cur_job) = &mut cur_job { - cur_job.add_palette(cur_palette); + cur_job.palette_rect = cur_palette; } } None @@ -122,7 +119,7 @@ pub(super) fn on_save_project_clicked(tim_manager: MutexTIMManager) -> impl FnMu } }).collect()); if let Some(cur_job) = cur_job { - project.push(cur_job); + project.jobs.push(cur_job); } let json_content = match serde_json::to_string(&project) { @@ -170,6 +167,22 @@ pub(super) fn on_browse_save_project_clicked() -> impl FnMut(&mut FileTab, &Main } } +fn add_unadded_tim(main_tab: &mut MainTab, tim_manager: MutexTIMManager, file_name: &String, encoding: Encoding) -> Result<(), Error> { + let mut tim_mgr = tim_manager.lock().expect("VRAM already locked"); + let (image, palette_image) = tim_mgr.get_converted_unadded_tim_image(encoding)?; + let (full_image, _) = tim_mgr.get_converted_unadded_tim_image(Encoding::FullColor)?; + let image = NewVRAMImageInfo::new(image, 0, 0, encoding); + let palette_image = if let Some(palette_image) = palette_image { Some(NewVRAMImageInfo::new(palette_image, 0, 0, Encoding::FullColor)) } else { None }; + + let images_created = main_tab.add_new_vram_file(file_name, full_image, image, palette_image); + if let Err(error) = tim_mgr.add_unadded_tim(images_created) { + main_tab.pop_vram_files(images_created); + return Err(error); + } + + Ok(()) +} + fn create_project_file_dialog() -> FileDialog { FileDialog::new() .add_filter("TIM project file (.tim_project)", &["tim_project"]) diff --git a/src/Tools/tim_tool/src/gui/file_tab/mod.rs b/src/Tools/tim_tool/src/gui/file_tab/mod.rs index 4d0b0232..cbe7aad6 100644 --- a/src/Tools/tim_tool/src/gui/file_tab/mod.rs +++ b/src/Tools/tim_tool/src/gui/file_tab/mod.rs @@ -73,7 +73,7 @@ impl FileTab { }); // Project functions - let (cloned_object, cloned_main_window, mut function) = (object.clone(), main_window.clone(), callbacks::on_load_project_clicked()); + let (cloned_object, cloned_main_window, mut function) = (object.clone(), main_window.clone(), callbacks::on_load_project_clicked(tim_manager.clone())); main_window.borrow().on_project_widget_load_project_clicked(move || { if let Err(error) = function(&mut cloned_object.borrow_mut(), &cloned_main_window.borrow()) { display_error("Loading project failed", &error.to_string()); diff --git a/src/Tools/tim_tool/src/gui/main_tab/mod.rs b/src/Tools/tim_tool/src/gui/main_tab/mod.rs index e4963730..4b9b3cc5 100644 --- a/src/Tools/tim_tool/src/gui/main_tab/mod.rs +++ b/src/Tools/tim_tool/src/gui/main_tab/mod.rs @@ -6,6 +6,19 @@ use slint::{Model, SharedString}; use std::{cell::RefCell, ops::RangeInclusive, rc::Rc, sync::{Arc, Mutex}}; use tim_tool::logic::tim::types::Encoding; +pub struct NewVRAMImageInfo { + pub image: slint::Image, + pub x: i32, + pub y: i32, + pub encoding: Encoding, +} + +impl NewVRAMImageInfo { + pub fn new(image: slint::Image, x: i32, y: i32, encoding: Encoding) -> NewVRAMImageInfo { + NewVRAMImageInfo{image, x, y, encoding} + } +} + struct VRAM { count: usize, file_list: Rc>, @@ -59,18 +72,18 @@ impl MainTab { object } - pub fn add_new_vram_file(&mut self, file_name: &String, full_image: slint::Image, image: slint::Image, encoding: Encoding, image_palette: Option) -> usize { + pub fn add_new_vram_file(&mut self, file_name: &String, full_image: slint::Image, texture: NewVRAMImageInfo, image_palette: Option) -> usize { let mut vram_data = self.vram.lock().expect("VRAM already locked"); - let mut add_new_image = |file_name: &String, full_image: slint::Image, image: slint::Image, encoding: Encoding, palette_count: i32, is_palette: bool| { - let encoding_str = if is_palette {""} else {encoding.to_str()}; + let mut add_new_image = |file_name: &String, full_image: slint::Image, vram_image: NewVRAMImageInfo, palette_count: i32, is_palette: bool| { + let encoding_str = if is_palette {""} else {vram_image.encoding.to_str()}; let vram_image = VRAMData { images: VRAMImgData { full_image, - image + image: vram_image.image, }, info: VRAMInfo { - x: 0, - y: 0, + x: vram_image.x, + y: vram_image.y, encoding_str: SharedString::from(encoding_str), palette_count, is_palette, @@ -81,12 +94,12 @@ impl MainTab { }; let mut images_added = 1; - add_new_image(file_name, full_image, image, encoding, if image_palette.is_some() {1} else {0}, false); + add_new_image(file_name, full_image, texture, if image_palette.is_some() {1} else {0}, false); if let Some(image_palette) = image_palette { let file_name = " => ".to_owned() + file_name.as_str() + " (Palette)"; images_added += 1; - add_new_image(&file_name, image_palette.clone(), image_palette, encoding, 0, true); + add_new_image(&file_name, image_palette.image.clone(), image_palette, 0, true); } images_added diff --git a/src/Tools/tim_tool/src/logic/project/mod.rs b/src/Tools/tim_tool/src/logic/project/mod.rs index bfaeca04..32a7d0a1 100644 --- a/src/Tools/tim_tool/src/logic/project/mod.rs +++ b/src/Tools/tim_tool/src/logic/project/mod.rs @@ -58,42 +58,26 @@ impl std::default::Default for PaletteRect { #[derive(Serialize, Deserialize)] pub struct Job { - name: String, - file_path: PathBuf, - image_pos: ImagePosition, - palette_rect: PaletteRect, - encoding: Encoding, + pub name: String, + pub file_path: PathBuf, + pub image_pos: ImagePosition, + pub palette_rect: PaletteRect, + pub encoding: Encoding, } impl Job { pub fn new(name: String, file_path: PathBuf, image_pos: (u16, u16), encoding: Encoding) -> Job { Job{name, file_path, image_pos: ImagePosition{x: image_pos.0, y: image_pos.1}, palette_rect: PaletteRect::default(), encoding} } - - pub fn add_encoding(&mut self, encoding: Encoding) { - self.encoding = encoding; - } - - pub fn add_palette(&mut self, palette_rect: PaletteRect) { - self.palette_rect = palette_rect; - } } #[derive(Serialize, Deserialize)] pub struct Project { - jobs: Vec + pub jobs: Vec } impl Project { pub fn new(jobs: Vec) -> Project { Project{jobs} } - - pub fn push(&mut self, job: Job) { - self.jobs.push(job); - } - - pub fn len(&self) -> usize { - self.jobs.len() - } } \ No newline at end of file