From ffff860c5a144c6a67768678f3719a23343eda06 Mon Sep 17 00:00:00 2001 From: Aceeri Date: Tue, 5 Mar 2024 16:10:51 -0800 Subject: [PATCH] Rename and clarify how to embed contexts inside of eachother --- crates/bevy_gizmos/src/circles.rs | 1 - crates/bevy_gizmos/src/gizmos.rs | 26 +++++++++++++------------ crates/bevy_gizmos/src/lib.rs | 32 +++++++++++++++++++------------ 3 files changed, 34 insertions(+), 25 deletions(-) diff --git a/crates/bevy_gizmos/src/circles.rs b/crates/bevy_gizmos/src/circles.rs index 4e756b5584f75b..af94ad9374b677 100644 --- a/crates/bevy_gizmos/src/circles.rs +++ b/crates/bevy_gizmos/src/circles.rs @@ -237,7 +237,6 @@ where segments: usize, } - impl Ellipse2dBuilder<'_, '_, '_, Config, Clear> where Config: GizmoConfigGroup, diff --git a/crates/bevy_gizmos/src/gizmos.rs b/crates/bevy_gizmos/src/gizmos.rs index 76c02af96c0168..eccdf47f77367e 100644 --- a/crates/bevy_gizmos/src/gizmos.rs +++ b/crates/bevy_gizmos/src/gizmos.rs @@ -78,11 +78,11 @@ where } } -/// Swap buffer for gizmos. +/// Swap buffer for a specific clearing context. /// -/// This is to stash/store the default gizmos so another context can +/// This is to stash/store the default/requested gizmos so another context can /// be substituted for that duration. -pub(crate) struct Swap; +pub struct Swap(PhantomData); /// A [`SystemParam`] for drawing gizmos. /// @@ -102,7 +102,7 @@ pub(crate) struct Swap; /// to [`FixedMain`](bevy_app::FixedMain)): /// /// ``` -/// use bevy_gizmos::{*, gizmos::GizmoStorage}; +/// use bevy_gizmos::{prelude::*, *, gizmos::GizmoStorage}; /// # use bevy_app::prelude::*; /// # use bevy_ecs::{schedule::ScheduleLabel, prelude::*}; /// # #[derive(ScheduleLabel, Clone, Debug, PartialEq, Eq, Hash)] @@ -117,16 +117,18 @@ pub(crate) struct Swap; /// struct ClearContextSetup; /// impl Plugin for ClearContextSetup { /// fn build(&self, app: &mut App) { -/// app.init_resource::>() -/// .add_systems(StartOfMyContext, stash_default_gizmos) -/// // If not running multiple times, put this with [`stash_default_gizmos`]. -/// .add_systems(StartOfRun, clear_gizmo_context::) -/// // If not running multiple times, put this with [`pop_default_gizmos`]. -/// .add_systems(EndOfRun, collect_default_gizmos::) -/// .add_systems(EndOfMyContext, pop_default_gizmos) +/// app.init_resource::>() +/// // Make sure this context starts/ends cleanly if inside another context. E.g. it +/// // should start after the parent context starts and end after the parent context ends. +/// .add_systems(StartOfMyContext, start_gizmo_context::) +/// // If not running multiple times, put this with [`start_gizmo_context`]. +/// .add_systems(StartOfRun, clear_gizmo_context::) +/// // If not running multiple times, put this with [`end_gizmo_context`]. +/// .add_systems(EndOfRun, collect_requested_gizmos::) +/// .add_systems(EndOfMyContext, end_gizmo_context::) /// .add_systems( /// Last, -/// propagate_gizmos::.before(UpdateGizmoMeshes), +/// propagate_gizmos::.before(UpdateGizmoMeshes), /// ); /// } /// } diff --git a/crates/bevy_gizmos/src/lib.rs b/crates/bevy_gizmos/src/lib.rs index add1f75fd1a26d..1ebff59ffa5292 100755 --- a/crates/bevy_gizmos/src/lib.rs +++ b/crates/bevy_gizmos/src/lib.rs @@ -187,18 +187,18 @@ impl AppGizmoBuilder for App { handles.list.insert(TypeId::of::(), None); handles.strip.insert(TypeId::of::(), None); - self.init_resource::>() - .init_resource::>() + self.init_resource::>() .init_resource::>() + .init_resource::>>() .add_systems( RunFixedMainLoop, - stash_default_gizmos::.before(bevy_time::run_fixed_main_schedule), + start_gizmo_context::.before(bevy_time::run_fixed_main_schedule), ) .add_systems(FixedFirst, clear_gizmo_context::) - .add_systems(FixedLast, collect_default_gizmos::) + .add_systems(FixedLast, collect_requested_gizmos::) .add_systems( RunFixedMainLoop, - pop_default_gizmos::.after(bevy_time::run_fixed_main_schedule), + end_gizmo_context::.after(bevy_time::run_fixed_main_schedule), ) .add_systems( Last, @@ -237,28 +237,36 @@ struct LineGizmoHandles { strip: TypeIdMap>>, } -/// Stash the default gizmos context in the [`Swap`] gizmo storage. -pub fn stash_default_gizmos(world: &mut World) +/// Start a new gizmo clearing context. +/// +/// Internally this pushes the parent default context into a swap buffer. +/// Gizmo contexts should be handled like a stack, so if you push a new context, +/// you must pop the context before the parent context ends. +pub fn start_gizmo_context(world: &mut World) where Config: GizmoConfigGroup, + Clear: 'static + Send + Sync, { world.resource_scope( - |world: &mut World, mut swap: Mut>| { + |world: &mut World, mut swap: Mut>>| { let mut default = world.resource_mut::>(); default.swap(&mut *swap); }, ); } -/// Pop the default gizmos context out of the [`Swap`] gizmo storage. +/// End this gizmo clearing context. +/// +/// Pop the default gizmos context out of the [`Swap`] gizmo storage. /// /// This must be called before [`UpdateGizmoMeshes`] in the [`Last`] schedule. -pub fn pop_default_gizmos(world: &mut World) +pub fn end_gizmo_context(world: &mut World) where Config: GizmoConfigGroup, + Clear: 'static + Send + Sync, { world.resource_scope( - |world: &mut World, mut swap: Mut>| { + |world: &mut World, mut swap: Mut>>| { let mut default = world.resource_mut::>(); default.clear(); default.swap(&mut *swap); @@ -267,7 +275,7 @@ where } /// Collect the requested gizmos into a specific clear context. -pub fn collect_default_gizmos(world: &mut World) +pub fn collect_requested_gizmos(world: &mut World) where Config: GizmoConfigGroup, Clear: 'static + Send + Sync,