Skip to content

Commit

Permalink
clip doll in viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
fralonra committed Aug 6, 2023
1 parent 5238ded commit 191654c
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 115 deletions.
16 changes: 8 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
17 changes: 8 additions & 9 deletions src/viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -23,12 +23,10 @@ struct ViewerApp {
viewport: Viewport,

ppd: Option<PaperdollFactory>,
paperdoll: Paperdoll,

textures_doll: HashMap<u32, TextureData>,
textures_fragment: HashMap<u32, TextureData>,
texture: Option<TextureData>,

actived_doll: u32,
slot_map: HashMap<u32, u32>,
slot_index_map: HashMap<u32, isize>,

window_about_visible: bool,
Expand All @@ -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,
Expand Down
28 changes: 15 additions & 13 deletions src/viewer/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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() {
Expand All @@ -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);
}
Expand All @@ -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 => {
Expand All @@ -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 {
Expand Down
105 changes: 21 additions & 84 deletions src/viewer/ui.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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),
);
}
});
}
Expand All @@ -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;
Expand Down Expand Up @@ -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,
);
}
});
}
Expand All @@ -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;
Expand Down Expand Up @@ -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<u32, TextureData>,
textures_fragment: &HashMap<u32, TextureData>,
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,
);
}
}

0 comments on commit 191654c

Please sign in to comment.