1use bevy::{
4 prelude::*,
5 render::view::screenshot::{save_to_disk, Capturing, Screenshot},
6 window::{CursorIcon, SystemCursorIcon},
7};
8
9fn main() {
10 App::new()
11 .add_plugins(DefaultPlugins)
12 .add_systems(Startup, setup)
13 .add_systems(Update, (screenshot_on_spacebar, screenshot_saving))
14 .run();
15}
16
17fn screenshot_on_spacebar(
18 mut commands: Commands,
19 input: Res<ButtonInput<KeyCode>>,
20 mut counter: Local<u32>,
21) {
22 if input.just_pressed(KeyCode::Space) {
23 let path = format!("./screenshot-{}.png", *counter);
24 *counter += 1;
25 commands
26 .spawn(Screenshot::primary_window())
27 .observe(save_to_disk(path));
28 }
29}
30
31fn screenshot_saving(
32 mut commands: Commands,
33 screenshot_saving: Query<Entity, With<Capturing>>,
34 window: Single<Entity, With<Window>>,
35) {
36 match screenshot_saving.iter().count() {
37 0 => {
38 commands.entity(*window).remove::<CursorIcon>();
39 }
40 x if x > 0 => {
41 commands
42 .entity(*window)
43 .insert(CursorIcon::from(SystemCursorIcon::Progress));
44 }
45 _ => {}
46 }
47}
48
49fn setup(
51 mut commands: Commands,
52 mut meshes: ResMut<Assets<Mesh>>,
53 mut materials: ResMut<Assets<StandardMaterial>>,
54) {
55 commands.spawn((
57 Mesh3d(meshes.add(Plane3d::default().mesh().size(5.0, 5.0))),
58 MeshMaterial3d(materials.add(Color::srgb(0.3, 0.5, 0.3))),
59 ));
60 commands.spawn((
62 Mesh3d(meshes.add(Cuboid::default())),
63 MeshMaterial3d(materials.add(Color::srgb(0.8, 0.7, 0.6))),
64 Transform::from_xyz(0.0, 0.5, 0.0),
65 ));
66 commands.spawn((
68 PointLight {
69 shadows_enabled: true,
70 ..default()
71 },
72 Transform::from_xyz(4.0, 8.0, 4.0),
73 ));
74 commands.spawn((
76 Camera3d::default(),
77 Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
78 ));
79
80 commands.spawn((
81 Text::new("Press <spacebar> to save a screenshot to disk"),
82 Node {
83 position_type: PositionType::Absolute,
84 top: px(12),
85 left: px(12),
86 ..default()
87 },
88 ));
89}