Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/bevy_dev_tools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
67 changes: 67 additions & 0 deletions crates/bevy_dev_tools/src/easy_screenshot.rs
Original file line number Diff line number Diff line change
@@ -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<PrimaryWindow>>| {
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)),
);
}
}
3 changes: 3 additions & 0 deletions crates/bevy_dev_tools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ 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;

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.
///
Expand Down
Loading