Skip to content

Commit

Permalink
viewer: add export feature
Browse files Browse the repository at this point in the history
  • Loading branch information
fralonra committed Dec 4, 2024
1 parent f903ec8 commit 317a23d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ pub fn create_file(filename: &str) -> Option<PathBuf> {
.save_file()
}

pub fn export_texture(filename: &str) -> Option<PathBuf> {
FileDialog::new()
.set_directory("~")
.add_filter("Images", &["png", "jpg", "jpeg", "webp"])
.set_file_name(filename)
.save_file()
}

pub fn open_image_rgba<P>(path: P) -> Result<ImageData>
where
P: AsRef<Path>,
Expand Down
19 changes: 18 additions & 1 deletion src/viewer/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ use paperdoll_tar::paperdoll::PaperdollFactory;

use crate::{
common::{allocate_size_fit_in_rect, upload_image_to_texture},
fs::select_file,
fs::{export_texture, select_file},
};

use super::ViewerApp;

pub enum Action {
AppQuit,
DollChanged,
Export,
FileOpen,
FileOpenPath(PathBuf),
PpdChanged(Option<PaperdollFactory>),
Expand All @@ -36,6 +37,22 @@ impl ViewerApp {
Action::DollChanged => {
self.actions.push_back(Action::TextureUpdate);
}
Action::Export => {
let Some(ppd) = &self.ppd else {
continue;
};

if let Some(path) = export_texture(&format!("{}.png", ppd.meta.name)) {
let image = ppd.render_paperdoll(&self.paperdoll)?;
image::save_buffer(
path,
&image.pixels,
image.width,
image.height,
image::ColorType::Rgba8,
)?;
}
}
Action::FileOpen => {
if let Some(path) = select_file() {
self.load_ppd_from_path(&path)?;
Expand Down
8 changes: 8 additions & 0 deletions src/viewer/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ impl ViewerApp {

ui.separator();

if ui.button("Export").clicked() {
self.actions.push_back(Action::Export);

ui.close_menu();
}

ui.separator();

if ui
.add(
Button::new("Quit")
Expand Down

0 comments on commit 317a23d

Please sign in to comment.