diff --git a/Cargo.lock b/Cargo.lock index ecc8ab1..772aca8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -498,9 +498,9 @@ checksum = "6d43a04d8753f35258c91f8ec639f792891f748a1edbd759cf1dcea3382ad83c" [[package]] name = "cfg-expr" -version = "0.15.3" +version = "0.15.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "215c0072ecc28f92eeb0eea38ba63ddfcb65c2828c46311d646f1a3ff5f9841c" +checksum = "b40ccee03b5175c18cde8f37e7d2a33bcef6f8ec8f7cc0d81090d1bb380949c9" dependencies = [ "smallvec", "target-lexicon", @@ -2054,9 +2054,9 @@ dependencies = [ [[package]] name = "paperdoll" -version = "0.0.1-alpha.3" +version = "0.0.1-alpha.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "466925123837e113747a3fbf448901faca406c557ca1612b4ab24390a45fa14d" +checksum = "400d20c75b99bc127f2b6a22ec9139dfeae960d6ca60997c4deeea155426f7e0" dependencies = [ "anyhow", "serde", @@ -2064,9 +2064,9 @@ dependencies = [ [[package]] name = "paperdoll-tar" -version = "0.0.1-alpha.2" +version = "0.0.1-alpha.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d2b7bc128a023ad5e815b5a0dc95399449d17a6b412e9bfb66379e18d72329f" +checksum = "9c7273e9994755f0b7296c1faed06cf4d0b41e8f70520d35107639479785fee1" dependencies = [ "anyhow", "image", @@ -2779,9 +2779,9 @@ dependencies = [ [[package]] name = "target-lexicon" -version = "0.12.9" +version = "0.12.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df8e77cb757a61f51b947ec4a7e3646efd825b73561db1c232a8ccb639e611a0" +checksum = "9d0e916b1148c8e263850e1ebcbd046f333e0683c724876bb0da63ea4373dc8a" [[package]] name = "tempdir" diff --git a/Cargo.toml b/Cargo.toml index cbcf12c..b55b38e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,5 +20,5 @@ font-kit = "0.11.0" image = "0.24.6" log = "0.4.19" material-icons = { git = "https://github.com/fschutt/material-icons.git" } -paperdoll-tar = "0.0.1-alpha.2" +paperdoll-tar = "0.0.1-alpha.3" rfd = "0.11.4" diff --git a/src/viewer.rs b/src/viewer.rs index e895d7d..a34f3b2 100644 --- a/src/viewer.rs +++ b/src/viewer.rs @@ -6,7 +6,7 @@ mod ui; use std::collections::{HashMap, VecDeque}; use eframe::{egui::Context, App, CreationContext, Frame}; -use paperdoll_tar::paperdoll::factory::PaperdollFactory; +use paperdoll_tar::paperdoll::{factory::PaperdollFactory, paperdoll::Paperdoll}; use crate::{ common::{load_fonts, setup_style, TextureData}, @@ -23,12 +23,10 @@ struct ViewerApp { viewport: Viewport, ppd: Option, + paperdoll: Paperdoll, - textures_doll: HashMap, - textures_fragment: HashMap, + texture: Option, - actived_doll: u32, - slot_map: HashMap, slot_index_map: HashMap, window_about_visible: bool, @@ -52,12 +50,13 @@ impl ViewerApp { viewport: Viewport::default(), ppd: None, + paperdoll: Paperdoll { + doll: 0, + slot_map: HashMap::new(), + }, - textures_doll: HashMap::new(), - textures_fragment: HashMap::new(), + texture: None, - actived_doll: 0, - slot_map: HashMap::new(), slot_index_map: HashMap::new(), window_about_visible: false, diff --git a/src/viewer/actions.rs b/src/viewer/actions.rs index e244ad2..cca9e8d 100644 --- a/src/viewer/actions.rs +++ b/src/viewer/actions.rs @@ -3,7 +3,7 @@ use eframe::{egui::Context, epaint::Vec2, Frame}; use paperdoll_tar::paperdoll::factory::PaperdollFactory; use crate::{ - common::{allocate_size_fit_in_rect, upload_ppd_textures}, + common::{allocate_size_fit_in_rect, upload_image_to_texture}, fs::select_file, }; @@ -43,9 +43,9 @@ impl ViewerApp { let ppd = ppd.unwrap(); - self.actived_doll = 0; + self.paperdoll.doll = 0; + self.paperdoll.slot_map.clear(); - self.slot_map.clear(); self.slot_index_map.clear(); for (id, slot) in ppd.slots() { @@ -56,15 +56,14 @@ impl ViewerApp { } if let Some(fragment_id) = slot.candidates.first() { - self.slot_map.insert(*id, *fragment_id); + self.paperdoll.slot_map.insert(*id, *fragment_id); self.slot_index_map.insert(*id, 0); } } - let (textures_doll, textures_fragment) = upload_ppd_textures(&ppd, ctx); - - self.textures_doll = textures_doll; - self.textures_fragment = textures_fragment; + if let Ok(image) = ppd.render_paperdoll(&self.paperdoll) { + self.texture = Some(upload_image_to_texture(&image, &ppd.meta.name, ctx)); + } self.ppd = Some(ppd); } @@ -75,14 +74,17 @@ impl ViewerApp { if let Some(fragment_id) = slot.candidates.iter().nth(candidate_index as usize) { - self.slot_map.insert(slot_id, *fragment_id); - - continue; + self.paperdoll.slot_map.insert(slot_id, *fragment_id); } } + } else { + self.paperdoll.slot_map.remove(&slot_id); } - self.slot_map.remove(&slot_id); + if let Ok(image) = ppd.render_paperdoll(&self.paperdoll) { + self.texture = + Some(upload_image_to_texture(&image, &ppd.meta.name, ctx)); + } } } Action::ViewportCenter => { @@ -92,7 +94,7 @@ impl ViewerApp { let doll = self .ppd .as_ref() - .map(|ppd| ppd.get_doll(self.actived_doll)) + .map(|ppd| ppd.get_doll(self.paperdoll.doll)) .flatten(); if let Some(doll) = doll { diff --git a/src/viewer/ui.rs b/src/viewer/ui.rs index 7ca8dc1..f877599 100644 --- a/src/viewer/ui.rs +++ b/src/viewer/ui.rs @@ -1,17 +1,15 @@ -use std::collections::HashMap; - use eframe::{ egui::{ - scroll_area::ScrollBarVisibility, Button, CentralPanel, ComboBox, Context, Grid, Painter, + scroll_area::ScrollBarVisibility, Button, CentralPanel, ComboBox, Context, Grid, PointerButton, RichText, ScrollArea, Sense, SidePanel, TopBottomPanel, Ui, Window, }, emath::Align2, - epaint::{pos2, vec2, Color32, Rect, TextureId, Vec2}, + epaint::{pos2, vec2, Color32, Rect, Stroke, Vec2}, }; use material_icons::{icon_to_char, Icon}; -use paperdoll_tar::paperdoll::{doll::Doll, render_material::RenderMaterial, slot::Slot}; +use paperdoll_tar::paperdoll::{doll::Doll, slot::Slot}; -use crate::common::{determine_doll_rect, drag_move, TextureData}; +use crate::common::{determine_doll_rect, drag_move}; use super::{actions::Action, ViewerApp}; @@ -116,14 +114,18 @@ impl ViewerApp { ui.label("Doll: "); let doll_title = ppd - .get_doll(self.actived_doll) + .get_doll(self.paperdoll.doll) .map_or("Doll Not Found".to_owned(), map_doll_title); ComboBox::from_label("") .selected_text(doll_title) .show_ui(ui, |ui| { for (id, doll) in ppd.dolls() { - ui.selectable_value(&mut self.actived_doll, *id, map_doll_title(doll)); + ui.selectable_value( + &mut self.paperdoll.doll, + *id, + map_doll_title(doll), + ); } }); } @@ -142,7 +144,7 @@ impl ViewerApp { .show(ui, |ui| { let ppd = self.ppd.as_ref().unwrap(); - let doll = ppd.get_doll(self.actived_doll); + let doll = ppd.get_doll(self.paperdoll.doll); if doll.is_none() { return; @@ -187,16 +189,15 @@ impl ViewerApp { let painter = ui.painter_at(ui.max_rect()); - if let Ok(material) = ppd.render(doll.id(), &self.slot_map, true) { - render_paperdoll( - material, - &self.textures_doll, - &self.textures_fragment, - self.viewport.scale, - self.viewport.offset * self.viewport.scale - + (doll_rect.min - ui.max_rect().min), - &painter, - ) + painter.rect_stroke(doll_rect, 0.0, Stroke::new(1.0, Color32::from_gray(60))); + + if let Some(texture) = &self.texture { + painter.image( + texture.texture.id(), + doll_rect, + Rect::from([pos2(0.0, 0.0), pos2(1.0, 1.0)]), + Color32::WHITE, + ); } }); } @@ -208,7 +209,7 @@ impl ViewerApp { let ppd = self.ppd.as_ref().unwrap(); - let doll = ppd.get_doll(self.actived_doll); + let doll = ppd.get_doll(self.paperdoll.doll); if doll.is_none() { return; @@ -391,67 +392,3 @@ fn map_slot_title(slot: &Slot) -> String { .then_some(format!("Unnamed Slot - {}", slot.id())) .map_or(slot.desc.clone(), |s| s) } - -fn render_paperdoll( - material: RenderMaterial, - textures_doll: &HashMap, - textures_fragment: &HashMap, - scale: f32, - offset: Vec2, - painter: &Painter, -) { - let RenderMaterial { doll, slots, .. } = material; - - if let Some(piece) = doll { - if let Some(texture) = textures_doll.get(&piece.id) { - render_texture( - &texture.texture.id(), - piece.position.x, - piece.position.y, - piece.image.width as f32, - piece.image.height as f32, - scale, - offset, - painter, - ) - } - } - - for piece in slots { - if let Some(texture) = textures_fragment.get(&piece.id) { - render_texture( - &texture.texture.id(), - piece.position.x, - piece.position.y, - piece.image.width as f32, - piece.image.height as f32, - scale, - offset, - painter, - ) - } - } - - fn render_texture( - texture_id: &TextureId, - left: f32, - top: f32, - width: f32, - height: f32, - scale: f32, - offset: Vec2, - painter: &Painter, - ) { - let min = painter.clip_rect().min + vec2(left, top) * scale; - let max = min + vec2(width, height) * scale; - - let rect = Rect::from([min, max]).translate(offset); - - painter.image( - *texture_id, - rect, - Rect::from([pos2(0.0, 0.0), pos2(1.0, 1.0)]), - Color32::WHITE, - ); - } -}