Compare commits

..

No commits in common. "5cc88e0b87f0c54e95fd7ec9e9f1f5103525e9a8" and "aae57e47e40a22e325ecbfc88744e2215b7403f1" have entirely different histories.

3 changed files with 28 additions and 87 deletions

View File

@ -1,10 +1,10 @@
use std::{fs::File, io::Write, path::PathBuf}; use std::{fs::File, io::Write};
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::{ImagePosition, Job, PaletteRect, Project}, tim::types::Encoding}; use tim_tool::logic::{project::{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,21 +62,8 @@ 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| {
let open_location = main_window.get_project_widget_open_project_path(); Err(Error::not_implemented("on_load_project_clicked"))
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(())
} }
} }
@ -94,17 +81,10 @@ 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:Option<Job> = None; let mut cur_job = None;
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))| {
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 {
let cur_palette = std::mem::replace(&mut cur_palette, None); // Add palette to cur_job
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
} }
@ -113,10 +93,12 @@ 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() {
cur_palette = Some(PaletteRect::new(0, 0, width, height)); job.add_palette(PaletteRect::new(0, 0, width, height));
} }
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()))); cur_job = Some(job);
} }
prev_job prev_job
} }
@ -141,23 +123,17 @@ 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| {
let file_path = create_project_file_dialog() Err(Error::not_implemented("on_browse_open_project_clicked"))
.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 = create_project_file_dialog() let file_path = FileDialog::new()
.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();
@ -168,11 +144,4 @@ 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,47 +1,34 @@
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)]
pub struct ImagePosition { 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::new(0, 0) ImagePosition{x: 0, y: 0}
} }
} }
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct ImageSize { 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::new(0, 0) ImageSize{width: 0, height: 0}
} }
} }
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct PaletteRect { pub struct PaletteRect {
pub pos: ImagePosition, pos: ImagePosition,
pub size: ImageSize, size: ImageSize,
} }
impl PaletteRect { impl PaletteRect {
@ -62,20 +49,16 @@ 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), encoding: Encoding) -> Job { pub fn new(name: String, file_path: PathBuf, image_pos: (u16, u16)) -> 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()}
} }
pub fn add_encoding(&mut self, encoding: Encoding) { pub fn add_palette(&mut self, palette_rect: PaletteRect) -> &mut Self {
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,6 +1,4 @@
use serde::{Deserialize, Serialize}; #[derive(Clone, Copy)]
#[derive(Clone, Copy, Deserialize, Serialize)]
pub enum Encoding { pub enum Encoding {
FourBit, FourBit,
EightBit, EightBit,
@ -19,13 +17,4 @@ 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,
}
}
} }