2020-04-06 21:20:53 +00:00
|
|
|
use super::Node;
|
|
|
|
use bevy_core::transform::run_on_hierarchy_subworld_mut;
|
2020-06-25 17:13:00 +00:00
|
|
|
use bevy_transform::prelude::{Children, Parent, Translation};
|
2020-04-06 21:20:53 +00:00
|
|
|
use bevy_window::Windows;
|
|
|
|
use glam::Vec2;
|
|
|
|
use legion::{prelude::*, systems::SubWorld};
|
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-06-27 17:18:27 +00:00
|
|
|
world: &mut SubWorld,
|
2020-06-25 17:13:00 +00:00
|
|
|
node_query: &mut Query<(Write<Node>, Write<Translation>)>,
|
|
|
|
_parent_query: &mut Query<Read<Parent>>,
|
|
|
|
_children_query: &mut Query<Read<Children>>,
|
|
|
|
) {
|
|
|
|
let window_size = if let Some(window) = windows.get_primary() {
|
|
|
|
Vec2::new(window.width as f32, window.height as f32)
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
let orphan_nodes = node_query
|
|
|
|
.iter_entities_mut(world)
|
|
|
|
// TODO: replace this filter with a legion query filter (when SimpleQuery gets support for filters)
|
|
|
|
.filter(|(entity, _)| world.get_component::<Parent>(*entity).is_none())
|
|
|
|
.map(|(e, _)| e)
|
|
|
|
.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 {
|
|
|
|
previous_sibling_result = run_on_hierarchy_subworld_mut(
|
|
|
|
world,
|
|
|
|
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(
|
|
|
|
world: &mut SubWorld,
|
|
|
|
entity: Entity,
|
|
|
|
parent_rect: Option<&mut Rect>,
|
|
|
|
previous_rect: Option<Rect>,
|
|
|
|
) -> Option<Rect> {
|
2020-05-03 00:56:30 +00:00
|
|
|
// TODO: Somehow remove this unsafe
|
|
|
|
unsafe {
|
|
|
|
if let Some(mut node) = world.get_component_mut_unchecked::<Node>(entity) {
|
2020-06-25 17:13:00 +00:00
|
|
|
if let Some(mut translation) = world.get_component_mut_unchecked::<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
|
|
|
|
};
|
|
|
|
|
|
|
|
z -= UI_Z_STEP;
|
2020-06-27 17:18:27 +00:00
|
|
|
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
|
|
|
}
|