Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
19 changes: 18 additions & 1 deletion 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 @@ -21,6 +21,23 @@ pub struct Name {
name: Cow<'static, str>,
}

impl FromReflect for Name {
fn from_reflect(reflect: &dyn Reflect) -> Option<Self> {
match reflect.reflect_ref() {
bevy_reflect::ReflectRef::Struct(strukt) => {
let name = Cow::from_reflect(
strukt
.field("name")
.expect("missing `name` field to construct a `Name`"),
)
.expect("`name` field is not a Cow<str>");
Some(Name::new(name))
}
_ => None,
}
}
}

impl Default for Name {
fn default() -> Self {
Name::new("")
Expand Down
8 changes: 4 additions & 4 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::{prelude::*, 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, Serialize, Deserialize, Clone, Debug, Default, FromReflect)]
#[reflect(Serialize, Deserialize)]
pub enum ClearColorConfig {
#[default]
Expand All @@ -17,8 +17,8 @@ pub enum ClearColorConfig {
///
/// This color appears as the "background" color for simple apps,
/// when there are portions of the screen with nothing rendered.
#[derive(Resource, Clone, Debug, Deref, DerefMut, ExtractResource, Reflect)]
#[reflect(Resource)]
#[derive(Resource, Clone, Debug, Deref, DerefMut, ExtractResource, Reflect, FromReflect)]
#[reflect(Resource, Default)]
pub struct ClearColor(pub Color);

impl Default for ClearColor {
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, tonemapping::Tonemapping};
use bevy_ecs::{prelude::*, query::QueryItem};
use bevy_reflect::Reflect;
use bevy_reflect::{FromReflect, Reflect};
use bevy_render::{
camera::{Camera, CameraProjection, CameraRenderGraph, OrthographicProjection},
extract_component::ExtractComponent,
Expand All @@ -9,7 +9,7 @@ use bevy_render::{
};
use bevy_transform::prelude::{GlobalTransform, Transform};

#[derive(Component, Default, Reflect, Clone)]
#[derive(Component, Default, Reflect, Clone, FromReflect)]
#[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, tonemapping::Tonemapping};
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, Clone, Default, FromReflect)]
#[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, Serialize, Deserialize, Clone, Debug, FromReflect)]
#[reflect(Serialize, Deserialize)]
pub enum Camera3dDepthLoadOp {
/// Clear with a specified value.
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ecs/src/entity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ impl Entity {
///
/// ```no_run
/// # use bevy_ecs::{prelude::*, component::*};
/// # use bevy_reflect::Reflect;
/// #[derive(Reflect, Component)]
/// # use bevy_reflect::{Reflect, prelude::*};
/// #[derive(Reflect, Component, FromReflect)]
/// #[reflect(Component)]
/// pub struct MyStruct {
/// pub entity: Entity,
Expand Down
49 changes: 30 additions & 19 deletions crates/bevy_ecs/src/reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use crate::{
component::Component,
entity::{Entity, EntityMap, MapEntities, MapEntitiesError},
system::Resource,
world::{FromWorld, World},
world::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 Down Expand Up @@ -63,7 +63,7 @@ impl ReflectComponentFns {
///
/// This is useful if you want to start with the default implementation before overriding some
/// of the functions to create a custom implementation.
pub fn new<T: Component + Reflect + FromWorld>() -> Self {
pub fn new<T: Component + Reflect + FromReflect>() -> Self {
<ReflectComponent as FromType<T>>::from_type().0
}
}
Expand Down Expand Up @@ -170,12 +170,16 @@ 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(ReflectComponentFns {
insert: |world, entity, reflected_component| {
let mut component = C::from_world(world);
component.apply(reflected_component);
let component = C::from_reflect(reflected_component).unwrap_or_else(|| {
panic!(
"failed to convert {reflected_component:?} to a {} using FromReflect",
std::any::type_name::<C>()
)
});
world.entity_mut(entity).insert(component);
},
apply: |world, entity, reflected_component| {
Expand All @@ -186,8 +190,12 @@ 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_or_else(|| {
panic!(
"failed to convert {reflected_component:?} to a {} using FromReflect",
std::any::type_name::<C>()
)
});
world.entity_mut(entity).insert(component);
}
},
Expand All @@ -196,8 +204,14 @@ 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_or_else(|| {
panic!(
"failed to convert {:?} to a {} using FromReflect",
source_component as &dyn Reflect,
std::any::type_name::<C>()
)
});
destination_world
.entity_mut(destination_entity)
.insert(destination_component);
Expand Down Expand Up @@ -276,7 +290,7 @@ impl ReflectResourceFns {
///
/// This is useful if you want to start with the default implementation before overriding some
/// of the functions to create a custom implementation.
pub fn new<T: Resource + Reflect + FromWorld>() -> Self {
pub fn new<T: Resource + Reflect + FromReflect>() -> Self {
<ReflectResource as FromType<T>>::from_type().0
}
}
Expand Down Expand Up @@ -356,12 +370,11 @@ impl ReflectResource {
}
}

impl<C: Resource + Reflect + FromWorld> FromType<C> for ReflectResource {
impl<C: Resource + Reflect + FromReflect> FromType<C> for ReflectResource {
fn from_type() -> Self {
ReflectResource(ReflectResourceFns {
insert: |world, reflected_resource| {
let mut resource = C::from_world(world);
resource.apply(reflected_resource);
let resource = C::from_reflect(reflected_resource).unwrap();
world.insert_resource(resource);
},
apply: |world, reflected_resource| {
Expand All @@ -372,8 +385,7 @@ impl<C: Resource + Reflect + FromWorld> FromType<C> for ReflectResource {
if let Some(mut resource) = world.get_resource_mut::<C>() {
resource.apply(reflected_resource);
} else {
let mut resource = C::from_world(world);
resource.apply(reflected_resource);
let resource = C::from_reflect(reflected_resource).unwrap();
world.insert_resource(resource);
}
},
Expand All @@ -393,8 +405,7 @@ impl<C: Resource + Reflect + FromWorld> FromType<C> for ReflectResource {
},
copy: |source_world, destination_world| {
let source_resource = source_world.resource::<C>();
let mut destination_resource = C::from_world(destination_world);
destination_resource.apply(source_resource);
let destination_resource = C::from_reflect(source_resource).unwrap();
destination_world.insert_resource(destination_resource);
},
})
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, Default, Component, FromReflect)]
#[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};

// TODO: add discussion about performance.
/// Sets how a material's base color alpha channel is used for transparency.
#[derive(Component, Debug, Default, Reflect, Copy, Clone, PartialEq)]
#[derive(Component, Debug, Default, Reflect, Copy, Clone, PartialEq, FromReflect)]
#[reflect(Component, Default)]
pub enum AlphaMode {
/// Base color alpha values are overridden to be fully opaque (1.0).
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
16 changes: 8 additions & 8 deletions crates/bevy_pbr/src/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,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 @@ -73,7 +73,7 @@ impl PointLight {
pub const DEFAULT_SHADOW_NORMAL_BIAS: f32 = 0.6;
}

#[derive(Resource, Clone, Debug, Reflect)]
#[derive(Resource, Clone, Debug, Reflect, FromReflect)]
#[reflect(Resource)]
pub struct PointLightShadowMap {
pub size: usize,
Expand All @@ -89,7 +89,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 @@ -167,7 +167,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 @@ -208,7 +208,7 @@ impl DirectionalLight {
pub const DEFAULT_SHADOW_NORMAL_BIAS: f32 = 0.6;
}

#[derive(Resource, Clone, Debug, Reflect)]
#[derive(Resource, Clone, Debug, Reflect, FromReflect)]
#[reflect(Resource)]
pub struct DirectionalLightShadowMap {
pub size: usize,
Expand All @@ -224,7 +224,7 @@ impl Default for DirectionalLightShadowMap {
}

/// An ambient light, which lights the entire scene equally.
#[derive(Resource, Clone, Debug, ExtractResource, Reflect)]
#[derive(Resource, Clone, Debug, ExtractResource, Reflect, FromReflect)]
#[reflect(Resource)]
pub struct AmbientLight {
pub color: Color,
Expand All @@ -242,11 +242,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, Default, FromReflect)]
#[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, Default, FromReflect)]
#[reflect(Component, Default)]
pub struct NotShadowReceiver;

Expand Down
Loading