diff --git a/crates/bevy_gizmos/src/gizmos.rs b/crates/bevy_gizmos/src/gizmos.rs index 0b81beb089af32..ceb53bfb11f5b9 100644 --- a/crates/bevy_gizmos/src/gizmos.rs +++ b/crates/bevy_gizmos/src/gizmos.rs @@ -35,79 +35,63 @@ pub(crate) struct GizmoStorage { /// A [`SystemParam`](bevy_ecs::system::SystemParam) for drawing gizmos. pub struct Gizmos<'s> { - buffer: &'s mut GizmoBuffer, -} - -#[derive(Default)] -struct GizmoBuffer { - /// Which fixed update tick this belongs to, `None` if this isn't from a fixed update. - fixed_time_update: Option, - list_positions: Vec, - list_colors: Vec, - strip_positions: Vec, - strip_colors: Vec, -} - -impl SystemBuffer for GizmoBuffer { - fn apply(&mut self, _system_meta: &SystemMeta, world: &mut World) { - let mut storages = world.resource_mut::(); - - let storage = if let Some(tick) = self.fixed_time_update { - // If a new fixed update has begun, clear gizmos from previous fixed update - if storages.fixed_update_tick < tick { - storages.fixed_update_tick = tick; - storages.fixed_update.list_positions.clear(); - storages.fixed_update.list_colors.clear(); - storages.fixed_update.strip_positions.clear(); - storages.fixed_update.strip_colors.clear(); - } - &mut storages.fixed_update - } else { - &mut storages.frame - }; - - storage.list_positions.append(&mut self.list_positions); - storage.list_colors.append(&mut self.list_colors); - storage.strip_positions.append(&mut self.strip_positions); - storage.strip_colors.append(&mut self.strip_colors); - } + buffer: &'s mut ::State, } // Wrap to keep GizmoBuffer hidden const _: () = { - pub struct Wrap(GizmoBuffer); + #[derive(Default)] + pub struct GizmoBuffer { + /// Which fixed update tick this belongs to, `None` if this isn't from a fixed update. + fixed_time_update: Option, + list_positions: Vec, + list_colors: Vec, + strip_positions: Vec, + strip_colors: Vec, + } // SAFETY: Only local state is accessed. unsafe impl SystemParam for Gizmos<'_> { - type State = Wrap; + type State = GizmoBuffer; type Item<'w, 's> = Gizmos<'s>; - fn init_state(world: &mut World, _system_meta: &mut SystemMeta) -> Self::State { - let fixed_time_update = world - .get_resource::() - .map(|current| current.update); - Wrap(GizmoBuffer { - fixed_time_update, - list_positions: default(), - list_colors: default(), - strip_positions: default(), - strip_colors: default(), - }) + fn init_state(_: &mut World, _system_meta: &mut SystemMeta) -> Self::State { + default() } - fn apply(state: &mut Self::State, system_meta: &SystemMeta, world: &mut World) { - state.0.apply(system_meta, world); + fn apply(state: &mut Self::State, _system_meta: &SystemMeta, world: &mut World) { + let mut storages = world.resource_mut::(); + + let storage = if let Some(tick) = state.fixed_time_update { + // If a new fixed update has begun, clear gizmos from previous fixed update + if storages.fixed_update_tick < tick { + storages.fixed_update_tick = tick; + storages.fixed_update.list_positions.clear(); + storages.fixed_update.list_colors.clear(); + storages.fixed_update.strip_positions.clear(); + storages.fixed_update.strip_colors.clear(); + } + &mut storages.fixed_update + } else { + &mut storages.frame + }; + + storage.list_positions.append(&mut state.list_positions); + storage.list_colors.append(&mut state.list_colors); + storage.strip_positions.append(&mut state.strip_positions); + storage.strip_colors.append(&mut state.strip_colors); } unsafe fn get_param<'w, 's>( state: &'s mut Self::State, _system_meta: &SystemMeta, - _world: UnsafeWorldCell<'w>, + world: UnsafeWorldCell<'w>, _change_tick: Tick, ) -> Self::Item<'w, 's> { - Gizmos { - buffer: &mut state.0, - } + state.fixed_time_update = world + .get_resource::() + .map(|current| current.update); + Gizmos { buffer: state } } } }; @@ -231,11 +215,8 @@ impl<'s> Gizmos<'s> { pub fn linestrip_gradient(&mut self, points: impl IntoIterator) { let points = points.into_iter(); - let GizmoBuffer { - strip_positions, - strip_colors, - .. - } = &mut *self.buffer; + let strip_positions = &mut self.buffer.strip_positions; + let strip_colors = &mut self.buffer.strip_colors; let (min, _) = points.size_hint(); strip_positions.reserve(min); diff --git a/examples/2d/2d_gizmos.rs b/examples/2d/2d_gizmos.rs index da7a639ba06bea..3d3b36243a5e50 100644 --- a/examples/2d/2d_gizmos.rs +++ b/examples/2d/2d_gizmos.rs @@ -7,8 +7,10 @@ use bevy::prelude::*; fn main() { App::new() .add_plugins(DefaultPlugins) + .insert_resource(FixedTime::new_from_secs(0.2)) .add_systems(Startup, setup) - .add_systems(Update, (system, update_config)) + .add_systems(Update, update_config) + .add_systems(FixedUpdate, system) .run(); }