bevy/crates/bevy_ui/src/ui_update_system.rs

54 lines
1.9 KiB
Rust
Raw Normal View History

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-03-29 08:49:35 +00:00
pub fn ui_update_system() -> Box<dyn Schedulable> {
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| {
if let Some(window) = windows.get_primary() {
let parent_size = glam::vec2(window.width as f32, window.height as f32);
let parent_position = glam::vec2(0.0, 0.0);
2020-05-04 02:30:31 +00:00
for entity in node_query
.iter_entities(world)
.map(|(e, _)| e)
.collect::<Vec<Entity>>()
{
run_on_hierarchy_subworld_mut(
world,
entity,
2020-05-04 02:30:31 +00:00
(parent_size, parent_position, 0.9999),
&mut update_node_entity,
);
}
2020-01-13 00:51:21 +00:00
}
})
}
2020-01-13 06:18:17 +00:00
fn update_node_entity(
world: &mut SubWorld,
entity: Entity,
2020-05-04 02:30:31 +00:00
parent_properties: (Vec2, Vec2, f32),
) -> Option<(Vec2, Vec2, f32)> {
let (parent_size, parent_position, z_index) = parent_properties;
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-04 02:30:31 +00:00
node.update(&mut rect, parent_size, parent_position, z_index);
return Some((rect.size, rect.position, z_index - 0.0001));
2020-05-03 00:56:30 +00:00
}
}
2020-01-13 06:18:17 +00:00
}
None
}