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

[Merged by Bors] - add #[reflect(Default)] to create default value for reflected types #3733

Closed
Closed
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion crates/bevy_core/src/name.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use bevy_ecs::{component::Component, reflect::ReflectComponent};
use bevy_reflect::std_traits::ReflectDefault;
use bevy_reflect::Reflect;
use bevy_utils::AHasher;
use std::{
Expand All @@ -14,7 +15,7 @@ use std::{
/// 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)]
#[reflect(Component)]
#[reflect(Component, Default)]
pub struct Name {
hash: u64, // TODO: Shouldn't be serialized
name: Cow<'static, str>,
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_core/src/time/stopwatch.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use bevy_ecs::{component::Component, reflect::ReflectComponent};
use bevy_reflect::std_traits::ReflectDefault;
use bevy_reflect::Reflect;
use bevy_utils::Duration;

Expand All @@ -24,7 +25,7 @@ use bevy_utils::Duration;
/// assert_eq!(stopwatch.elapsed_secs(), 0.0);
/// ```
#[derive(Component, Clone, Debug, Default, Reflect)]
#[reflect(Component)]
#[reflect(Component, Default)]
pub struct Stopwatch {
elapsed: Duration,
paused: bool,
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_core/src/time/timer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::Stopwatch;
use bevy_ecs::{component::Component, reflect::ReflectComponent};
use bevy_reflect::std_traits::ReflectDefault;
use bevy_reflect::Reflect;
use bevy_utils::Duration;

Expand All @@ -11,7 +12,7 @@ use bevy_utils::Duration;
///
/// Paused timers will not have elapsed time increased.
#[derive(Component, Clone, Debug, Default, Reflect)]
#[reflect(Component)]
#[reflect(Component, Default)]
pub struct Timer {
stopwatch: Stopwatch,
duration: Duration,
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_pbr/src/alpha.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use bevy_ecs::{component::Component, reflect::ReflectComponent};
use bevy_reflect::std_traits::ReflectDefault;
use bevy_reflect::Reflect;

// FIXME: This should probably be part of bevy_render2!
/// Alpha mode
#[derive(Component, Debug, Reflect, Copy, Clone, PartialEq)]
#[reflect(Component)]
#[reflect(Component, Default)]
pub enum AlphaMode {
Opaque,
/// An alpha cutoff must be supplied where alpha values >= the cutoff
Expand Down
5 changes: 3 additions & 2 deletions crates/bevy_pbr/src/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::HashSet;

use bevy_ecs::prelude::*;
use bevy_math::{Mat4, UVec2, UVec3, Vec2, Vec3, Vec3Swizzles, Vec4, Vec4Swizzles};
use bevy_reflect::std_traits::ReflectDefault;
use bevy_reflect::Reflect;
use bevy_render::{
camera::{Camera, CameraProjection, OrthographicProjection},
Expand Down Expand Up @@ -35,7 +36,7 @@ use crate::{
///
/// Source: [Wikipedia](https://en.wikipedia.org/wiki/Lumen_(unit)#Lighting)
#[derive(Component, Debug, Clone, Copy, Reflect)]
#[reflect(Component)]
#[reflect(Component, Default)]
pub struct PointLight {
pub color: Color,
pub intensity: f32,
Expand Down Expand Up @@ -107,7 +108,7 @@ impl Default for PointLightShadowMap {
///
/// Source: [Wikipedia](https://en.wikipedia.org/wiki/Lux)
#[derive(Component, Debug, Clone, Reflect)]
#[reflect(Component)]
#[reflect(Component, Default)]
pub struct DirectionalLight {
pub color: Color,
/// Illuminance in lux
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_pbr/src/wireframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use bevy_app::Plugin;
use bevy_asset::{Assets, Handle, HandleUntyped};
use bevy_core_pipeline::Opaque3d;
use bevy_ecs::{prelude::*, reflect::ReflectComponent};
use bevy_reflect::std_traits::ReflectDefault;
use bevy_reflect::{Reflect, TypeUuid};
use bevy_render::render_resource::PolygonMode;
use bevy_render::{
Expand Down Expand Up @@ -57,7 +58,7 @@ fn extract_wireframes(mut commands: Commands, query: Query<Entity, With<Wirefram

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

#[derive(Debug, Clone, Default)]
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_reflect/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ mod impls {
}

pub mod serde;
pub mod std_traits;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this module. It makes it obvious where to find std-based Reflect*** structs.

Though, imo this module should also be put in the prelude since I can see them being used a lot (as far as reflection usage goes at least haha).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that this makes more sense to go in prelude. Would be annoying to want to slap standard derives on a component and need to add one additional import to do that.


pub mod prelude {
#[doc(hidden)]
pub use crate::{
Expand Down
20 changes: 20 additions & 0 deletions crates/bevy_reflect/src/std_traits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use crate::{FromType, Reflect};

#[derive(Clone)]
pub struct ReflectDefault {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I know it's a relatively straightforward struct, but I think a doc comment could still be nice.

default: fn() -> Box<dyn Reflect>,
}

impl ReflectDefault {
pub fn default(&self) -> Box<dyn Reflect> {
(self.default)()
}
}

impl<T: Reflect + Default> FromType<T> for ReflectDefault {
fn from_type() -> Self {
ReflectDefault {
default: || Box::new(T::default()),
}
}
}
3 changes: 2 additions & 1 deletion crates/bevy_render/src/camera/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ use bevy_ecs::{
system::{QuerySet, Res},
};
use bevy_math::{Mat4, Vec2, Vec3};
use bevy_reflect::std_traits::ReflectDefault;
use bevy_reflect::{Reflect, ReflectDeserialize};
use bevy_transform::components::GlobalTransform;
use bevy_window::{WindowCreated, WindowId, WindowResized, Windows};
use serde::{Deserialize, Serialize};

#[derive(Component, Default, Debug, Reflect)]
#[reflect(Component)]
#[reflect(Component, Default)]
pub struct Camera {
pub projection_matrix: Mat4,
pub name: Option<String>,
Expand Down
5 changes: 3 additions & 2 deletions crates/bevy_render/src/camera/projection.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::DepthCalculation;
use bevy_ecs::{component::Component, reflect::ReflectComponent};
use bevy_math::Mat4;
use bevy_reflect::std_traits::ReflectDefault;
use bevy_reflect::{Reflect, ReflectDeserialize};
use serde::{Deserialize, Serialize};

Expand All @@ -12,7 +13,7 @@ pub trait CameraProjection {
}

#[derive(Component, Debug, Clone, Reflect)]
#[reflect(Component)]
#[reflect(Component, Default)]
pub struct PerspectiveProjection {
pub fov: f32,
pub aspect_ratio: f32,
Expand Down Expand Up @@ -72,7 +73,7 @@ pub enum ScalingMode {
}

#[derive(Component, Debug, Clone, Reflect)]
#[reflect(Component)]
#[reflect(Component, Default)]
pub struct OrthographicProjection {
pub left: f32,
pub right: f32,
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_render/src/view/visibility/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub use render_layers::*;
use bevy_app::{CoreStage, Plugin};
use bevy_asset::{Assets, Handle};
use bevy_ecs::prelude::*;
use bevy_reflect::std_traits::ReflectDefault;
use bevy_reflect::Reflect;
use bevy_transform::{components::GlobalTransform, TransformSystem};

Expand All @@ -16,7 +17,7 @@ use crate::{

/// User indication of whether an entity is visible
#[derive(Component, Clone, Reflect, Debug)]
#[reflect(Component)]
#[reflect(Component, Default)]
pub struct Visibility {
pub is_visible: bool,
}
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_render/src/view/visibility/render_layers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use bevy_ecs::prelude::{Component, ReflectComponent};
use bevy_reflect::std_traits::ReflectDefault;
use bevy_reflect::Reflect;

type LayerMask = u32;
Expand All @@ -20,7 +21,7 @@ pub type Layer = u8;
///
/// Entities without this component belong to layer `0`.
#[derive(Component, Copy, Clone, Reflect, PartialEq, Eq, PartialOrd, Ord)]
#[reflect(Component, PartialEq)]
#[reflect(Component, Default, PartialEq)]
pub struct RenderLayers(LayerMask);

impl std::fmt::Debug for RenderLayers {
Expand Down
5 changes: 3 additions & 2 deletions crates/bevy_text/src/text.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use bevy_asset::Handle;
use bevy_ecs::{prelude::Component, reflect::ReflectComponent};
use bevy_math::Size;
use bevy_reflect::std_traits::ReflectDefault;
use bevy_reflect::{FromReflect, Reflect, ReflectDeserialize};
use bevy_render::color::Color;
use serde::{Deserialize, Serialize};

use crate::Font;

#[derive(Component, Debug, Default, Clone, Reflect)]
#[reflect(Component)]
#[reflect(Component, Default)]
pub struct Text {
pub sections: Vec<TextSection>,
pub alignment: TextAlignment,
Expand Down Expand Up @@ -152,7 +153,7 @@ impl Default for TextStyle {
}

#[derive(Component, Default, Copy, Clone, Debug, Reflect)]
#[reflect(Component)]
#[reflect(Component, Default)]
pub struct Text2dSize {
pub size: Size,
}
3 changes: 2 additions & 1 deletion crates/bevy_transform/src/components/global_transform.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::Transform;
use bevy_ecs::{component::Component, reflect::ReflectComponent};
use bevy_math::{Mat3, Mat4, Quat, Vec3};
use bevy_reflect::std_traits::ReflectDefault;
use bevy_reflect::Reflect;
use std::ops::Mul;

Expand Down Expand Up @@ -34,7 +35,7 @@ use std::ops::Mul;
/// update the[`Transform`] of an entity in this stage or after, you will notice a 1 frame lag
/// before the [`GlobalTransform`] is updated.
#[derive(Component, Debug, PartialEq, Clone, Copy, Reflect)]
#[reflect(Component, PartialEq)]
#[reflect(Component, Default, PartialEq)]
pub struct GlobalTransform {
/// The position of the global transform
pub translation: Vec3,
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_transform/src/components/transform.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::GlobalTransform;
use bevy_ecs::{component::Component, reflect::ReflectComponent};
use bevy_math::{Mat3, Mat4, Quat, Vec3};
use bevy_reflect::std_traits::ReflectDefault;
use bevy_reflect::Reflect;
use std::ops::Mul;

Expand Down Expand Up @@ -35,7 +36,7 @@ use std::ops::Mul;
/// update the[`Transform`] of an entity in this stage or after, you will notice a 1 frame lag
/// before the [`GlobalTransform`] is updated.
#[derive(Component, Debug, PartialEq, Clone, Copy, Reflect)]
#[reflect(Component, PartialEq)]
#[reflect(Component, Default, PartialEq)]
pub struct Transform {
/// Position of the entity. In 2d, the last value of the `Vec3` is used for z-ordering.
pub translation: Vec3,
Expand Down
9 changes: 5 additions & 4 deletions crates/bevy_ui/src/ui_node.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use bevy_asset::Handle;
use bevy_ecs::{prelude::Component, reflect::ReflectComponent};
use bevy_math::{Rect, Size, Vec2};
use bevy_reflect::std_traits::ReflectDefault;
use bevy_reflect::{Reflect, ReflectDeserialize};
use bevy_render::{
color::Color,
Expand All @@ -11,7 +12,7 @@ use std::ops::{Add, AddAssign};

/// Describes the size of a UI node
#[derive(Component, Debug, Clone, Default, Reflect)]
#[reflect(Component)]
#[reflect(Component, Default)]
pub struct Node {
/// The size of the node as width and height in pixels
pub size: Vec2,
Expand Down Expand Up @@ -67,7 +68,7 @@ impl AddAssign<f32> for Val {
/// **Note:** Bevy's UI is upside down compared to how Flexbox normally works, to stay consistent with engine paradigms about layouting from
/// the upper left corner of the display
#[derive(Component, Clone, PartialEq, Debug, Reflect)]
#[reflect(Component, PartialEq)]
#[reflect(Component, Default, PartialEq)]
pub struct Style {
/// Whether to arrange this node and its children with flexbox layout
pub display: Display,
Expand Down Expand Up @@ -358,7 +359,7 @@ pub struct CalculatedSize {

/// The color of the node
#[derive(Component, Default, Copy, Clone, Debug, Reflect)]
#[reflect(Component)]
#[reflect(Component, Default)]
pub struct UiColor(pub Color);

impl From<Color> for UiColor {
Expand All @@ -369,7 +370,7 @@ impl From<Color> for UiColor {

/// The image of the node
#[derive(Component, Clone, Debug, Reflect)]
#[reflect(Component)]
#[reflect(Component, Default)]
pub struct UiImage(pub Handle<Image>);

impl Default for UiImage {
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_ui/src/widget/button.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use bevy_ecs::prelude::Component;
use bevy_ecs::reflect::ReflectComponent;
use bevy_reflect::std_traits::ReflectDefault;
use bevy_reflect::Reflect;

/// Marker struct for buttons
#[derive(Component, Debug, Default, Clone, Copy, Reflect)]
#[reflect(Component)]
#[reflect(Component, Default)]
pub struct Button;