Code clean up from live-stream #20

Merged
jaby merged 4 commits from topic/jb/more-improvements into main 2025-03-13 19:56:22 +00:00
3 changed files with 78 additions and 76 deletions
Showing only changes of commit af2526d829 - Show all commits

View File

@ -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<Option<TIMInfo>>,
unadded_tim: Option<TIMInfo>,
}
impl Logic {
pub fn new() -> Logic {
Logic{added_tims: Default::default(), unadded_tim: None}
}
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 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<Image>), 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<Option<Image>, Error> {
if let Some(unadded_tim) = &mut self.unadded_tim {
unadded_tim.change_palette_size(width, height)
}
else {
Ok(None)
}
}
}
pub use tim_mgr::TIMManager;

View File

@ -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<Option<TIMInfo>>,
unadded_tim: Option<TIMInfo>,
}
impl TIMManager {
pub fn new() -> TIMManager {
TIMManager{added_tims: Default::default(), unadded_tim: None}
}
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 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<Image>), 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<Option<Image>, Error> {
if let Some(unadded_tim) = &mut self.unadded_tim {
unadded_tim.change_palette_size(width, height)
}
else {
Ok(None)
}
}
}

View File

@ -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<RefCell<GUIElements>>, logic_ref: Rc<RefCell<Logic>>) {
fn setup_main_tab(gui_elements_ref: Rc<RefCell<GUIElements>>, logic_ref: Rc<RefCell<TIMManager>>) {
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<RefCell<GUIElements>>, logic_ref: Rc<RefC
});
}
fn setup_file_tab(gui_elements_ref: Rc<RefCell<GUIElements>>, logic_ref: Rc<RefCell<Logic>>) {
fn setup_file_tab(gui_elements_ref: Rc<RefCell<GUIElements>>, logic_ref: Rc<RefCell<TIMManager>>) {
let gui_elements = gui_elements_ref.borrow();
let logic = logic_ref.clone();