Skip to content
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
11 changes: 9 additions & 2 deletions crates/bevy_ui_render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,9 @@ pub struct UiCameraMap<'w, 's> {
}

impl<'w, 's> UiCameraMap<'w, 's> {
/// Get the default camera and create the mapper
/// Creates a [`UiCameraMapper`] for performing repeated camera-to-render-entity lookups.
///
/// The last successful mapping is cached to avoid redundant queries.
pub fn get_mapper(&'w self) -> UiCameraMapper<'w, 's> {
UiCameraMapper {
mapping: &self.mapping,
Expand All @@ -311,14 +313,18 @@ impl<'w, 's> UiCameraMap<'w, 's> {
}
}

/// Helper for mapping UI target camera entities to their corresponding render entities,
/// with caching to avoid repeated lookups for the same camera.
pub struct UiCameraMapper<'w, 's> {
mapping: &'w Query<'w, 's, RenderEntity>,
/// Cached camera entity from the last successful `map` call.
camera_entity: Entity,
/// Cached camera entity from the last successful `map` call.
render_entity: Entity,
}

impl<'w, 's> UiCameraMapper<'w, 's> {
/// Returns the render entity corresponding to the given `UiTargetCamera` or the default camera if `None`.
/// Returns the render entity corresponding to the given [`ComputedUiTargetCamera`]'s camera, or none if no corresponding entity was found.
pub fn map(&mut self, computed_target: &ComputedUiTargetCamera) -> Option<Entity> {
let camera_entity = computed_target.camera()?;
if self.camera_entity != camera_entity {
Expand All @@ -330,6 +336,7 @@ impl<'w, 's> UiCameraMapper<'w, 's> {
Some(self.render_entity)
}

/// Returns the cached camera entity from the last successful `map` call.
pub fn current_camera(&self) -> Entity {
self.camera_entity
}
Expand Down
Loading