Skip to content

Commit

Permalink
Insert Gizmos config instead of Init (#11580)
Browse files Browse the repository at this point in the history
# 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 <alice.i.cecile@gmail.com>
  • Loading branch information
pablo-lua and alice-i-cecile authored Jan 28, 2024
1 parent 755917f commit 397d111
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions crates/bevy_gizmos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ pub trait AppGizmoBuilder {
///
/// Configurations can be set using the [`GizmoConfigStore`] [`Resource`].
fn init_gizmo_group<T: GizmoConfigGroup + Default>(&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<T: GizmoConfigGroup>(
&mut self,
group: T,
config: GizmoConfig,
) -> &mut Self;
}

impl AppGizmoBuilder for App {
Expand All @@ -169,6 +178,31 @@ impl AppGizmoBuilder for App {

self
}

fn insert_gizmo_group<T: GizmoConfigGroup>(
&mut self,
group: T,
config: GizmoConfig,
) -> &mut Self {
if self.world.contains_resource::<GizmoStorage<T>>() {
return self;
}

self.init_resource::<GizmoStorage<T>>()
.add_systems(Last, update_gizmo_meshes::<T>);

self.world
.get_resource_or_insert_with::<GizmoConfigStore>(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::<T>);

self
}
}

#[derive(Resource, Default)]
Expand Down

0 comments on commit 397d111

Please sign in to comment.