Use Ref instead of &T and Changed<T> (#7175)

# Objective
Follow up #7097. Use `Ref<T>` instead of `&T` and the change detection query params.

## Solution
Replace them.
This commit is contained in:
James Liu 2023-01-12 22:39:59 +00:00
parent 689eab6fb7
commit b47c466880
2 changed files with 21 additions and 30 deletions

View File

@ -1,10 +1,10 @@
use bevy_asset::Assets; use bevy_asset::Assets;
use bevy_ecs::{ use bevy_ecs::{
bundle::Bundle, bundle::Bundle,
change_detection::{DetectChanges, Ref},
component::Component, component::Component,
entity::Entity, entity::Entity,
event::EventReader, event::EventReader,
query::Changed,
reflect::ReflectComponent, reflect::ReflectComponent,
system::{Commands, Local, Query, Res, ResMut}, system::{Commands, Local, Query, Res, ResMut},
}; };
@ -166,8 +166,7 @@ pub fn update_text2d_layout(
mut text_pipeline: ResMut<TextPipeline>, mut text_pipeline: ResMut<TextPipeline>,
mut text_query: Query<( mut text_query: Query<(
Entity, Entity,
Changed<Text>, Ref<Text>,
&Text,
Option<&Text2dBounds>, Option<&Text2dBounds>,
&mut Text2dSize, &mut Text2dSize,
Option<&mut TextLayoutInfo>, Option<&mut TextLayoutInfo>,
@ -177,10 +176,8 @@ pub fn update_text2d_layout(
let factor_changed = scale_factor_changed.iter().last().is_some(); let factor_changed = scale_factor_changed.iter().last().is_some();
let scale_factor = windows.scale_factor(WindowId::primary()); let scale_factor = windows.scale_factor(WindowId::primary());
for (entity, text_changed, text, maybe_bounds, mut calculated_size, text_layout_info) in for (entity, text, maybe_bounds, mut calculated_size, text_layout_info) in &mut text_query {
&mut text_query if factor_changed || text.is_changed() || queue.remove(&entity) {
{
if factor_changed || text_changed || queue.remove(&entity) {
let text_bounds = match maybe_bounds { let text_bounds = match maybe_bounds {
Some(bounds) => Vec2::new( Some(bounds) => Vec2::new(
scale_value(bounds.size.x, scale_factor), scale_value(bounds.size.x, scale_factor),

View File

@ -1,5 +1,8 @@
use crate::components::{GlobalTransform, Transform}; use crate::components::{GlobalTransform, Transform};
use bevy_ecs::prelude::{Changed, Entity, Query, With, Without}; use bevy_ecs::{
change_detection::Ref,
prelude::{Changed, DetectChanges, Entity, Query, With, Without},
};
use bevy_hierarchy::{Children, Parent}; use bevy_hierarchy::{Children, Parent};
/// Update [`GlobalTransform`] component of entities that aren't in the hierarchy /// Update [`GlobalTransform`] component of entities that aren't in the hierarchy
@ -24,32 +27,26 @@ pub fn sync_simple_transforms(
/// to propagate transforms correctly. /// to propagate transforms correctly.
pub fn propagate_transforms( pub fn propagate_transforms(
mut root_query: Query< mut root_query: Query<
( (Entity, Ref<Children>, Ref<Transform>, &mut GlobalTransform),
Entity,
&Children,
&Transform,
Changed<Transform>,
Changed<Children>,
&mut GlobalTransform,
),
Without<Parent>, Without<Parent>,
>, >,
transform_query: Query<(&Transform, Changed<Transform>, &mut GlobalTransform), With<Parent>>, transform_query: Query<(Ref<Transform>, &mut GlobalTransform), With<Parent>>,
parent_query: Query<&Parent>, parent_query: Query<&Parent>,
children_query: Query<(&Children, Changed<Children>), (With<Parent>, With<GlobalTransform>)>, children_query: Query<Ref<Children>, (With<Parent>, With<GlobalTransform>)>,
) { ) {
root_query.par_for_each_mut( root_query.par_for_each_mut(
// The differing depths and sizes of hierarchy trees causes the work for each root to be // The differing depths and sizes of hierarchy trees causes the work for each root to be
// different. A batch size of 1 ensures that each tree gets it's own task and multiple // different. A batch size of 1 ensures that each tree gets it's own task and multiple
// large trees are not clumped together. // large trees are not clumped together.
1, 1,
|(entity, children, transform, mut changed, children_changed, mut global_transform)| { |(entity, children, transform, mut global_transform)| {
let mut changed = transform.is_changed();
if changed { if changed {
*global_transform = GlobalTransform::from(*transform); *global_transform = GlobalTransform::from(*transform);
} }
// If our `Children` has changed, we need to recalculate everything below us // If our `Children` has changed, we need to recalculate everything below us
changed |= children_changed; changed |= children.is_changed();
for child in children.iter() { for child in children.iter() {
// SAFETY: // SAFETY:
@ -87,12 +84,9 @@ pub fn propagate_transforms(
/// nor any of its descendants. /// nor any of its descendants.
unsafe fn propagate_recursive( unsafe fn propagate_recursive(
parent: &GlobalTransform, parent: &GlobalTransform,
unsafe_transform_query: &Query< unsafe_transform_query: &Query<(Ref<Transform>, &mut GlobalTransform), With<Parent>>,
(&Transform, Changed<Transform>, &mut GlobalTransform),
With<Parent>,
>,
parent_query: &Query<&Parent>, parent_query: &Query<&Parent>,
children_query: &Query<(&Children, Changed<Children>), (With<Parent>, With<GlobalTransform>)>, children_query: &Query<Ref<Children>, (With<Parent>, With<GlobalTransform>)>,
expected_parent: Entity, expected_parent: Entity,
entity: Entity, entity: Entity,
mut changed: bool, mut changed: bool,
@ -106,7 +100,7 @@ unsafe fn propagate_recursive(
); );
let global_matrix = { let global_matrix = {
let Ok((transform, transform_changed, mut global_transform)) = let Ok((transform, mut global_transform)) =
// SAFETY: This call cannot create aliased mutable references. // SAFETY: This call cannot create aliased mutable references.
// - The top level iteration parallelizes on the roots of the hierarchy. // - The top level iteration parallelizes on the roots of the hierarchy.
// - The above assertion ensures that each child has one and only one unique parent throughout the entire // - The above assertion ensures that each child has one and only one unique parent throughout the entire
@ -137,19 +131,19 @@ unsafe fn propagate_recursive(
return; return;
}; };
changed |= transform_changed; changed |= transform.is_changed();
if changed { if changed {
*global_transform = parent.mul_transform(*transform); *global_transform = parent.mul_transform(*transform);
} }
*global_transform *global_transform
}; };
let Ok((children, changed_children)) = children_query.get(entity) else { let Ok(children) = children_query.get(entity) else {
return return
}; };
// If our `Children` has changed, we need to recalculate everything below us // If our `Children` has changed, we need to recalculate everything below us
changed |= changed_children; changed |= children.is_changed();
for child in children { for child in &children {
// SAFETY: The caller guarantees that `unsafe_transform_query` will not be fetched // SAFETY: The caller guarantees that `unsafe_transform_query` will not be fetched
// for any descendants of `entity`, so it is safe to call `propagate_recursive` for each child. // for any descendants of `entity`, so it is safe to call `propagate_recursive` for each child.
unsafe { unsafe {