2020-04-06 21:20:53 +00:00
|
|
|
use super::Node;
|
2020-07-25 06:04:45 +00:00
|
|
|
use crate::FlexSurfaceId;
|
|
|
|
use bevy_ecs::{Entity, Query, With, Without};
|
2020-07-16 23:32:39 +00:00
|
|
|
use bevy_transform::{
|
|
|
|
hierarchy,
|
2020-07-20 03:33:55 +00:00
|
|
|
prelude::{Children, LocalTransform, Parent},
|
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(
|
|
|
|
mut root_node_query: Query<With<Node, Without<Parent, (Entity, &FlexSurfaceId)>>>,
|
|
|
|
mut node_query: Query<(Entity, &Node, &mut FlexSurfaceId, &mut LocalTransform)>,
|
2020-07-10 04:18:35 +00:00
|
|
|
children_query: Query<&Children>,
|
2020-06-25 17:13:00 +00:00
|
|
|
) {
|
2020-07-25 06:04:45 +00:00
|
|
|
let mut window_z = 0.0;
|
|
|
|
|
|
|
|
// PERF: we can probably avoid an allocation here by making root_node_query and node_query non-overlapping
|
|
|
|
let root_nodes = (&mut root_node_query.iter())
|
2020-07-10 04:18:35 +00:00
|
|
|
.iter()
|
2020-07-25 06:04:45 +00:00
|
|
|
.map(|(e, s)| (e, *s))
|
|
|
|
.collect::<Vec<(Entity, FlexSurfaceId)>>();
|
2020-06-25 17:13:00 +00:00
|
|
|
|
2020-07-25 06:04:45 +00:00
|
|
|
for (entity, flex_surface_id) in root_nodes {
|
|
|
|
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-25 06:04:45 +00:00
|
|
|
Some((flex_surface_id, window_z)),
|
|
|
|
Some((flex_surface_id, window_z)),
|
2020-06-25 17:13:00 +00:00
|
|
|
&mut update_node_entity,
|
2020-07-25 06:04:45 +00:00
|
|
|
) {
|
|
|
|
window_z = result.1;
|
|
|
|
}
|
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-07-25 06:04:45 +00:00
|
|
|
node_query: &mut Query<(Entity, &Node, &mut FlexSurfaceId, &mut LocalTransform)>,
|
2020-06-27 17:18:27 +00:00
|
|
|
entity: Entity,
|
2020-07-25 06:04:45 +00:00
|
|
|
parent_result: Option<(FlexSurfaceId, f32)>,
|
|
|
|
previous_result: Option<(FlexSurfaceId, f32)>,
|
|
|
|
) -> Option<(FlexSurfaceId, f32)> {
|
|
|
|
let mut surface_id = node_query.get_mut::<FlexSurfaceId>(entity).unwrap();
|
|
|
|
let mut transform = node_query.get_mut::<LocalTransform>(entity).unwrap();
|
|
|
|
let (parent_surface_id, _) = parent_result?;
|
|
|
|
let mut z = UI_Z_STEP;
|
|
|
|
if let Some((_, previous_z)) = previous_result {
|
|
|
|
z += previous_z;
|
|
|
|
};
|
2020-06-25 17:13:00 +00:00
|
|
|
|
2020-07-25 06:04:45 +00:00
|
|
|
let mut position = transform.w_axis();
|
|
|
|
position.set_z(z);
|
|
|
|
transform.set_w_axis(position);
|
|
|
|
|
|
|
|
*surface_id = parent_surface_id;
|
2020-01-13 06:18:17 +00:00
|
|
|
|
2020-07-25 06:04:45 +00:00
|
|
|
return Some((parent_surface_id, z));
|
2020-06-27 17:18:27 +00:00
|
|
|
}
|