Skip to content

Commit

Permalink
Rename TargetCamera to UiTargetCamera (#17403)
Browse files Browse the repository at this point in the history
# Objective

It's not immediately obvious that `TargetCamera` only works with UI node
entities. It's natural to assume from looking at something like the
`multiple_windows` example that it will work with everything.

## Solution

Rename `TargetCamera` to `UiTargetCamera`.

## Migration Guide

`TargetCamera` has been renamed to `UiTargetCamera`.
  • Loading branch information
ickshonpe authored Jan 19, 2025
1 parent 7c8da1c commit adc33b5
Show file tree
Hide file tree
Showing 18 changed files with 68 additions and 62 deletions.
2 changes: 1 addition & 1 deletion crates/bevy_dev_tools/src/picking_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ pub fn debug_draw(
},
))
.insert(Pickable::IGNORE)
.insert(TargetCamera(camera));
.insert(UiTargetCamera(camera));
}
}
}
6 changes: 3 additions & 3 deletions crates/bevy_ui/src/focus.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
CalculatedClip, ComputedNode, DefaultUiCamera, ResolvedBorderRadius, TargetCamera, UiStack,
CalculatedClip, ComputedNode, DefaultUiCamera, ResolvedBorderRadius, UiStack, UiTargetCamera,
};
use bevy_ecs::{
change_detection::DetectChangesMut,
Expand Down Expand Up @@ -141,7 +141,7 @@ pub struct NodeQuery {
focus_policy: Option<&'static FocusPolicy>,
calculated_clip: Option<&'static CalculatedClip>,
view_visibility: Option<&'static ViewVisibility>,
target_camera: Option<&'static TargetCamera>,
target_camera: Option<&'static UiTargetCamera>,
}

/// The system that sets Interaction for all UI elements based on the mouse cursor activity
Expand Down Expand Up @@ -239,7 +239,7 @@ pub fn ui_focus_system(
}
let camera_entity = node
.target_camera
.map(TargetCamera::entity)
.map(UiTargetCamera::entity)
.or(default_camera_entity)?;

let node_rect = Rect::from_center_size(
Expand Down
20 changes: 10 additions & 10 deletions crates/bevy_ui/src/layout/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
experimental::{UiChildren, UiRootNodes},
BorderRadius, ComputedNode, ContentSize, DefaultUiCamera, Display, LayoutConfig, Node, Outline,
OverflowAxis, ScrollPosition, TargetCamera, UiScale, Val,
OverflowAxis, ScrollPosition, UiScale, UiTargetCamera, Val,
};
use bevy_ecs::{
entity::{EntityHashMap, EntityHashSet},
Expand Down Expand Up @@ -105,7 +105,7 @@ pub fn ui_layout_system(
Entity,
Ref<Node>,
Option<&mut ContentSize>,
Option<&TargetCamera>,
Option<&UiTargetCamera>,
)>,
computed_node_query: Query<(Entity, Option<Ref<Parent>>), With<ComputedNode>>,
ui_children: UiChildren,
Expand All @@ -132,8 +132,8 @@ pub fn ui_layout_system(
let (cameras, default_ui_camera) = camera_data;

let default_camera = default_ui_camera.get();
let camera_with_default = |target_camera: Option<&TargetCamera>| {
target_camera.map(TargetCamera::entity).or(default_camera)
let camera_with_default = |target_camera: Option<&UiTargetCamera>| {
target_camera.map(UiTargetCamera::entity).or(default_camera)
};

resized_windows.clear();
Expand Down Expand Up @@ -165,7 +165,7 @@ pub fn ui_layout_system(
Some(camera_entity) => {
let Ok((_, camera)) = cameras.get(camera_entity) else {
warn!(
"TargetCamera (of root UI node {entity}) is pointing to a camera {} which doesn't exist",
"UiTargetCamera (of root UI node {entity}) is pointing to a camera {} which doesn't exist",
camera_entity
);
return;
Expand All @@ -181,7 +181,7 @@ pub fn ui_layout_system(
} else {
warn!(
"Multiple cameras found, causing UI target ambiguity. \
To fix this, add an explicit `TargetCamera` component to the root UI node {}",
To fix this, add an explicit `UiTargetCamera` component to the root UI node {}",
entity
);
}
Expand Down Expand Up @@ -621,7 +621,7 @@ mod tests {
let camera_entity = world.spawn(Camera2d).id();

let ui_entity = world
.spawn((Node::default(), TargetCamera(camera_entity)))
.spawn((Node::default(), UiTargetCamera(camera_entity)))
.id();

// `ui_layout_system` should map `camera_entity` to a ui node in `UiSurface::camera_entity_to_taffy`
Expand Down Expand Up @@ -926,7 +926,7 @@ mod tests {
for moving_ui_entity in moving_ui_query.iter() {
commands
.entity(moving_ui_entity)
.insert(TargetCamera(target_camera_entity))
.insert(UiTargetCamera(target_camera_entity))
.insert(Node {
position_type: PositionType::Absolute,
top: Val::Px(pos.y),
Expand All @@ -944,8 +944,8 @@ mod tests {
) {
world.run_system_once_with(move_ui_node, new_pos).unwrap();
ui_schedule.run(world);
let (ui_node_entity, TargetCamera(target_camera_entity)) = world
.query_filtered::<(Entity, &TargetCamera), With<MovingUiNode>>()
let (ui_node_entity, UiTargetCamera(target_camera_entity)) = world
.query_filtered::<(Entity, &UiTargetCamera), With<MovingUiNode>>()
.get_single(world)
.expect("missing MovingUiNode");
assert_eq!(expected_camera_entity, target_camera_entity);
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl Plugin for UiPlugin {
.register_type::<Node>()
.register_type::<RelativeCursorPosition>()
.register_type::<ScrollPosition>()
.register_type::<TargetCamera>()
.register_type::<UiTargetCamera>()
.register_type::<ImageNode>()
.register_type::<ImageNodeSize>()
.register_type::<UiRect>()
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_ui/src/picking_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub struct NodeQuery {
pickable: Option<&'static Pickable>,
calculated_clip: Option<&'static CalculatedClip>,
view_visibility: Option<&'static ViewVisibility>,
target_camera: Option<&'static TargetCamera>,
target_camera: Option<&'static UiTargetCamera>,
}

/// Computes the UI node entities under each pointer.
Expand Down Expand Up @@ -132,7 +132,7 @@ pub fn ui_picking(
}
let Some(camera_entity) = node
.target_camera
.map(TargetCamera::entity)
.map(UiTargetCamera::entity)
.or(default_camera_entity)
else {
continue;
Expand Down Expand Up @@ -188,7 +188,7 @@ pub fn ui_picking(
for node in node_query.iter_many(hovered_nodes) {
let Some(camera_entity) = node
.target_camera
.map(TargetCamera::entity)
.map(UiTargetCamera::entity)
.or(default_camera_entity)
else {
continue;
Expand Down
7 changes: 4 additions & 3 deletions crates/bevy_ui/src/render/box_shadow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::{hash::Hash, ops::Range};

use crate::{
BoxShadow, BoxShadowSamples, CalculatedClip, ComputedNode, DefaultUiCamera, RenderUiSystem,
ResolvedBorderRadius, TargetCamera, TransparentUi, Val,
ResolvedBorderRadius, TransparentUi, UiTargetCamera, Val,
};
use bevy_app::prelude::*;
use bevy_asset::*;
Expand Down Expand Up @@ -246,7 +246,7 @@ pub fn extract_shadows(
&ViewVisibility,
&BoxShadow,
Option<&CalculatedClip>,
Option<&TargetCamera>,
Option<&UiTargetCamera>,
)>,
>,
mapping: Extract<Query<RenderEntity>>,
Expand All @@ -255,7 +255,8 @@ pub fn extract_shadows(

for (entity, uinode, transform, view_visibility, box_shadow, clip, camera) in &box_shadow_query
{
let Some(camera_entity) = camera.map(TargetCamera::entity).or(default_camera_entity) else {
let Some(camera_entity) = camera.map(UiTargetCamera::entity).or(default_camera_entity)
else {
continue;
};

Expand Down
7 changes: 4 additions & 3 deletions crates/bevy_ui/src/render/debug_overlay.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::CalculatedClip;
use crate::ComputedNode;
use crate::DefaultUiCamera;
use crate::TargetCamera;
use crate::UiTargetCamera;
use bevy_asset::AssetId;
use bevy_color::Hsla;
use bevy_ecs::entity::Entity;
Expand Down Expand Up @@ -66,7 +66,7 @@ pub fn extract_debug_overlay(
&ViewVisibility,
Option<&CalculatedClip>,
&GlobalTransform,
Option<&TargetCamera>,
Option<&UiTargetCamera>,
)>,
>,
mapping: Extract<Query<RenderEntity>>,
Expand All @@ -82,7 +82,8 @@ pub fn extract_debug_overlay(
continue;
}

let Some(camera_entity) = camera.map(TargetCamera::entity).or(default_camera_entity) else {
let Some(camera_entity) = camera.map(UiTargetCamera::entity).or(default_camera_entity)
else {
continue;
};

Expand Down
20 changes: 11 additions & 9 deletions crates/bevy_ui/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod debug_overlay;
use crate::widget::ImageNode;
use crate::{
BackgroundColor, BorderColor, BoxShadowSamples, CalculatedClip, ComputedNode, DefaultUiCamera,
Outline, ResolvedBorderRadius, TargetCamera, UiAntiAlias,
Outline, ResolvedBorderRadius, UiAntiAlias, UiTargetCamera,
};
use bevy_app::prelude::*;
use bevy_asset::{load_internal_asset, AssetEvent, AssetId, Assets, Handle};
Expand Down Expand Up @@ -287,7 +287,7 @@ pub fn extract_uinode_background_colors(
&GlobalTransform,
&ViewVisibility,
Option<&CalculatedClip>,
Option<&TargetCamera>,
Option<&UiTargetCamera>,
&BackgroundColor,
)>,
>,
Expand All @@ -297,7 +297,8 @@ pub fn extract_uinode_background_colors(
for (entity, uinode, transform, view_visibility, clip, camera, background_color) in
&uinode_query
{
let Some(camera_entity) = camera.map(TargetCamera::entity).or(default_camera_entity) else {
let Some(camera_entity) = camera.map(UiTargetCamera::entity).or(default_camera_entity)
else {
continue;
};

Expand Down Expand Up @@ -349,15 +350,16 @@ pub fn extract_uinode_images(
&GlobalTransform,
&ViewVisibility,
Option<&CalculatedClip>,
Option<&TargetCamera>,
Option<&UiTargetCamera>,
&ImageNode,
)>,
>,
mapping: Extract<Query<RenderEntity>>,
) {
let default_camera_entity = default_ui_camera.get();
for (entity, uinode, transform, view_visibility, clip, camera, image) in &uinode_query {
let Some(camera_entity) = camera.map(TargetCamera::entity).or(default_camera_entity) else {
let Some(camera_entity) = camera.map(UiTargetCamera::entity).or(default_camera_entity)
else {
continue;
};

Expand Down Expand Up @@ -439,7 +441,7 @@ pub fn extract_uinode_borders(
&GlobalTransform,
&ViewVisibility,
Option<&CalculatedClip>,
Option<&TargetCamera>,
Option<&UiTargetCamera>,
AnyOf<(&BorderColor, &Outline)>,
)>,
>,
Expand All @@ -459,7 +461,7 @@ pub fn extract_uinode_borders(
) in &uinode_query
{
let Some(camera_entity) = maybe_camera
.map(TargetCamera::entity)
.map(UiTargetCamera::entity)
.or(default_camera_entity)
else {
continue;
Expand Down Expand Up @@ -678,7 +680,7 @@ pub fn extract_text_sections(
&GlobalTransform,
&ViewVisibility,
Option<&CalculatedClip>,
Option<&TargetCamera>,
Option<&UiTargetCamera>,
&ComputedTextBlock,
&TextLayoutInfo,
)>,
Expand All @@ -701,7 +703,7 @@ pub fn extract_text_sections(
text_layout_info,
) in &uinode_query
{
let Some(camera_entity) = camera.map(TargetCamera::entity).or(default_ui_camera) else {
let Some(camera_entity) = camera.map(UiTargetCamera::entity).or(default_ui_camera) else {
continue;
};

Expand Down
5 changes: 3 additions & 2 deletions crates/bevy_ui/src/render/ui_material_pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ pub fn extract_ui_material_nodes<M: UiMaterial>(
&MaterialNode<M>,
&ViewVisibility,
Option<&CalculatedClip>,
Option<&TargetCamera>,
Option<&UiTargetCamera>,
)>,
>,
mapping: Extract<Query<RenderEntity>>,
Expand All @@ -380,7 +380,8 @@ pub fn extract_ui_material_nodes<M: UiMaterial>(
let default_single_camera = default_ui_camera.get();

for (entity, uinode, transform, handle, view_visibility, clip, camera) in uinode_query.iter() {
let Some(camera_entity) = camera.map(TargetCamera::entity).or(default_single_camera) else {
let Some(camera_entity) = camera.map(UiTargetCamera::entity).or(default_single_camera)
else {
continue;
};

Expand Down
5 changes: 3 additions & 2 deletions crates/bevy_ui/src/render/ui_texture_slice_pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ pub fn extract_ui_texture_slices(
&GlobalTransform,
&ViewVisibility,
Option<&CalculatedClip>,
Option<&TargetCamera>,
Option<&UiTargetCamera>,
&ImageNode,
)>,
>,
Expand All @@ -265,7 +265,8 @@ pub fn extract_ui_texture_slices(
let default_camera_entity = default_ui_camera.get();

for (entity, uinode, transform, view_visibility, clip, camera, image) in &slicers_query {
let Some(camera_entity) = camera.map(TargetCamera::entity).or(default_camera_entity) else {
let Some(camera_entity) = camera.map(UiTargetCamera::entity).or(default_camera_entity)
else {
continue;
};

Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_ui/src/ui_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2613,9 +2613,9 @@ mod tests {
/// Optional if there is only one camera in the world. Required otherwise.
#[derive(Component, Clone, Debug, Reflect, Eq, PartialEq)]
#[reflect(Component, Debug, PartialEq)]
pub struct TargetCamera(pub Entity);
pub struct UiTargetCamera(pub Entity);

impl TargetCamera {
impl UiTargetCamera {
pub fn entity(&self) -> Entity {
self.0
}
Expand All @@ -2625,7 +2625,7 @@ impl TargetCamera {
///
/// This is useful if the [`PrimaryWindow`] has two cameras, one of them used
/// just for debug purposes and the user wants a way to choose the default [`Camera`]
/// without having to add a [`TargetCamera`] to the root node.
/// without having to add a [`UiTargetCamera`] to the root node.
///
/// Another use is when the user wants the Ui to be in another window by default,
/// all that is needed is to place this component on the camera
Expand All @@ -2649,7 +2649,7 @@ impl TargetCamera {
/// ..Default::default()
/// },
/// // We add the Marker here so all Ui will spawn in
/// // another window if no TargetCamera is specified
/// // another window if no UiTargetCamera is specified
/// IsDefaultUiCamera
/// ));
/// }
Expand Down
Loading

0 comments on commit adc33b5

Please sign in to comment.