Compare commits

...

5 Commits

8 changed files with 147 additions and 57 deletions

View File

@ -1,10 +1,10 @@
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;
use tim_tool::logic::{project::{ImagePosition, Job, PaletteRect, Project}, tim::types::Encoding}; use tim_tool::logic::{project::{ImagePosition, Job, PaletteRect, Project}, tim::types::Encoding, TIMManager};
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 {
@ -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, &mut tim_manager.lock().expect("VRAM already locked"), UnaddedTIM::new(&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,21 +54,37 @@ 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, &mut MainTab, &MainWindow) -> Result<(), Error> + 'static {
move |_file_tab, main_window| { move |_file_tab, main_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() {
return Err(Error::from_str("Please specify location for project file to open")); 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 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));
} }
}; };
let mut tim_manager = tim_manager.lock().expect("VRAM already locked");
if !main_window.get_project_widget_append_project_elements() {
clear_all_vram_images(main_tab, &mut tim_manager);
main_window.invoke_clear_file_tab_current_selected_file();
}
for job in new_project.jobs {
let (_, _) = tim_manager.load_unadded_tim(&job.file_path)?;
let (pal_width, pal_height) = job.palette_rect.size.into();
tim_manager.change_unadded_tim_palette_size(pal_width, pal_height)?;
add_unadded_tim(main_tab, &mut tim_manager, UnaddedTIM::from_job(&job))?;
}
main_window.invoke_change_to_main();
Ok(()) Ok(())
} }
} }
@ -102,7 +111,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 +131,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 +179,45 @@ pub(super) fn on_browse_save_project_clicked() -> impl FnMut(&mut FileTab, &Main
} }
} }
struct UnaddedTIM<'a> {
pub file_name: &'a String,
pub image_x: u16,
pub image_y: u16,
pub palette_x: u16,
pub palette_y: u16,
pub encoding: Encoding,
}
impl<'a> UnaddedTIM<'a> {
pub fn new(file_name: &String, encoding: Encoding,) -> UnaddedTIM {
UnaddedTIM{file_name, image_x: 0, image_y: 0, palette_x: 0, palette_y: 0, encoding}
}
pub fn from_job(job: &Job) -> UnaddedTIM {
UnaddedTIM{file_name: &job.name, image_x: job.image_pos.x, image_y: job.image_pos.y, palette_x: job.palette_rect.pos.x, palette_y: job.palette_rect.pos.y, encoding: job.encoding }
}
}
fn add_unadded_tim(main_tab: &mut MainTab, tim_manager: &mut TIMManager, unadded_tim: UnaddedTIM) -> Result<(), Error> {
let (image, palette_image) = tim_manager.get_converted_unadded_tim_image(unadded_tim.encoding)?;
let (full_image, _) = tim_manager.get_converted_unadded_tim_image(Encoding::FullColor)?;
let image = NewVRAMImageInfo::new(image, unadded_tim.image_x, unadded_tim.image_y, unadded_tim.encoding);
let palette_image = if let Some(palette_image) = palette_image { Some(NewVRAMImageInfo::new(palette_image, unadded_tim.palette_x, unadded_tim.palette_y, Encoding::FullColor)) } else { None };
let images_created = main_tab.add_new_vram_file(unadded_tim.file_name, full_image, image, palette_image);
if let Err(error) = tim_manager.add_unadded_tim(images_created) {
main_tab.pop_vram_files(images_created);
return Err(error);
}
Ok(())
}
fn clear_all_vram_images(main_tab: &mut MainTab, tim_manager: &mut TIMManager) {
tim_manager.clear();
main_tab.clear();
}
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"])

View File

@ -73,9 +73,9 @@ 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_tab, cloned_main_window, mut function) = (object.clone(), main_tab.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(), &mut cloned_main_tab.borrow_mut(), &cloned_main_window.borrow()) {
display_error("Loading project failed", &error.to_string()); display_error("Loading project failed", &error.to_string());
} }
}); });

View File

@ -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: u16,
pub y: u16,
pub encoding: Encoding,
}
impl NewVRAMImageInfo {
pub fn new(image: slint::Image, x: u16, y: u16, 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,27 @@ 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 clear(&mut self) {
let mut vram_data = self.vram.lock().expect("VRAM already locked");
let count = vram_data.count;
for _ in 0..count {
vram_data.pop();
}
}
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 as i32,
y: 0, y: vram_image.y as i32,
encoding_str: SharedString::from(encoding_str), encoding_str: SharedString::from(encoding_str),
palette_count, palette_count,
is_palette, is_palette,
@ -81,12 +103,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

View File

@ -20,7 +20,7 @@ impl std::default::Default for ImagePosition {
} }
} }
#[derive(Serialize, Deserialize)] #[derive(Clone, Copy, Serialize, Deserialize)]
pub struct ImageSize { pub struct ImageSize {
pub width: u16, pub width: u16,
pub height: u16, pub height: u16,
@ -32,6 +32,12 @@ impl ImageSize {
} }
} }
impl From<ImageSize> for (u16, u16) {
fn from(value: ImageSize) -> Self {
(value.width, value.height)
}
}
impl std::default::Default for ImageSize { impl std::default::Default for ImageSize {
fn default() -> Self { fn default() -> Self {
ImageSize::new(0, 0) ImageSize::new(0, 0)
@ -58,42 +64,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()
}
} }

View File

@ -13,6 +13,10 @@ impl TIMManager {
TIMManager{added_tims: Default::default(), unadded_tim: None} TIMManager{added_tims: Default::default(), unadded_tim: None}
} }
pub fn clear(&mut self) {
self.added_tims.clear();
}
pub fn remove_added_tim(&mut self, range: RangeInclusive<usize>) { pub fn remove_added_tim(&mut self, range: RangeInclusive<usize>) {
let idx = *range.start(); let idx = *range.start();
for _ in range { for _ in range {

View File

@ -14,6 +14,8 @@ export component MainWindow inherits Window {
// Project widget values // Project widget values
in-out property project_widget-open_project_path <=> file_tab.project_widget-open_project_path; in-out property project_widget-open_project_path <=> file_tab.project_widget-open_project_path;
in-out property project_widget-save_project_path <=> file_tab.project_widget-save_project_path; in-out property project_widget-save_project_path <=> file_tab.project_widget-save_project_path;
in-out property project_widget-append_project_elements <=> file_tab.project_widget-append_project_elements;
in-out property project_widget-use_rel_paths <=> file_tab.project_widget-use_rel_paths;
callback project_widget-load_project_clicked <=> file_tab.project_widget-load_project_clicked; callback project_widget-load_project_clicked <=> file_tab.project_widget-load_project_clicked;
callback project_widget-save_project_clicked <=> file_tab.project_widget-save_project_clicked; callback project_widget-save_project_clicked <=> file_tab.project_widget-save_project_clicked;
callback project_widget-browse_open_project_clicked <=> file_tab.project_widget-browse_open_project_clicked; callback project_widget-browse_open_project_clicked <=> file_tab.project_widget-browse_open_project_clicked;
@ -80,4 +82,8 @@ export component MainWindow inherits Window {
public function change_to_main() { public function change_to_main() {
tab_widget.current-index = 1; tab_widget.current-index = 1;
} }
public function clear_file_tab-current_selected_file() {
main_tab.clear_current_selection();
}
} }

View File

@ -8,6 +8,8 @@ export enum State {
component ProjectWidget inherits Rectangle { component ProjectWidget inherits Rectangle {
in-out property <string> open_project_path; in-out property <string> open_project_path;
in-out property <string> save_project_path; in-out property <string> save_project_path;
in-out property <bool> append_project_elements;
in-out property <bool> use_rel_paths;
callback load_project_clicked(); callback load_project_clicked();
callback save_project_clicked(); callback save_project_clicked();
@ -24,7 +26,7 @@ component ProjectWidget inherits Rectangle {
VerticalLayout { VerticalLayout {
alignment: start; alignment: start;
Text { Text {
text: "Select a project to open. Any existing changes will be dropped"; text: "Select a project to open.";
} }
HorizontalLayout { HorizontalLayout {
alignment: start; alignment: start;
@ -39,6 +41,15 @@ component ProjectWidget inherits Rectangle {
} }
} }
} }
HorizontalLayout {
alignment: start;
CheckBox {
text: "Append elements from project to existing elements";
toggled() => {
root.append_project_elements = self.checked;
}
}
}
HorizontalLayout { HorizontalLayout {
alignment: start; alignment: start;
Button { Button {
@ -72,7 +83,8 @@ component ProjectWidget inherits Rectangle {
} }
} }
CheckBox { CheckBox {
text: "Use relative path for files"; text: "Use relative path for files";
checked: root.use_rel_paths;
} }
HorizontalLayout { HorizontalLayout {
alignment: start; alignment: start;
@ -282,6 +294,8 @@ export component FileTab inherits Rectangle {
// For project widget // For project widget
in-out property <string> project_widget-open_project_path; in-out property <string> project_widget-open_project_path;
in-out property <string> project_widget-save_project_path; in-out property <string> project_widget-save_project_path;
in-out property <bool> project_widget-append_project_elements;
in-out property <bool> project_widget-use_rel_paths;
callback project_widget-load_project_clicked(); callback project_widget-load_project_clicked();
callback project_widget-save_project_clicked(); callback project_widget-save_project_clicked();
callback project_widget-browse_open_project_clicked(); callback project_widget-browse_open_project_clicked();
@ -332,8 +346,10 @@ export component FileTab inherits Rectangle {
padding: 4px; padding: 4px;
alignment: start; alignment: start;
if root.state == State.Project : ProjectWidget { if root.state == State.Project : ProjectWidget {
open_project_path <=> root.project_widget-open_project_path; open_project_path <=> root.project_widget-open_project_path;
save_project_path <=> root.project_widget-save_project_path; save_project_path <=> root.project_widget-save_project_path;
append_project_elements <=> root.project_widget-append_project_elements;
use_rel_paths <=> root.project_widget-use_rel_paths;
load_project_clicked() => { load_project_clicked() => {
root.project_widget-load_project_clicked(); root.project_widget-load_project_clicked();

View File

@ -179,10 +179,7 @@ export component MainTab inherits Rectangle {
text: "Remove file"; text: "Remove file";
clicked => { clicked => {
root.remove_file_clicked(vram_files_list.current_item); root.remove_file_clicked(vram_files_list.current_item);
vram_files_list.current-item = -1; root.clear_current_selection();
cur_sel_x.text = 0;
cur_sel_y.text = 0;
cur_sel_img.visible = false;
} }
} }
} }
@ -285,4 +282,11 @@ export component MainTab inherits Rectangle {
function get_border_width() -> int { function get_border_width() -> int {
return 4; return 4;
} }
public function clear_current_selection() {
vram_files_list.current-item = -1;
cur_sel_x.text = 0;
cur_sel_y.text = 0;
cur_sel_img.visible = false;
}
} }