use bevy::prelude::*;
use bevy::remote::BrpResult;
use serde_json::{json, Value};
use crate::plugin::commands::DebugCommands;
use crate::plugin::scene::ScenePlayback;
#[derive(Resource)]
pub struct DebugReady(pub bool);
#[derive(Resource, Default)]
pub(crate) struct FrameTicks(pub u32);
pub(crate) fn tick_frames(mut ticks: ResMut<FrameTicks>) {
ticks.0 = ticks.0.wrapping_add(1);
}
pub(crate) fn handle_status(
In(_): In<Option<Value>>,
ready: Res<DebugReady>,
ticks: Res<FrameTicks>,
commands: Res<DebugCommands>,
playback: Option<Res<ScenePlayback>>,
#[cfg(feature = "plugin-screenshot")] target: Option<Res<crate::plugin::screenshot::DebugCaptureTarget>>,
#[cfg(feature = "plugin-screenshot")] windows: Query<(), With<bevy::window::PrimaryWindow>>,
) -> BrpResult {
let replay = match playback {
Some(p) => json!({
"path": p.path,
"done": p.next,
"total": p.steps.len(),
"failed": p.failed,
}),
None => Value::Null,
};
Ok(json!({
"ready": ready.0,
"frame": ticks.0,
"commands": commands.iter().count(),
"capture": capture_kind(
#[cfg(feature = "plugin-screenshot")]
target.is_some(),
#[cfg(feature = "plugin-screenshot")]
!windows.is_empty(),
),
"replay": replay,
}))
}
fn capture_kind(
#[cfg(feature = "plugin-screenshot")] offscreen: bool,
#[cfg(feature = "plugin-screenshot")] window: bool,
) -> &'static str {
#[cfg(feature = "plugin-screenshot")]
{
if offscreen {
return "offscreen";
}
if window {
return "window";
}
}
"none"
}