Compare commits

..

4 Commits

Author SHA1 Message Date
Jaby 5cc88e0b87 Deserialize project 2025-03-20 21:37:19 +01:00
Jaby 5f4ade47bd Support selecting project to open 2025-03-20 21:26:32 +01:00
Jaby 2345b3da57 Save encoding information 2025-03-20 21:16:41 +01:00
Jaby e0a266317e Support saving palette information 2025-03-20 20:57:10 +01:00
3 changed files with 87 additions and 28 deletions

View File

@ -1,10 +1,10 @@
use std::{fs::File, io::Write}; 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, MutexTIMManager, VRAM_HEIGHT, VRAM_WIDTH}, MainWindow};
use super::FileTab; use super::FileTab;
use rfd::FileDialog; use rfd::FileDialog;
use slint::SharedString; use slint::SharedString;
use tim_tool::logic::{project::{Job, PaletteRect, Project}, tim::types::Encoding}; use tim_tool::logic::{project::{ImagePosition, Job, PaletteRect, Project}, tim::types::Encoding};
use tool_helper::Error; use tool_helper::Error;
pub(super) fn on_browse_file(tim_manager: MutexTIMManager) -> impl FnMut(&mut FileTab, &MainWindow) -> Result<(), Error> + 'static { pub(super) fn on_browse_file(tim_manager: MutexTIMManager) -> impl FnMut(&mut FileTab, &MainWindow) -> Result<(), Error> + 'static {
@ -62,8 +62,21 @@ 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() -> impl FnMut(&mut FileTab, &MainWindow) -> Result<(), Error> + 'static {
move |_file_tab, _main_window| { move |_file_tab, main_window| {
Err(Error::not_implemented("on_load_project_clicked")) let open_location = main_window.get_project_widget_open_project_path();
if open_location.is_empty() {
return Err(Error::from_str("Please specify location for project file to open"));
}
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) {
Ok(project) => project,
Err(error) => {
return Err(Error::from_error(error));
}
};
Ok(())
} }
} }
@ -81,10 +94,17 @@ pub(super) fn on_save_project_clicked(tim_manager: MutexTIMManager) -> impl FnMu
return Err(Error::from_str("TIM and VRAM info not in sync! Please close program")); return Err(Error::from_str("TIM and VRAM info not in sync! Please close program"));
} }
let mut cur_job = None; let mut cur_job:Option<Job> = None;
let mut project = Project::new(tim_info.into_iter().zip(vram_info.into_iter()).filter_map(|(tim, (file_name, vram))| { let mut cur_palette:Option<PaletteRect> = None;
let mut project = Project::new(tim_info.into_iter().zip(vram_info.into_iter()).filter_map(|(tim, (file_name, vram))| {
if vram.is_palette { if vram.is_palette {
// Add palette to cur_job let cur_palette = std::mem::replace(&mut cur_palette, None);
if let Some(mut cur_palette) = cur_palette {
cur_palette.pos = ImagePosition::new(vram.x as u16, vram.y as u16);
if let Some(cur_job) = &mut cur_job {
cur_job.add_palette(cur_palette);
}
}
None None
} }
@ -93,12 +113,10 @@ pub(super) fn on_save_project_clicked(tim_manager: MutexTIMManager) -> impl FnMu
let prev_job = std::mem::replace(&mut cur_job, None); let prev_job = std::mem::replace(&mut cur_job, None);
if let Some(tim) = tim { if let Some(tim) = tim {
let mut job = Job::new(file_name, tim.get_path(), (vram.x as u16, vram.y as u16));
if let Some((width, height)) = tim.get_palette_size() { if let Some((width, height)) = tim.get_palette_size() {
job.add_palette(PaletteRect::new(0, 0, width, height)); cur_palette = Some(PaletteRect::new(0, 0, width, height));
} }
cur_job = Some(job); cur_job = Some(Job::new(file_name, tim.get_path(), (vram.x as u16, vram.y as u16), Encoding::from_str(vram.encoding_str.as_str())));
} }
prev_job prev_job
} }
@ -123,17 +141,23 @@ pub(super) fn on_save_project_clicked(tim_manager: MutexTIMManager) -> impl FnMu
} }
pub(super) fn on_browse_open_project_clicked() -> impl FnMut(&mut FileTab, &MainWindow) -> Result<(), Error> + 'static { pub(super) fn on_browse_open_project_clicked() -> impl FnMut(&mut FileTab, &MainWindow) -> Result<(), Error> + 'static {
move |_file_tab, _main_window| { move |_file_tab, main_window| {
Err(Error::not_implemented("on_browse_open_project_clicked")) let file_path = create_project_file_dialog()
.set_title("Save location for TIM project file")
.pick_file();
if let Some(file_path) = file_path {
if let Some(file_path) = file_path.to_str() {
main_window.set_project_widget_open_project_path(SharedString::from(file_path));
}
}
Ok(())
} }
} }
pub(super) fn on_browse_save_project_clicked() -> impl FnMut(&mut FileTab, &MainWindow) -> Result<(), Error> + 'static { pub(super) fn on_browse_save_project_clicked() -> impl FnMut(&mut FileTab, &MainWindow) -> Result<(), Error> + 'static {
move |_file_tab, main_window| { move |_file_tab, main_window| {
let file_path = FileDialog::new() let file_path = create_project_file_dialog()
.add_filter("TIM project file (.tim_project)", &["tim_project"])
.add_filter("JSON file (.json; .jsn)", &["json", "jsn"])
.add_filter("All", &["*"])
.set_title("Save location for TIM project file") .set_title("Save location for TIM project file")
.save_file(); .save_file();
@ -144,4 +168,11 @@ pub(super) fn on_browse_save_project_clicked() -> impl FnMut(&mut FileTab, &Main
} }
Ok(()) Ok(())
} }
}
fn create_project_file_dialog() -> FileDialog {
FileDialog::new()
.add_filter("TIM project file (.tim_project)", &["tim_project"])
.add_filter("JSON file (.json; .jsn)", &["json", "jsn"])
.add_filter("All", &["*"])
} }

View File

@ -1,34 +1,47 @@
use super::tim::types::Encoding;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::path::PathBuf; use std::path::PathBuf;
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
struct ImagePosition { pub struct ImagePosition {
pub x: u16, pub x: u16,
pub y: u16, pub y: u16,
} }
impl ImagePosition {
pub fn new(x: u16, y: u16) -> ImagePosition {
ImagePosition{x, y}
}
}
impl std::default::Default for ImagePosition { impl std::default::Default for ImagePosition {
fn default() -> Self { fn default() -> Self {
ImagePosition{x: 0, y: 0} ImagePosition::new(0, 0)
} }
} }
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
struct ImageSize { pub struct ImageSize {
pub width: u16, pub width: u16,
pub height: u16, pub height: u16,
} }
impl ImageSize {
pub fn new(width: u16, height: u16) -> ImageSize {
ImageSize{width, height}
}
}
impl std::default::Default for ImageSize { impl std::default::Default for ImageSize {
fn default() -> Self { fn default() -> Self {
ImageSize{width: 0, height: 0} ImageSize::new(0, 0)
} }
} }
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct PaletteRect { pub struct PaletteRect {
pos: ImagePosition, pub pos: ImagePosition,
size: ImageSize, pub size: ImageSize,
} }
impl PaletteRect { impl PaletteRect {
@ -49,16 +62,20 @@ pub struct Job {
file_path: PathBuf, file_path: PathBuf,
image_pos: ImagePosition, image_pos: ImagePosition,
palette_rect: PaletteRect, palette_rect: PaletteRect,
encoding: Encoding,
} }
impl Job { impl Job {
pub fn new(name: String, file_path: PathBuf, image_pos: (u16, u16)) -> 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()} Job{name, file_path, image_pos: ImagePosition{x: image_pos.0, y: image_pos.1}, palette_rect: PaletteRect::default(), encoding}
} }
pub fn add_palette(&mut self, palette_rect: PaletteRect) -> &mut Self { 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; self.palette_rect = palette_rect;
self
} }
} }

View File

@ -1,4 +1,6 @@
#[derive(Clone, Copy)] use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Deserialize, Serialize)]
pub enum Encoding { pub enum Encoding {
FourBit, FourBit,
EightBit, EightBit,
@ -17,4 +19,13 @@ impl Encoding {
Encoding::FullColor => Self::FULL_COLOR_NAME, Encoding::FullColor => Self::FULL_COLOR_NAME,
} }
} }
pub fn from_str(str: &str) -> Encoding {
match str {
Self::FOUR_BIT_NAME => Encoding::FourBit,
Self::EIGHT_BIT_NAME => Encoding::EightBit,
Self::FULL_COLOR_NAME => Encoding::FullColor,
_ => Encoding::FullColor,
}
}
} }