Projects: Inital support for saving and loading projects #22
|
@ -1,6 +1,6 @@
|
||||||
use std::{fs::File, io::Write, path::PathBuf};
|
use std::{fs::File, io::Write, path::PathBuf};
|
||||||
|
|
||||||
use crate::{gui::{self, main_tab::MainTab, MutexTIMManager, VRAM_HEIGHT, VRAM_WIDTH}, MainWindow};
|
use crate::{gui::{self, main_tab::{MainTab, NewVRAMImageInfo}, MutexTIMManager, VRAM_HEIGHT, VRAM_WIDTH}, MainWindow};
|
||||||
use super::FileTab;
|
use super::FileTab;
|
||||||
use rfd::FileDialog;
|
use rfd::FileDialog;
|
||||||
use slint::SharedString;
|
use slint::SharedString;
|
||||||
|
@ -45,15 +45,8 @@ pub(super) fn on_add_image(tim_manager: MutexTIMManager) -> impl FnMut(&mut File
|
||||||
return move |file_tab, main_tab, main_window| {
|
return move |file_tab, main_tab, main_window| {
|
||||||
let file_name = file_tab.get_file_name();
|
let file_name = file_tab.get_file_name();
|
||||||
let encoding = file_tab.get_encoding()?;
|
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);
|
add_unadded_tim(main_tab, tim_manager.clone(), &file_name, encoding)?;
|
||||||
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();
|
file_tab.clear_load();
|
||||||
main_window.invoke_change_to_main();
|
main_window.invoke_change_to_main();
|
||||||
|
@ -61,7 +54,7 @@ pub(super) fn on_add_image(tim_manager: MutexTIMManager) -> impl FnMut(&mut File
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn on_load_project_clicked() -> impl FnMut(&mut FileTab, &MainWindow) -> Result<(), Error> + 'static {
|
pub(super) fn on_load_project_clicked(tim_manager: MutexTIMManager) -> impl FnMut(&mut FileTab, &MainWindow) -> Result<(), Error> + 'static {
|
||||||
move |_file_tab, main_window| {
|
move |_file_tab, main_window| {
|
||||||
let open_location = main_window.get_project_widget_open_project_path();
|
let open_location = main_window.get_project_widget_open_project_path();
|
||||||
if open_location.is_empty() {
|
if open_location.is_empty() {
|
||||||
|
@ -69,13 +62,17 @@ pub(super) fn on_load_project_clicked() -> impl FnMut(&mut FileTab, &MainWindow)
|
||||||
}
|
}
|
||||||
|
|
||||||
let file_content = tool_helper::read_file_to_string(&PathBuf::from(open_location.as_str()))?;
|
let file_content = tool_helper::read_file_to_string(&PathBuf::from(open_location.as_str()))?;
|
||||||
let _new_project = match serde_json::from_str::<Project>(&file_content) {
|
let new_project = match serde_json::from_str::<Project>(&file_content) {
|
||||||
Ok(project) => project,
|
Ok(project) => project,
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
return Err(Error::from_error(error));
|
return Err(Error::from_error(error));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
for job in new_project.jobs {
|
||||||
|
let (_, _) = tim_manager.lock().expect("VRAM already locked").load_unadded_tim(&job.file_path)?;
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -102,7 +99,7 @@ pub(super) fn on_save_project_clicked(tim_manager: MutexTIMManager) -> impl FnMu
|
||||||
if let Some(mut cur_palette) = cur_palette {
|
if let Some(mut cur_palette) = cur_palette {
|
||||||
cur_palette.pos = ImagePosition::new(vram.x as u16, vram.y as u16);
|
cur_palette.pos = ImagePosition::new(vram.x as u16, vram.y as u16);
|
||||||
if let Some(cur_job) = &mut cur_job {
|
if let Some(cur_job) = &mut cur_job {
|
||||||
cur_job.add_palette(cur_palette);
|
cur_job.palette_rect = cur_palette;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None
|
None
|
||||||
|
@ -122,7 +119,7 @@ pub(super) fn on_save_project_clicked(tim_manager: MutexTIMManager) -> impl FnMu
|
||||||
}
|
}
|
||||||
}).collect());
|
}).collect());
|
||||||
if let Some(cur_job) = cur_job {
|
if let Some(cur_job) = cur_job {
|
||||||
project.push(cur_job);
|
project.jobs.push(cur_job);
|
||||||
}
|
}
|
||||||
|
|
||||||
let json_content = match serde_json::to_string(&project) {
|
let json_content = match serde_json::to_string(&project) {
|
||||||
|
@ -170,6 +167,22 @@ pub(super) fn on_browse_save_project_clicked() -> impl FnMut(&mut FileTab, &Main
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn add_unadded_tim(main_tab: &mut MainTab, tim_manager: MutexTIMManager, file_name: &String, encoding: Encoding) -> Result<(), Error> {
|
||||||
|
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 image = NewVRAMImageInfo::new(image, 0, 0, encoding);
|
||||||
|
let palette_image = if let Some(palette_image) = palette_image { Some(NewVRAMImageInfo::new(palette_image, 0, 0, Encoding::FullColor)) } else { None };
|
||||||
|
|
||||||
|
let images_created = main_tab.add_new_vram_file(file_name, full_image, image, palette_image);
|
||||||
|
if let Err(error) = tim_mgr.add_unadded_tim(images_created) {
|
||||||
|
main_tab.pop_vram_files(images_created);
|
||||||
|
return Err(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
fn create_project_file_dialog() -> FileDialog {
|
fn create_project_file_dialog() -> FileDialog {
|
||||||
FileDialog::new()
|
FileDialog::new()
|
||||||
.add_filter("TIM project file (.tim_project)", &["tim_project"])
|
.add_filter("TIM project file (.tim_project)", &["tim_project"])
|
||||||
|
|
|
@ -73,7 +73,7 @@ impl FileTab {
|
||||||
});
|
});
|
||||||
|
|
||||||
// Project functions
|
// Project functions
|
||||||
let (cloned_object, cloned_main_window, mut function) = (object.clone(), main_window.clone(), callbacks::on_load_project_clicked());
|
let (cloned_object, cloned_main_window, mut function) = (object.clone(), main_window.clone(), callbacks::on_load_project_clicked(tim_manager.clone()));
|
||||||
main_window.borrow().on_project_widget_load_project_clicked(move || {
|
main_window.borrow().on_project_widget_load_project_clicked(move || {
|
||||||
if let Err(error) = function(&mut cloned_object.borrow_mut(), &cloned_main_window.borrow()) {
|
if let Err(error) = function(&mut cloned_object.borrow_mut(), &cloned_main_window.borrow()) {
|
||||||
display_error("Loading project failed", &error.to_string());
|
display_error("Loading project failed", &error.to_string());
|
||||||
|
|
|
@ -6,6 +6,19 @@ use slint::{Model, SharedString};
|
||||||
use std::{cell::RefCell, ops::RangeInclusive, rc::Rc, sync::{Arc, Mutex}};
|
use std::{cell::RefCell, ops::RangeInclusive, rc::Rc, sync::{Arc, Mutex}};
|
||||||
use tim_tool::logic::tim::types::Encoding;
|
use tim_tool::logic::tim::types::Encoding;
|
||||||
|
|
||||||
|
pub struct NewVRAMImageInfo {
|
||||||
|
pub image: slint::Image,
|
||||||
|
pub x: i32,
|
||||||
|
pub y: i32,
|
||||||
|
pub encoding: Encoding,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl NewVRAMImageInfo {
|
||||||
|
pub fn new(image: slint::Image, x: i32, y: i32, encoding: Encoding) -> NewVRAMImageInfo {
|
||||||
|
NewVRAMImageInfo{image, x, y, encoding}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
struct VRAM {
|
struct VRAM {
|
||||||
count: usize,
|
count: usize,
|
||||||
file_list: Rc<slint::VecModel<slint::StandardListViewItem>>,
|
file_list: Rc<slint::VecModel<slint::StandardListViewItem>>,
|
||||||
|
@ -59,18 +72,18 @@ impl MainTab {
|
||||||
object
|
object
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_new_vram_file(&mut self, file_name: &String, full_image: slint::Image, image: slint::Image, encoding: Encoding, image_palette: Option<slint::Image>) -> usize {
|
pub fn add_new_vram_file(&mut self, file_name: &String, full_image: slint::Image, texture: NewVRAMImageInfo, image_palette: Option<NewVRAMImageInfo>) -> usize {
|
||||||
let mut vram_data = self.vram.lock().expect("VRAM already locked");
|
let mut vram_data = self.vram.lock().expect("VRAM already locked");
|
||||||
let mut add_new_image = |file_name: &String, full_image: slint::Image, image: slint::Image, encoding: Encoding, palette_count: i32, is_palette: bool| {
|
let mut add_new_image = |file_name: &String, full_image: slint::Image, vram_image: NewVRAMImageInfo, palette_count: i32, is_palette: bool| {
|
||||||
let encoding_str = if is_palette {"<Palette>"} else {encoding.to_str()};
|
let encoding_str = if is_palette {"<Palette>"} else {vram_image.encoding.to_str()};
|
||||||
let vram_image = VRAMData {
|
let vram_image = VRAMData {
|
||||||
images: VRAMImgData {
|
images: VRAMImgData {
|
||||||
full_image,
|
full_image,
|
||||||
image
|
image: vram_image.image,
|
||||||
},
|
},
|
||||||
info: VRAMInfo {
|
info: VRAMInfo {
|
||||||
x: 0,
|
x: vram_image.x,
|
||||||
y: 0,
|
y: vram_image.y,
|
||||||
encoding_str: SharedString::from(encoding_str),
|
encoding_str: SharedString::from(encoding_str),
|
||||||
palette_count,
|
palette_count,
|
||||||
is_palette,
|
is_palette,
|
||||||
|
@ -81,12 +94,12 @@ impl MainTab {
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut images_added = 1;
|
let mut images_added = 1;
|
||||||
add_new_image(file_name, full_image, image, encoding, if image_palette.is_some() {1} else {0}, false);
|
add_new_image(file_name, full_image, texture, if image_palette.is_some() {1} else {0}, false);
|
||||||
if let Some(image_palette) = image_palette {
|
if let Some(image_palette) = image_palette {
|
||||||
let file_name = " => ".to_owned() + file_name.as_str() + " (Palette)";
|
let file_name = " => ".to_owned() + file_name.as_str() + " (Palette)";
|
||||||
|
|
||||||
images_added += 1;
|
images_added += 1;
|
||||||
add_new_image(&file_name, image_palette.clone(), image_palette, encoding, 0, true);
|
add_new_image(&file_name, image_palette.image.clone(), image_palette, 0, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
images_added
|
images_added
|
||||||
|
|
|
@ -58,42 +58,26 @@ impl std::default::Default for PaletteRect {
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct Job {
|
pub struct Job {
|
||||||
name: String,
|
pub name: String,
|
||||||
file_path: PathBuf,
|
pub file_path: PathBuf,
|
||||||
image_pos: ImagePosition,
|
pub image_pos: ImagePosition,
|
||||||
palette_rect: PaletteRect,
|
pub palette_rect: PaletteRect,
|
||||||
encoding: Encoding,
|
pub encoding: Encoding,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Job {
|
impl Job {
|
||||||
pub fn new(name: String, file_path: PathBuf, image_pos: (u16, u16), encoding: Encoding) -> Job {
|
pub fn new(name: String, file_path: PathBuf, image_pos: (u16, u16), encoding: Encoding) -> Job {
|
||||||
Job{name, file_path, image_pos: ImagePosition{x: image_pos.0, y: image_pos.1}, palette_rect: PaletteRect::default(), encoding}
|
Job{name, file_path, image_pos: ImagePosition{x: image_pos.0, y: image_pos.1}, palette_rect: PaletteRect::default(), encoding}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_encoding(&mut self, encoding: Encoding) {
|
|
||||||
self.encoding = encoding;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn add_palette(&mut self, palette_rect: PaletteRect) {
|
|
||||||
self.palette_rect = palette_rect;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct Project {
|
pub struct Project {
|
||||||
jobs: Vec<Job>
|
pub jobs: Vec<Job>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Project {
|
impl Project {
|
||||||
pub fn new(jobs: Vec<Job>) -> Project {
|
pub fn new(jobs: Vec<Job>) -> Project {
|
||||||
Project{jobs}
|
Project{jobs}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn push(&mut self, job: Job) {
|
|
||||||
self.jobs.push(job);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn len(&self) -> usize {
|
|
||||||
self.jobs.len()
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue