2020-04-06 21:20:53 +00:00
|
|
|
use super::Node;
|
2020-07-15 02:05:39 +00:00
|
|
|
use bevy_ecs::{Entity, Query, Res, Without};
|
2020-07-17 00:23:50 +00:00
|
|
|
use bevy_math::Vec2;
|
2020-07-16 23:32:39 +00:00
|
|
|
use bevy_transform::{
|
|
|
|
hierarchy,
|
|
|
|
prelude::{Children, Parent, Translation},
|
|
|
|
};
|
2020-04-06 21:20:53 +00:00
|
|
|
use bevy_window::Windows;
|
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-06-25 17:13:00 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Rect {
|
|
|
|
pub z: f32,
|
|
|
|
pub size: Vec2,
|
|
|
|
}
|
2020-05-06 20:49:07 +00:00
|
|
|
|
2020-06-25 17:13:00 +00:00
|
|
|
pub fn ui_update_system(
|
|
|
|
windows: Res<Windows>,
|
2020-07-15 02:05:39 +00:00
|
|
|
mut orphan_node_query: Query<Without<Parent, (Entity, &mut Node, &mut Translation)>>,
|
2020-07-10 04:18:35 +00:00
|
|
|
mut node_query: Query<(Entity, &mut Node, &mut Translation)>,
|
|
|
|
children_query: Query<&Children>,
|
2020-06-25 17:13:00 +00:00
|
|
|
) {
|
|
|
|
let window_size = if let Some(window) = windows.get_primary() {
|
|
|
|
Vec2::new(window.width as f32, window.height as f32)
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
2020-07-15 02:05:39 +00:00
|
|
|
let orphan_nodes = orphan_node_query
|
2020-07-10 04:18:35 +00:00
|
|
|
.iter()
|
|
|
|
.iter()
|
|
|
|
.map(|(e, _, _)| e)
|
2020-06-25 17:13:00 +00:00
|
|
|
.collect::<Vec<Entity>>();
|
|
|
|
let mut window_rect = Rect {
|
|
|
|
z: 0.0,
|
|
|
|
size: window_size,
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut previous_sibling_result = Some(Rect {
|
|
|
|
z: 999.0,
|
|
|
|
size: window_size,
|
|
|
|
});
|
|
|
|
for entity in orphan_nodes {
|
2020-07-16 23:32:39 +00:00
|
|
|
previous_sibling_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,
|
|
|
|
Some(&mut window_rect),
|
|
|
|
previous_sibling_result,
|
|
|
|
&mut update_node_entity,
|
|
|
|
);
|
|
|
|
}
|
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-10 04:18:35 +00:00
|
|
|
node_query: &mut Query<(Entity, &mut Node, &mut Translation)>,
|
2020-06-27 17:18:27 +00:00
|
|
|
entity: Entity,
|
|
|
|
parent_rect: Option<&mut Rect>,
|
|
|
|
previous_rect: Option<Rect>,
|
|
|
|
) -> Option<Rect> {
|
2020-07-10 04:18:35 +00:00
|
|
|
if let Ok(mut node) = node_query.get_mut::<Node>(entity) {
|
|
|
|
if let Ok(mut translation) = node_query.get_mut::<Translation>(entity) {
|
|
|
|
let parent_rect = parent_rect.unwrap();
|
|
|
|
let mut z = parent_rect.z;
|
|
|
|
if let Some(previous_rect) = previous_rect {
|
|
|
|
z = previous_rect.z
|
|
|
|
};
|
2020-06-25 17:13:00 +00:00
|
|
|
|
2020-07-10 04:18:35 +00:00
|
|
|
z -= UI_Z_STEP;
|
|
|
|
node.update(&mut translation, z - parent_rect.z, parent_rect.size);
|
|
|
|
return Some(Rect { size: node.size, z });
|
2020-05-03 00:56:30 +00:00
|
|
|
}
|
2020-01-13 06:18:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
None
|
2020-06-27 17:18:27 +00:00
|
|
|
}
|