Collect all dependecies into an application

This commit is contained in:
Jaby 2025-03-12 21:29:29 +01:00
parent af2526d829
commit aeed26357d
1 changed files with 105 additions and 86 deletions

View File

@ -4,57 +4,68 @@
mod gui; mod gui;
use gui::{GUIElements, VRAM_WIDTH, VRAM_HEIGHT, display_information}; use gui::{GUIElements, VRAM_WIDTH, VRAM_HEIGHT, display_information};
use rfd::FileDialog; use rfd::FileDialog;
use std::{cell::RefCell, rc::Rc}; use std::{cell::RefCell, rc::Rc, sync::Mutex};
use slint::SharedString; use slint::SharedString;
use tim_tool::logic::{tim::types::Encoding, TIMManager}; use tim_tool::logic::{tim::types::Encoding, TIMManager};
use tool_helper::Error; use tool_helper::Error;
slint::include_modules!(); slint::include_modules!();
fn main() -> Result<(), slint::PlatformError> { struct Application {
let logic_ref = Rc::new(RefCell::new(TIMManager::new())); main_window: Rc<RefCell<MainWindow>>,
let main_window_ref = Rc::new(RefCell::new(MainWindow::new()?)); gui_elements: Rc<RefCell<GUIElements>>,
let gui_elements_ref = Rc::new(RefCell::new(GUIElements::new(main_window_ref.clone())?)); tim_manager: Rc<Mutex<TIMManager>>,
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()
} }
fn setup_main_tab(gui_elements_ref: Rc<RefCell<GUIElements>>, logic_ref: Rc<RefCell<TIMManager>>) { impl Application {
let gui_elements = gui_elements_ref.borrow(); fn new() -> Result<Application, slint::PlatformError> {
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| { fn setup(&self) {
self.setup_main_tab();
self.setup_file_tab();
self.setup_about_tab();
}
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); main_tab.move_vram_image(idx as usize, dx, dy);
}); });
let logic = logic_ref.clone(); let tim_manager = self.tim_manager.clone();
gui_elements.main_tab.on_remove_file(gui_elements_ref.clone(), move |main_tab, _main_window, idx| { self.gui_elements.borrow().main_tab.on_remove_file(self.gui_elements.clone(), move |main_tab, _main_window, idx| {
if idx >= 0 { if idx >= 0 {
match main_tab.remove_vram_file(idx as usize) { match main_tab.remove_vram_file(idx as usize) {
Ok(range) => logic.borrow_mut().remove_added_tim(range), Ok(range) => tim_manager.lock().expect("VRAM already locked").remove_added_tim(range),
Err(error) => display_information("Removing VRAM file", error) Err(error) => display_information("Removing VRAM file", error)
} }
} }
}); });
} }
fn setup_file_tab(gui_elements_ref: Rc<RefCell<GUIElements>>, logic_ref: Rc<RefCell<TIMManager>>) { fn setup_file_tab(&self) {
let gui_elements = gui_elements_ref.borrow(); 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 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; let file_tab = &gui_elements.file_tab;
file_tab.update_palette(logic.borrow_mut().change_unadded_tim_palette_size(width, height)?); file_tab.update_palette(tim_manager.lock().expect("VRAM already locked").change_unadded_tim_palette_size(width, height)?);
Ok(()) Ok(())
}); });
let logic = logic_ref.clone(); let tim_manager = self.tim_manager.clone();
gui_elements.file_tab.on_browse_file(gui_elements_ref.clone(), move |gui_elements, main_window| -> Result<(), Error> { self.gui_elements.borrow().file_tab.on_browse_file(self.gui_elements.clone(), move |gui_elements, main_window| -> Result<(), Error> {
let file = FileDialog::new() let file = FileDialog::new()
.add_filter("PNG image (.png)", &["png"]) .add_filter("PNG image (.png)", &["png"])
.set_title("PNG image file") .set_title("PNG image file")
@ -68,7 +79,7 @@ fn setup_file_tab(gui_elements_ref: Rc<RefCell<GUIElements>>, logic_ref: Rc<RefC
let file_tab = &gui_elements.file_tab; 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 file_name = if let Some(name) = file.file_name() {Some(name.to_string_lossy().to_string())} else {None};
let (image, palette) = logic.borrow_mut().load_unadded_tim(&file)?; let (image, palette) = tim_manager.lock().expect("VRAM already locked").load_unadded_tim(&file)?;
let img_size = image.size(); let img_size = image.size();
if img_size.width > VRAM_WIDTH as u32 || img_size.height > VRAM_HEIGHT as u32 { if img_size.width > VRAM_WIDTH as u32 || img_size.height > VRAM_HEIGHT as u32 {
@ -80,18 +91,19 @@ fn setup_file_tab(gui_elements_ref: Rc<RefCell<GUIElements>>, logic_ref: Rc<RefC
Ok(()) Ok(())
}); });
let logic = logic_ref.clone(); let tim_manager = self.tim_manager.clone();
gui_elements.file_tab.on_add_image(gui_elements_ref.clone(), move |gui_elements, main_window| { 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 main_tab = &mut gui_elements.main_tab;
let file_tab = &gui_elements.file_tab; let file_tab = &gui_elements.file_tab;
let file_name = file_tab.get_file_name(); let file_name = file_tab.get_file_name();
let encoding = file_tab.get_encoding()?; let encoding = file_tab.get_encoding()?;
let (image, palette_image) = logic.borrow().get_converted_unadded_tim_image(encoding)?; let mut tim_mgr = tim_manager.lock().expect("VRAM already locked");
let (full_image, _) = logic.borrow().get_converted_unadded_tim_image(Encoding::FullColor)?; 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); 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) { if let Err(error) = tim_mgr.add_unadded_tim(images_created) {
main_tab.pop_vram_files(images_created); main_tab.pop_vram_files(images_created);
return Err(error); return Err(error);
} }
@ -100,11 +112,18 @@ fn setup_file_tab(gui_elements_ref: Rc<RefCell<GUIElements>>, logic_ref: Rc<RefC
main_window.invoke_change_to_main(); main_window.invoke_change_to_main();
Ok(()) Ok(())
}); });
} }
fn setup_about_tab(main_window_ref: Rc<RefCell<MainWindow>>) { fn setup_about_tab(&self) {
const VERSION: &str = env!("CARGO_PKG_VERSION"); const VERSION: &str = env!("CARGO_PKG_VERSION");
let main_window = main_window_ref.borrow(); self.main_window.borrow().invoke_set_version(SharedString::from(VERSION));
main_window.invoke_set_version(SharedString::from(VERSION)); }
}
fn main() -> Result<(), slint::PlatformError> {
let application = Application::new()?;
application.setup();
application.run()
} }