From 8f61dadba0121568ec2cec3e022a06a7b411b176 Mon Sep 17 00:00:00 2001 From: SvenTS <73037851+SvenTS@users.noreply.github.com> Date: Tue, 24 Nov 2020 19:41:59 +0100 Subject: [PATCH] Further simplify system after change --- crates/bevy_ui/src/update.rs | 54 ++++++++++-------------------------- 1 file changed, 15 insertions(+), 39 deletions(-) diff --git a/crates/bevy_ui/src/update.rs b/crates/bevy_ui/src/update.rs index a9b4426c57d830..1681a277c2f670 100644 --- a/crates/bevy_ui/src/update.rs +++ b/crates/bevy_ui/src/update.rs @@ -10,17 +10,14 @@ pub fn ui_z_system( children_query: Query<&Children>, ) { let mut current_global_z = 0.0; - for entity in root_node_query.iter() { - if let Some(result) = update_hierarchy( + current_global_z = update_hierarchy( &children_query, &mut node_query, entity, current_global_z, - Some(current_global_z), - ) { - current_global_z = result; - } + current_global_z, + ); } } @@ -28,48 +25,27 @@ fn update_hierarchy( children_query: &Query<&Children>, node_query: &mut Query<&mut Transform, With>, entity: Entity, - parent_result: f32, - mut previous_result: Option, -) -> Option { - let parent_result = update_node_entity(node_query, entity, parent_result, previous_result); - previous_result = None; + parent_global_z: f32, + mut current_global_z: f32, +) -> f32 { + current_global_z += UI_Z_STEP; + let current_parent_global_z = current_global_z; + if let Ok(mut transform) = node_query.get_mut(entity) { + transform.translation.z = current_global_z - parent_global_z; + } if let Ok(children) = children_query.get(entity) { for child in children.iter().cloned() { - previous_result = update_hierarchy( + current_global_z = update_hierarchy( children_query, node_query, child, - parent_result, - previous_result, + current_parent_global_z, + current_global_z, ); } } - - previous_result.or(Some(parent_result)) + current_global_z } - -fn update_node_entity( - node_query: &mut Query<&mut Transform, With>, - entity: Entity, - parent_global_z: f32, - previous_result: Option, -) -> f32 { - if let Ok(mut transform) = node_query.get_mut(entity) { - let local_z = if let Some(previous_global_z) = previous_result { - previous_global_z - parent_global_z + UI_Z_STEP - } else { - UI_Z_STEP - }; - transform.translation.z = local_z; - } - - if let Some(previous_global_z) = previous_result { - previous_global_z + UI_Z_STEP - } else { - parent_global_z + UI_Z_STEP - } -} - #[cfg(test)] mod tests { use bevy_ecs::{Commands, IntoSystem, Resources, Schedule, World};