Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_dev_tools/src/ci_testing/systems.rs
9308 views
1
use crate::CameraMovement;
2
3
use super::config::*;
4
use bevy_app::AppExit;
5
use bevy_camera::Camera;
6
use bevy_ecs::prelude::*;
7
use bevy_render::view::screenshot::{save_to_disk, Screenshot};
8
use tracing::{debug, info};
9
10
pub(crate) fn send_events(world: &mut World, mut current_frame: Local<u32>) {
11
let mut config = world.resource_mut::<CiTestingConfig>();
12
13
// Take all events for the current frame, leaving all the remaining alone.
14
let events = core::mem::take(&mut config.events);
15
let (to_run, remaining): (Vec<_>, _) = events
16
.into_iter()
17
.partition(|event| event.0 == *current_frame);
18
config.events = remaining;
19
20
for CiTestingEventOnFrame(_, event) in to_run {
21
debug!("Handling event: {:?}", event);
22
match event {
23
CiTestingEvent::AppExit => {
24
world.write_message(AppExit::Success);
25
info!("Exiting after {} frames. Test successful!", *current_frame);
26
}
27
CiTestingEvent::ScreenshotAndExit => {
28
let this_frame = *current_frame;
29
world.spawn(Screenshot::primary_window()).observe(
30
move |captured: On<bevy_render::view::screenshot::ScreenshotCaptured>,
31
mut app_exit_writer: MessageWriter<AppExit>| {
32
let path = format!("./screenshot-{this_frame}.png");
33
save_to_disk(path)(captured);
34
info!("Exiting. Test successful!");
35
app_exit_writer.write(AppExit::Success);
36
},
37
);
38
info!("Took a screenshot at frame {}.", *current_frame);
39
}
40
CiTestingEvent::Screenshot => {
41
let path = format!("./screenshot-{}.png", *current_frame);
42
world
43
.spawn(Screenshot::primary_window())
44
.observe(save_to_disk(path));
45
info!("Took a screenshot at frame {}.", *current_frame);
46
}
47
CiTestingEvent::NamedScreenshot(name) => {
48
let path = format!("./screenshot-{name}.png");
49
world
50
.spawn(Screenshot::primary_window())
51
.observe(save_to_disk(path));
52
info!(
53
"Took a screenshot at frame {} for {}.",
54
*current_frame, name
55
);
56
}
57
CiTestingEvent::StartScreenRecording => {
58
info!("Started recording screen at frame {}.", *current_frame);
59
#[cfg(feature = "screenrecording")]
60
world.write_message(crate::RecordScreen::Start);
61
}
62
CiTestingEvent::StopScreenRecording => {
63
info!("Stopped recording screen at frame {}.", *current_frame);
64
#[cfg(feature = "screenrecording")]
65
world.write_message(crate::RecordScreen::Stop);
66
}
67
CiTestingEvent::MoveCamera {
68
translation,
69
rotation,
70
} => {
71
info!("Moved camera at frame {}.", *current_frame);
72
if let Ok(camera) = world.query_filtered::<Entity, With<Camera>>().single(world) {
73
world.entity_mut(camera).insert(CameraMovement {
74
translation,
75
rotation,
76
});
77
}
78
}
79
// Custom events are forwarded to the world.
80
CiTestingEvent::Custom(event_string) => {
81
world.write_message(CiTestingCustomEvent(event_string));
82
}
83
}
84
}
85
86
*current_frame += 1;
87
}
88
89