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,107 +4,126 @@
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) {
main_tab.move_vram_image(idx as usize, dx, dy); self.setup_main_tab();
}); self.setup_file_tab();
self.setup_about_tab();
}
let logic = logic_ref.clone(); fn run(&self) -> Result<(), slint::PlatformError> {
gui_elements.main_tab.on_remove_file(gui_elements_ref.clone(), move |main_tab, _main_window, idx| { let main_window = self.main_window.borrow();
if idx >= 0 {
match main_tab.remove_vram_file(idx as usize) { main_window.run()
Ok(range) => logic.borrow_mut().remove_added_tim(range), }
Err(error) => display_information("Removing VRAM file", error)
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(&self) {
fn setup_file_tab(gui_elements_ref: Rc<RefCell<GUIElements>>, logic_ref: Rc<RefCell<TIMManager>>) { let tim_manager = self.tim_manager.clone();
let gui_elements = gui_elements_ref.borrow(); 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;
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));
}
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};
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 {
let img_size = image.size(); return Err(Error::from_text(format!("Image size ({}; {}) is to big for VRAM ({}, {})", img_size.width, img_size.height, VRAM_WIDTH, VRAM_HEIGHT)));
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(())
});
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 logic = logic_ref.clone(); let main_tab = &mut gui_elements.main_tab;
gui_elements.file_tab.on_add_image(gui_elements_ref.clone(), move |gui_elements, main_window| { let file_tab = &gui_elements.file_tab;
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 file_name = file_tab.get_file_name(); let mut tim_mgr = tim_manager.lock().expect("VRAM already locked");
let encoding = file_tab.get_encoding()?; let (image, palette_image) = tim_mgr.get_converted_unadded_tim_image(encoding)?;
let (image, palette_image) = logic.borrow().get_converted_unadded_tim_image(encoding)?; let (full_image, _) = tim_mgr.get_converted_unadded_tim_image(Encoding::FullColor)?;
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);
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) {
if let Err(error) = logic.borrow_mut().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); }
}
file_tab.clear_load();
file_tab.clear_load(); main_window.invoke_change_to_main();
main_window.invoke_change_to_main(); Ok(())
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<RefCell<MainWindow>>) { fn main() -> Result<(), slint::PlatformError> {
const VERSION: &str = env!("CARGO_PKG_VERSION"); let application = Application::new()?;
let main_window = main_window_ref.borrow(); application.setup();
main_window.invoke_set_version(SharedString::from(VERSION)); application.run()
} }