99 lines
3.9 KiB
Rust
99 lines
3.9 KiB
Rust
use crate::{gui::{VRAM_HEIGHT, VRAM_WIDTH}, MainWindow, VRAMImage};
|
|
use super::{GUIElementsRef, MainWindowRef};
|
|
|
|
use slint::Model;
|
|
use std::{rc::Rc, sync::{Arc, Mutex}};
|
|
|
|
pub struct MainTab {
|
|
main_window: MainWindowRef,
|
|
mtx: Arc<Mutex<i32>>,
|
|
vram_file_list: Rc<slint::VecModel<slint::StandardListViewItem>>,
|
|
vram_image_list: Rc<slint::VecModel<VRAMImage>>,
|
|
}
|
|
|
|
impl MainTab {
|
|
pub fn new(main_window: MainWindowRef) -> MainTab {
|
|
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()));
|
|
|
|
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, mtx: Arc::new(Mutex::new(0)), vram_file_list, vram_image_list}
|
|
}
|
|
|
|
pub fn add_new_vram_file(&mut self, file_name: &String, image: slint::Image, image_palette: Option<slint::Image>) {
|
|
let add_new_image = |file_name: &String, image: slint::Image| {
|
|
let vram_image = VRAMImage{
|
|
img: image,
|
|
x: 0,
|
|
y: 0,
|
|
palette_count: 0,
|
|
is_palette: false,
|
|
};
|
|
|
|
self.vram_file_list.push(slint::StandardListViewItem::from(file_name.as_str()));
|
|
self.vram_image_list.push(vram_image);
|
|
};
|
|
|
|
let _lock = self.mtx.lock().unwrap();
|
|
add_new_image(file_name, image);
|
|
}
|
|
|
|
pub fn remove_vram_file(&mut self, idx: usize) -> bool {
|
|
let _lock = self.mtx.lock().unwrap();
|
|
|
|
if let Some(element) = self.vram_image_list.iter().skip(idx).next() {
|
|
if element.is_palette {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
self.vram_file_list.remove(idx);
|
|
self.vram_image_list.remove(idx);
|
|
return true;
|
|
}
|
|
|
|
pub fn move_vram_image(&mut self, idx: usize, dx: i32, dy: i32) {
|
|
if let Some(mut vram_info) = self.vram_image_list.row_data(idx) {
|
|
vram_info.x += dx;
|
|
vram_info.y += dy;
|
|
|
|
if vram_info.x < 0 {
|
|
vram_info.x = 0;
|
|
}
|
|
|
|
if vram_info.y < 0 {
|
|
vram_info.y = 0;
|
|
}
|
|
|
|
let (vram_img_width, vram_img_height) = (vram_info.img.size().width as i32, vram_info.img.size().height as i32);
|
|
if (vram_info.x + vram_img_width) > VRAM_WIDTH as i32 {
|
|
vram_info.x = VRAM_WIDTH as i32 - vram_img_width;
|
|
}
|
|
|
|
if (vram_info.y + vram_img_height) > VRAM_HEIGHT as i32 {
|
|
vram_info.y = VRAM_HEIGHT as i32 - vram_img_height;
|
|
}
|
|
|
|
self.vram_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();
|
|
let gui_cloned = gui_elements.clone();
|
|
self.main_window.borrow().on_move_vram_image(move |idx, dx, dy| {
|
|
function(&mut gui_cloned.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();
|
|
let gui_cloned = gui_elements.clone();
|
|
self.main_window.borrow().on_main_tab_remove_file_clicked(move |idx| {
|
|
function(&mut gui_cloned.borrow_mut().main_tab, &main_window_cloned.borrow(), idx);
|
|
});
|
|
}
|
|
} |