Skip to content

Commit

Permalink
Bevy Reflection (#926)
Browse files Browse the repository at this point in the history
Bevy Reflection
  • Loading branch information
cart authored Nov 28, 2020
1 parent 01c4dd9 commit 72b2fc9
Show file tree
Hide file tree
Showing 138 changed files with 4,931 additions and 3,519 deletions.
22 changes: 17 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ bevy_internal = {path = "crates/bevy_internal", version = "0.3.0", default-featu
[dev-dependencies]
anyhow = "1.0"
rand = "0.7.3"
ron = "0.6"
ron = "0.6.2"
serde = {version = "1", features = ["derive"]}

[[example]]
Expand Down Expand Up @@ -255,12 +255,24 @@ name = "touch_input_events"
path = "examples/input/touch_input_events.rs"

[[example]]
name = "scene"
path = "examples/scene/scene.rs"
name = "reflection"
path = "examples/reflection/reflection.rs"

[[example]]
name = "reflection_types"
path = "examples/reflection/reflection_types.rs"

[[example]]
name = "generic_reflection"
path = "examples/reflection/generic_reflection.rs"

[[example]]
name = "trait_reflection"
path = "examples/reflection/trait_reflection.rs"

[[example]]
name = "properties"
path = "examples/scene/properties.rs"
name = "scene"
path = "examples/scene/scene.rs"

[[example]]
name = "mesh_custom_attribute"
Expand Down
54 changes: 43 additions & 11 deletions assets/scenes/load_scene_example.scn
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,42 @@
entity: 0,
components: [
{
"type": "ComponentB",
"map": {
"value": "hello",
"type": "bevy_transform::components::transform::Transform",
"struct": {
"translation": {
"type": "glam::f32::vec3::Vec3",
"value": (0.0, 0.0, 0.0),
},
"rotation": {
"type": "glam::f32::quat::Quat",
"value": (0.0, 0.0, 0.0, 1.0),
},
"scale": {
"type": "glam::f32::vec3::Vec3",
"value": (1.0, 1.0, 1.0),
},
},
},
{
"type": "ComponentA",
"map": {
"x": 1.0,
"y": 2.0,
"type": "scene::ComponentB",
"struct": {
"value": {
"type": "alloc::string::String",
"value": "hello",
},
},
},
{
"type": "scene::ComponentA",
"struct": {
"x": {
"type": "f32",
"value": 1.0,
},
"y": {
"type": "f32",
"value": 2.0,
},
},
},
],
Expand All @@ -21,10 +47,16 @@
entity: 1,
components: [
{
"type": "ComponentA",
"map": {
"x": 3.0,
"y": 4.0,
"type": "scene::ComponentA",
"struct": {
"x": {
"type": "f32",
"value": 3.0,
},
"y": {
"type": "f32",
"value": 4.0,
},
},
},
],
Expand Down
4 changes: 1 addition & 3 deletions crates/bevy_asset/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ filesystem_watcher = ["notify"]
# bevy
bevy_app = { path = "../bevy_app", version = "0.3.0" }
bevy_ecs = { path = "../bevy_ecs", version = "0.3.0" }
bevy_reflect = { path = "../bevy_reflect", version = "0.3.0", features = ["bevy"] }
bevy_tasks = { path = "../bevy_tasks", version = "0.3.0" }
bevy_type_registry = { path = "../bevy_type_registry", version = "0.3.0" }
bevy_property = { path = "../bevy_property", version = "0.3.0" }
bevy_utils = { path = "../bevy_utils", version = "0.3.0" }

# other
uuid = { version = "0.8", features = ["v4", "serde"] }
serde = { version = "1", features = ["derive"] }
ron = "0.6.2"
crossbeam-channel = "0.4.4"
Expand Down
3 changes: 1 addition & 2 deletions crates/bevy_asset/src/asset_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ use crate::{
use anyhow::Result;
use bevy_ecs::Res;
use bevy_tasks::TaskPool;
use bevy_utils::HashMap;
use bevy_utils::{HashMap, Uuid};
use crossbeam_channel::TryRecvError;
use parking_lot::RwLock;
use std::{collections::hash_map::Entry, path::Path, sync::Arc};
use thiserror::Error;
use uuid::Uuid;

/// Errors that occur while loading assets with an AssetServer
#[derive(Error, Debug)]
Expand Down
2 changes: 0 additions & 2 deletions crates/bevy_asset/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::{
};
use bevy_app::{prelude::Events, AppBuilder};
use bevy_ecs::{FromResources, ResMut};
use bevy_type_registry::RegisterType;
use bevy_utils::HashMap;
use crossbeam_channel::Sender;
use std::fmt::Debug;
Expand Down Expand Up @@ -218,7 +217,6 @@ impl AddAsset for AppBuilder {
};

self.add_resource(assets)
.register_component::<Handle<T>>()
.add_system_to_stage(super::stage::ASSET_EVENTS, Assets::<T>::asset_event_system)
.add_system_to_stage(crate::stage::LOAD_ASSETS, update_asset_storage_system::<T>)
.add_event::<AssetEvent<T>>()
Expand Down
22 changes: 11 additions & 11 deletions crates/bevy_asset/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ use std::{
marker::PhantomData,
};

use bevy_property::{Properties, Property};
use crossbeam_channel::{Receiver, Sender};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::{
path::{AssetPath, AssetPathId},
Asset, Assets,
};
use bevy_reflect::{Reflect, ReflectDeserialize};
use bevy_utils::Uuid;
use crossbeam_channel::{Receiver, Sender};
use serde::{Deserialize, Serialize};

/// A unique, stable asset id
#[derive(
Debug, Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd, Serialize, Deserialize, Property,
Debug, Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd, Serialize, Deserialize, Reflect,
)]
#[reflect_value(Serialize, Deserialize, PartialEq, Hash)]
pub enum HandleId {
Id(Uuid, u64),
AssetPathId(AssetPathId),
Expand Down Expand Up @@ -56,15 +56,15 @@ impl HandleId {
/// A handle into a specific Asset of type `T`
///
/// Handles contain a unique id that corresponds to a specific asset in the [Assets](crate::Assets) collection.
#[derive(Properties)]
#[derive(Reflect)]
pub struct Handle<T>
where
T: 'static,
{
pub id: HandleId,
#[property(ignore)]
#[reflect(ignore)]
handle_type: HandleType,
#[property(ignore)]
#[reflect(ignore)]
marker: PhantomData<T>,
}

Expand Down Expand Up @@ -184,7 +184,7 @@ impl<T> From<&Handle<T>> for HandleId {

impl<T> Hash for Handle<T> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.id.hash(state);
Hash::hash(&self.id, state);
}
}

Expand Down Expand Up @@ -311,7 +311,7 @@ impl From<&HandleUntyped> for HandleId {

impl Hash for HandleUntyped {
fn hash<H: Hasher>(&self, state: &mut H) {
self.id.hash(state);
Hash::hash(&self.id, state);
}
}

Expand Down
3 changes: 1 addition & 2 deletions crates/bevy_asset/src/info.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::{path::AssetPath, LabelId};
use bevy_utils::{HashMap, HashSet};
use bevy_utils::{HashMap, HashSet, Uuid};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use uuid::Uuid;

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SourceMeta {
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod path;

pub use asset_server::*;
pub use assets::*;
use bevy_reflect::RegisterTypeBuilder;
use bevy_tasks::IoTaskPool;
pub use handle::*;
pub use info::*;
Expand All @@ -31,7 +32,6 @@ pub mod prelude {
}

use bevy_app::{prelude::Plugin, AppBuilder};
use bevy_type_registry::RegisterType;

/// Adds support for Assets to an App. Assets are typed collections with change tracking, which are added as App Resources.
/// Examples of assets: textures, sounds, 3d models, maps, scenes
Expand Down Expand Up @@ -76,7 +76,7 @@ impl Plugin for AssetPlugin {
app.add_stage_before(bevy_app::stage::PRE_UPDATE, stage::LOAD_ASSETS)
.add_stage_after(bevy_app::stage::POST_UPDATE, stage::ASSET_EVENTS)
.add_resource(asset_server)
.register_property::<HandleId>()
.register_type::<HandleId>()
.add_system_to_stage(
bevy_app::stage::PRE_UPDATE,
asset_server::free_unused_assets_system,
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_asset/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
};
use anyhow::Result;
use bevy_ecs::{Res, ResMut, Resource};
use bevy_type_registry::{TypeUuid, TypeUuidDynamic};
use bevy_reflect::{TypeUuid, TypeUuidDynamic};
use bevy_utils::{BoxedFuture, HashMap};
use crossbeam_channel::{Receiver, Sender};
use downcast_rs::{impl_downcast, Downcast};
Expand Down
11 changes: 7 additions & 4 deletions crates/bevy_asset/src/path.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bevy_property::Property;
use bevy_reflect::{Reflect, ReflectDeserialize};
use bevy_utils::AHasher;
use serde::{Deserialize, Serialize};
use std::{
Expand Down Expand Up @@ -58,18 +58,21 @@ impl<'a> AssetPath<'a> {
}

#[derive(
Debug, Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd, Serialize, Deserialize, Property,
Debug, Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd, Serialize, Deserialize, Reflect,
)]
#[reflect_value(PartialEq, Hash, Serialize, Deserialize)]
pub struct AssetPathId(SourcePathId, LabelId);

#[derive(
Debug, Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd, Serialize, Deserialize, Property,
Debug, Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd, Serialize, Deserialize, Reflect,
)]
#[reflect_value(PartialEq, Hash, Serialize, Deserialize)]
pub struct SourcePathId(u64);

#[derive(
Debug, Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd, Serialize, Deserialize, Property,
Debug, Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd, Serialize, Deserialize, Reflect,
)]
#[reflect_value(PartialEq, Hash, Serialize, Deserialize)]
pub struct LabelId(u64);

impl<'a> From<&'a Path> for SourcePathId {
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_audio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ keywords = ["bevy"]
bevy_app = { path = "../bevy_app", version = "0.3.0" }
bevy_asset = { path = "../bevy_asset", version = "0.3.0" }
bevy_ecs = { path = "../bevy_ecs", version = "0.3.0" }
bevy_type_registry = { path = "../bevy_type_registry", version = "0.3.0" }
bevy_reflect = { path = "../bevy_reflect", version = "0.3.0", features = ["bevy"] }
bevy_utils = { path = "../bevy_utils", version = "0.3.0" }

# other
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_audio/src/audio_source.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;
use bevy_asset::{AssetLoader, LoadContext, LoadedAsset};
use bevy_type_registry::TypeUuid;
use bevy_reflect::TypeUuid;
use bevy_utils::BoxedFuture;
use std::{io::Cursor, sync::Arc};

Expand Down
5 changes: 2 additions & 3 deletions crates/bevy_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ keywords = ["bevy"]
bevy_app = { path = "../bevy_app", version = "0.3.0" }
bevy_derive = { path = "../bevy_derive", version = "0.3.0" }
bevy_ecs = { path = "../bevy_ecs", version = "0.3.0" }
bevy_property = { path = "../bevy_property", version = "0.3.0" }
bevy_type_registry = { path = "../bevy_type_registry", version = "0.3.0" }
bevy_math = { path = "../bevy_math", version = "0.3.0" }
bevy_utils = { path = "../bevy_utils", version = "0.3.0" }
bevy_reflect = { path = "../bevy_reflect", version = "0.3.0", features = ["bevy"] }
bevy_tasks = { path = "../bevy_tasks", version = "0.3.0" }
bevy_utils = { path = "../bevy_utils", version = "0.3.0" }
4 changes: 2 additions & 2 deletions crates/bevy_core/src/label.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bevy_ecs::prelude::*;
use bevy_property::Properties;
use bevy_reflect::Reflect;
use bevy_utils::{HashMap, HashSet};
use std::{
borrow::Cow,
Expand All @@ -8,7 +8,7 @@ use std::{
};

/// A collection of labels
#[derive(Default, Properties)]
#[derive(Default, Reflect)]
pub struct Labels {
labels: HashSet<Cow<'static, str>>,
}
Expand Down
15 changes: 6 additions & 9 deletions crates/bevy_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ mod label;
mod task_pool_options;
mod time;

use std::ops::Range;

use bevy_reflect::RegisterTypeBuilder;
pub use bytes::*;
pub use float_ord::*;
pub use label::*;
Expand All @@ -15,8 +18,6 @@ pub mod prelude {
}

use bevy_app::prelude::*;
use bevy_math::{Mat3, Mat4, Quat, Vec2, Vec3};
use bevy_type_registry::RegisterType;

/// Adds core functionality to Apps.
#[derive(Default)]
Expand All @@ -32,13 +33,9 @@ impl Plugin for CorePlugin {

app.init_resource::<Time>()
.init_resource::<EntityLabels>()
.register_component::<Timer>()
.register_property::<Vec2>()
.register_property::<Vec3>()
.register_property::<Mat3>()
.register_property::<Mat4>()
.register_property::<Quat>()
.register_property::<Option<String>>()
.register_type::<Option<String>>()
.register_type::<Range<f32>>()
.register_type::<Timer>()
.add_system_to_stage(stage::FIRST, time_system)
.add_system_to_stage(stage::PRE_UPDATE, entity_labels_system);
}
Expand Down
5 changes: 3 additions & 2 deletions crates/bevy_core/src/time/timer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bevy_property::Properties;
use bevy_reflect::{Reflect, ReflectComponent};
use bevy_utils::Duration;

/// Tracks elapsed time. Enters the finished state once `duration` is reached.
Expand All @@ -7,7 +7,8 @@ use bevy_utils::Duration;
/// Repeating timers will only be in the finished state on each tick `duration` is reached or exceeded, and can still be reset at any given point.
///
/// Paused timers will not have elapsed time increased.
#[derive(Clone, Debug, Default, Properties)]
#[derive(Clone, Debug, Default, Reflect)]
#[reflect(Component)]
pub struct Timer {
elapsed: f32,
duration: f32,
Expand Down
1 change: 0 additions & 1 deletion crates/bevy_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,3 @@ find-crate = "0.6"
proc-macro2 = "1.0"
quote = "1.0"
syn = "1.0"
uuid = { version = "0.8", features = ["v4", "serde"] }
Loading

0 comments on commit 72b2fc9

Please sign in to comment.