From 397d111ea7b4353babdf448e175c24ebad935d12 Mon Sep 17 00:00:00 2001 From: pablo-lua <126117294+pablo-lua@users.noreply.github.com> Date: Sun, 28 Jan 2024 12:00:09 -0300 Subject: [PATCH] Insert Gizmos config instead of Init (#11580) # Objective - Fixes #11569 ## Solution - Add new methods to the Ext Trait --- ## Changelog ### Added - Added new methods to the trait `AppGizmoBuilder` --------- Co-authored-by: Alice Cecile --- crates/bevy_gizmos/src/lib.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/crates/bevy_gizmos/src/lib.rs b/crates/bevy_gizmos/src/lib.rs index 8696477ac7..c275eef712 100644 --- a/crates/bevy_gizmos/src/lib.rs +++ b/crates/bevy_gizmos/src/lib.rs @@ -146,6 +146,15 @@ pub trait AppGizmoBuilder { /// /// Configurations can be set using the [`GizmoConfigStore`] [`Resource`]. fn init_gizmo_group(&mut self) -> &mut Self; + + /// Insert the [`GizmoConfigGroup`] in the app with the given value and [`GizmoConfig`]. + /// + /// This method should be preferred over [`AppGizmoBuilder::init_gizmo_group`] if and only if you need to configure fields upon initialization. + fn insert_gizmo_group( + &mut self, + group: T, + config: GizmoConfig, + ) -> &mut Self; } impl AppGizmoBuilder for App { @@ -169,6 +178,31 @@ impl AppGizmoBuilder for App { self } + + fn insert_gizmo_group( + &mut self, + group: T, + config: GizmoConfig, + ) -> &mut Self { + if self.world.contains_resource::>() { + return self; + } + + self.init_resource::>() + .add_systems(Last, update_gizmo_meshes::); + + self.world + .get_resource_or_insert_with::(Default::default) + .insert(config, group); + + let Ok(render_app) = self.get_sub_app_mut(RenderApp) else { + return self; + }; + + render_app.add_systems(ExtractSchedule, extract_gizmo_data::); + + self + } } #[derive(Resource, Default)]