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