Skip to content
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

Allow user to choose default ui camera #11436

Merged
merged 15 commits into from
Jan 27, 2024
Merged
27 changes: 18 additions & 9 deletions crates/bevy_ui/src/ui_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1844,22 +1844,31 @@ impl TargetCamera {
}
}

#[derive(Component)]
/// Marker used to demarcate default cameras, they will have priority over `PrimaryWindow` cameras
pablo-lua marked this conversation as resolved.
Show resolved Hide resolved
pablo-lua marked this conversation as resolved.
Show resolved Hide resolved
pablo-lua marked this conversation as resolved.
Show resolved Hide resolved
pub struct UiDefaultCamera;
pablo-lua marked this conversation as resolved.
Show resolved Hide resolved

#[derive(SystemParam)]
pub struct DefaultUiCamera<'w, 's> {
cameras: Query<'w, 's, (Entity, &'static Camera)>,
default_cameras: Query<'w, 's, Entity, (With<Camera>, With<UiDefaultCamera>)>,
primary_window: Query<'w, 's, Entity, With<PrimaryWindow>>,
}

impl<'w, 's> DefaultUiCamera<'w, 's> {
pub fn get(&self) -> Option<Entity> {
self.cameras
.iter()
.filter(|(_, c)| match c.target {
RenderTarget::Window(WindowRef::Primary) => true,
RenderTarget::Window(WindowRef::Entity(w)) => self.primary_window.get(w).is_ok(),
_ => false,
})
.max_by_key(|(e, c)| (c.order, *e))
.map(|(e, _)| e)
self.default_cameras.get_single().ok().or_else(|| {
pablo-lua marked this conversation as resolved.
Show resolved Hide resolved
pablo-lua marked this conversation as resolved.
Show resolved Hide resolved
self.cameras
.iter()
.filter(|(_, c)| match c.target {
RenderTarget::Window(WindowRef::Primary) => true,
RenderTarget::Window(WindowRef::Entity(w)) => {
self.primary_window.get(w).is_ok()
}
_ => false,
})
.max_by_key(|(e, c)| (c.order, *e))
.map(|(e, _)| e)
})
}
}