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