Skip to content

Commit

Permalink
Fix UI node Transform change detection (#4138)
Browse files Browse the repository at this point in the history
# Objective

Fixes #4133 

## Solution

Add comparisons to make sure we don't dereference `Mut<>` in the two places where `Transform` is being mutated. `GlobalTransform` implementation already works properly so fixing Transform automatically fixed that as well.
  • Loading branch information
oceantume committed Mar 8, 2022
1 parent b4483db commit e41c5c2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
15 changes: 10 additions & 5 deletions crates/bevy_ui/src/flex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,17 +278,22 @@ pub fn flex_node_system(
to_logical(layout.size.width),
to_logical(layout.size.height),
);
// only trigger change detection when the new value is different
if node.size != new_size {
node.size = new_size;
}
let position = &mut transform.translation;
position.x = to_logical(layout.location.x + layout.size.width / 2.0);
position.y = to_logical(layout.location.y + layout.size.height / 2.0);
let mut new_position = transform.translation;
new_position.x = to_logical(layout.location.x + layout.size.width / 2.0);
new_position.y = to_logical(layout.location.y + layout.size.height / 2.0);
if let Some(parent) = parent {
if let Ok(parent_layout) = flex_surface.get_layout(parent.0) {
position.x -= to_logical(parent_layout.size.width / 2.0);
position.y -= to_logical(parent_layout.size.height / 2.0);
new_position.x -= to_logical(parent_layout.size.width / 2.0);
new_position.y -= to_logical(parent_layout.size.height / 2.0);
}
}
// only trigger change detection when the new value is different
if transform.translation != new_position {
transform.translation = new_position;
}
}
}
6 changes: 5 additions & 1 deletion crates/bevy_ui/src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ fn update_hierarchy(
) -> f32 {
current_global_z += UI_Z_STEP;
if let Ok(mut transform) = node_query.get_mut(entity) {
transform.translation.z = current_global_z - parent_global_z;
let new_z = current_global_z - parent_global_z;
// only trigger change detection when the new value is different
if transform.translation.z != new_z {
transform.translation.z = new_z;
}
}
if let Ok(children) = children_query.get(entity) {
let current_parent_global_z = current_global_z;
Expand Down

0 comments on commit e41c5c2

Please sign in to comment.