From 54cba7abd99229b6643f92562b7e71d0c29a1840 Mon Sep 17 00:00:00 2001 From: Matthew Pomes Date: Wed, 14 Sep 2022 10:11:05 -0500 Subject: [PATCH] Move EntityPath and NameLookup to core --- crates/bevy_animation/src/lib.rs | 129 +++---------------------------- crates/bevy_core/Cargo.toml | 1 + crates/bevy_core/src/name.rs | 98 ++++++++++++++++++++++- crates/bevy_gltf/src/loader.rs | 2 +- 4 files changed, 110 insertions(+), 120 deletions(-) diff --git a/crates/bevy_animation/src/lib.rs b/crates/bevy_animation/src/lib.rs index f4c9b5a5b0e06..f001fb913ed97 100644 --- a/crates/bevy_animation/src/lib.rs +++ b/crates/bevy_animation/src/lib.rs @@ -6,15 +6,15 @@ use std::ops::Deref; use bevy_app::{App, CoreStage, Plugin}; use bevy_asset::{AddAsset, Assets, Handle}; -use bevy_core::Name; +use bevy_core::{EntityPath, Name, NameLookup}; use bevy_ecs::{ change_detection::DetectChanges, entity::Entity, prelude::Component, + query::QueryEntityError, reflect::ReflectComponent, schedule::ParallelSystemDescriptorCoercion, system::{Query, Res, SystemParam}, - query::QueryEntityError, }; use bevy_hierarchy::Children; use bevy_math::{Quat, Vec3}; @@ -26,94 +26,9 @@ use bevy_utils::{tracing::warn, HashMap}; #[allow(missing_docs)] pub mod prelude { #[doc(hidden)] - pub use crate::{ - AnimationClip, AnimationPlayer, AnimationPlugin, EntityPath, Keyframes, VariableCurve, - }; -} - -/// System param to enable entity lookup of an entity via EntityPath -#[derive(SystemParam)] -pub struct NameLookup<'w, 's> { - named: Query<'w, 's, (Entity, &'static Name)>, - children: Query<'w, 's, &'static Children>, -} - -/// Errors when looking up an entity by name -pub enum LookupError { - /// An entity could not be found, this either means the entity has been - /// despawned, or the entity doesn't have the required components - Query(QueryEntityError), - /// The root node does not have the corrent name - // TODO: add expected / found name - RootNotFound, - /// A child was not found - // TODO: add expected name - ChildNotFound, - /// The name does not uniquely identify an entity - // TODO: add name - NameNotUnique, -} - -impl From for LookupError { - fn from(q: QueryEntityError) -> Self { - Self::Query(q) - } -} - -impl<'w, 's> NameLookup<'w, 's> { - /// Find an entity by entity path, may return an error if the root name isn't unique - pub fn lookup_any(&self, path: &EntityPath) -> Result { - let mut path = path.parts.iter(); - let root_name = path.next().unwrap(); - let mut root = None; - for (entity, name) in self.named.iter() { - if root_name == name { - if root.is_some() { - return Err(LookupError::NameNotUnique); - } - root = Some(entity); - } - } - let mut current_node = root.ok_or(LookupError::RootNotFound)?; - for part in path { - current_node = self.find_child(current_node, part)?; - } - Ok(current_node) - } - - /// Find an entity by the root & entity path - pub fn lookup(&self, root: Entity, path: &EntityPath) -> Result { - let mut path = path.parts.iter(); - let (_, root_name) = self.named.get(root)?; - if root_name != path.next().unwrap() { - return Err(LookupError::RootNotFound); - } - let mut current_node = root; - for part in path { - current_node = self.find_child(current_node, part)?; - } - Ok(current_node) - } - - /// Internal function to get the child of `current_node` that has the name `part` - fn find_child(&self, current_node: Entity, part: &Name) -> Result { - let children = self.children.get(current_node)?; - let mut ret = Err(LookupError::ChildNotFound); - for child in children { - if let Ok((_, name)) = self.named.get(*child) { - if name == part { - if ret.is_ok() { - return Err(LookupError::NameNotUnique); - } - ret = Ok(*child); - } - } - } - ret - } + pub use crate::{AnimationClip, AnimationPlayer, AnimationPlugin, Keyframes, VariableCurve}; } - /// List of keyframes for one of the attribute of a [`Transform`]. #[derive(Clone, Debug)] pub enum Keyframes { @@ -136,13 +51,6 @@ pub struct VariableCurve { pub keyframes: Keyframes, } -/// Path to an entity, with [`Name`]s. Each entity in a path must have a name. -#[derive(Clone, Debug, Hash, PartialEq, Eq, Default)] -pub struct EntityPath { - /// Parts of the path - pub parts: Vec, -} - /// A list of [`VariableCurve`], and the [`EntityPath`] to which they apply. #[derive(Clone, TypeUuid, Debug, Default)] #[uuid = "d81b7179-0448-4eb0-89fe-c067222725bf"] @@ -263,9 +171,8 @@ pub fn animation_player( time: Res