From af2526d82935f078cebea90fbea50d01cdd49701 Mon Sep 17 00:00:00 2001 From: Jaby Date: Wed, 12 Mar 2025 20:22:46 +0100 Subject: [PATCH] Rename logic to TIMManager --- src/Tools/tim_tool/src/logic/mod.rs | 73 +------------------------ src/Tools/tim_tool/src/logic/tim_mgr.rs | 72 ++++++++++++++++++++++++ src/Tools/tim_tool/src/main.rs | 9 ++- 3 files changed, 78 insertions(+), 76 deletions(-) create mode 100644 src/Tools/tim_tool/src/logic/tim_mgr.rs diff --git a/src/Tools/tim_tool/src/logic/mod.rs b/src/Tools/tim_tool/src/logic/mod.rs index 9c7791cf..e5dc964f 100644 --- a/src/Tools/tim_tool/src/logic/mod.rs +++ b/src/Tools/tim_tool/src/logic/mod.rs @@ -1,73 +1,4 @@ pub mod tim; -use std::{ops::RangeInclusive, path::PathBuf}; -use slint::Image; -use tim::{types::Encoding, TIMInfo}; -use tool_helper::Error; +pub mod tim_mgr; -pub struct Logic { - added_tims: Vec>, - unadded_tim: Option, -} - -impl Logic { - pub fn new() -> Logic { - Logic{added_tims: Default::default(), unadded_tim: None} - } - - pub fn remove_added_tim(&mut self, range: RangeInclusive) { - let idx = *range.start(); - for _ in range { - self.added_tims.remove(idx); - } - } - - pub fn load_unadded_tim(&mut self, path: &PathBuf) -> Result<(Image, Option), Error> { - let tim_info = TIMInfo::from_image(path)?; - let image = tim_info.get_slint_images(Encoding::FullColor); - - self.unadded_tim = Some(tim_info); - Ok(image) - } - - pub fn get_converted_unadded_tim_image(&self, encoding: Encoding) -> Result<(Image, Option), Error> { - if let Some(unadded_tim) = &self.unadded_tim { - let (image, palette) = unadded_tim.get_slint_images(encoding); - Ok((image, palette)) - } - - else { - Err(Error::from_str("No data found for loaded image")) - } - } - - 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, Error> { - if let Some(unadded_tim) = &mut self.unadded_tim { - unadded_tim.change_palette_size(width, height) - } - - else { - Ok(None) - } - } -} \ No newline at end of file +pub use tim_mgr::TIMManager; \ No newline at end of file diff --git a/src/Tools/tim_tool/src/logic/tim_mgr.rs b/src/Tools/tim_tool/src/logic/tim_mgr.rs new file mode 100644 index 00000000..36d09310 --- /dev/null +++ b/src/Tools/tim_tool/src/logic/tim_mgr.rs @@ -0,0 +1,72 @@ +use slint::Image; +use std::{ops::RangeInclusive, path::PathBuf}; +use super::tim::{types::Encoding, TIMInfo}; +use tool_helper::Error; + +pub struct TIMManager { + added_tims: Vec>, + unadded_tim: Option, +} + +impl TIMManager { + pub fn new() -> TIMManager { + TIMManager{added_tims: Default::default(), unadded_tim: None} + } + + pub fn remove_added_tim(&mut self, range: RangeInclusive) { + let idx = *range.start(); + for _ in range { + self.added_tims.remove(idx); + } + } + + pub fn load_unadded_tim(&mut self, path: &PathBuf) -> Result<(Image, Option), Error> { + let tim_info = TIMInfo::from_image(path)?; + let image = tim_info.get_slint_images(Encoding::FullColor); + + self.unadded_tim = Some(tim_info); + Ok(image) + } + + pub fn get_converted_unadded_tim_image(&self, encoding: Encoding) -> Result<(Image, Option), Error> { + if let Some(unadded_tim) = &self.unadded_tim { + let (image, palette) = unadded_tim.get_slint_images(encoding); + Ok((image, palette)) + } + + else { + Err(Error::from_str("No data found for loaded image")) + } + } + + 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, Error> { + if let Some(unadded_tim) = &mut self.unadded_tim { + unadded_tim.change_palette_size(width, height) + } + + else { + Ok(None) + } + } +} \ No newline at end of file diff --git a/src/Tools/tim_tool/src/main.rs b/src/Tools/tim_tool/src/main.rs index 44cf7809..b2a63aef 100644 --- a/src/Tools/tim_tool/src/main.rs +++ b/src/Tools/tim_tool/src/main.rs @@ -6,14 +6,13 @@ use gui::{GUIElements, VRAM_WIDTH, VRAM_HEIGHT, display_information}; use rfd::FileDialog; use std::{cell::RefCell, rc::Rc}; use slint::SharedString; -use tim_tool::logic::{tim::types::Encoding, Logic}; +use tim_tool::logic::{tim::types::Encoding, TIMManager}; use tool_helper::Error; slint::include_modules!(); - fn main() -> Result<(), slint::PlatformError> { - let logic_ref = Rc::new(RefCell::new(Logic::new())); + let logic_ref = Rc::new(RefCell::new(TIMManager::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())?)); @@ -25,7 +24,7 @@ fn main() -> Result<(), slint::PlatformError> { main_window.run() } -fn setup_main_tab(gui_elements_ref: Rc>, logic_ref: Rc>) { +fn setup_main_tab(gui_elements_ref: Rc>, logic_ref: Rc>) { 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| { @@ -43,7 +42,7 @@ fn setup_main_tab(gui_elements_ref: Rc>, logic_ref: Rc>, logic_ref: Rc>) { +fn setup_file_tab(gui_elements_ref: Rc>, logic_ref: Rc>) { let gui_elements = gui_elements_ref.borrow(); let logic = logic_ref.clone();