topic/bg/project-gui #21

Merged
jaby merged 7 commits from topic/bg/project-gui into main 2025-03-18 19:58:12 +00:00
5 changed files with 193 additions and 175 deletions
Showing only changes of commit e3a85fc2d9 - Show all commits

View File

@ -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());
}
});

View File

@ -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);
});
}
}

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

@ -4,108 +4,125 @@
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>>,
}
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()))
})
}
fn setup(&self) {
self.setup_main_tab();
self.setup_file_tab();
self.setup_about_tab();
}
fn run(&self) -> Result<(), slint::PlatformError> {
let main_window = self.main_window.borrow();
main_window.run()
}
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 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) => tim_manager.lock().expect("VRAM already locked").remove_added_tim(range),
Err(error) => display_information("Removing VRAM file", 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(tim_manager.lock().expect("VRAM already locked").change_unadded_tim_palette_size(width, height)?);
Ok(())
});
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")
.pick_file();
if let Some(file) = file {
if let Some(file_path) = file.to_str() {
main_window.set_file_tab_browse_path(SharedString::from(file_path));
}
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) = 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 {
return Err(Error::from_text(format!("Image size ({}; {}) is to big for VRAM ({}, {})", img_size.width, img_size.height, VRAM_WIDTH, VRAM_HEIGHT)));
}
return file_tab.update_new_loaded_file(file_name, image, palette);
}
Ok(())
});
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 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) = tim_mgr.add_unadded_tim(images_created) {
main_tab.pop_vram_files(images_created);
return Err(error);
}
file_tab.clear_load();
main_window.invoke_change_to_main();
Ok(())
});
}
fn setup_about_tab(&self) {
const VERSION: &str = env!("CARGO_PKG_VERSION");
self.main_window.borrow().invoke_set_version(SharedString::from(VERSION));
}
}
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())?));
let application = Application::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());
let main_window = main_window_ref.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| {
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| {
if idx >= 0 {
match main_tab.remove_vram_file(idx as usize) {
Ok(range) => logic.borrow_mut().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> {
let file_tab = &gui_elements.file_tab;
file_tab.update_palette(logic.borrow_mut().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 file = FileDialog::new()
.add_filter("PNG image (.png)", &["png"])
.set_title("PNG image file")
.pick_file();
if let Some(file) = file {
if let Some(file_path) = file.to_str() {
main_window.set_file_tab_browse_path(SharedString::from(file_path));
}
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 img_size = image.size();
if img_size.width > VRAM_WIDTH as u32 || img_size.height > VRAM_HEIGHT as u32 {
return Err(Error::from_text(format!("Image size ({}; {}) is to big for VRAM ({}, {})", img_size.width, img_size.height, VRAM_WIDTH, VRAM_HEIGHT)));
}
return file_tab.update_new_loaded_file(file_name, image, palette);
}
Ok(())
});
let logic = logic_ref.clone();
gui_elements.file_tab.on_add_image(gui_elements_ref.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 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) {
main_tab.pop_vram_files(images_created);
return Err(error);
}
file_tab.clear_load();
main_window.invoke_change_to_main();
Ok(())
});
}
fn setup_about_tab(main_window_ref: Rc<RefCell<MainWindow>>) {
const VERSION: &str = env!("CARGO_PKG_VERSION");
let main_window = main_window_ref.borrow();
main_window.invoke_set_version(SharedString::from(VERSION));
application.setup();
application.run()
}