Skip to content

Insert Gizmos config instead of Init #11580

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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