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
6601 views
1
use super::config::*;
2
use bevy_app::AppExit;
3
use bevy_ecs::prelude::*;
4
use bevy_render::view::screenshot::{save_to_disk, Screenshot};
5
use tracing::{debug, info};
6
7
pub(crate) fn send_events(world: &mut World, mut current_frame: Local<u32>) {
8
let mut config = world.resource_mut::<CiTestingConfig>();
9
10
// Take all events for the current frame, leaving all the remaining alone.
11
let events = core::mem::take(&mut config.events);
12
let (to_run, remaining): (Vec<_>, _) = events
13
.into_iter()
14
.partition(|event| event.0 == *current_frame);
15
config.events = remaining;
16
17
for CiTestingEventOnFrame(_, event) in to_run {
18
debug!("Handling event: {:?}", event);
19
match event {
20
CiTestingEvent::AppExit => {
21
world.write_event(AppExit::Success);
22
info!("Exiting after {} frames. Test successful!", *current_frame);
23
}
24
CiTestingEvent::ScreenshotAndExit => {
25
let this_frame = *current_frame;
26
world.spawn(Screenshot::primary_window()).observe(
27
move |captured: On<bevy_render::view::screenshot::ScreenshotCaptured>,
28
mut exit_event: EventWriter<AppExit>| {
29
let path = format!("./screenshot-{this_frame}.png");
30
save_to_disk(path)(captured);
31
info!("Exiting. Test successful!");
32
exit_event.write(AppExit::Success);
33
},
34
);
35
info!("Took a screenshot at frame {}.", *current_frame);
36
}
37
CiTestingEvent::Screenshot => {
38
let path = format!("./screenshot-{}.png", *current_frame);
39
world
40
.spawn(Screenshot::primary_window())
41
.observe(save_to_disk(path));
42
info!("Took a screenshot at frame {}.", *current_frame);
43
}
44
CiTestingEvent::NamedScreenshot(name) => {
45
let path = format!("./screenshot-{name}.png");
46
world
47
.spawn(Screenshot::primary_window())
48
.observe(save_to_disk(path));
49
info!(
50
"Took a screenshot at frame {} for {}.",
51
*current_frame, name
52
);
53
}
54
// Custom events are forwarded to the world.
55
CiTestingEvent::Custom(event_string) => {
56
world.write_event(CiTestingCustomEvent(event_string));
57
}
58
}
59
}
60
61
*current_frame += 1;
62
}
63
64