Further improvements
This commit is contained in:
parent
4f0103e8fa
commit
2398053c61
|
@ -1,6 +1,7 @@
|
|||
use crate::MainWindow;
|
||||
use super::{GUIElements, GUIElementsRef, MainWindowRef};
|
||||
use super::{GUIElements, GUIElementsRef, MainWindowRef, display_error};
|
||||
use slint::Image;
|
||||
use tool_helper::Error;
|
||||
|
||||
pub struct FileTab {
|
||||
main_window: MainWindowRef
|
||||
|
@ -38,21 +39,25 @@ impl FileTab {
|
|||
self.main_window.borrow().get_file_tab_image_name().to_string()
|
||||
}
|
||||
|
||||
pub fn on_browse_file(&self, gui_elements: GUIElementsRef, mut function: impl FnMut(&mut GUIElements, &MainWindow) + 'static) {
|
||||
pub fn on_browse_file(&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_browse_convert_image(move || {
|
||||
function(&mut gui_cloned.borrow_mut(), &main_window_cloned.borrow());
|
||||
if let Err(error) = function(&mut gui_cloned.borrow_mut(), &main_window_cloned.borrow()) {
|
||||
display_error("Loadind file failed", &error.to_string());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub fn on_add_image(&self, gui_elements: GUIElementsRef, mut function: impl FnMut(&mut GUIElements, &MainWindow) + 'static) {
|
||||
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 || {
|
||||
function(&mut gui_cloned.borrow_mut(), &main_window_cloned.borrow());
|
||||
if let Err(error) = function(&mut gui_cloned.borrow_mut(), &main_window_cloned.borrow()) {
|
||||
display_error("Adding file failed", &error.to_string());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
|
@ -3,6 +3,7 @@ mod gui_elements;
|
|||
mod main_tab;
|
||||
|
||||
use crate::MainWindow;
|
||||
use rfd::MessageDialog;
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
use slint::{Rgba8Pixel, SharedPixelBuffer};
|
||||
use tiny_skia::{Rect, Transform};
|
||||
|
@ -15,6 +16,10 @@ pub const VRAM_HEIGHT:usize = 512;
|
|||
type MainWindowRef = Rc<RefCell<MainWindow>>;
|
||||
type GUIElementsRef = Rc<RefCell<GUIElements>>;
|
||||
|
||||
fn display_error(title: &str, text: &String) {
|
||||
MessageDialog::new().set_title(title).set_level(rfd::MessageLevel::Error).set_description(text).show();
|
||||
}
|
||||
|
||||
fn create_vram_bg(rect_width: u32, rect_height: u32) -> Option<SharedPixelBuffer<Rgba8Pixel>> {
|
||||
let mut pixel_buffer = SharedPixelBuffer::<Rgba8Pixel>::new(VRAM_WIDTH as u32, VRAM_HEIGHT as u32);
|
||||
let width = pixel_buffer.width();
|
||||
|
|
|
@ -1,45 +1,37 @@
|
|||
pub mod tim;
|
||||
|
||||
use std::path::PathBuf;
|
||||
use slint::Image;
|
||||
use tim::TIMInfo;
|
||||
use tool_helper::Error;
|
||||
|
||||
pub struct Logic {
|
||||
unadded_tim: UnaddedTIM,
|
||||
unadded_tim: Option<TIMInfo>,
|
||||
}
|
||||
|
||||
impl Logic {
|
||||
pub fn new() -> Logic {
|
||||
Logic{unadded_tim: UnaddedTIM::empty()}
|
||||
Logic{unadded_tim: None}
|
||||
}
|
||||
|
||||
pub fn set_unadded_tim(&mut self, info: TIMInfo, image: Image) {
|
||||
self.unadded_tim = UnaddedTIM::new(info, image);
|
||||
pub fn set_unadded_tim(&mut self, path: &PathBuf) -> Result<Image, Error> {
|
||||
let tim_info = TIMInfo::from_image(path)?;
|
||||
let image = tim_info.get_slint_images();
|
||||
|
||||
self.unadded_tim = Some(tim_info);
|
||||
Ok(image)
|
||||
}
|
||||
|
||||
pub fn add_unadded_tim_as(&mut self, _name: &String) -> Image {
|
||||
let _info = self.unadded_tim.take_info();
|
||||
self.unadded_tim.take_image()
|
||||
}
|
||||
}
|
||||
|
||||
struct UnaddedTIM {
|
||||
info: TIMInfo,
|
||||
image: Image,
|
||||
}
|
||||
|
||||
impl UnaddedTIM {
|
||||
pub fn new(info: TIMInfo, image: Image) -> UnaddedTIM {
|
||||
UnaddedTIM{info, image}
|
||||
}
|
||||
|
||||
pub fn take_info(&mut self) -> TIMInfo {
|
||||
std::mem::take(&mut self.info)
|
||||
}
|
||||
|
||||
pub fn take_image(&mut self) -> Image {
|
||||
std::mem::take(&mut self.image)
|
||||
}
|
||||
|
||||
fn empty() -> UnaddedTIM {
|
||||
UnaddedTIM{info: TIMInfo::default(), image: Image::default()}
|
||||
pub fn add_unadded_tim_as(&mut self, _name: &String) -> Result<Image, Error> {
|
||||
if let Some(unadded_tim) = &self.unadded_tim {
|
||||
let image = unadded_tim.get_slint_images();
|
||||
|
||||
self.unadded_tim = None;
|
||||
Ok(image)
|
||||
}
|
||||
|
||||
else {
|
||||
Err(Error::from_str("No data found for loaded image"))
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
use std::{default, fmt::format, fs::File, path::PathBuf};
|
||||
use std::{fs::File, path::PathBuf};
|
||||
use slint::{Rgba8Pixel, SharedPixelBuffer};
|
||||
use tool_helper::Error;
|
||||
|
||||
|
@ -7,13 +7,8 @@ pub struct TIMInfo {
|
|||
palette: Option<Vec<Rgba8Pixel>>,
|
||||
}
|
||||
|
||||
impl std::default::Default for TIMInfo {
|
||||
fn default() -> Self {
|
||||
TIMInfo{image_data: SharedPixelBuffer::new(1, 1), palette: None}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load_image(path: &PathBuf) -> Result<(slint::Image, TIMInfo), Error> {
|
||||
impl TIMInfo {
|
||||
pub fn from_image(path: &PathBuf) -> Result<TIMInfo, Error> {
|
||||
fn make_color(iter: &mut dyn std::iter::Iterator<Item = &u8>) -> Option<Rgba8Pixel> {
|
||||
Some(Rgba8Pixel::new(
|
||||
*iter.next()?, *iter.next()?, *iter.next()?,
|
||||
|
@ -53,11 +48,15 @@ pub fn load_image(path: &PathBuf) -> Result<(slint::Image, TIMInfo), Error> {
|
|||
}
|
||||
}
|
||||
|
||||
let image = slint::Image::from_rgba8_premultiplied(image_data.clone());
|
||||
Ok((image, TIMInfo{image_data, palette: Some(palette_colors)}))
|
||||
Ok(TIMInfo{image_data, palette: Some(palette_colors)})
|
||||
}
|
||||
|
||||
else {
|
||||
Err(Error::not_implemented("Support for non indexed images"))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_slint_images(&self) -> slint::Image {
|
||||
slint::Image::from_rgba8_premultiplied(self.image_data.clone())
|
||||
}
|
||||
}
|
|
@ -3,10 +3,11 @@
|
|||
|
||||
mod gui;
|
||||
use gui::{GUIElements, VRAM_WIDTH, VRAM_HEIGHT};
|
||||
use rfd::{FileDialog, MessageDialog};
|
||||
use rfd::FileDialog;
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
use slint::SharedString;
|
||||
use tim_tool::logic::{tim::load_image, Logic};
|
||||
use tim_tool::logic::Logic;
|
||||
use tool_helper::Error;
|
||||
|
||||
slint::include_modules!();
|
||||
|
||||
|
@ -41,11 +42,7 @@ fn setup_file_tab(gui_elements_ref: Rc<RefCell<GUIElements>>, logic_ref: Rc<RefC
|
|||
let gui_elements = gui_elements_ref.borrow();
|
||||
|
||||
let logic = logic_ref.clone();
|
||||
gui_elements.file_tab.on_browse_file(gui_elements_ref.clone(), move |gui_elements, main_window| {
|
||||
fn show_error_message(text: String) {
|
||||
MessageDialog::new().set_title("Loading Image failed").set_level(rfd::MessageLevel::Error).set_description(text).show();
|
||||
}
|
||||
|
||||
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")
|
||||
|
@ -59,25 +56,16 @@ 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, info) = match load_image(&file) {
|
||||
Ok((image, info)) => (image, info),
|
||||
Err(error) => {
|
||||
file_tab.clear_load();
|
||||
show_error_message(error.to_string());
|
||||
return;
|
||||
}
|
||||
};
|
||||
let image = logic.borrow_mut().set_unadded_tim(&file)?;
|
||||
|
||||
let img_size = image.size();
|
||||
if img_size.width > VRAM_WIDTH as u32 || img_size.height > VRAM_HEIGHT as u32 {
|
||||
file_tab.clear_load();
|
||||
show_error_message(format!("Image size ({}; {}) is to big for VRAM ({}, {})", img_size.width, img_size.height, VRAM_WIDTH, VRAM_HEIGHT));
|
||||
return;
|
||||
return Err(Error::from_text(format!("Image size ({}; {}) is to big for VRAM ({}, {})", img_size.width, img_size.height, VRAM_WIDTH, VRAM_HEIGHT)));
|
||||
}
|
||||
|
||||
logic.borrow_mut().set_unadded_tim(info, image.clone());
|
||||
file_tab.update_new_load(file_name, image);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
});
|
||||
|
||||
let logic = logic_ref.clone();
|
||||
|
@ -86,9 +74,10 @@ fn setup_file_tab(gui_elements_ref: Rc<RefCell<GUIElements>>, logic_ref: Rc<RefC
|
|||
let file_tab = &gui_elements.file_tab;
|
||||
|
||||
let file_name = file_tab.get_file_name();
|
||||
main_tab.add_new_vram_file(&file_name, logic.borrow_mut().add_unadded_tim_as(&file_name));
|
||||
main_tab.add_new_vram_file(&file_name, logic.borrow_mut().add_unadded_tim_as(&file_name)?);
|
||||
|
||||
file_tab.clear_load();
|
||||
main_window.invoke_change_to_main();
|
||||
Ok(())
|
||||
});
|
||||
}
|
Loading…
Reference in New Issue