From 2be646af80207f20493f26f498dd6f7c5a8ba9c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Mockers?= Date: Fri, 26 Sep 2025 23:56:42 +0200 Subject: [PATCH 1/2] easy screenshot plugin --- crates/bevy_dev_tools/Cargo.toml | 1 + crates/bevy_dev_tools/src/easy_screenshot.rs | 69 ++++++++++++++++++++ crates/bevy_dev_tools/src/lib.rs | 3 + 3 files changed, 73 insertions(+) create mode 100644 crates/bevy_dev_tools/src/easy_screenshot.rs 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..0c1df107de455 --- /dev/null +++ b/crates/bevy_dev_tools/src/easy_screenshot.rs @@ -0,0 +1,69 @@ +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. /// From 081bfd71d86d0e751c7e1a3804331f494fa960f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Mockers?= Date: Sat, 27 Sep 2025 00:11:08 +0200 Subject: [PATCH 2/2] clippy --- crates/bevy_dev_tools/src/easy_screenshot.rs | 36 +++++++++----------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/crates/bevy_dev_tools/src/easy_screenshot.rs b/crates/bevy_dev_tools/src/easy_screenshot.rs index 0c1df107de455..4bfacf995f261 100644 --- a/crates/bevy_dev_tools/src/easy_screenshot.rs +++ b/crates/bevy_dev_tools/src/easy_screenshot.rs @@ -43,26 +43,24 @@ impl Plugin for EasyScreenshotPlugin { 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"); + (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", - } - ))); - } - })() + 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)), ); }