Various improvements; Logic is now being reported about added and removed elements to be in sync with GUI

This commit is contained in:
Jaby 2025-03-08 17:04:26 +01:00
parent 5952b97b6c
commit 33e7b23ab2
6 changed files with 127 additions and 61 deletions

View File

@ -2,7 +2,7 @@ use crate::MainWindow;
use super::{GUIElements, GUIElementsRef, MainWindowRef, display_error}; use super::{GUIElements, GUIElementsRef, MainWindowRef, display_error};
use slint::{Image, SharedString}; use slint::{Image, SharedString};
use std::rc::Rc; use std::rc::Rc;
use tim_tool::logic::tim::Encoding; use tim_tool::logic::tim::types::Encoding;
use tool_helper::Error; use tool_helper::Error;
pub struct FileTab { pub struct FileTab {

View File

@ -2,12 +2,31 @@ use crate::{gui::{VRAM_HEIGHT, VRAM_WIDTH}, MainWindow, VRAMImage};
use super::{GUIElementsRef, MainWindowRef}; use super::{GUIElementsRef, MainWindowRef};
use slint::{Model, SharedString}; use slint::{Model, SharedString};
use tim_tool::logic::tim::Encoding; use tim_tool::logic::tim::types::Encoding;
use std::{rc::Rc, sync::{Arc, Mutex}}; use std::{ops::RangeInclusive, rc::Rc, sync::{Arc, Mutex}};
struct VRAM { struct VRAM {
pub file_list: Rc<slint::VecModel<slint::StandardListViewItem>>, count: usize,
pub image_list: Rc<slint::VecModel<VRAMImage>> file_list: Rc<slint::VecModel<slint::StandardListViewItem>>,
image_list: Rc<slint::VecModel<VRAMImage>>
}
impl VRAM {
pub fn push(&mut self, name: slint::StandardListViewItem, image: VRAMImage) {
self.file_list.push(name);
self.image_list.push(image);
self.count += 1;
}
pub fn remove(&mut self, idx: usize) {
self.count -= 1;
self.file_list.remove(idx);
self.image_list.remove(idx);
}
pub fn pop(&mut self) {
self.remove(self.count - 1);
}
} }
pub struct MainTab { pub struct MainTab {
@ -24,12 +43,12 @@ impl MainTab {
main_window.borrow().set_main_tab_vram_file_list(vram_file_list.clone().into()); 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()); main_window.borrow().set_main_tab_vram_images(vram_image_list.clone().into());
MainTab{main_window, vram: Arc::new(Mutex::new(VRAM{file_list: vram_file_list, image_list: vram_image_list}))} MainTab{main_window, vram: Arc::new(Mutex::new(VRAM{count: 0, file_list: vram_file_list, image_list: vram_image_list}))}
} }
pub fn add_new_vram_file(&mut self, file_name: &String, image: slint::Image, encoding: Encoding, image_palette: Option<slint::Image>) { pub fn add_new_vram_file(&mut self, file_name: &String, image: slint::Image, encoding: Encoding, image_palette: Option<slint::Image>) -> usize {
let vram_data = self.vram.lock().expect("VRAM already locked"); let mut vram_data = self.vram.lock().expect("VRAM already locked");
let add_new_image = |file_name: &String, image: slint::Image, encoding: Encoding, palette_count: i32, is_palette: bool| { let mut add_new_image = |file_name: &String, image: slint::Image, encoding: Encoding, palette_count: i32, is_palette: bool| {
let encoding_str = if is_palette {"<Palette>"} else {encoding.to_str()}; let encoding_str = if is_palette {"<Palette>"} else {encoding.to_str()};
let vram_image = VRAMImage{ let vram_image = VRAMImage{
img: image, img: image,
@ -40,28 +59,27 @@ impl MainTab {
is_palette, is_palette,
}; };
vram_data.file_list.push(slint::StandardListViewItem::from(file_name.as_str())); vram_data.push(slint::StandardListViewItem::from(file_name.as_str()), vram_image);
vram_data.image_list.push(vram_image);
}; };
let mut images_added = 1;
add_new_image(file_name, image, encoding, if image_palette.is_some() {1} else {0}, false); add_new_image(file_name, image, encoding, if image_palette.is_some() {1} else {0}, false);
if let Some(image_palette) = image_palette { if let Some(image_palette) = image_palette {
let file_name = " => ".to_owned() + file_name.as_str() + " (Palette)"; let file_name = " => ".to_owned() + file_name.as_str() + " (Palette)";
images_added += 1;
add_new_image(&file_name, image_palette, encoding, 0, true); add_new_image(&file_name, image_palette, encoding, 0, true);
} }
images_added
} }
pub fn remove_vram_file(&mut self, idx: usize) -> bool { pub fn remove_vram_file(&mut self, idx: usize) -> Result<RangeInclusive<usize>, &'static str> {
let vram_data = self.vram.lock().expect("VRAM already locked"); let mut vram_data = self.vram.lock().expect("VRAM already locked");
let remove_image = |idx: usize| {
vram_data.file_list.remove(idx);
vram_data.image_list.remove(idx);
};
let extras = { let extras = {
if let Some(element) = vram_data.image_list.iter().skip(idx).next() { if let Some(element) = vram_data.image_list.iter().skip(idx).next() {
if element.is_palette { if element.is_palette {
return false; return Err("Can not remove palette. Delete image instead");
} }
else { else {
@ -74,10 +92,22 @@ impl MainTab {
} }
}; };
for _ in idx..=idx+extras { let range = idx..=idx+extras;
let mut remove_image = |idx: usize| {
vram_data.remove(idx);
};
for _ in range.clone() {
remove_image(idx); remove_image(idx);
} }
return true; return Ok(range);
}
pub fn pop_vram_files(&mut self, count: usize) {
let mut vram_data = self.vram.lock().expect("VRAM already locked");
for _ in 0..count {
vram_data.pop();
}
} }
pub fn move_vram_image(&mut self, idx: usize, dx: i32, dy: i32) { pub fn move_vram_image(&mut self, idx: usize, dx: i32, dy: i32) {

View File

@ -1,20 +1,27 @@
pub mod tim; pub mod tim;
use std::{ops::RangeInclusive, path::PathBuf};
use std::path::PathBuf;
use slint::Image; use slint::Image;
use tim::{Encoding, TIMInfo}; use tim::{types::Encoding, TIMInfo};
use tool_helper::Error; use tool_helper::Error;
pub struct Logic { pub struct Logic {
added_tims: Vec<Option<TIMInfo>>,
unadded_tim: Option<TIMInfo>, unadded_tim: Option<TIMInfo>,
} }
impl Logic { impl Logic {
pub fn new() -> Logic { pub fn new() -> Logic {
Logic{unadded_tim: None} Logic{added_tims: Default::default(), unadded_tim: None}
} }
pub fn set_unadded_tim(&mut self, path: &PathBuf) -> Result<(Image, Option<Image>), Error> { pub fn remove_added_tim(&mut self, range: RangeInclusive<usize>) {
let idx = *range.start();
for _ in range {
self.added_tims.remove(idx);
}
}
pub fn load_unadded_tim(&mut self, path: &PathBuf) -> Result<(Image, Option<Image>), Error> {
let tim_info = TIMInfo::from_image(path)?; let tim_info = TIMInfo::from_image(path)?;
let image = tim_info.get_slint_images(Encoding::FullColor); let image = tim_info.get_slint_images(Encoding::FullColor);
@ -22,11 +29,9 @@ impl Logic {
Ok(image) Ok(image)
} }
pub fn add_unadded_tim_as(&mut self, _name: &String, encoding: Encoding) -> Result<(Image, Option<Image>), Error> { pub fn get_converted_unadded_tim_image(&self, encoding: Encoding) -> Result<(Image, Option<Image>), Error> {
if let Some(unadded_tim) = &self.unadded_tim { if let Some(unadded_tim) = &self.unadded_tim {
let (image, palette) = unadded_tim.get_slint_images(encoding); let (image, palette) = unadded_tim.get_slint_images(encoding);
self.unadded_tim = None;
Ok((image, palette)) Ok((image, palette))
} }
@ -35,6 +40,27 @@ impl Logic {
} }
} }
pub fn add_unadded_tim(&mut self, images_added: usize) -> Result<(), Error> {
if let Some(_) = &self.unadded_tim {
if images_added >= 1 {
self.added_tims.push(std::mem::replace(&mut self.unadded_tim, None));
for _ in 0..(images_added - 1) {
self.added_tims.push(None);
}
Ok(())
}
else {
Err(Error::from_str("Can not add 0 images"))
}
}
else {
Err(Error::from_str("No data found for loaded image"))
}
}
pub fn change_unadded_tim_palette_size(&mut self, width: u32, height: u32) -> Result<Option<Image>, Error> { pub fn change_unadded_tim_palette_size(&mut self, width: u32, height: u32) -> Result<Option<Image>, Error> {
if let Some(unadded_tim) = &mut self.unadded_tim { if let Some(unadded_tim) = &mut self.unadded_tim {
unadded_tim.change_palette_size(width, height) unadded_tim.change_palette_size(width, height)

View File

@ -1,29 +1,12 @@
pub mod types;
use std::{fs::File, path::PathBuf}; use std::{fs::File, path::PathBuf};
use slint::{Rgba8Pixel, SharedPixelBuffer}; use slint::{Rgba8Pixel, SharedPixelBuffer};
use tool_helper::Error; use tool_helper::Error;
use types::Encoding;
#[derive(Clone, Copy)]
pub enum Encoding {
FourBit,
EightBit,
FullColor,
}
impl Encoding {
const FOUR_BIT_NAME:&str = "4 bit";
const EIGHT_BIT_NAME:&str = "8 bit";
const FULL_COLOR_NAME:&str = "Full color";
pub const fn to_str(&self) -> &str {
match self {
Encoding::FourBit => Self::FOUR_BIT_NAME,
Encoding::EightBit => Self::EIGHT_BIT_NAME,
Encoding::FullColor => Self::FULL_COLOR_NAME,
}
}
}
pub struct TIMInfo { pub struct TIMInfo {
_path: PathBuf,
image_data: SharedPixelBuffer<Rgba8Pixel>, image_data: SharedPixelBuffer<Rgba8Pixel>,
palette: Option<PaletteInfo>, palette: Option<PaletteInfo>,
} }
@ -69,7 +52,7 @@ impl TIMInfo {
_ => {return Err(Error::from_str("Only 4 and 8bit color depth are supported for indexed color images"));} _ => {return Err(Error::from_str("Only 4 and 8bit color depth are supported for indexed color images"));}
} }
} }
Ok(TIMInfo{image_data, palette: Some(PaletteInfo::new(palette_colors))}) Ok(TIMInfo{_path: path.clone(), image_data, palette: Some(PaletteInfo::new(palette_colors))})
} }
else { else {
@ -87,7 +70,7 @@ impl TIMInfo {
_ => {return Err(Error::from_str("Only 8bit color depth are supported for direct color images"));} _ => {return Err(Error::from_str("Only 8bit color depth are supported for direct color images"));}
} }
} }
Ok(TIMInfo{image_data, palette: None}) Ok(TIMInfo{_path: path.clone(), image_data, palette: None})
} }
} }

View File

@ -0,0 +1,20 @@
#[derive(Clone, Copy)]
pub enum Encoding {
FourBit,
EightBit,
FullColor,
}
impl Encoding {
const FOUR_BIT_NAME:&str = "4 bit";
const EIGHT_BIT_NAME:&str = "8 bit";
const FULL_COLOR_NAME:&str = "Full color";
pub const fn to_str(&self) -> &str {
match self {
Encoding::FourBit => Self::FOUR_BIT_NAME,
Encoding::EightBit => Self::EIGHT_BIT_NAME,
Encoding::FullColor => Self::FULL_COLOR_NAME,
}
}
}

View File

@ -17,24 +17,26 @@ fn main() -> Result<(), slint::PlatformError> {
let main_window_ref = Rc::new(RefCell::new(MainWindow::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())?)); let gui_elements_ref = Rc::new(RefCell::new(GUIElements::new(main_window_ref.clone())?));
setup_main_tab(gui_elements_ref.clone()); setup_main_tab(gui_elements_ref.clone(), logic_ref.clone());
setup_file_tab(gui_elements_ref.clone(), logic_ref); setup_file_tab(gui_elements_ref.clone(), logic_ref.clone());
let main_window = main_window_ref.borrow(); let main_window = main_window_ref.borrow();
main_window.run() main_window.run()
} }
fn setup_main_tab(gui_elements_ref: Rc<RefCell<GUIElements>>) { fn setup_main_tab(gui_elements_ref: Rc<RefCell<GUIElements>>, logic_ref: Rc<RefCell<Logic>>) {
let gui_elements = gui_elements_ref.borrow(); let gui_elements = gui_elements_ref.borrow();
gui_elements.main_tab.on_move_vram_image(gui_elements_ref.clone(), move |main_tab, _main_window, idx, dx, dy| { 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); main_tab.move_vram_image(idx as usize, dx, dy);
}); });
let logic = logic_ref.clone();
gui_elements.main_tab.on_remove_file(gui_elements_ref.clone(), move |main_tab, _main_window, idx| { gui_elements.main_tab.on_remove_file(gui_elements_ref.clone(), move |main_tab, _main_window, idx| {
if idx >= 0 { if idx >= 0 {
if !main_tab.remove_vram_file(idx as usize) { match main_tab.remove_vram_file(idx as usize) {
display_information("Removing VRAM file", "Can not remove palette. Delete image instead"); Ok(range) => logic.borrow_mut().remove_added_tim(range),
Err(error) => display_information("Removing VRAM file", error)
} }
} }
}); });
@ -66,7 +68,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().set_unadded_tim(&file)?; let (image, palette) = logic.borrow_mut().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 {
@ -85,9 +87,14 @@ fn setup_file_tab(gui_elements_ref: Rc<RefCell<GUIElements>>, logic_ref: Rc<RefC
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_mut().add_unadded_tim_as(&file_name, encoding)?; let (image, palette_image) = logic.borrow().get_converted_unadded_tim_image(encoding)?;
let images_created = main_tab.add_new_vram_file(&file_name, 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);
}
main_tab.add_new_vram_file(&file_name, image, encoding, palette_image);
file_tab.clear_load(); file_tab.clear_load();
main_window.invoke_change_to_main(); main_window.invoke_change_to_main();
Ok(()) Ok(())