2020-04-06 21:20:53 +00:00
|
|
|
use super::Node;
|
2020-07-25 06:04:45 +00:00
|
|
|
use bevy_ecs::{Entity, Query, With, Without};
|
2020-07-16 23:32:39 +00:00
|
|
|
use bevy_transform::{
|
|
|
|
hierarchy,
|
2020-09-14 21:00:32 +00:00
|
|
|
prelude::{Children, Parent, Transform},
|
2020-07-16 23:32:39 +00:00
|
|
|
};
|
2020-01-13 00:51:21 +00:00
|
|
|
|
2020-06-24 22:29:10 +00:00
|
|
|
pub const UI_Z_STEP: f32 = 0.001;
|
2020-05-06 20:49:07 +00:00
|
|
|
|
2020-07-25 06:04:45 +00:00
|
|
|
pub fn ui_z_system(
|
2020-10-30 06:39:55 +00:00
|
|
|
root_node_query: Query<With<Node, Without<Parent, Entity>>>,
|
2020-09-14 21:00:32 +00:00
|
|
|
mut node_query: Query<(Entity, &Node, &mut Transform)>,
|
2020-07-10 04:18:35 +00:00
|
|
|
children_query: Query<&Children>,
|
2020-06-25 17:13:00 +00:00
|
|
|
) {
|
2020-07-29 07:28:44 +00:00
|
|
|
let mut current_global_z = 0.0;
|
2020-07-25 06:04:45 +00:00
|
|
|
|
2020-10-30 06:39:55 +00:00
|
|
|
for entity in root_node_query.iter() {
|
2020-07-25 06:04:45 +00:00
|
|
|
if let Some(result) = hierarchy::run_on_hierarchy(
|
2020-07-10 04:18:35 +00:00
|
|
|
&children_query,
|
|
|
|
&mut node_query,
|
2020-06-25 17:13:00 +00:00
|
|
|
entity,
|
2020-07-29 07:28:44 +00:00
|
|
|
Some(current_global_z),
|
|
|
|
Some(current_global_z),
|
2020-06-25 17:13:00 +00:00
|
|
|
&mut update_node_entity,
|
2020-07-25 06:04:45 +00:00
|
|
|
) {
|
2020-07-29 07:28:44 +00:00
|
|
|
current_global_z = result;
|
2020-07-25 06:04:45 +00:00
|
|
|
}
|
2020-06-25 17:13:00 +00:00
|
|
|
}
|
2020-01-13 00:51:21 +00:00
|
|
|
}
|
2020-01-13 06:18:17 +00:00
|
|
|
|
2020-06-27 17:18:27 +00:00
|
|
|
fn update_node_entity(
|
2020-09-14 21:00:32 +00:00
|
|
|
node_query: &mut Query<(Entity, &Node, &mut Transform)>,
|
2020-06-27 17:18:27 +00:00
|
|
|
entity: Entity,
|
2020-07-29 07:28:44 +00:00
|
|
|
parent_result: Option<f32>,
|
2020-07-25 20:14:36 +00:00
|
|
|
previous_result: Option<f32>,
|
|
|
|
) -> Option<f32> {
|
2020-07-25 06:04:45 +00:00
|
|
|
let mut z = UI_Z_STEP;
|
2020-07-29 07:28:44 +00:00
|
|
|
let parent_global_z = parent_result.unwrap();
|
|
|
|
if let Some(previous_global_z) = previous_result {
|
|
|
|
z += previous_global_z - parent_global_z;
|
2020-07-25 06:04:45 +00:00
|
|
|
};
|
2020-07-29 07:28:44 +00:00
|
|
|
let global_z = z + parent_global_z;
|
2020-06-25 17:13:00 +00:00
|
|
|
|
2020-10-31 01:04:33 +00:00
|
|
|
if let Ok(mut transform) = node_query.get_component_mut::<Transform>(entity) {
|
2020-10-30 06:39:55 +00:00
|
|
|
transform.translation.set_z(z);
|
|
|
|
}
|
2020-07-25 06:04:45 +00:00
|
|
|
|
2020-08-16 07:30:04 +00:00
|
|
|
Some(global_z)
|
2020-06-27 17:18:27 +00:00
|
|
|
}
|