From 77fe5a2f3135f24a75f7d479ec2f49c560b18f71 Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Fri, 30 Oct 2020 15:44:00 -0700 Subject: [PATCH] Rename query.entity() to query.get() and query.get() to query.get_component() --- .../src/schedule/parallel_executor.rs | 4 +-- crates/bevy_ecs/src/system/into_system.rs | 16 +++++----- crates/bevy_ecs/src/system/query/mod.rs | 13 +++++---- .../src/camera/visible_entities.rs | 29 +++++++++---------- .../src/render_graph/nodes/camera_node.rs | 14 ++++----- .../bevy_transform/src/hierarchy/hierarchy.rs | 2 +- .../hierarchy/hierarchy_maintenance_system.rs | 8 ++--- .../src/transform_propagate_system.rs | 29 ++++++++++--------- crates/bevy_ui/src/focus.rs | 4 ++- crates/bevy_ui/src/update.rs | 2 +- crates/bevy_ui/src/widget/text.rs | 2 +- examples/3d/z_sort_debug.rs | 4 +-- examples/ecs/hierarchy.rs | 4 +-- examples/ui/button.rs | 2 +- 14 files changed, 65 insertions(+), 68 deletions(-) diff --git a/crates/bevy_ecs/src/schedule/parallel_executor.rs b/crates/bevy_ecs/src/schedule/parallel_executor.rs index bbba641a50f99..d9e0fd9f342c9 100644 --- a/crates/bevy_ecs/src/schedule/parallel_executor.rs +++ b/crates/bevy_ecs/src/schedule/parallel_executor.rs @@ -564,7 +564,7 @@ mod tests { for entity in &mut entities.iter() { // query.get() does a "system permission check" that will fail if the entity is from a // new archetype which hasnt been "prepared yet" - query.get::(entity).unwrap(); + query.get_component::(entity).unwrap(); } assert_eq!(1, entities.iter().count()); @@ -595,7 +595,7 @@ mod tests { for entity in &mut entities.iter() { // query.get() does a "system permission check" that will fail if the entity is from a // new archetype which hasnt been "prepared yet" - query.get::(entity).unwrap(); + query.get_component::(entity).unwrap(); } assert_eq!(1, entities.iter().count()); diff --git a/crates/bevy_ecs/src/system/into_system.rs b/crates/bevy_ecs/src/system/into_system.rs index aec14cc0a34a0..63a1df7276a85 100644 --- a/crates/bevy_ecs/src/system/into_system.rs +++ b/crates/bevy_ecs/src/system/into_system.rs @@ -437,35 +437,35 @@ mod tests { ) { let entities = entity_query.iter().collect::>(); assert!( - b_query.get::(entities[0]).is_err(), + b_query.get_component::(entities[0]).is_err(), "entity 0 should not have B" ); assert!( - b_query.get::(entities[1]).is_ok(), + b_query.get_component::(entities[1]).is_ok(), "entity 1 should have B" ); assert!( - b_query.get::(entities[1]).is_err(), + b_query.get_component::(entities[1]).is_err(), "entity 1 should have A, but b_query shouldn't have access to it" ); assert!( - b_query.get::(entities[3]).is_err(), + b_query.get_component::(entities[3]).is_err(), "entity 3 should have D, but it shouldn't be accessible from b_query" ); assert!( - b_query.get::(entities[2]).is_err(), + b_query.get_component::(entities[2]).is_err(), "entity 2 has C, but it shouldn't be accessible from b_query" ); assert!( - a_c_query.get::(entities[2]).is_ok(), + a_c_query.get_component::(entities[2]).is_ok(), "entity 2 has C, and it should be accessible from a_c_query" ); assert!( - a_c_query.get::(entities[3]).is_err(), + a_c_query.get_component::(entities[3]).is_err(), "entity 3 should have D, but it shouldn't be accessible from b_query" ); assert!( - d_query.get::(entities[3]).is_ok(), + d_query.get_component::(entities[3]).is_ok(), "entity 3 should have D" ); diff --git a/crates/bevy_ecs/src/system/query/mod.rs b/crates/bevy_ecs/src/system/query/mod.rs index efe15ed041817..162d356922205 100644 --- a/crates/bevy_ecs/src/system/query/mod.rs +++ b/crates/bevy_ecs/src/system/query/mod.rs @@ -75,7 +75,7 @@ impl<'a, Q: HecsQuery> Query<'a, Q> { } /// Gets the query result for the given `entity` - pub fn entity(&self, entity: Entity) -> Result<::Item, QueryError> + pub fn get(&self, entity: Entity) -> Result<::Item, QueryError> where Q::Fetch: ReadOnlyFetch, { @@ -88,7 +88,7 @@ impl<'a, Q: HecsQuery> Query<'a, Q> { } /// Gets the query result for the given `entity` - pub fn entity_mut(&mut self, entity: Entity) -> Result<::Item, QueryError> { + pub fn get_mut(&mut self, entity: Entity) -> Result<::Item, QueryError> { // SAFE: system runs without conflicts with other systems. same-system queries have runtime borrow checks when they conflict unsafe { self.world @@ -111,7 +111,7 @@ impl<'a, Q: HecsQuery> Query<'a, Q> { /// Gets a reference to the entity's component of the given type. This will fail if the entity does not have /// the given component type or if the given component type does not match this query. - pub fn get(&self, entity: Entity) -> Result<&T, QueryError> { + pub fn get_component(&self, entity: Entity) -> Result<&T, QueryError> { if let Some(location) = self.world.get_entity_location(entity) { if self .component_access @@ -133,7 +133,10 @@ impl<'a, Q: HecsQuery> Query<'a, Q> { /// Gets a mutable reference to the entity's component of the given type. This will fail if the entity does not have /// the given component type or if the given component type does not match this query. - pub fn get_mut(&mut self, entity: Entity) -> Result, QueryError> { + pub fn get_component_mut( + &mut self, + entity: Entity, + ) -> Result, QueryError> { let location = match self.world.get_entity_location(entity) { None => return Err(QueryError::ComponentError(ComponentError::NoSuchEntity)), Some(location) => location, @@ -174,7 +177,7 @@ impl<'a, Q: HecsQuery> Query<'a, Q> { /// Sets the entity's component to the given value. This will fail if the entity does not already have /// the given component type or if the given component type does not match this query. pub fn set(&mut self, entity: Entity, component: T) -> Result<(), QueryError> { - let mut current = self.get_mut::(entity)?; + let mut current = self.get_component_mut::(entity)?; *current = component; Ok(()) } diff --git a/crates/bevy_render/src/camera/visible_entities.rs b/crates/bevy_render/src/camera/visible_entities.rs index 5b1abb76c9bfb..6c2227c9da735 100644 --- a/crates/bevy_render/src/camera/visible_entities.rs +++ b/crates/bevy_render/src/camera/visible_entities.rs @@ -1,7 +1,7 @@ use super::{Camera, DepthCalculation}; use crate::Draw; use bevy_core::FloatOrd; -use bevy_ecs::{Entity, Query}; +use bevy_ecs::{Entity, Query, With}; use bevy_property::Properties; use bevy_transform::prelude::GlobalTransform; @@ -26,7 +26,7 @@ impl VisibleEntities { pub fn visible_entities_system( mut camera_query: Query<(&Camera, &GlobalTransform, &mut VisibleEntities)>, draw_query: Query<(Entity, &Draw)>, - draw_transform_query: Query<(&Draw, &GlobalTransform)>, + draw_transform_query: Query>, ) { for (camera, camera_global_transform, mut visible_entities) in camera_query.iter_mut() { visible_entities.value.clear(); @@ -39,19 +39,18 @@ pub fn visible_entities_system( continue; } - let order = - if let Ok(global_transform) = draw_transform_query.get::(entity) { - let position = global_transform.translation; - // smaller distances are sorted to lower indices by using the distance from the camera - FloatOrd(match camera.depth_calculation { - DepthCalculation::ZDifference => camera_position.z() - position.z(), - DepthCalculation::Distance => (camera_position - position).length(), - }) - } else { - let order = FloatOrd(no_transform_order); - no_transform_order += 0.1; - order - }; + let order = if let Ok(global_transform) = draw_transform_query.get(entity) { + let position = global_transform.translation; + // smaller distances are sorted to lower indices by using the distance from the camera + FloatOrd(match camera.depth_calculation { + DepthCalculation::ZDifference => camera_position.z() - position.z(), + DepthCalculation::Distance => (camera_position - position).length(), + }) + } else { + let order = FloatOrd(no_transform_order); + no_transform_order += 0.1; + order + }; if draw.is_transparent { transparent_entities.push(VisibleEntity { entity, order }) diff --git a/crates/bevy_render/src/render_graph/nodes/camera_node.rs b/crates/bevy_render/src/render_graph/nodes/camera_node.rs index 3cda1c54d3ea8..c9af7fe0e93cf 100644 --- a/crates/bevy_render/src/render_graph/nodes/camera_node.rs +++ b/crates/bevy_render/src/render_graph/nodes/camera_node.rs @@ -78,15 +78,11 @@ pub fn camera_node_system( ) { let render_resource_context = &**render_resource_context; - let (camera, global_transform) = - if let Some(camera_entity) = active_cameras.get(&state.camera_name) { - ( - query.get::(camera_entity).unwrap(), - query.get::(camera_entity).unwrap(), - ) - } else { - return; - }; + let (camera, global_transform) = if let Some(entity) = active_cameras.get(&state.camera_name) { + query.get(entity).unwrap() + } else { + return; + }; let staging_buffer = if let Some(staging_buffer) = state.staging_buffer { render_resource_context.map_buffer(staging_buffer); diff --git a/crates/bevy_transform/src/hierarchy/hierarchy.rs b/crates/bevy_transform/src/hierarchy/hierarchy.rs index 3be2d22f6ab8c..d15780ec96058 100644 --- a/crates/bevy_transform/src/hierarchy/hierarchy.rs +++ b/crates/bevy_transform/src/hierarchy/hierarchy.rs @@ -14,7 +14,7 @@ where { let parent_result = run(state, entity, parent_result, previous_result); previous_result = None; - if let Ok(children) = children_query.entity(entity) { + if let Ok(children) = children_query.get(entity) { for child in children.iter().cloned() { previous_result = run_on_hierarchy( children_query, diff --git a/crates/bevy_transform/src/hierarchy/hierarchy_maintenance_system.rs b/crates/bevy_transform/src/hierarchy/hierarchy_maintenance_system.rs index 7957fa658fbbd..9663bea17b15d 100644 --- a/crates/bevy_transform/src/hierarchy/hierarchy_maintenance_system.rs +++ b/crates/bevy_transform/src/hierarchy/hierarchy_maintenance_system.rs @@ -14,7 +14,7 @@ pub fn parent_update_system( // them from the `Children` of the `PreviousParent`. for (entity, previous_parent) in removed_parent_query.iter() { log::trace!("Parent was removed from {:?}", entity); - if let Ok(mut previous_parent_children) = children_query.entity_mut(previous_parent.0) { + if let Ok(mut previous_parent_children) = children_query.get_mut(previous_parent.0) { log::trace!(" > Removing {:?} from it's prev parent's children", entity); previous_parent_children.0.retain(|e| *e != entity); commands.remove_one::(entity); @@ -35,9 +35,7 @@ pub fn parent_update_system( } // Remove from `PreviousParent.Children`. - if let Ok(mut previous_parent_children) = - children_query.get_mut::(previous_parent.0) - { + if let Ok(mut previous_parent_children) = children_query.get_mut(previous_parent.0) { log::trace!(" > Removing {:?} from prev parent's children", entity); (*previous_parent_children).0.retain(|e| *e != entity); } @@ -52,7 +50,7 @@ pub fn parent_update_system( // Add to the parent's `Children` (either the real component, or // `children_additions`). log::trace!("Adding {:?} to it's new parent {:?}", entity, parent.0); - if let Ok(mut new_parent_children) = children_query.get_mut::(parent.0) { + if let Ok(mut new_parent_children) = children_query.get_mut(parent.0) { // This is the parent log::trace!( " > The new parent {:?} already has a `Children`, adding to it.", diff --git a/crates/bevy_transform/src/transform_propagate_system.rs b/crates/bevy_transform/src/transform_propagate_system.rs index 625eefa160460..287aa9a7a870f 100644 --- a/crates/bevy_transform/src/transform_propagate_system.rs +++ b/crates/bevy_transform/src/transform_propagate_system.rs @@ -8,14 +8,20 @@ pub fn transform_propagate_system( With, &Transform, &mut GlobalTransform)>, >, >, - mut transform_query: Query)>>, + mut transform_query: Query>, + children_query: Query>>>, ) { for (children, transform, mut global_transform) in root_query.iter_mut() { *global_transform = GlobalTransform::from(*transform); if let Some(children) = children { for child in children.0.iter() { - propagate_recursive(&global_transform, &mut transform_query, *child); + propagate_recursive( + &global_transform, + &mut transform_query, + &children_query, + *child, + ); } } } @@ -23,15 +29,14 @@ pub fn transform_propagate_system( fn propagate_recursive( parent: &GlobalTransform, - transform_query: &mut Query< - With)>, - >, + transform_query: &mut Query>, + children_query: &Query>>>, entity: Entity, ) { log::trace!("Updating Transform for {:?}", entity); let global_matrix = { - if let Ok((transform, mut global_transform, _)) = transform_query.entity_mut(entity) { + if let Ok((transform, mut global_transform)) = transform_query.get_mut(entity) { *global_transform = parent.mul_transform(*transform); *global_transform } else { @@ -39,14 +44,10 @@ fn propagate_recursive( } }; - // Collect children - let children = transform_query - .get::(entity) - .map(|e| e.0.iter().cloned().collect::>()) - .unwrap_or_default(); - - for child in children { - propagate_recursive(&global_matrix, transform_query, child); + if let Ok(Some(children)) = children_query.get(entity) { + for child in children.0.iter() { + propagate_recursive(&global_matrix, transform_query, children_query, *child); + } } } diff --git a/crates/bevy_ui/src/focus.rs b/crates/bevy_ui/src/focus.rs index 88e6af5c0ec2f..86277c9d9ace2 100644 --- a/crates/bevy_ui/src/focus.rs +++ b/crates/bevy_ui/src/focus.rs @@ -124,7 +124,9 @@ pub fn ui_focus_system( if let Some(new_hovered_entity) = hovered_entity { if let Some(old_hovered_entity) = state.hovered_entity { if new_hovered_entity != old_hovered_entity { - if let Ok(mut interaction) = node_query.get_mut::(old_hovered_entity) { + if let Ok(mut interaction) = + node_query.get_component_mut::(old_hovered_entity) + { if *interaction == Interaction::Hovered { *interaction = Interaction::None; } diff --git a/crates/bevy_ui/src/update.rs b/crates/bevy_ui/src/update.rs index fdb25f34c0404..93c91e9de96bb 100644 --- a/crates/bevy_ui/src/update.rs +++ b/crates/bevy_ui/src/update.rs @@ -41,7 +41,7 @@ fn update_node_entity( }; let global_z = z + parent_global_z; - if let Ok(mut transform) = node_query.get_mut::(entity) { + if let Ok(mut transform) = node_query.get_component_mut::(entity) { transform.translation.set_z(z); } diff --git a/crates/bevy_ui/src/widget/text.rs b/crates/bevy_ui/src/widget/text.rs index 47db1442279c8..05228381bf77c 100644 --- a/crates/bevy_ui/src/widget/text.rs +++ b/crates/bevy_ui/src/widget/text.rs @@ -38,7 +38,7 @@ pub fn text_system( // add queued text to atlases let mut new_queued_text = Vec::new(); for entity in queued_text.entities.drain(..) { - if let Ok((text, mut calculated_size)) = queries.q1_mut().entity_mut(entity) { + if let Ok((text, mut calculated_size)) = queries.q1_mut().get_mut(entity) { let font_atlases = font_atlas_sets .get_or_insert_with(text.font.id, || FontAtlasSet::new(text.font.clone_weak())); // TODO: this call results in one or more TextureAtlases, whose render resources are created in the RENDER_GRAPH_SYSTEMS diff --git a/examples/3d/z_sort_debug.rs b/examples/3d/z_sort_debug.rs index 001f4b76d0094..4466687ab6be8 100644 --- a/examples/3d/z_sort_debug.rs +++ b/examples/3d/z_sort_debug.rs @@ -32,9 +32,7 @@ fn camera_order_color_system( ) { for (_camera, visible_entities) in camera_query.iter() { for visible_entity in visible_entities.iter() { - if let Ok(material_handle) = - material_query.get::>(visible_entity.entity) - { + if let Ok(material_handle) = material_query.get(visible_entity.entity) { let material = materials.get_mut(&*material_handle).unwrap(); let value = 1.0 - (visible_entity.order.0 - 10.0) / 7.0; material.albedo = Color::rgb(value, value, value); diff --git a/examples/ecs/hierarchy.rs b/examples/ecs/hierarchy.rs index 2badb53ab3c8d..8687a6bb07991 100644 --- a/examples/ecs/hierarchy.rs +++ b/examples/ecs/hierarchy.rs @@ -96,14 +96,14 @@ fn rotate( ) { let angle = std::f32::consts::PI / 2.0; for (parent, mut children, _) in parents_query.iter_mut() { - if let Ok(mut transform) = transform_query.entity_mut(parent) { + if let Ok(mut transform) = transform_query.get_mut(parent) { transform.rotate(Quat::from_rotation_z(-angle * time.delta_seconds)); } // To iterate through the entities children, just treat the Children component as a Vec // Alternatively, you could query entities that have a Parent component for child in children.iter() { - if let Ok(mut transform) = transform_query.entity_mut(*child) { + if let Ok(mut transform) = transform_query.get_mut(*child) { transform.rotate(Quat::from_rotation_z(angle * 2.0 * time.delta_seconds)); } } diff --git a/examples/ui/button.rs b/examples/ui/button.rs index 0c7bf6f439310..d04d5a0d8409b 100644 --- a/examples/ui/button.rs +++ b/examples/ui/button.rs @@ -38,7 +38,7 @@ fn button_system( mut text_query: Query<&mut Text>, ) { for (_button, interaction, mut material, children) in interaction_query.iter_mut() { - let mut text = text_query.get_mut::(children[0]).unwrap(); + let mut text = text_query.get_mut(children[0]).unwrap(); match *interaction { Interaction::Clicked => { text.value = "Press".to_string();