Support saving palette information

This commit is contained in:
Jaby 2025-03-20 20:57:10 +01:00
parent aae57e47e4
commit e0a266317e
2 changed files with 31 additions and 14 deletions

View File

@ -4,7 +4,7 @@ use crate::{gui::{self, main_tab::MainTab, MutexTIMManager, VRAM_HEIGHT, VRAM_WI
use super::FileTab;
use rfd::FileDialog;
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;
pub(super) fn on_browse_file(tim_manager: MutexTIMManager) -> impl FnMut(&mut FileTab, &MainWindow) -> Result<(), Error> + 'static {
@ -81,10 +81,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"));
}
let mut cur_job = None;
let mut cur_job:Option<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))| {
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
}
@ -93,12 +100,10 @@ pub(super) fn on_save_project_clicked(tim_manager: MutexTIMManager) -> impl FnMu
let prev_job = std::mem::replace(&mut cur_job, None);
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() {
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)));
}
prev_job
}

View File

@ -2,33 +2,45 @@ use serde::{Deserialize, Serialize};
use std::path::PathBuf;
#[derive(Serialize, Deserialize)]
struct ImagePosition {
pub struct ImagePosition {
pub x: u16,
pub y: u16,
}
impl ImagePosition {
pub fn new(x: u16, y: u16) -> ImagePosition {
ImagePosition{x, y}
}
}
impl std::default::Default for ImagePosition {
fn default() -> Self {
ImagePosition{x: 0, y: 0}
ImagePosition::new(0, 0)
}
}
#[derive(Serialize, Deserialize)]
struct ImageSize {
pub struct ImageSize {
pub width: u16,
pub height: u16,
}
impl ImageSize {
pub fn new(width: u16, height: u16) -> ImageSize {
ImageSize{width, height}
}
}
impl std::default::Default for ImageSize {
fn default() -> Self {
ImageSize{width: 0, height: 0}
ImageSize::new(0, 0)
}
}
#[derive(Serialize, Deserialize)]
pub struct PaletteRect {
pos: ImagePosition,
size: ImageSize,
pub pos: ImagePosition,
pub size: ImageSize,
}
impl PaletteRect {