diff --git a/crates/bevy_dev_tools/Cargo.toml b/crates/bevy_dev_tools/Cargo.toml index 14a72cf121d46..8c78426c497fd 100644 --- a/crates/bevy_dev_tools/Cargo.toml +++ b/crates/bevy_dev_tools/Cargo.toml @@ -19,6 +19,7 @@ bevy_camera = { path = "../bevy_camera", version = "0.18.0-dev" } bevy_color = { path = "../bevy_color", version = "0.18.0-dev" } bevy_diagnostic = { path = "../bevy_diagnostic", version = "0.18.0-dev" } bevy_ecs = { path = "../bevy_ecs", version = "0.18.0-dev" } +bevy_input = { path = "../bevy_input", version = "0.18.0-dev" } bevy_math = { path = "../bevy_math", version = "0.18.0-dev" } bevy_picking = { path = "../bevy_picking", version = "0.18.0-dev" } bevy_render = { path = "../bevy_render", version = "0.18.0-dev" } diff --git a/crates/bevy_dev_tools/src/easy_screenshot.rs b/crates/bevy_dev_tools/src/easy_screenshot.rs new file mode 100644 index 0000000000000..4bfacf995f261 --- /dev/null +++ b/crates/bevy_dev_tools/src/easy_screenshot.rs @@ -0,0 +1,67 @@ +use std::time::{SystemTime, UNIX_EPOCH}; + +use bevy_app::{App, Plugin, Update}; +use bevy_ecs::prelude::*; +use bevy_input::{common_conditions::input_just_pressed, keyboard::KeyCode}; +use bevy_render::view::screenshot::{save_to_disk, Screenshot}; +use bevy_window::{PrimaryWindow, Window}; + +/// File format the screenshot will be saved in +#[derive(Clone, Copy)] +pub enum ScreenshotFormat { + /// JPEG format + Jpeg, + /// PNG format + Png, + /// BMP format + Bmp, +} + +/// Add this plugin to your app to enable easy screenshotting. +/// +/// Add this plugin, press the key, and you have a screenshot 🎉 +pub struct EasyScreenshotPlugin { + /// Key that will trigger a screenshot + pub trigger: KeyCode, + /// Format of the screenshot + /// + /// The corresponding image format must be supported by bevy renderer + pub format: ScreenshotFormat, +} + +impl Default for EasyScreenshotPlugin { + fn default() -> Self { + EasyScreenshotPlugin { + trigger: KeyCode::PrintScreen, + format: ScreenshotFormat::Png, + } + } +} + +impl Plugin for EasyScreenshotPlugin { + fn build(&self, app: &mut App) { + let format = self.format; + app.add_systems( + Update, + (move |mut commands: Commands, window: Single<&Window, With>| { + let since_the_epoch = SystemTime::now() + .duration_since(UNIX_EPOCH) + .expect("time should go forward"); + + commands + .spawn(Screenshot::primary_window()) + .observe(save_to_disk(format!( + "{}-{}.{}", + window.title, + since_the_epoch.as_millis(), + match format { + ScreenshotFormat::Jpeg => "jpg", + ScreenshotFormat::Png => "png", + ScreenshotFormat::Bmp => "bmp", + } + ))); + }) + .run_if(input_just_pressed(self.trigger)), + ); + } +} diff --git a/crates/bevy_dev_tools/src/lib.rs b/crates/bevy_dev_tools/src/lib.rs index 8efea87f0007c..dd1e33363be7d 100644 --- a/crates/bevy_dev_tools/src/lib.rs +++ b/crates/bevy_dev_tools/src/lib.rs @@ -13,6 +13,7 @@ use bevy_app::prelude::*; #[cfg(feature = "bevy_ci_testing")] pub mod ci_testing; +mod easy_screenshot; pub mod fps_overlay; pub mod frame_time_graph; @@ -20,6 +21,8 @@ pub mod picking_debug; pub mod states; +pub use easy_screenshot::*; + /// Enables developer tools in an [`App`]. This plugin is added automatically with `bevy_dev_tools` /// feature. ///