Skip to content

Commit

Permalink
Added ability to save screenshot to file
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanRace committed Nov 24, 2022
1 parent 72a85bf commit 9a5d132
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ assets/fonts/lato.zip
*.anno

*.xml

*.tif
116 changes: 116 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ image = "0.24.1"
tiff = "0.7.4"
imageproc = "0.23"
rfd = "0.10"
chrono = "0.4.23"

# Interacting with clipboard
arboard = "3.2.0"
Expand Down
16 changes: 13 additions & 3 deletions src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use bevy::{
window::{WindowId, WindowResized},
};
use bevy_egui::EguiContext;
use image::RgbaImage;

use crate::{
image_copy::{ImageCopier, ImageCopyPlugin},
Expand Down Expand Up @@ -51,7 +52,7 @@ impl Plugin for CameraPlugin {
// .after(UiLabel::Display),
)
.add_system(ui_changed)
.add_system(copy_to_clipboard.before("issue_camera_commands")) // This should be before handling camera events, to force it to be run on the next frame - otherwise the screenshot is empty
.add_system(save_view_to_target.before("issue_camera_commands")) // This should be before handling camera events, to force it to be run on the next frame - otherwise the screenshot is empty
.add_system_to_stage(CoreStage::Update, update_camera)
.add_system(issue_camera_commands.label("issue_camera_commands"))
.add_system(
Expand Down Expand Up @@ -213,7 +214,7 @@ fn issue_camera_commands(
}
}

fn copy_to_clipboard(
fn save_view_to_target(
mut commands: Commands,
q_copier: Query<(Entity, &ImageCopier, &SaveToTarget)>,
camera_setup: Res<CameraSetup>,
Expand Down Expand Up @@ -272,7 +273,16 @@ fn copy_to_clipboard(
});
}
}
SaveToTarget::File(path) => {}
SaveToTarget::File(path) => {
if let Some(image) = RgbaImage::from_vec(size.x as u32, size.y as u32, data) {
if let Err(error) = image.save(path) {
commands.spawn(Message {
severity: Severity::Error,
message: error.to_string(),
});
}
}
}
}

commands.entity(entity).despawn_recursive();
Expand Down
14 changes: 14 additions & 0 deletions src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use bevy_egui::{
},
EguiContext, EguiPlugin, EguiSettings,
};
use chrono::{DateTime, Datelike, Local, Timelike, Utc};
use imc_rs::ChannelIdentifier;

use crate::{
Expand Down Expand Up @@ -847,7 +848,20 @@ fn ui_camera_panel(world: &mut World, ui: &mut Ui) {
camera_events.push(CameraCommand::SaveTo(SaveToTarget::Clipboard));
}
if ui.button("Save view as file").clicked() {
// let now = Utc::now();
let now: DateTime<Local> = Local::now();
let (is_common_era, year) = now.year_ce();

if let Some(path) = rfd::FileDialog::new()
.set_file_name(&format!(
"{}-{:02}-{:02}_{}-{}-{}_biquinho-screenshot.tif",
year,
now.month(),
now.day(),
now.hour(),
now.minute(),
now.second()
))
.add_filter("TIFF (.tif, .tiff)", &["tif", "tiff"])
.add_filter("JPEG (.jpg, .jpeg)", &["jpg", "jpeg"])
.save_file()
Expand Down

0 comments on commit 9a5d132

Please sign in to comment.