Compare commits

..

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

8 changed files with 57 additions and 147 deletions

View File

@ -1,10 +1,10 @@
use std::{fs::File, io::Write, path::PathBuf};
use crate::{gui::{self, main_tab::{MainTab, NewVRAMImageInfo}, MutexTIMManager, VRAM_HEIGHT, VRAM_WIDTH}, MainWindow};
use crate::{gui::{self, main_tab::MainTab, MutexTIMManager, VRAM_HEIGHT, VRAM_WIDTH}, MainWindow};
use super::FileTab;
use rfd::FileDialog;
use slint::SharedString;
use tim_tool::logic::{project::{ImagePosition, Job, PaletteRect, Project}, tim::types::Encoding, TIMManager};
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 {
@ -45,8 +45,15 @@ pub(super) fn on_add_image(tim_manager: MutexTIMManager) -> impl FnMut(&mut File
return move |file_tab, main_tab, main_window| {
let file_name = file_tab.get_file_name();
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)?;
add_unadded_tim(main_tab, &mut tim_manager.lock().expect("VRAM already locked"), UnaddedTIM::new(&file_name, encoding))?;
let images_created = main_tab.add_new_vram_file(&file_name, full_image, image, encoding, palette_image);
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();
main_window.invoke_change_to_main();
@ -54,37 +61,21 @@ pub(super) fn on_add_image(tim_manager: MutexTIMManager) -> impl FnMut(&mut File
};
}
pub(super) fn on_load_project_clicked(tim_manager: MutexTIMManager) -> impl FnMut(&mut FileTab, &mut MainTab, &MainWindow) -> Result<(), Error> + 'static {
move |_file_tab, main_tab, main_window| {
pub(super) fn on_load_project_clicked() -> impl FnMut(&mut FileTab, &MainWindow) -> Result<(), Error> + 'static {
move |_file_tab, main_window| {
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) {
let _new_project = match serde_json::from_str::<Project>(&file_content) {
Ok(project) => project,
Err(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(())
}
}
@ -111,7 +102,7 @@ pub(super) fn on_save_project_clicked(tim_manager: MutexTIMManager) -> impl FnMu
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.palette_rect = cur_palette;
cur_job.add_palette(cur_palette);
}
}
None
@ -131,7 +122,7 @@ pub(super) fn on_save_project_clicked(tim_manager: MutexTIMManager) -> impl FnMu
}
}).collect());
if let Some(cur_job) = cur_job {
project.jobs.push(cur_job);
project.push(cur_job);
}
let json_content = match serde_json::to_string(&project) {
@ -179,45 +170,6 @@ 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 {
FileDialog::new()
.add_filter("TIM project file (.tim_project)", &["tim_project"])

View File

@ -73,9 +73,9 @@ impl FileTab {
});
// Project functions
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()));
let (cloned_object, cloned_main_window, mut function) = (object.clone(), main_window.clone(), callbacks::on_load_project_clicked());
main_window.borrow().on_project_widget_load_project_clicked(move || {
if let Err(error) = function(&mut cloned_object.borrow_mut(), &mut cloned_main_tab.borrow_mut(), &cloned_main_window.borrow()) {
if let Err(error) = function(&mut cloned_object.borrow_mut(), &cloned_main_window.borrow()) {
display_error("Loading project failed", &error.to_string());
}
});

View File

@ -6,19 +6,6 @@ use slint::{Model, SharedString};
use std::{cell::RefCell, ops::RangeInclusive, rc::Rc, sync::{Arc, Mutex}};
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 {
count: usize,
file_list: Rc<slint::VecModel<slint::StandardListViewItem>>,
@ -72,27 +59,18 @@ impl MainTab {
object
}
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 {
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 {
let mut vram_data = self.vram.lock().expect("VRAM already locked");
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 {vram_image.encoding.to_str()};
let mut add_new_image = |file_name: &String, full_image: slint::Image, image: slint::Image, encoding: Encoding, palette_count: i32, is_palette: bool| {
let encoding_str = if is_palette {"<Palette>"} else {encoding.to_str()};
let vram_image = VRAMData {
images: VRAMImgData {
full_image,
image: vram_image.image,
image
},
info: VRAMInfo {
x: vram_image.x as i32,
y: vram_image.y as i32,
x: 0,
y: 0,
encoding_str: SharedString::from(encoding_str),
palette_count,
is_palette,
@ -103,12 +81,12 @@ impl MainTab {
};
let mut images_added = 1;
add_new_image(file_name, full_image, texture, if image_palette.is_some() {1} else {0}, false);
add_new_image(file_name, full_image, image, encoding, if image_palette.is_some() {1} else {0}, false);
if let Some(image_palette) = image_palette {
let file_name = " => ".to_owned() + file_name.as_str() + " (Palette)";
images_added += 1;
add_new_image(&file_name, image_palette.image.clone(), image_palette, 0, true);
add_new_image(&file_name, image_palette.clone(), image_palette, encoding, 0, true);
}
images_added

View File

@ -20,7 +20,7 @@ impl std::default::Default for ImagePosition {
}
}
#[derive(Clone, Copy, Serialize, Deserialize)]
#[derive(Serialize, Deserialize)]
pub struct ImageSize {
pub width: u16,
pub height: u16,
@ -32,12 +32,6 @@ impl ImageSize {
}
}
impl From<ImageSize> for (u16, u16) {
fn from(value: ImageSize) -> Self {
(value.width, value.height)
}
}
impl std::default::Default for ImageSize {
fn default() -> Self {
ImageSize::new(0, 0)
@ -64,26 +58,42 @@ impl std::default::Default for PaletteRect {
#[derive(Serialize, Deserialize)]
pub struct Job {
pub name: String,
pub file_path: PathBuf,
pub image_pos: ImagePosition,
pub palette_rect: PaletteRect,
pub encoding: Encoding,
name: String,
file_path: PathBuf,
image_pos: ImagePosition,
palette_rect: PaletteRect,
encoding: Encoding,
}
impl 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}
}
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)]
pub struct Project {
pub jobs: Vec<Job>
jobs: Vec<Job>
}
impl Project {
pub fn new(jobs: Vec<Job>) -> Project {
Project{jobs}
}
pub fn push(&mut self, job: Job) {
self.jobs.push(job);
}
pub fn len(&self) -> usize {
self.jobs.len()
}
}

View File

@ -13,10 +13,6 @@ impl TIMManager {
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>) {
let idx = *range.start();
for _ in range {

View File

@ -14,8 +14,6 @@ export component MainWindow inherits Window {
// Project widget values
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-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-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;
@ -82,8 +80,4 @@ export component MainWindow inherits Window {
public function change_to_main() {
tab_widget.current-index = 1;
}
public function clear_file_tab-current_selected_file() {
main_tab.clear_current_selection();
}
}

View File

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

View File

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