From aeed26357d204bd15fdbde1f03ffeca3783459cf Mon Sep 17 00:00:00 2001 From: Jaby Date: Wed, 12 Mar 2025 21:29:29 +0100 Subject: [PATCH] Collect all dependecies into an application --- src/Tools/tim_tool/src/main.rs | 191 ++++++++++++++++++--------------- 1 file changed, 105 insertions(+), 86 deletions(-) diff --git a/src/Tools/tim_tool/src/main.rs b/src/Tools/tim_tool/src/main.rs index b2a63aef..a90ed188 100644 --- a/src/Tools/tim_tool/src/main.rs +++ b/src/Tools/tim_tool/src/main.rs @@ -4,107 +4,126 @@ mod gui; use gui::{GUIElements, VRAM_WIDTH, VRAM_HEIGHT, display_information}; use rfd::FileDialog; -use std::{cell::RefCell, rc::Rc}; +use std::{cell::RefCell, rc::Rc, sync::Mutex}; use slint::SharedString; use tim_tool::logic::{tim::types::Encoding, TIMManager}; use tool_helper::Error; slint::include_modules!(); -fn main() -> Result<(), slint::PlatformError> { - let logic_ref = Rc::new(RefCell::new(TIMManager::new())); - let main_window_ref = Rc::new(RefCell::new(MainWindow::new()?)); - let gui_elements_ref = Rc::new(RefCell::new(GUIElements::new(main_window_ref.clone())?)); - - setup_main_tab(gui_elements_ref.clone(), logic_ref.clone()); - setup_file_tab(gui_elements_ref.clone(), logic_ref.clone()); - setup_about_tab(main_window_ref.clone()); - - let main_window = main_window_ref.borrow(); - main_window.run() +struct Application { + main_window: Rc>, + gui_elements: Rc>, + tim_manager: Rc>, } -fn setup_main_tab(gui_elements_ref: Rc>, logic_ref: Rc>) { - let gui_elements = gui_elements_ref.borrow(); +impl Application { + fn new() -> Result { + let main_window = Rc::new(RefCell::new(MainWindow::new()?)); + Ok(Application{ + main_window: main_window.clone(), + gui_elements: Rc::new(RefCell::new(GUIElements::new(main_window.clone())?)), + tim_manager: Rc::new(Mutex::new(TIMManager::new())) + }) + } - gui_elements.main_tab.on_move_vram_image(gui_elements_ref.clone(), move |main_tab, _main_window, idx, dx, dy| { - main_tab.move_vram_image(idx as usize, dx, dy); - }); + fn setup(&self) { + self.setup_main_tab(); + self.setup_file_tab(); + self.setup_about_tab(); + } - let logic = logic_ref.clone(); - gui_elements.main_tab.on_remove_file(gui_elements_ref.clone(), move |main_tab, _main_window, idx| { - if idx >= 0 { - match main_tab.remove_vram_file(idx as usize) { - Ok(range) => logic.borrow_mut().remove_added_tim(range), - Err(error) => display_information("Removing VRAM file", error) + fn run(&self) -> Result<(), slint::PlatformError> { + let main_window = self.main_window.borrow(); + + main_window.run() + } + + fn setup_main_tab(&self) { + self.gui_elements.borrow().main_tab.on_move_vram_image(self.gui_elements.clone(), move |main_tab, _main_window, idx, dx, dy| { + main_tab.move_vram_image(idx as usize, dx, dy); + }); + + let tim_manager = self.tim_manager.clone(); + self.gui_elements.borrow().main_tab.on_remove_file(self.gui_elements.clone(), move |main_tab, _main_window, idx| { + if idx >= 0 { + match main_tab.remove_vram_file(idx as usize) { + Ok(range) => tim_manager.lock().expect("VRAM already locked").remove_added_tim(range), + Err(error) => display_information("Removing VRAM file", error) + } } - } - }); -} - -fn setup_file_tab(gui_elements_ref: Rc>, logic_ref: Rc>) { - let gui_elements = gui_elements_ref.borrow(); - - let logic = logic_ref.clone(); - gui_elements.file_tab.on_update_palette_size(gui_elements_ref.clone(), move |gui_elements, _main_window, width, height| -> Result<(), Error> { - let file_tab = &gui_elements.file_tab; - - file_tab.update_palette(logic.borrow_mut().change_unadded_tim_palette_size(width, height)?); - Ok(()) - }); - - let logic = logic_ref.clone(); - gui_elements.file_tab.on_browse_file(gui_elements_ref.clone(), move |gui_elements, main_window| -> Result<(), Error> { - let file = FileDialog::new() - .add_filter("PNG image (.png)", &["png"]) - .set_title("PNG image file") - .pick_file(); - - if let Some(file) = file { - if let Some(file_path) = file.to_str() { - main_window.set_file_tab_browse_path(SharedString::from(file_path)); - } - + }); + } + + fn setup_file_tab(&self) { + let tim_manager = self.tim_manager.clone(); + self.gui_elements.borrow().file_tab.on_update_palette_size(self.gui_elements.clone(), move |gui_elements, _main_window, width, height| -> Result<(), Error> { let file_tab = &gui_elements.file_tab; - let file_name = if let Some(name) = file.file_name() {Some(name.to_string_lossy().to_string())} else {None}; + + file_tab.update_palette(tim_manager.lock().expect("VRAM already locked").change_unadded_tim_palette_size(width, height)?); + Ok(()) + }); + + let tim_manager = self.tim_manager.clone(); + self.gui_elements.borrow().file_tab.on_browse_file(self.gui_elements.clone(), move |gui_elements, main_window| -> Result<(), Error> { + let file = FileDialog::new() + .add_filter("PNG image (.png)", &["png"]) + .set_title("PNG image file") + .pick_file(); + + if let Some(file) = file { + if let Some(file_path) = file.to_str() { + main_window.set_file_tab_browse_path(SharedString::from(file_path)); + } + + let file_tab = &gui_elements.file_tab; + let file_name = if let Some(name) = file.file_name() {Some(name.to_string_lossy().to_string())} else {None}; + + let (image, palette) = tim_manager.lock().expect("VRAM already locked").load_unadded_tim(&file)?; - let (image, palette) = logic.borrow_mut().load_unadded_tim(&file)?; - - let img_size = image.size(); - if img_size.width > VRAM_WIDTH as u32 || img_size.height > VRAM_HEIGHT as u32 { - return Err(Error::from_text(format!("Image size ({}; {}) is to big for VRAM ({}, {})", img_size.width, img_size.height, VRAM_WIDTH, VRAM_HEIGHT))); + let img_size = image.size(); + if img_size.width > VRAM_WIDTH as u32 || img_size.height > VRAM_HEIGHT as u32 { + return Err(Error::from_text(format!("Image size ({}; {}) is to big for VRAM ({}, {})", img_size.width, img_size.height, VRAM_WIDTH, VRAM_HEIGHT))); + } + return file_tab.update_new_loaded_file(file_name, image, palette); } - return file_tab.update_new_loaded_file(file_name, image, palette); - } - - Ok(()) - }); - - let logic = logic_ref.clone(); - gui_elements.file_tab.on_add_image(gui_elements_ref.clone(), move |gui_elements, main_window| { - let main_tab = &mut gui_elements.main_tab; - let file_tab = &gui_elements.file_tab; - - let file_name = file_tab.get_file_name(); - let encoding = file_tab.get_encoding()?; - let (image, palette_image) = logic.borrow().get_converted_unadded_tim_image(encoding)?; - let (full_image, _) = logic.borrow().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) = logic.borrow_mut().add_unadded_tim(images_created) { - main_tab.pop_vram_files(images_created); - return Err(error); - } - - file_tab.clear_load(); - main_window.invoke_change_to_main(); - Ok(()) - }); + + Ok(()) + }); + + let tim_manager = self.tim_manager.clone(); + self.gui_elements.borrow().file_tab.on_add_image(self.gui_elements.clone(), move |gui_elements, main_window| { + let main_tab = &mut gui_elements.main_tab; + let file_tab = &gui_elements.file_tab; + + 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); + } + + file_tab.clear_load(); + main_window.invoke_change_to_main(); + Ok(()) + }); + } + + fn setup_about_tab(&self) { + const VERSION: &str = env!("CARGO_PKG_VERSION"); + + self.main_window.borrow().invoke_set_version(SharedString::from(VERSION)); + } } -fn setup_about_tab(main_window_ref: Rc>) { - const VERSION: &str = env!("CARGO_PKG_VERSION"); +fn main() -> Result<(), slint::PlatformError> { + let application = Application::new()?; - let main_window = main_window_ref.borrow(); - main_window.invoke_set_version(SharedString::from(VERSION)); + application.setup(); + application.run() } \ No newline at end of file