Rework callbacks
This commit is contained in:
parent
e3a85fc2d9
commit
c114745a8e
|
@ -0,0 +1,63 @@
|
|||
use crate::{gui::{main_tab::MainTab, VRAM_HEIGHT, VRAM_WIDTH},MainWindow};
|
||||
use super::FileTab;
|
||||
use rfd::FileDialog;
|
||||
use slint::SharedString;
|
||||
use std::{rc::Rc, sync::Mutex};
|
||||
use tim_tool::logic::{tim::types::Encoding, TIMManager};
|
||||
use tool_helper::Error;
|
||||
|
||||
pub type MutexTIMManager = Rc<Mutex<TIMManager>>;
|
||||
|
||||
pub fn on_browse_file(tim_manager: MutexTIMManager) -> impl FnMut(&mut FileTab, &MainWindow) -> Result<(), Error> + 'static {
|
||||
return move |file_tab, 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_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 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);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
};
|
||||
}
|
||||
|
||||
pub fn on_update_palette_size(tim_manager: MutexTIMManager) -> impl FnMut(&mut FileTab, &MainWindow, u32, u32) -> Result<(), Error> + 'static {
|
||||
return move |file_tab, _main_window, width, height| -> Result<(), Error> {
|
||||
file_tab.update_palette(tim_manager.lock().expect("VRAM already locked").change_unadded_tim_palette_size(width, height)?);
|
||||
Ok(())
|
||||
};
|
||||
}
|
||||
|
||||
pub fn on_add_image(tim_manager: MutexTIMManager) -> impl FnMut(&mut FileTab, &mut MainTab, &MainWindow) -> Result<(), Error> + 'static {
|
||||
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);
|
||||
}
|
||||
|
||||
file_tab.clear_load();
|
||||
main_window.invoke_change_to_main();
|
||||
Ok(())
|
||||
};
|
||||
}
|
|
@ -1,15 +1,23 @@
|
|||
mod callbacks;
|
||||
|
||||
use crate::MainWindow;
|
||||
use super::main_tab::SharedMainTab;
|
||||
use super::{GUIElements, GUIElementsRef, MainWindowRef, display_error};
|
||||
use slint::{Image, SharedString};
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
use tim_tool::logic::tim::types::Encoding;
|
||||
use tool_helper::Error;
|
||||
|
||||
pub use callbacks::MutexTIMManager;
|
||||
|
||||
pub struct FileTab {
|
||||
main_window: MainWindowRef,
|
||||
encoding_options: Rc<slint::VecModel<SharedString>>
|
||||
}
|
||||
|
||||
pub type SharedFileTab = Rc<RefCell<FileTab>>;
|
||||
|
||||
impl FileTab {
|
||||
fn update_encoding_options(&self, width: u32, height: u32, has_palette: bool) -> Result<&str, Error> {
|
||||
self.encoding_options.clear();
|
||||
|
@ -40,11 +48,34 @@ impl FileTab {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn new(main_window: MainWindowRef) -> FileTab {
|
||||
pub fn new(main_window: MainWindowRef, main_tab: SharedMainTab, tim_manager: MutexTIMManager) -> SharedFileTab {
|
||||
let encoding_options = Rc::new(slint::VecModel::from(Vec::<SharedString>::new()));
|
||||
|
||||
main_window.borrow().set_file_tab_encoding_options(encoding_options.clone().into());
|
||||
FileTab{main_window, encoding_options}
|
||||
|
||||
let object = Rc::new(RefCell::new(FileTab{main_window: main_window.clone(), encoding_options}));
|
||||
|
||||
let (cloned_object, cloned_main_window, mut function) = (object.clone(), main_window.clone(), callbacks::on_browse_file(tim_manager.clone()));
|
||||
main_window.borrow().on_file_tab_browse_convert_image(move || {
|
||||
if let Err(error) = function(&mut cloned_object.borrow_mut(), &cloned_main_window.borrow()) {
|
||||
display_error("Loading file failed", &error.to_string());
|
||||
}
|
||||
});
|
||||
|
||||
let (cloned_object, cloned_main_window, mut function) = (object.clone(), main_window.clone(), callbacks::on_update_palette_size(tim_manager.clone()));
|
||||
main_window.borrow().on_file_tab_update_palette_size(move |width, height| {
|
||||
if let Err(error) = function(&mut cloned_object.borrow_mut(), &cloned_main_window.borrow(), width as u32, height as u32) {
|
||||
display_error("Loadind file failed", &error.to_string());
|
||||
}
|
||||
});
|
||||
|
||||
let (cloned_object, cloned_main_window, mut function) = (object.clone(), main_window.clone(), callbacks::on_add_image(tim_manager.clone()));
|
||||
main_window.borrow().on_file_tab_add_convert_image(move || {
|
||||
if let Err(error) = function(&mut cloned_object.borrow_mut(), &mut main_tab.borrow_mut(), &cloned_main_window.borrow()) {
|
||||
display_error("Adding file failed", &error.to_string());
|
||||
}
|
||||
});
|
||||
|
||||
object
|
||||
}
|
||||
|
||||
pub fn clear_load(&self) {
|
||||
|
@ -128,35 +159,4 @@ impl FileTab {
|
|||
Err(Error::from_str("Failed to obtain encoding"))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn on_browse_file(&self, gui_elements: GUIElementsRef, mut function: impl FnMut(&mut GUIElements, &MainWindow) -> Result<(), Error> + 'static) {
|
||||
let main_window_cloned = self.main_window.clone();
|
||||
let gui_cloned = gui_elements.clone();
|
||||
|
||||
self.main_window.borrow().on_file_tab_browse_convert_image(move || {
|
||||
if let Err(error) = function(&mut gui_cloned.borrow_mut(), &main_window_cloned.borrow()) {
|
||||
display_error("Loadind file failed", &error.to_string());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub fn on_update_palette_size(&self, gui_elements: GUIElementsRef, mut function: impl FnMut(&mut GUIElements, &MainWindow, u32, u32) -> Result<(), Error> + 'static) {
|
||||
let main_window_cloned = self.main_window.clone();
|
||||
|
||||
self.main_window.borrow().on_file_tab_update_palette_size(move |width, height| {
|
||||
if let Err(error) = function(&mut gui_elements.borrow_mut(), &main_window_cloned.borrow(), width as u32, height as u32) {
|
||||
display_error("Loadind file failed", &error.to_string());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub fn on_add_image(&self, gui_elements: GUIElementsRef, mut function: impl FnMut(&mut GUIElements, &MainWindow) -> Result<(), Error> + 'static) {
|
||||
let main_window_cloned = self.main_window.clone();
|
||||
|
||||
self.main_window.borrow().on_file_tab_add_convert_image(move || {
|
||||
if let Err(error) = function(&mut gui_elements.borrow_mut(), &main_window_cloned.borrow()) {
|
||||
display_error("Adding file failed", &error.to_string());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
|
@ -1,17 +1,25 @@
|
|||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
use super::create_vram_bg;
|
||||
|
||||
use super::main_tab::SharedMainTab;
|
||||
use super::MainWindowRef;
|
||||
use super::{file_tab::FileTab, main_tab::MainTab};
|
||||
use super::{file_tab::{FileTab, SharedFileTab, MutexTIMManager}, main_tab::MainTab};
|
||||
|
||||
pub struct GUIElements {
|
||||
pub file_tab: FileTab,
|
||||
pub main_tab: MainTab,
|
||||
pub file_tab: SharedFileTab,
|
||||
pub main_tab: SharedMainTab,
|
||||
}
|
||||
|
||||
impl GUIElements {
|
||||
pub fn new(main_window: MainWindowRef) -> Result<GUIElements, slint::PlatformError> {
|
||||
pub fn new(main_window: MainWindowRef, tim_manager: MutexTIMManager) -> Result<GUIElements, slint::PlatformError> {
|
||||
main_window.borrow().set_main_tab_vram_bg(GUIElements::create_bg()?);
|
||||
Ok(GUIElements{file_tab: FileTab::new(main_window.clone()), main_tab: MainTab::new(main_window.clone())})
|
||||
|
||||
let main_tab = MainTab::new(main_window.clone(), tim_manager.clone());
|
||||
let file_tab = FileTab::new(main_window.clone(), main_tab.clone(), tim_manager);
|
||||
|
||||
Ok(GUIElements{file_tab, main_tab})
|
||||
}
|
||||
|
||||
fn create_bg() -> Result<slint::Image, slint::PlatformError> {
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
use crate::{gui::{VRAM_HEIGHT, VRAM_WIDTH}, MainWindow, display_information};
|
||||
use super::MainTab;
|
||||
use std::{rc::Rc, sync::Mutex};
|
||||
use tim_tool::logic::{tim::types::Encoding, TIMManager};
|
||||
|
||||
pub type MutexTIMManager = Rc<Mutex<TIMManager>>;
|
||||
|
||||
pub fn on_move_vram_image() -> impl FnMut(&mut MainTab, &MainWindow, i32, i32, i32) + 'static {
|
||||
return move |main_tab, _main_window, idx, dx, dy| {
|
||||
main_tab.move_vram_image(idx as usize, dx, dy);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn on_remove_file(tim_manager: MutexTIMManager) -> impl FnMut(&mut MainTab, &MainWindow, i32) + 'static {
|
||||
return 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,9 +1,11 @@
|
|||
mod callbacks;
|
||||
|
||||
use crate::{gui::{VRAM_HEIGHT, VRAM_WIDTH}, MainWindow, VRAMImage};
|
||||
use super::{GUIElementsRef, MainWindowRef};
|
||||
|
||||
use slint::{Model, SharedString};
|
||||
use tim_tool::logic::tim::types::Encoding;
|
||||
use std::{ops::RangeInclusive, rc::Rc, sync::{Arc, Mutex}};
|
||||
use std::{cell::RefCell, ops::RangeInclusive, rc::Rc, sync::{Arc, Mutex}};
|
||||
|
||||
struct VRAM {
|
||||
count: usize,
|
||||
|
@ -34,8 +36,10 @@ pub struct MainTab {
|
|||
vram: Arc<Mutex<VRAM>>,
|
||||
}
|
||||
|
||||
pub type SharedMainTab = Rc<RefCell<MainTab>>;
|
||||
|
||||
impl MainTab {
|
||||
pub fn new(main_window: MainWindowRef) -> MainTab {
|
||||
pub fn new(main_window: MainWindowRef, tim_manager: callbacks::MutexTIMManager) -> SharedMainTab {
|
||||
let vram_file_list:Vec<slint::StandardListViewItem> = main_window.borrow().get_main_tab_vram_file_list().iter().collect();
|
||||
let vram_file_list = Rc::new(slint::VecModel::from(vram_file_list));
|
||||
let vram_image_list = Rc::new(slint::VecModel::from(Vec::<VRAMImage>::new()));
|
||||
|
@ -43,7 +47,18 @@ impl MainTab {
|
|||
main_window.borrow().set_main_tab_vram_file_list(vram_file_list.clone().into());
|
||||
main_window.borrow().set_main_tab_vram_images(vram_image_list.clone().into());
|
||||
|
||||
MainTab{main_window, vram: Arc::new(Mutex::new(VRAM{count: 0, file_list: vram_file_list, image_list: vram_image_list}))}
|
||||
let object = SharedMainTab::new(MainTab{main_window: main_window.clone(), vram: Arc::new(Mutex::new(VRAM{count: 0, file_list: vram_file_list, image_list: vram_image_list}))}.into());
|
||||
|
||||
let (cloned_object, cloned_main_window, mut function) = (object.clone(), main_window.clone(), callbacks::on_move_vram_image());
|
||||
main_window.borrow().on_move_vram_image(move |idx, dx, dy| {
|
||||
function(&mut cloned_object.borrow_mut(), &cloned_main_window.borrow(), idx, dx, dy);
|
||||
});
|
||||
|
||||
let (cloned_object, cloned_main_window, mut function) = (object.clone(), main_window.clone(), callbacks::on_remove_file(tim_manager));
|
||||
main_window.borrow().on_main_tab_remove_file_clicked(move |idx| {
|
||||
function(&mut cloned_object.borrow_mut(), &cloned_main_window.borrow(), idx);
|
||||
});
|
||||
object
|
||||
}
|
||||
|
||||
pub fn add_new_vram_file(&mut self, file_name: &String, full_image: slint::Image, image: slint::Image, encoding: Encoding, image_palette: Option<slint::Image>) -> usize {
|
||||
|
@ -137,20 +152,4 @@ impl MainTab {
|
|||
vram_data.image_list.set_row_data(idx, vram_info);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn on_move_vram_image(&self, gui_elements: GUIElementsRef, mut function: impl FnMut(&mut MainTab, &MainWindow, i32, i32, i32) + 'static) {
|
||||
let main_window_cloned = self.main_window.clone();
|
||||
|
||||
self.main_window.borrow().on_move_vram_image(move |idx, dx, dy| {
|
||||
function(&mut gui_elements.borrow_mut().main_tab, &main_window_cloned.borrow(), idx, dx, dy);
|
||||
});
|
||||
}
|
||||
|
||||
pub fn on_remove_file(&self, gui_elements: GUIElementsRef, mut function: impl FnMut(&mut MainTab, &MainWindow, i32) + 'static) {
|
||||
let main_window_cloned = self.main_window.clone();
|
||||
|
||||
self.main_window.borrow().on_main_tab_remove_file_clicked(move |idx| {
|
||||
function(&mut gui_elements.borrow_mut().main_tab, &main_window_cloned.borrow(), idx);
|
||||
});
|
||||
}
|
||||
}
|
|
@ -20,16 +20,16 @@ struct Application {
|
|||
impl Application {
|
||||
fn new() -> Result<Application, slint::PlatformError> {
|
||||
let main_window = Rc::new(RefCell::new(MainWindow::new()?));
|
||||
let tim_manager = Rc::new(Mutex::new(TIMManager::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: Rc::new(RefCell::new(GUIElements::new(main_window.clone(), tim_manager.clone())?)),
|
||||
tim_manager: tim_manager,
|
||||
})
|
||||
}
|
||||
|
||||
fn setup(&self) {
|
||||
self.setup_main_tab();
|
||||
self.setup_file_tab();
|
||||
self.setup_about_tab();
|
||||
}
|
||||
|
||||
|
@ -38,81 +38,6 @@ impl Application {
|
|||
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(&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;
|
||||
|
||||
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 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);
|
||||
}
|
||||
|
||||
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");
|
||||
|
||||
|
|
Loading…
Reference in New Issue