From aae57e47e40a22e325ecbfc88744e2215b7403f1 Mon Sep 17 00:00:00 2001 From: Jaby Date: Wed, 19 Mar 2025 22:01:32 +0100 Subject: [PATCH] Implement writing image and half the platte information --- .../tim_tool/src/gui/file_tab/callbacks.rs | 11 +++- src/Tools/tim_tool/src/gui/file_tab/mod.rs | 2 +- src/Tools/tim_tool/src/logic/project/mod.rs | 65 ++++++++++++++++--- src/Tools/tim_tool/src/logic/tim/mod.rs | 18 +++-- src/Tools/tim_tool/src/logic/tim_mgr.rs | 2 +- src/Tools/tim_tool/ui/tab/main-tab.slint | 2 +- 6 files changed, 82 insertions(+), 18 deletions(-) diff --git a/src/Tools/tim_tool/src/gui/file_tab/callbacks.rs b/src/Tools/tim_tool/src/gui/file_tab/callbacks.rs index 2e60d382..7f689469 100644 --- a/src/Tools/tim_tool/src/gui/file_tab/callbacks.rs +++ b/src/Tools/tim_tool/src/gui/file_tab/callbacks.rs @@ -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, Project}, tim::types::Encoding}; +use tim_tool::logic::{project::{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 { @@ -34,7 +34,7 @@ pub(super) fn on_browse_file(tim_manager: MutexTIMManager) -> impl FnMut(&mut Fi }; } -pub(super) fn on_update_palette_size(tim_manager: MutexTIMManager) -> impl FnMut(&mut FileTab, &MainWindow, u32, u32) -> Result<(), Error> + 'static { +pub(super) fn on_update_palette_size(tim_manager: MutexTIMManager) -> impl FnMut(&mut FileTab, &MainWindow, u16, u16) -> Result<(), Error> + 'static { return move |file_tab, _main_window, width, height| -> Result<(), Error> { file_tab.update_palette(tim_manager.lock().expect("VRAM already locked").change_unadded_tim_palette_size(width, height)?); Ok(()) @@ -93,7 +93,12 @@ 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 { - cur_job = Some(Job::new(file_name, tim.get_path())); + 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_job = Some(job); } prev_job } diff --git a/src/Tools/tim_tool/src/gui/file_tab/mod.rs b/src/Tools/tim_tool/src/gui/file_tab/mod.rs index 1c06be47..4d0b0232 100644 --- a/src/Tools/tim_tool/src/gui/file_tab/mod.rs +++ b/src/Tools/tim_tool/src/gui/file_tab/mod.rs @@ -60,7 +60,7 @@ impl FileTab { let (cloned_object, cloned_main_window, mut function) = (object.clone(), main_window.clone(), callbacks::on_update_palette_size(tim_manager.clone())); main_window.borrow().on_file_tab_update_palette_size(move |width, height| { - if let Err(error) = function(&mut cloned_object.borrow_mut(), &cloned_main_window.borrow(), width as u32, height as u32) { + if let Err(error) = function(&mut cloned_object.borrow_mut(), &cloned_main_window.borrow(), width as u16, height as u16) { display_error("Updating palette failed", &error.to_string()); } }); diff --git a/src/Tools/tim_tool/src/logic/project/mod.rs b/src/Tools/tim_tool/src/logic/project/mod.rs index f7934a9a..240d080c 100644 --- a/src/Tools/tim_tool/src/logic/project/mod.rs +++ b/src/Tools/tim_tool/src/logic/project/mod.rs @@ -1,19 +1,68 @@ use serde::{Deserialize, Serialize}; use std::path::PathBuf; -#[derive(Debug, Serialize, Deserialize)] -pub struct Job { - name: String, - file_path: PathBuf, +#[derive(Serialize, Deserialize)] +struct ImagePosition { + pub x: u16, + pub y: u16, } -impl Job { - pub fn new(name: String, file_path: PathBuf) -> Job { - Job{name, file_path} +impl std::default::Default for ImagePosition { + fn default() -> Self { + ImagePosition{x: 0, y: 0} } } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Serialize, Deserialize)] +struct ImageSize { + pub width: u16, + pub height: u16, +} + +impl std::default::Default for ImageSize { + fn default() -> Self { + ImageSize{width: 0, height: 0} + } +} + +#[derive(Serialize, Deserialize)] +pub struct PaletteRect { + pos: ImagePosition, + size: ImageSize, +} + +impl PaletteRect { + pub fn new(x: u16, y: u16, width: u16, height: u16) -> PaletteRect { + PaletteRect{pos: ImagePosition{x, y}, size: ImageSize{width, height}} + } +} + +impl std::default::Default for PaletteRect { + fn default() -> Self { + PaletteRect{pos: ImagePosition::default(), size: ImageSize::default()} + } +} + +#[derive(Serialize, Deserialize)] +pub struct Job { + name: String, + file_path: PathBuf, + image_pos: ImagePosition, + palette_rect: PaletteRect, +} + +impl 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()} + } + + pub fn add_palette(&mut self, palette_rect: PaletteRect) -> &mut Self { + self.palette_rect = palette_rect; + self + } +} + +#[derive(Serialize, Deserialize)] pub struct Project { jobs: Vec } diff --git a/src/Tools/tim_tool/src/logic/tim/mod.rs b/src/Tools/tim_tool/src/logic/tim/mod.rs index 1321bd33..bb27bd7d 100644 --- a/src/Tools/tim_tool/src/logic/tim/mod.rs +++ b/src/Tools/tim_tool/src/logic/tim/mod.rs @@ -75,7 +75,7 @@ impl TIMInfo { } } - pub fn change_palette_size(&mut self, width: u32, height: u32) -> Result, Error> { + pub fn change_palette_size(&mut self, width: u16, height: u16) -> Result, Error> { if let Some(palette) = &mut self.palette { if width == 0 || height == 0 { return Err(Error::from_text(format!("{}px x {}px is not a valid size for palette", width, height))); @@ -119,6 +119,16 @@ impl TIMInfo { }) } + pub fn get_palette_size(&self) -> Option<(u16, u16)> { + if let Some(palette_info) = &self.palette { + Some((palette_info.width, palette_info.height)) + } + + else { + None + } + } + pub fn get_path(&self) -> std::path::PathBuf { self.path.clone() } @@ -127,8 +137,8 @@ impl TIMInfo { #[derive(Clone)] struct PaletteInfo { data: Vec, - width: u32, - height: u32, + width: u16, + height: u16, } impl PaletteInfo { @@ -138,7 +148,7 @@ impl PaletteInfo { } pub fn get_image(&self) -> slint::Image { - let mut image_data = SharedPixelBuffer::new(self.width, self.height); + let mut image_data = SharedPixelBuffer::new(self.width as u32, self.height as u32); let dst_pixels = image_data.make_mut_slice(); for (idx, byte) in dst_pixels.iter_mut().enumerate() { diff --git a/src/Tools/tim_tool/src/logic/tim_mgr.rs b/src/Tools/tim_tool/src/logic/tim_mgr.rs index 63c6e994..2476c173 100644 --- a/src/Tools/tim_tool/src/logic/tim_mgr.rs +++ b/src/Tools/tim_tool/src/logic/tim_mgr.rs @@ -60,7 +60,7 @@ impl TIMManager { } } - pub fn change_unadded_tim_palette_size(&mut self, width: u32, height: u32) -> Result, Error> { + pub fn change_unadded_tim_palette_size(&mut self, width: u16, height: u16) -> Result, Error> { if let Some(unadded_tim) = &mut self.unadded_tim { unadded_tim.change_palette_size(width, height) } diff --git a/src/Tools/tim_tool/ui/tab/main-tab.slint b/src/Tools/tim_tool/ui/tab/main-tab.slint index 1c7ef2c1..13a22654 100644 --- a/src/Tools/tim_tool/ui/tab/main-tab.slint +++ b/src/Tools/tim_tool/ui/tab/main-tab.slint @@ -11,7 +11,7 @@ struct VRAMInfo { y: int, encoding_str: string, palette_count: int, - is_palette: bool, + is_palette: bool, // < Can we combine the palette into this, instead of having it separate? } struct VRAMData {