diff --git a/src/ecs/mod.rs b/src/ecs/mod.rs index fa54b4e1fd..79b738b2e3 100644 --- a/src/ecs/mod.rs +++ b/src/ecs/mod.rs @@ -1,6 +1,28 @@ use crate::prelude::{Children, Entity, SubWorld, World}; pub fn run_on_hierarchy( + world: &World, + entity: Entity, + input: T, + func: &mut dyn FnMut(&World, Entity, T) -> Option, +) where + T: Copy, +{ + let result = func(world, entity, input); + + if let Some(result) = result { + match world.get_component::(entity) { + Some(children) => Some( + for child in children.iter() { + run_on_hierarchy(world, *child, result, func); + } + ), + None => None, + }; + } +} + +pub fn run_on_hierarchy_mut( world: &mut World, entity: Entity, input: T, @@ -24,17 +46,39 @@ pub fn run_on_hierarchy( if let Some(result) = result { if let Some(children) = children { for child in children { - run_on_hierarchy(world, child, result, func); + run_on_hierarchy_mut(world, child, result, func); } } } } pub fn run_on_hierarchy_subworld( - world: &mut legion::system::SubWorld, + world: &SubWorld, entity: Entity, input: T, - func: &dyn Fn(&mut SubWorld, Entity, T) -> Option, + func: &mut dyn FnMut(&SubWorld, Entity, T) -> Option, +) where + T: Copy, +{ + let result = func(world, entity, input); + + if let Some(result) = result { + match world.get_component::(entity) { + Some(children) => Some( + for child in children.iter() { + run_on_hierarchy_subworld(world, *child, result, func); + } + ), + None => None, + }; + } +} + +pub fn run_on_hierarchy_subworld_mut( + world: &mut SubWorld, + entity: Entity, + input: T, + func: &mut dyn FnMut(&mut SubWorld, Entity, T) -> Option, ) where T: Copy, { @@ -54,8 +98,8 @@ pub fn run_on_hierarchy_subworld( if let Some(result) = result { if let Some(children) = children { for child in children { - run_on_hierarchy_subworld(world, child, result, func); + run_on_hierarchy_subworld_mut(world, child, result, func); } } } -} +} \ No newline at end of file diff --git a/src/render/passes/ui/mod.rs b/src/render/passes/ui/mod.rs index 2650dc1711..f619ecacdc 100644 --- a/src/render/passes/ui/mod.rs +++ b/src/render/passes/ui/mod.rs @@ -52,7 +52,7 @@ impl UiPipeline { // TODO: this probably isn't the best way to handle z-ordering let mut z = 0.9999; { - let mut add_data: Box Option<()>> = + let mut add_data: Box Option<()>> = Box::new(|world, entity, _| { let node = world.get_component::(entity).unwrap(); data.push(RectData { diff --git a/src/ui/ui_update_system.rs b/src/ui/ui_update_system.rs index 5e4925d1fe..d903783c76 100644 --- a/src/ui/ui_update_system.rs +++ b/src/ui/ui_update_system.rs @@ -12,11 +12,11 @@ pub fn build_ui_update_system() -> Box { let parent_size = math::vec2(window_size.width as f32, window_size.height as f32); let parent_position = math::vec2(0.0, 0.0); for (entity, _) in node_query.iter_entities_mut(world) { - ecs::run_on_hierarchy_subworld( + ecs::run_on_hierarchy_subworld_mut( world, entity, (parent_size, parent_position), - &update_node_entity, + &mut update_node_entity, ); } })