2020-04-06 21:20:53 +00:00
|
|
|
use super::Node;
|
2020-05-03 00:56:30 +00:00
|
|
|
use crate::Rect;
|
2020-04-06 21:20:53 +00:00
|
|
|
use bevy_core::transform::run_on_hierarchy_subworld_mut;
|
|
|
|
use bevy_transform::prelude::{Children, Parent};
|
|
|
|
use bevy_window::Windows;
|
|
|
|
use glam::Vec2;
|
|
|
|
use legion::{prelude::*, systems::SubWorld};
|
2020-01-13 00:51:21 +00:00
|
|
|
|
2020-05-06 20:49:07 +00:00
|
|
|
pub const UI_Z_STEP: f32 = 0.0001;
|
|
|
|
|
2020-03-29 08:49:35 +00:00
|
|
|
pub fn ui_update_system() -> Box<dyn Schedulable> {
|
2020-03-30 05:44:38 +00:00
|
|
|
SystemBuilder::new("ui_update")
|
2020-03-30 21:53:32 +00:00
|
|
|
.read_resource::<Windows>()
|
2020-05-04 02:30:31 +00:00
|
|
|
.with_query(<Read<Node>>::query().filter(!component::<Parent>()))
|
2020-01-13 06:18:17 +00:00
|
|
|
.write_component::<Node>()
|
2020-05-03 00:56:30 +00:00
|
|
|
.write_component::<Rect>()
|
2020-01-13 06:18:17 +00:00
|
|
|
.read_component::<Children>()
|
2020-03-30 21:53:32 +00:00
|
|
|
.build(move |_, world, windows, node_query| {
|
2020-04-07 00:03:21 +00:00
|
|
|
if let Some(window) = windows.get_primary() {
|
2020-05-06 20:49:07 +00:00
|
|
|
let mut window_rect = Rect {
|
|
|
|
size: Vec2::new(window.width as f32, window.height as f32),
|
|
|
|
position: Vec2::new(0.0, 0.0),
|
|
|
|
z_index: 0.9999,
|
|
|
|
};
|
2020-05-04 02:30:31 +00:00
|
|
|
for entity in node_query
|
|
|
|
.iter_entities(world)
|
|
|
|
.map(|(e, _)| e)
|
|
|
|
.collect::<Vec<Entity>>()
|
|
|
|
{
|
2020-05-06 20:49:07 +00:00
|
|
|
let result = run_on_hierarchy_subworld_mut(
|
2020-04-07 00:03:21 +00:00
|
|
|
world,
|
|
|
|
entity,
|
2020-05-06 20:49:07 +00:00
|
|
|
window_rect.clone(),
|
2020-04-07 00:03:21 +00:00
|
|
|
&mut update_node_entity,
|
2020-05-06 20:49:07 +00:00
|
|
|
&mut process_child_result,
|
2020-04-07 00:03:21 +00:00
|
|
|
);
|
2020-05-06 20:49:07 +00:00
|
|
|
|
|
|
|
if let Some(result) = result {
|
|
|
|
window_rect.z_index = result.z_index;
|
|
|
|
}
|
2020-04-07 00:03:21 +00:00
|
|
|
}
|
2020-01-13 00:51:21 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-01-13 06:18:17 +00:00
|
|
|
|
2020-05-06 20:49:07 +00:00
|
|
|
fn update_node_entity(world: &mut SubWorld, entity: Entity, parent_rect: 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) {
|
|
|
|
if let Some(mut rect) = world.get_component_mut_unchecked::<Rect>(entity) {
|
2020-05-06 20:49:07 +00:00
|
|
|
node.update(
|
|
|
|
&mut rect,
|
|
|
|
parent_rect.size,
|
|
|
|
parent_rect.position,
|
|
|
|
parent_rect.z_index,
|
|
|
|
);
|
|
|
|
return Some(Rect {
|
|
|
|
size: rect.size,
|
|
|
|
position: rect.position - rect.size / 2.0,
|
|
|
|
z_index: rect.z_index - UI_Z_STEP,
|
|
|
|
});
|
2020-05-03 00:56:30 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-13 06:18:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
2020-05-06 20:49:07 +00:00
|
|
|
|
|
|
|
fn process_child_result(_parent_result: Rect, child_result: Rect) -> Rect {
|
|
|
|
// "earlier" children are sorted behind "later" children
|
|
|
|
let mut result = child_result.clone();
|
|
|
|
result.z_index -= UI_Z_STEP;
|
|
|
|
result
|
|
|
|
}
|