Skip to content

Commit 803a843

Browse files
authored
Split up ComputedUiTargetCamera (#20535)
# Objective Remove the render target info from `ComputedUiTargetCamera` so it just holds the camera id that is only needed for rendering and picking. Fixes #20534 ## Solution * Remove the render target info from `ComputedUiTargetCamera`. * Create a new component `ComputedUiRenderTargetInfo` and move the render target info (physical size and scale factor) into it. * Update the systems to use the `ComputedUiRenderTargetInfo` as needed. ## Testing The behaviour of the UI examples should be unchanged.
1 parent a76e0a2 commit 803a843

File tree

15 files changed

+124
-62
lines changed

15 files changed

+124
-62
lines changed

crates/bevy_core_widgets/src/core_scrollbar.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use bevy_math::Vec2;
1212
use bevy_picking::events::{Cancel, Drag, DragEnd, DragStart, Pointer, Press};
1313
use bevy_reflect::{prelude::ReflectDefault, Reflect};
1414
use bevy_ui::{
15-
ComputedNode, ComputedUiTargetCamera, Node, ScrollPosition, UiGlobalTransform, UiScale, Val,
15+
ComputedNode, ComputedUiRenderTargetInfo, Node, ScrollPosition, UiGlobalTransform, UiScale, Val,
1616
};
1717

1818
/// Used to select the orientation of a scrollbar, slider, or other oriented control.
@@ -104,7 +104,7 @@ fn scrollbar_on_pointer_down(
104104
mut q_scrollbar: Query<(
105105
&CoreScrollbar,
106106
&ComputedNode,
107-
&ComputedUiTargetCamera,
107+
&ComputedUiRenderTargetInfo,
108108
&UiGlobalTransform,
109109
)>,
110110
mut q_scroll_pos: Query<(&mut ScrollPosition, &ComputedNode), Without<CoreScrollbar>>,

crates/bevy_core_widgets/src/core_slider.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use bevy_math::ops;
2424
use bevy_picking::events::{Drag, DragEnd, DragStart, Pointer, Press};
2525
use bevy_reflect::{prelude::ReflectDefault, Reflect};
2626
use bevy_ui::{
27-
ComputedNode, ComputedUiTargetCamera, InteractionDisabled, UiGlobalTransform, UiScale,
27+
ComputedNode, ComputedUiRenderTargetInfo, InteractionDisabled, UiGlobalTransform, UiScale,
2828
};
2929

3030
use crate::{Callback, Notify, ValueChange};
@@ -236,7 +236,7 @@ pub(crate) fn slider_on_pointer_down(
236236
&SliderStep,
237237
Option<&SliderPrecision>,
238238
&ComputedNode,
239-
&ComputedUiTargetCamera,
239+
&ComputedUiRenderTargetInfo,
240240
&UiGlobalTransform,
241241
Has<InteractionDisabled>,
242242
)>,

crates/bevy_ui/src/focus.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ pub fn ui_focus_system(
235235
}
236236
return None;
237237
}
238-
let camera_entity = node.target_camera.camera()?;
238+
let camera_entity = node.target_camera.get()?;
239239

240240
let cursor_position = camera_cursor_positions.get(&camera_entity);
241241

crates/bevy_ui/src/layout/mod.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::{
22
experimental::{UiChildren, UiRootNodes},
33
ui_transform::{UiGlobalTransform, UiTransform},
4-
BorderRadius, ComputedNode, ComputedUiTargetCamera, ContentSize, Display, LayoutConfig, Node,
5-
Outline, OverflowAxis, ScrollPosition,
4+
BorderRadius, ComputedNode, ComputedUiRenderTargetInfo, ContentSize, Display, LayoutConfig,
5+
Node, Outline, OverflowAxis, ScrollPosition,
66
};
77
use bevy_ecs::{
88
change_detection::{DetectChanges, DetectChangesMut},
@@ -77,7 +77,7 @@ pub fn ui_layout_system(
7777
Entity,
7878
Ref<Node>,
7979
Option<&mut ContentSize>,
80-
Ref<ComputedUiTargetCamera>,
80+
Ref<ComputedUiRenderTargetInfo>,
8181
)>,
8282
added_node_query: Query<(), Added<Node>>,
8383
mut node_update_query: Query<(
@@ -378,6 +378,9 @@ mod tests {
378378
app.add_plugins(HierarchyPropagatePlugin::<ComputedUiTargetCamera>::new(
379379
PostUpdate,
380380
));
381+
app.add_plugins(HierarchyPropagatePlugin::<ComputedUiRenderTargetInfo>::new(
382+
PostUpdate,
383+
));
381384
app.init_resource::<UiScale>();
382385
app.init_resource::<UiSurface>();
383386
app.init_resource::<bevy_text::TextPipeline>();
@@ -405,6 +408,13 @@ mod tests {
405408
.before(ui_layout_system),
406409
);
407410

411+
app.configure_sets(
412+
PostUpdate,
413+
PropagateSet::<ComputedUiRenderTargetInfo>::default()
414+
.after(propagate_ui_target_cameras)
415+
.before(ui_layout_system),
416+
);
417+
408418
let world = app.world_mut();
409419
// spawn a dummy primary window and camera
410420
world.spawn((

crates/bevy_ui/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,13 @@ impl Plugin for UiPlugin {
159159
.add_plugins(HierarchyPropagatePlugin::<ComputedUiTargetCamera>::new(
160160
PostUpdate,
161161
))
162+
.configure_sets(
163+
PostUpdate,
164+
PropagateSet::<ComputedUiRenderTargetInfo>::default().in_set(UiSystems::Propagate),
165+
)
166+
.add_plugins(HierarchyPropagatePlugin::<ComputedUiRenderTargetInfo>::new(
167+
PostUpdate,
168+
))
162169
.add_systems(
163170
PreUpdate,
164171
ui_focus_system.in_set(UiSystems::Focus).after(InputSystems),

crates/bevy_ui/src/picking_backend.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ pub fn ui_picking(
180180
{
181181
continue;
182182
}
183-
let Some(camera_entity) = node.target_camera.camera() else {
183+
let Some(camera_entity) = node.target_camera.get() else {
184184
continue;
185185
};
186186

@@ -224,7 +224,7 @@ pub fn ui_picking(
224224
for (hovered_node, position) in hovered {
225225
let node = node_query.get(*hovered_node).unwrap();
226226

227-
let Some(camera_entity) = node.target_camera.camera() else {
227+
let Some(camera_entity) = node.target_camera.get() else {
228228
continue;
229229
};
230230

crates/bevy_ui/src/ui_node.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ impl From<Vec2> for ScrollPosition {
381381
#[require(
382382
ComputedNode,
383383
ComputedUiTargetCamera,
384+
ComputedUiRenderTargetInfo,
384385
UiTransform,
385386
BackgroundColor,
386387
BorderColor,
@@ -2806,29 +2807,43 @@ impl<'w, 's> DefaultUiCamera<'w, 's> {
28062807
#[reflect(Component, Default, PartialEq, Clone)]
28072808
pub struct ComputedUiTargetCamera {
28082809
pub(crate) camera: Entity,
2809-
/// The scale factor of the target camera's render target.
2810-
pub(crate) scale_factor: f32,
2811-
/// The size of the target camera's viewport in physical pixels.
2812-
pub(crate) physical_size: UVec2,
28132810
}
28142811

28152812
impl Default for ComputedUiTargetCamera {
28162813
fn default() -> Self {
28172814
Self {
28182815
camera: Entity::PLACEHOLDER,
2819-
scale_factor: 1.,
2820-
physical_size: UVec2::ZERO,
28212816
}
28222817
}
28232818
}
28242819

28252820
impl ComputedUiTargetCamera {
28262821
/// Returns the id of the target camera for this UI node.
2827-
pub fn camera(&self) -> Option<Entity> {
2822+
pub fn get(&self) -> Option<Entity> {
28282823
Some(self.camera).filter(|&entity| entity != Entity::PLACEHOLDER)
28292824
}
2825+
}
2826+
2827+
/// Derived information about the render target for this UI node.
2828+
#[derive(Component, Clone, Copy, Debug, Reflect, PartialEq)]
2829+
#[reflect(Component, Default, PartialEq, Clone)]
2830+
pub struct ComputedUiRenderTargetInfo {
2831+
/// The scale factor of the target camera's render target.
2832+
pub(crate) scale_factor: f32,
2833+
/// The size of the target camera's viewport in physical pixels.
2834+
pub(crate) physical_size: UVec2,
2835+
}
2836+
2837+
impl Default for ComputedUiRenderTargetInfo {
2838+
fn default() -> Self {
2839+
Self {
2840+
scale_factor: 1.,
2841+
physical_size: UVec2::ZERO,
2842+
}
2843+
}
2844+
}
28302845

2831-
/// Returns the scale factor of the target camera's render target.
2846+
impl ComputedUiRenderTargetInfo {
28322847
pub const fn scale_factor(&self) -> f32 {
28332848
self.scale_factor
28342849
}

0 commit comments

Comments
 (0)