Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReflectComponent Commands #5541

Closed
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/bevy_animation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use bevy_ecs::{
};
use bevy_hierarchy::Children;
use bevy_math::{Quat, Vec3};
use bevy_reflect::{Reflect, TypeUuid};
use bevy_reflect::{FromReflect, Reflect, TypeUuid};
use bevy_time::Time;
use bevy_transform::{prelude::Transform, TransformSystem};
use bevy_utils::{tracing::warn, HashMap};
Expand Down Expand Up @@ -91,7 +91,7 @@ impl AnimationClip {
}

/// Animation controls
#[derive(Component, Reflect)]
#[derive(Component, Reflect, FromReflect)]
#[reflect(Component)]
pub struct AnimationPlayer {
paused: bool,
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_core/src/name.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bevy_ecs::{component::Component, reflect::ReflectComponent};
use bevy_reflect::std_traits::ReflectDefault;
use bevy_reflect::Reflect;
use bevy_reflect::{FromReflect, Reflect};
use bevy_utils::AHasher;
use std::{
borrow::Cow,
Expand All @@ -14,7 +14,7 @@ use std::{
/// [`Name`] should not be treated as a globally unique identifier for entities,
/// as multiple entities can have the same name. [`bevy_ecs::entity::Entity`] should be
/// used instead as the default unique identifier.
#[derive(Component, Debug, Clone, Reflect)]
#[derive(Component, Debug, Clone, Reflect, FromReflect)]
#[reflect(Component, Default)]
pub struct Name {
hash: u64, // TODO: Shouldn't be serialized
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_core_pipeline/src/clear_color.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use bevy_derive::{Deref, DerefMut};
use bevy_ecs::prelude::*;
use bevy_reflect::{Reflect, ReflectDeserialize, ReflectSerialize};
use bevy_reflect::{FromReflect, Reflect, ReflectDeserialize, ReflectSerialize};
use bevy_render::{color::Color, extract_resource::ExtractResource};
use serde::{Deserialize, Serialize};

#[derive(Reflect, Serialize, Deserialize, Clone, Debug, Default)]
#[derive(Reflect, FromReflect, Serialize, Deserialize, Clone, Debug, Default)]
#[reflect_value(Serialize, Deserialize)]
pub enum ClearColorConfig {
#[default]
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_core_pipeline/src/core_2d/camera_2d.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::clear_color::ClearColorConfig;
use bevy_ecs::{prelude::*, query::QueryItem};
use bevy_reflect::Reflect;
use bevy_reflect::{FromReflect, Reflect};
use bevy_render::{
camera::{
Camera, CameraProjection, CameraRenderGraph, DepthCalculation, OrthographicProjection,
Expand All @@ -11,7 +11,7 @@ use bevy_render::{
};
use bevy_transform::prelude::{GlobalTransform, Transform};

#[derive(Component, Default, Reflect, Clone)]
#[derive(Component, Default, Reflect, FromReflect, Clone)]
#[reflect(Component)]
pub struct Camera2d {
pub clear_color: ClearColorConfig,
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_core_pipeline/src/core_3d/camera_3d.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::clear_color::ClearColorConfig;
use bevy_ecs::{prelude::*, query::QueryItem};
use bevy_reflect::{Reflect, ReflectDeserialize, ReflectSerialize};
use bevy_reflect::{FromReflect, Reflect, ReflectDeserialize, ReflectSerialize};
use bevy_render::{
camera::{Camera, CameraRenderGraph, Projection},
extract_component::ExtractComponent,
Expand All @@ -12,7 +12,7 @@ use bevy_transform::prelude::{GlobalTransform, Transform};
use serde::{Deserialize, Serialize};

/// Configuration for the "main 3d render graph".
#[derive(Component, Reflect, Clone, Default)]
#[derive(Component, Reflect, FromReflect, Clone, Default)]
#[reflect(Component)]
pub struct Camera3d {
/// The clear color operation to perform for the main 3d pass.
Expand All @@ -22,7 +22,7 @@ pub struct Camera3d {
}

/// The depth clear operation to perform for the main 3d pass.
#[derive(Reflect, Serialize, Deserialize, Clone, Debug)]
#[derive(Reflect, FromReflect, Serialize, Deserialize, Clone, Debug)]
#[reflect_value(Serialize, Deserialize)]
pub enum Camera3dDepthLoadOp {
/// Clear with a specified value.
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_ecs/src/entity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ impl Entity {
/// ```no_run
/// # use bevy_ecs::{prelude::*, component::*};
/// # use bevy_reflect::Reflect;
/// #[derive(Reflect, Component)]
/// # use bevy_reflect::FromReflect;
/// #[derive(Reflect, FromReflect, Component)]
/// #[reflect(Component)]
/// pub struct MyStruct {
/// pub entity: Entity,
Expand Down
26 changes: 17 additions & 9 deletions crates/bevy_ecs/src/reflect.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
//! Types that enable reflection support.

pub use crate::change_detection::ReflectMut;
use crate::system::EntityCommands;
use crate::{
component::Component,
entity::{Entity, EntityMap, MapEntities, MapEntitiesError},
system::Resource,
world::{FromWorld, World},
};
use bevy_reflect::{
impl_from_reflect_value, impl_reflect_value, FromType, Reflect, ReflectDeserialize,
ReflectSerialize,
impl_from_reflect_value, impl_reflect_value, FromReflect, FromType, Reflect,
ReflectDeserialize, ReflectSerialize,
};

/// A struct used to operate on reflected [`Component`] of a type.
Expand All @@ -19,6 +20,7 @@ use bevy_reflect::{
#[derive(Clone)]
pub struct ReflectComponent {
insert: fn(&mut World, Entity, &dyn Reflect),
insert_command: fn(&mut EntityCommands, &dyn Reflect),
apply: fn(&mut World, Entity, &dyn Reflect),
apply_or_insert: fn(&mut World, Entity, &dyn Reflect),
remove: fn(&mut World, Entity),
Expand All @@ -37,6 +39,11 @@ impl ReflectComponent {
(self.insert)(world, entity, component);
}

/// Insert a reflected [`Component`] into the entity like [`EntityCommands::insert()`]
pub fn insert_command(&self, commands: &mut EntityCommands, component: &dyn Reflect) {
(self.insert_command)(commands, component);
}

/// Uses reflection to set the value of this [`Component`] type in the entity to the given value.
///
/// # Panics
Expand Down Expand Up @@ -110,14 +117,17 @@ impl ReflectComponent {
}
}

impl<C: Component + Reflect + FromWorld> FromType<C> for ReflectComponent {
impl<C: Component + Reflect + FromReflect> FromType<C> for ReflectComponent {
fn from_type() -> Self {
ReflectComponent {
insert: |world, entity, reflected_component| {
let mut component = C::from_world(world);
component.apply(reflected_component);
let component = C::from_reflect(reflected_component).unwrap();
PROMETHIA-27 marked this conversation as resolved.
Show resolved Hide resolved
world.entity_mut(entity).insert(component);
},
insert_command: |commands, reflected_component| {
let component = C::from_reflect(reflected_component).unwrap();
commands.insert(component);
},
apply: |world, entity, reflected_component| {
let mut component = world.get_mut::<C>(entity).unwrap();
component.apply(reflected_component);
Expand All @@ -126,8 +136,7 @@ impl<C: Component + Reflect + FromWorld> FromType<C> for ReflectComponent {
if let Some(mut component) = world.get_mut::<C>(entity) {
component.apply(reflected_component);
} else {
let mut component = C::from_world(world);
component.apply(reflected_component);
let component = C::from_reflect(reflected_component).unwrap();
world.entity_mut(entity).insert(component);
}
},
Expand All @@ -136,8 +145,7 @@ impl<C: Component + Reflect + FromWorld> FromType<C> for ReflectComponent {
},
copy: |source_world, destination_world, source_entity, destination_entity| {
let source_component = source_world.get::<C>(source_entity).unwrap();
let mut destination_component = C::from_world(destination_world);
destination_component.apply(source_component);
let destination_component = C::from_reflect(source_component).unwrap();
destination_world
.entity_mut(destination_entity)
.insert(destination_component);
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_gltf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use bevy_app::prelude::*;
use bevy_asset::{AddAsset, Handle};
use bevy_ecs::{prelude::Component, reflect::ReflectComponent};
use bevy_pbr::StandardMaterial;
use bevy_reflect::{Reflect, TypeUuid};
use bevy_reflect::{FromReflect, Reflect, TypeUuid};
use bevy_render::mesh::Mesh;
use bevy_scene::Scene;

Expand Down Expand Up @@ -72,7 +72,7 @@ pub struct GltfPrimitive {
pub material: Option<Handle<StandardMaterial>>,
}

#[derive(Clone, Debug, Reflect, Default, Component)]
#[derive(Clone, Debug, Reflect, FromReflect, Default, Component)]
#[reflect(Component)]
pub struct GltfExtras {
pub value: String,
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_hierarchy/src/components/children.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ use bevy_ecs::{
reflect::{ReflectComponent, ReflectMapEntities},
world::World,
};
use bevy_reflect::Reflect;
use bevy_reflect::{FromReflect, Reflect};
use core::slice;
use smallvec::SmallVec;
use std::ops::Deref;

/// Contains references to the child entities of this entity
#[derive(Component, Debug, Reflect)]
#[derive(Component, Debug, Reflect, FromReflect)]
#[reflect(Component, MapEntities)]
pub struct Children(pub(crate) SmallVec<[Entity; 8]>);

Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_hierarchy/src/components/parent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use bevy_ecs::{
reflect::{ReflectComponent, ReflectMapEntities},
world::{FromWorld, World},
};
use bevy_reflect::Reflect;
use bevy_reflect::{FromReflect, Reflect};
use std::ops::Deref;

/// Holds a reference to the parent entity of this entity.
/// This component should only be present on entities that actually have a parent entity.
#[derive(Component, Debug, Eq, PartialEq, Reflect)]
#[derive(Component, Debug, Eq, PartialEq, Reflect, FromReflect)]
#[reflect(Component, MapEntities, PartialEq)]
pub struct Parent(pub(crate) Entity);

Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_pbr/src/alpha.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use bevy_ecs::{component::Component, reflect::ReflectComponent};
use bevy_reflect::std_traits::ReflectDefault;
use bevy_reflect::Reflect;
use bevy_reflect::{FromReflect, Reflect};

// FIXME: This should probably be part of bevy_render2!
/// Alpha mode
#[derive(Component, Debug, Default, Reflect, Copy, Clone, PartialEq)]
#[derive(Component, Debug, Default, Reflect, FromReflect, Copy, Clone, PartialEq)]
#[reflect(Component, Default)]
pub enum AlphaMode {
#[default]
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_pbr/src/bundle.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{DirectionalLight, Material, PointLight, SpotLight, StandardMaterial};
use bevy_asset::Handle;
use bevy_ecs::{bundle::Bundle, component::Component, reflect::ReflectComponent};
use bevy_reflect::Reflect;
use bevy_reflect::{FromReflect, Reflect};
use bevy_render::{
mesh::Mesh,
primitives::{CubemapFrusta, Frustum},
Expand Down Expand Up @@ -38,7 +38,7 @@ impl<M: Material> Default for MaterialMeshBundle<M> {
}
}

#[derive(Component, Clone, Debug, Default, Reflect)]
#[derive(Component, Clone, Debug, Default, Reflect, FromReflect)]
#[reflect(Component)]
pub struct CubemapVisibleEntities {
#[reflect(ignore)]
Expand Down
11 changes: 6 additions & 5 deletions crates/bevy_pbr/src/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::collections::HashSet;
use bevy_ecs::prelude::*;
use bevy_math::{Mat4, UVec2, UVec3, Vec2, Vec3, Vec3A, Vec3Swizzles, Vec4, Vec4Swizzles};
use bevy_reflect::prelude::*;
use bevy_reflect::FromReflect;
use bevy_render::{
camera::{Camera, CameraProjection, OrthographicProjection},
color::Color,
Expand Down Expand Up @@ -38,7 +39,7 @@ use crate::{
/// | 4000 | 300 | | 75-100 | 40.5 |
///
/// Source: [Wikipedia](https://en.wikipedia.org/wiki/Lumen_(unit)#Lighting)
#[derive(Component, Debug, Clone, Copy, Reflect)]
#[derive(Component, Debug, Clone, Copy, Reflect, FromReflect)]
#[reflect(Component, Default)]
pub struct PointLight {
pub color: Color,
Expand Down Expand Up @@ -89,7 +90,7 @@ impl Default for PointLightShadowMap {
/// Behaves like a point light in a perfectly absorbant housing that
/// shines light only in a given direction. The direction is taken from
/// the transform, and can be specified with [`Transform::looking_at`](bevy_transform::components::Transform::looking_at).
#[derive(Component, Debug, Clone, Copy, Reflect)]
#[derive(Component, Debug, Clone, Copy, Reflect, FromReflect)]
#[reflect(Component, Default)]
pub struct SpotLight {
pub color: Color,
Expand Down Expand Up @@ -164,7 +165,7 @@ impl Default for SpotLight {
/// | 32,000–100,000 | Direct sunlight |
///
/// Source: [Wikipedia](https://en.wikipedia.org/wiki/Lux)
#[derive(Component, Debug, Clone, Reflect)]
#[derive(Component, Debug, Clone, Reflect, FromReflect)]
#[reflect(Component, Default)]
pub struct DirectionalLight {
pub color: Color,
Expand Down Expand Up @@ -239,11 +240,11 @@ impl Default for AmbientLight {
}

/// Add this component to make a [`Mesh`](bevy_render::mesh::Mesh) not cast shadows.
#[derive(Component, Reflect, Default)]
#[derive(Component, Reflect, FromReflect, Default)]
#[reflect(Component, Default)]
pub struct NotShadowCaster;
/// Add this component to make a [`Mesh`](bevy_render::mesh::Mesh) not receive shadows.
#[derive(Component, Reflect, Default)]
#[derive(Component, Reflect, FromReflect, Default)]
#[reflect(Component, Default)]
pub struct NotShadowReceiver;

Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_pbr/src/wireframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bevy_asset::{load_internal_asset, Handle, HandleUntyped};
use bevy_core_pipeline::core_3d::Opaque3d;
use bevy_ecs::{prelude::*, reflect::ReflectComponent};
use bevy_reflect::std_traits::ReflectDefault;
use bevy_reflect::{Reflect, TypeUuid};
use bevy_reflect::{FromReflect, Reflect, TypeUuid};
use bevy_render::Extract;
use bevy_render::{
extract_resource::{ExtractResource, ExtractResourcePlugin},
Expand Down Expand Up @@ -58,7 +58,7 @@ fn extract_wireframes(mut commands: Commands, query: Extract<Query<Entity, With<
}

/// Controls whether an entity should rendered in wireframe-mode if the [`WireframePlugin`] is enabled
#[derive(Component, Debug, Clone, Default, Reflect)]
#[derive(Component, Debug, Clone, Default, Reflect, FromReflect)]
#[reflect(Component, Default)]
pub struct Wireframe;

Expand Down
7 changes: 4 additions & 3 deletions crates/bevy_render/src/camera/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use bevy_ecs::{
};
use bevy_math::{Mat4, UVec2, Vec2, Vec3};
use bevy_reflect::prelude::*;
use bevy_reflect::FromReflect;
use bevy_transform::components::GlobalTransform;
use bevy_utils::HashSet;
use bevy_window::{WindowCreated, WindowId, WindowResized, Windows};
Expand Down Expand Up @@ -71,7 +72,7 @@ pub struct ComputedCameraValues {
target_info: Option<RenderTargetInfo>,
}

#[derive(Component, Debug, Reflect, Clone)]
#[derive(Component, Debug, Reflect, FromReflect, Clone)]
#[reflect(Component)]
pub struct Camera {
/// If set, this camera will render to the given [`Viewport`] rectangle within the configured [`RenderTarget`].
Expand Down Expand Up @@ -230,7 +231,7 @@ impl Camera {
}

/// Configures the [`RenderGraph`](crate::render_graph::RenderGraph) name assigned to be run for a given [`Camera`] entity.
#[derive(Component, Deref, DerefMut, Reflect, Default)]
#[derive(Component, Deref, DerefMut, Reflect, FromReflect, Default)]
#[reflect(Component)]
pub struct CameraRenderGraph(Cow<'static, str>);

Expand Down Expand Up @@ -309,7 +310,7 @@ impl RenderTarget {
}
}

#[derive(Debug, Clone, Copy, Default, Reflect, Serialize, Deserialize)]
#[derive(Debug, Clone, Copy, Default, Reflect, FromReflect, Serialize, Deserialize)]
#[reflect_value(Serialize, Deserialize)]
pub enum DepthCalculation {
/// Pythagorean distance; works everywhere, more expensive to compute.
Expand Down
Loading