From bab01bbd869a82a232bdaa03a35e521860c2d274 Mon Sep 17 00:00:00 2001 From: Jaby Date: Tue, 1 Apr 2025 16:03:35 +0200 Subject: [PATCH] Load VRAM images from project file --- .../tim_tool/src/gui/file_tab/callbacks.rs | 51 ++++++++++++++----- src/Tools/tim_tool/src/gui/file_tab/mod.rs | 4 +- src/Tools/tim_tool/src/gui/main_tab/mod.rs | 10 ++-- src/Tools/tim_tool/src/logic/project/mod.rs | 8 ++- 4 files changed, 52 insertions(+), 21 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 8567251d..fec59379 100644 --- a/src/Tools/tim_tool/src/gui/file_tab/callbacks.rs +++ b/src/Tools/tim_tool/src/gui/file_tab/callbacks.rs @@ -4,7 +4,7 @@ use crate::{gui::{self, main_tab::{MainTab, NewVRAMImageInfo}, MutexTIMManager, use super::FileTab; use rfd::FileDialog; use slint::SharedString; -use tim_tool::logic::{project::{ImagePosition, Job, PaletteRect, Project}, tim::types::Encoding}; +use tim_tool::logic::{project::{ImagePosition, Job, PaletteRect, Project}, tim::types::Encoding, TIMManager}; use tool_helper::Error; pub(super) fn on_browse_file(tim_manager: MutexTIMManager) -> impl FnMut(&mut FileTab, &MainWindow) -> Result<(), Error> + 'static { @@ -46,7 +46,7 @@ pub(super) fn on_add_image(tim_manager: MutexTIMManager) -> impl FnMut(&mut File let file_name = file_tab.get_file_name(); let encoding = file_tab.get_encoding()?; - add_unadded_tim(main_tab, tim_manager.clone(), &file_name, encoding)?; + add_unadded_tim(main_tab, &mut tim_manager.lock().expect("VRAM already locked"), UnaddedTIM::new(&file_name, encoding))?; file_tab.clear_load(); main_window.invoke_change_to_main(); @@ -54,8 +54,8 @@ pub(super) fn on_add_image(tim_manager: MutexTIMManager) -> impl FnMut(&mut File }; } -pub(super) fn on_load_project_clicked(tim_manager: MutexTIMManager) -> impl FnMut(&mut FileTab, &MainWindow) -> Result<(), Error> + 'static { - move |_file_tab, main_window| { +pub(super) fn on_load_project_clicked(tim_manager: MutexTIMManager) -> impl FnMut(&mut FileTab, &mut MainTab, &MainWindow) -> Result<(), Error> + 'static { + move |_file_tab, main_tab, main_window| { let open_location = main_window.get_project_widget_open_project_path(); if open_location.is_empty() { return Err(Error::from_str("Please specify location for project file to open")); @@ -70,9 +70,16 @@ pub(super) fn on_load_project_clicked(tim_manager: MutexTIMManager) -> impl FnMu }; for job in new_project.jobs { - let (_, _) = tim_manager.lock().expect("VRAM already locked").load_unadded_tim(&job.file_path)?; + let mut tim_manager = tim_manager.lock().expect("VRAM already locked"); + + let (_, _) = tim_manager.load_unadded_tim(&job.file_path)?; + let (pal_width, pal_height) = job.palette_rect.size.into(); + + tim_manager.change_unadded_tim_palette_size(pal_width, pal_height)?; + add_unadded_tim(main_tab, &mut tim_manager, UnaddedTIM::from_job(&job))?; } + main_window.invoke_change_to_main(); Ok(()) } } @@ -167,15 +174,33 @@ 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 }; +struct UnaddedTIM<'a> { + pub file_name: &'a String, + pub image_x: u16, + pub image_y: u16, + pub palette_x: u16, + pub palette_y: u16, + pub encoding: Encoding, +} + +impl<'a> UnaddedTIM<'a> { + pub fn new(file_name: &String, encoding: Encoding,) -> UnaddedTIM { + UnaddedTIM{file_name, image_x: 0, image_y: 0, palette_x: 0, palette_y: 0, encoding} + } + + pub fn from_job(job: &Job) -> UnaddedTIM { + UnaddedTIM{file_name: &job.name, image_x: job.image_pos.x, image_y: job.image_pos.y, palette_x: job.palette_rect.pos.x, palette_y: job.palette_rect.pos.y, encoding: job.encoding } + } +} + +fn add_unadded_tim(main_tab: &mut MainTab, tim_manager: &mut TIMManager, unadded_tim: UnaddedTIM) -> Result<(), Error> { + let (image, palette_image) = tim_manager.get_converted_unadded_tim_image(unadded_tim.encoding)?; + let (full_image, _) = tim_manager.get_converted_unadded_tim_image(Encoding::FullColor)?; + let image = NewVRAMImageInfo::new(image, unadded_tim.image_x, unadded_tim.image_y, unadded_tim.encoding); + let palette_image = if let Some(palette_image) = palette_image { Some(NewVRAMImageInfo::new(palette_image, unadded_tim.palette_x, unadded_tim.palette_y, 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) { + let images_created = main_tab.add_new_vram_file(unadded_tim.file_name, full_image, image, palette_image); + if let Err(error) = tim_manager.add_unadded_tim(images_created) { main_tab.pop_vram_files(images_created); return Err(error); } 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 cbe7aad6..b5ad144c 100644 --- a/src/Tools/tim_tool/src/gui/file_tab/mod.rs +++ b/src/Tools/tim_tool/src/gui/file_tab/mod.rs @@ -73,9 +73,9 @@ impl FileTab { }); // Project functions - let (cloned_object, cloned_main_window, mut function) = (object.clone(), main_window.clone(), callbacks::on_load_project_clicked(tim_manager.clone())); + let (cloned_object, cloned_main_tab, cloned_main_window, mut function) = (object.clone(), main_tab.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()) { + if let Err(error) = function(&mut cloned_object.borrow_mut(), &mut cloned_main_tab.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 4b9b3cc5..71ed1135 100644 --- a/src/Tools/tim_tool/src/gui/main_tab/mod.rs +++ b/src/Tools/tim_tool/src/gui/main_tab/mod.rs @@ -8,13 +8,13 @@ use tim_tool::logic::tim::types::Encoding; pub struct NewVRAMImageInfo { pub image: slint::Image, - pub x: i32, - pub y: i32, + pub x: u16, + pub y: u16, pub encoding: Encoding, } impl NewVRAMImageInfo { - pub fn new(image: slint::Image, x: i32, y: i32, encoding: Encoding) -> NewVRAMImageInfo { + pub fn new(image: slint::Image, x: u16, y: u16, encoding: Encoding) -> NewVRAMImageInfo { NewVRAMImageInfo{image, x, y, encoding} } } @@ -82,8 +82,8 @@ impl MainTab { image: vram_image.image, }, info: VRAMInfo { - x: vram_image.x, - y: vram_image.y, + x: vram_image.x as i32, + y: vram_image.y as i32, encoding_str: SharedString::from(encoding_str), palette_count, is_palette, diff --git a/src/Tools/tim_tool/src/logic/project/mod.rs b/src/Tools/tim_tool/src/logic/project/mod.rs index 32a7d0a1..32f7b726 100644 --- a/src/Tools/tim_tool/src/logic/project/mod.rs +++ b/src/Tools/tim_tool/src/logic/project/mod.rs @@ -20,7 +20,7 @@ impl std::default::Default for ImagePosition { } } -#[derive(Serialize, Deserialize)] +#[derive(Clone, Copy, Serialize, Deserialize)] pub struct ImageSize { pub width: u16, pub height: u16, @@ -32,6 +32,12 @@ impl ImageSize { } } +impl From for (u16, u16) { + fn from(value: ImageSize) -> Self { + (value.width, value.height) + } +} + impl std::default::Default for ImageSize { fn default() -> Self { ImageSize::new(0, 0)