bevy/crates/bevy_ui/src/update.rs
bjorn3 d6eb647451
Misc cleanups (#879)
* Remove cfg!(feature = "metal-auto-capture")

This cfg! has existed since the initial commit, but the corresponding
feature has never been part of Cargo.toml

* Remove unnecessary handle_create_window_events call

* Remove EventLoopProxyPtr wrapper

* Remove unnecessary statics

* Fix unrelated deprecation warning to fix CI
2020-11-17 13:40:18 -08:00

49 lines
1.3 KiB
Rust

use super::Node;
use bevy_ecs::{Entity, Query, With, Without};
use bevy_transform::{
hierarchy,
prelude::{Children, Parent, Transform},
};
pub const UI_Z_STEP: f32 = 0.001;
pub fn ui_z_system(
root_node_query: Query<Entity, (With<Node>, Without<Parent>)>,
mut node_query: Query<(Entity, &mut Transform), With<Node>>,
children_query: Query<&Children>,
) {
let mut current_global_z = 0.0;
for entity in root_node_query.iter() {
if let Some(result) = hierarchy::run_on_hierarchy(
&children_query,
&mut node_query,
entity,
Some(current_global_z),
Some(current_global_z),
&mut update_node_entity,
) {
current_global_z = result;
}
}
}
fn update_node_entity(
node_query: &mut Query<(Entity, &mut Transform), With<Node>>,
entity: Entity,
parent_result: Option<f32>,
previous_result: Option<f32>,
) -> Option<f32> {
let mut z = UI_Z_STEP;
let parent_global_z = parent_result.unwrap();
if let Some(previous_global_z) = previous_result {
z += previous_global_z - parent_global_z;
};
let global_z = z + parent_global_z;
if let Ok(mut transform) = node_query.get_component_mut::<Transform>(entity) {
transform.translation.z = z;
}
Some(global_z)
}