Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions crates/bevy_core_widgets/src/core_scrollbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use bevy_math::Vec2;
use bevy_picking::events::{Cancel, Drag, DragEnd, DragStart, Pointer, Press};
use bevy_reflect::{prelude::ReflectDefault, Reflect};
use bevy_ui::{
ComputedNode, ComputedUiTargetCamera, Node, ScrollPosition, UiGlobalTransform, UiScale, Val,
ComputedNode, ComputedUiRenderTargetInfo, Node, ScrollPosition, UiGlobalTransform, UiScale, Val,
};

/// Used to select the orientation of a scrollbar, slider, or other oriented control.
Expand Down Expand Up @@ -104,7 +104,7 @@ fn scrollbar_on_pointer_down(
mut q_scrollbar: Query<(
&CoreScrollbar,
&ComputedNode,
&ComputedUiTargetCamera,
&ComputedUiRenderTargetInfo,
&UiGlobalTransform,
)>,
mut q_scroll_pos: Query<(&mut ScrollPosition, &ComputedNode), Without<CoreScrollbar>>,
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_core_widgets/src/core_slider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use bevy_math::ops;
use bevy_picking::events::{Drag, DragEnd, DragStart, Pointer, Press};
use bevy_reflect::{prelude::ReflectDefault, Reflect};
use bevy_ui::{
ComputedNode, ComputedUiTargetCamera, InteractionDisabled, UiGlobalTransform, UiScale,
ComputedNode, ComputedUiRenderTargetInfo, InteractionDisabled, UiGlobalTransform, UiScale,
};

use crate::{Callback, Notify, ValueChange};
Expand Down Expand Up @@ -236,7 +236,7 @@ pub(crate) fn slider_on_pointer_down(
&SliderStep,
Option<&SliderPrecision>,
&ComputedNode,
&ComputedUiTargetCamera,
&ComputedUiRenderTargetInfo,
&UiGlobalTransform,
Has<InteractionDisabled>,
)>,
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ui/src/focus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ pub fn ui_focus_system(
}
return None;
}
let camera_entity = node.target_camera.camera()?;
let camera_entity = node.target_camera.get()?;

let cursor_position = camera_cursor_positions.get(&camera_entity);

Expand Down
16 changes: 13 additions & 3 deletions crates/bevy_ui/src/layout/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{
experimental::{UiChildren, UiRootNodes},
ui_transform::{UiGlobalTransform, UiTransform},
BorderRadius, ComputedNode, ComputedUiTargetCamera, ContentSize, Display, LayoutConfig, Node,
Outline, OverflowAxis, ScrollPosition,
BorderRadius, ComputedNode, ComputedUiRenderTargetInfo, ContentSize, Display, LayoutConfig,
Node, Outline, OverflowAxis, ScrollPosition,
};
use bevy_ecs::{
change_detection::{DetectChanges, DetectChangesMut},
Expand Down Expand Up @@ -77,7 +77,7 @@ pub fn ui_layout_system(
Entity,
Ref<Node>,
Option<&mut ContentSize>,
Ref<ComputedUiTargetCamera>,
Ref<ComputedUiRenderTargetInfo>,
)>,
added_node_query: Query<(), Added<Node>>,
mut node_update_query: Query<(
Expand Down Expand Up @@ -378,6 +378,9 @@ mod tests {
app.add_plugins(HierarchyPropagatePlugin::<ComputedUiTargetCamera>::new(
PostUpdate,
));
app.add_plugins(HierarchyPropagatePlugin::<ComputedUiRenderTargetInfo>::new(
PostUpdate,
));
app.init_resource::<UiScale>();
app.init_resource::<UiSurface>();
app.init_resource::<bevy_text::TextPipeline>();
Expand Down Expand Up @@ -405,6 +408,13 @@ mod tests {
.before(ui_layout_system),
);

app.configure_sets(
PostUpdate,
PropagateSet::<ComputedUiRenderTargetInfo>::default()
.after(propagate_ui_target_cameras)
.before(ui_layout_system),
);

let world = app.world_mut();
// spawn a dummy primary window and camera
world.spawn((
Expand Down
7 changes: 7 additions & 0 deletions crates/bevy_ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ impl Plugin for UiPlugin {
.add_plugins(HierarchyPropagatePlugin::<ComputedUiTargetCamera>::new(
PostUpdate,
))
.configure_sets(
PostUpdate,
PropagateSet::<ComputedUiRenderTargetInfo>::default().in_set(UiSystems::Propagate),
)
.add_plugins(HierarchyPropagatePlugin::<ComputedUiRenderTargetInfo>::new(
PostUpdate,
))
.add_systems(
PreUpdate,
ui_focus_system.in_set(UiSystems::Focus).after(InputSystems),
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ui/src/picking_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ pub fn ui_picking(
{
continue;
}
let Some(camera_entity) = node.target_camera.camera() else {
let Some(camera_entity) = node.target_camera.get() else {
continue;
};

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

let Some(camera_entity) = node.target_camera.camera() else {
let Some(camera_entity) = node.target_camera.get() else {
continue;
};

Expand Down
31 changes: 23 additions & 8 deletions crates/bevy_ui/src/ui_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ impl From<Vec2> for ScrollPosition {
#[require(
ComputedNode,
ComputedUiTargetCamera,
ComputedUiRenderTargetInfo,
UiTransform,
BackgroundColor,
BorderColor,
Expand Down Expand Up @@ -2806,29 +2807,43 @@ impl<'w, 's> DefaultUiCamera<'w, 's> {
#[reflect(Component, Default, PartialEq, Clone)]
pub struct ComputedUiTargetCamera {
pub(crate) camera: Entity,
/// The scale factor of the target camera's render target.
pub(crate) scale_factor: f32,
/// The size of the target camera's viewport in physical pixels.
pub(crate) physical_size: UVec2,
}

impl Default for ComputedUiTargetCamera {
fn default() -> Self {
Self {
camera: Entity::PLACEHOLDER,
scale_factor: 1.,
physical_size: UVec2::ZERO,
}
}
}

impl ComputedUiTargetCamera {
/// Returns the id of the target camera for this UI node.
pub fn camera(&self) -> Option<Entity> {
pub fn get(&self) -> Option<Entity> {
Some(self.camera).filter(|&entity| entity != Entity::PLACEHOLDER)
}
}

/// Derived information about the render target for this UI node.
#[derive(Component, Clone, Copy, Debug, Reflect, PartialEq)]
#[reflect(Component, Default, PartialEq, Clone)]
pub struct ComputedUiRenderTargetInfo {
/// The scale factor of the target camera's render target.
pub(crate) scale_factor: f32,
/// The size of the target camera's viewport in physical pixels.
pub(crate) physical_size: UVec2,
}

impl Default for ComputedUiRenderTargetInfo {
fn default() -> Self {
Self {
scale_factor: 1.,
physical_size: UVec2::ZERO,
}
}
}

/// Returns the scale factor of the target camera's render target.
impl ComputedUiRenderTargetInfo {
pub const fn scale_factor(&self) -> f32 {
self.scale_factor
}
Expand Down
Loading
Loading