Code clean up from live-stream #20
|
@ -142,10 +142,9 @@ impl FileTab {
|
|||
|
||||
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();
|
||||
let gui_cloned = gui_elements.clone();
|
||||
|
||||
self.main_window.borrow().on_file_tab_update_palette_size(move |width, height| {
|
||||
if let Err(error) = function(&mut gui_cloned.borrow_mut(), &main_window_cloned.borrow(), width as u32, height as u32) {
|
||||
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());
|
||||
}
|
||||
});
|
||||
|
@ -153,10 +152,9 @@ impl FileTab {
|
|||
|
||||
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();
|
||||
let gui_cloned = gui_elements.clone();
|
||||
|
||||
self.main_window.borrow().on_file_tab_add_convert_image(move || {
|
||||
if let Err(error) = function(&mut gui_cloned.borrow_mut(), &main_window_cloned.borrow()) {
|
||||
if let Err(error) = function(&mut gui_elements.borrow_mut(), &main_window_cloned.borrow()) {
|
||||
display_error("Adding file failed", &error.to_string());
|
||||
}
|
||||
});
|
||||
|
|
|
@ -140,17 +140,17 @@ impl MainTab {
|
|||
|
||||
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);
|
||||
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();
|
||||
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);
|
||||
function(&mut gui_elements.borrow_mut().main_tab, &main_window_cloned.borrow(), idx);
|
||||
});
|
||||
}
|
||||
}
|
|
@ -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;
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,58 +4,67 @@
|
|||
mod gui;
|
||||
use gui::{GUIElements, VRAM_WIDTH, VRAM_HEIGHT, display_information};
|
||||
use rfd::FileDialog;
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
use std::{cell::RefCell, rc::Rc, sync::Mutex};
|
||||
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!();
|
||||
|
||||
struct Application {
|
||||
main_window: Rc<RefCell<MainWindow>>,
|
||||
gui_elements: Rc<RefCell<GUIElements>>,
|
||||
tim_manager: Rc<Mutex<TIMManager>>,
|
||||
}
|
||||
|
||||
fn main() -> Result<(), slint::PlatformError> {
|
||||
let logic_ref = Rc::new(RefCell::new(Logic::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())?));
|
||||
impl Application {
|
||||
fn new() -> Result<Application, slint::PlatformError> {
|
||||
let main_window = Rc::new(RefCell::new(MainWindow::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()))
|
||||
})
|
||||
}
|
||||
|
||||
setup_main_tab(gui_elements_ref.clone(), logic_ref.clone());
|
||||
setup_file_tab(gui_elements_ref.clone(), logic_ref.clone());
|
||||
setup_about_tab(main_window_ref.clone());
|
||||
fn setup(&self) {
|
||||
self.setup_main_tab();
|
||||
self.setup_file_tab();
|
||||
self.setup_about_tab();
|
||||
}
|
||||
|
||||
let main_window = main_window_ref.borrow();
|
||||
fn run(&self) -> Result<(), slint::PlatformError> {
|
||||
let main_window = self.main_window.borrow();
|
||||
main_window.run()
|
||||
}
|
||||
|
||||
fn setup_main_tab(gui_elements_ref: Rc<RefCell<GUIElements>>, logic_ref: Rc<RefCell<Logic>>) {
|
||||
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| {
|
||||
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 logic = logic_ref.clone();
|
||||
gui_elements.main_tab.on_remove_file(gui_elements_ref.clone(), move |main_tab, _main_window, idx| {
|
||||
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) => logic.borrow_mut().remove_added_tim(range),
|
||||
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(gui_elements_ref: Rc<RefCell<GUIElements>>, logic_ref: Rc<RefCell<Logic>>) {
|
||||
let gui_elements = gui_elements_ref.borrow();
|
||||
|
||||
let logic = logic_ref.clone();
|
||||
gui_elements.file_tab.on_update_palette_size(gui_elements_ref.clone(), move |gui_elements, _main_window, width, height| -> Result<(), 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(logic.borrow_mut().change_unadded_tim_palette_size(width, height)?);
|
||||
file_tab.update_palette(tim_manager.lock().expect("VRAM already locked").change_unadded_tim_palette_size(width, height)?);
|
||||
Ok(())
|
||||
});
|
||||
|
||||
let logic = logic_ref.clone();
|
||||
gui_elements.file_tab.on_browse_file(gui_elements_ref.clone(), move |gui_elements, main_window| -> Result<(), Error> {
|
||||
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")
|
||||
|
@ -69,7 +78,7 @@ fn setup_file_tab(gui_elements_ref: Rc<RefCell<GUIElements>>, logic_ref: Rc<RefC
|
|||
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) = logic.borrow_mut().load_unadded_tim(&file)?;
|
||||
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 {
|
||||
|
@ -81,18 +90,19 @@ fn setup_file_tab(gui_elements_ref: Rc<RefCell<GUIElements>>, logic_ref: Rc<RefC
|
|||
Ok(())
|
||||
});
|
||||
|
||||
let logic = logic_ref.clone();
|
||||
gui_elements.file_tab.on_add_image(gui_elements_ref.clone(), move |gui_elements, main_window| {
|
||||
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 (image, palette_image) = logic.borrow().get_converted_unadded_tim_image(encoding)?;
|
||||
let (full_image, _) = logic.borrow().get_converted_unadded_tim_image(Encoding::FullColor)?;
|
||||
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) = logic.borrow_mut().add_unadded_tim(images_created) {
|
||||
if let Err(error) = tim_mgr.add_unadded_tim(images_created) {
|
||||
main_tab.pop_vram_files(images_created);
|
||||
return Err(error);
|
||||
}
|
||||
|
@ -103,9 +113,16 @@ fn setup_file_tab(gui_elements_ref: Rc<RefCell<GUIElements>>, logic_ref: Rc<RefC
|
|||
});
|
||||
}
|
||||
|
||||
fn setup_about_tab(main_window_ref: Rc<RefCell<MainWindow>>) {
|
||||
fn setup_about_tab(&self) {
|
||||
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
|
||||
let main_window = main_window_ref.borrow();
|
||||
main_window.invoke_set_version(SharedString::from(VERSION));
|
||||
self.main_window.borrow().invoke_set_version(SharedString::from(VERSION));
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<(), slint::PlatformError> {
|
||||
let application = Application::new()?;
|
||||
|
||||
application.setup();
|
||||
application.run()
|
||||
}
|
Loading…
Reference in New Issue