2021-05-19 19:03:36 +00:00
|
|
|
use crate as bevy_reflect;
|
2020-11-28 00:39:59 +00:00
|
|
|
use crate::ReflectDeserialize;
|
Add FromReflect trait to convert dynamic types to concrete types (#1395)
Dynamic types (`DynamicStruct`, `DynamicTupleStruct`, `DynamicTuple`, `DynamicList` and `DynamicMap`) are used when deserializing scenes, but currently they can only be applied to existing concrete types. This leads to issues when trying to spawn non trivial deserialized scene.
For components, the issue is avoided by requiring that reflected components implement ~~`FromResources`~~ `FromWorld` (or `Default`). When spawning, a new concrete type is created that way, and the dynamic type is applied to it. Unfortunately, some components don't have any valid implementation of these traits.
In addition, any `Vec` or `HashMap` inside a component will panic when a dynamic type is pushed into it (for instance, `Text` panics when adding a text section).
To solve this issue, this PR adds the `FromReflect` trait that creates a concrete type from a dynamic type that represent it, derives the trait alongside the `Reflect` trait, drops the ~~`FromResources`~~ `FromWorld` requirement on reflected components, ~~and enables reflection for UI and Text bundles~~. It also adds the requirement that fields ignored with `#[reflect(ignore)]` implement `Default`, since we need to initialize them somehow.
Co-authored-by: Carter Anderson <mcanders1@gmail.com>
2021-12-26 18:49:01 +00:00
|
|
|
use bevy_reflect_derive::{impl_from_reflect_value, impl_reflect_value};
|
2022-02-24 08:12:27 +00:00
|
|
|
use glam::*;
|
2020-11-28 00:39:59 +00:00
|
|
|
|
2021-02-19 22:25:07 +00:00
|
|
|
impl_reflect_value!(IVec2(PartialEq, Serialize, Deserialize));
|
|
|
|
impl_reflect_value!(IVec3(PartialEq, Serialize, Deserialize));
|
|
|
|
impl_reflect_value!(IVec4(PartialEq, Serialize, Deserialize));
|
|
|
|
impl_reflect_value!(UVec2(PartialEq, Serialize, Deserialize));
|
|
|
|
impl_reflect_value!(UVec3(PartialEq, Serialize, Deserialize));
|
|
|
|
impl_reflect_value!(UVec4(PartialEq, Serialize, Deserialize));
|
2020-11-28 00:39:59 +00:00
|
|
|
impl_reflect_value!(Vec2(PartialEq, Serialize, Deserialize));
|
|
|
|
impl_reflect_value!(Vec3(PartialEq, Serialize, Deserialize));
|
2022-02-24 08:12:27 +00:00
|
|
|
impl_reflect_value!(Vec3A(PartialEq, Serialize, Deserialize));
|
2020-11-28 00:39:59 +00:00
|
|
|
impl_reflect_value!(Vec4(PartialEq, Serialize, Deserialize));
|
2022-02-24 08:12:27 +00:00
|
|
|
impl_reflect_value!(DVec2(PartialEq, Serialize, Deserialize));
|
|
|
|
impl_reflect_value!(DVec3(PartialEq, Serialize, Deserialize));
|
|
|
|
impl_reflect_value!(DVec4(PartialEq, Serialize, Deserialize));
|
2020-11-28 00:39:59 +00:00
|
|
|
impl_reflect_value!(Mat3(PartialEq, Serialize, Deserialize));
|
|
|
|
impl_reflect_value!(Mat4(PartialEq, Serialize, Deserialize));
|
|
|
|
impl_reflect_value!(Quat(PartialEq, Serialize, Deserialize));
|
Add FromReflect trait to convert dynamic types to concrete types (#1395)
Dynamic types (`DynamicStruct`, `DynamicTupleStruct`, `DynamicTuple`, `DynamicList` and `DynamicMap`) are used when deserializing scenes, but currently they can only be applied to existing concrete types. This leads to issues when trying to spawn non trivial deserialized scene.
For components, the issue is avoided by requiring that reflected components implement ~~`FromResources`~~ `FromWorld` (or `Default`). When spawning, a new concrete type is created that way, and the dynamic type is applied to it. Unfortunately, some components don't have any valid implementation of these traits.
In addition, any `Vec` or `HashMap` inside a component will panic when a dynamic type is pushed into it (for instance, `Text` panics when adding a text section).
To solve this issue, this PR adds the `FromReflect` trait that creates a concrete type from a dynamic type that represent it, derives the trait alongside the `Reflect` trait, drops the ~~`FromResources`~~ `FromWorld` requirement on reflected components, ~~and enables reflection for UI and Text bundles~~. It also adds the requirement that fields ignored with `#[reflect(ignore)]` implement `Default`, since we need to initialize them somehow.
Co-authored-by: Carter Anderson <mcanders1@gmail.com>
2021-12-26 18:49:01 +00:00
|
|
|
|
|
|
|
impl_from_reflect_value!(IVec2);
|
|
|
|
impl_from_reflect_value!(IVec3);
|
|
|
|
impl_from_reflect_value!(IVec4);
|
|
|
|
impl_from_reflect_value!(UVec2);
|
|
|
|
impl_from_reflect_value!(UVec3);
|
|
|
|
impl_from_reflect_value!(UVec4);
|
|
|
|
impl_from_reflect_value!(Vec2);
|
|
|
|
impl_from_reflect_value!(Vec3);
|
|
|
|
impl_from_reflect_value!(Vec4);
|
2022-02-24 08:12:27 +00:00
|
|
|
impl_from_reflect_value!(Vec3A);
|
|
|
|
impl_from_reflect_value!(DVec2);
|
|
|
|
impl_from_reflect_value!(DVec3);
|
|
|
|
impl_from_reflect_value!(DVec4);
|
Add FromReflect trait to convert dynamic types to concrete types (#1395)
Dynamic types (`DynamicStruct`, `DynamicTupleStruct`, `DynamicTuple`, `DynamicList` and `DynamicMap`) are used when deserializing scenes, but currently they can only be applied to existing concrete types. This leads to issues when trying to spawn non trivial deserialized scene.
For components, the issue is avoided by requiring that reflected components implement ~~`FromResources`~~ `FromWorld` (or `Default`). When spawning, a new concrete type is created that way, and the dynamic type is applied to it. Unfortunately, some components don't have any valid implementation of these traits.
In addition, any `Vec` or `HashMap` inside a component will panic when a dynamic type is pushed into it (for instance, `Text` panics when adding a text section).
To solve this issue, this PR adds the `FromReflect` trait that creates a concrete type from a dynamic type that represent it, derives the trait alongside the `Reflect` trait, drops the ~~`FromResources`~~ `FromWorld` requirement on reflected components, ~~and enables reflection for UI and Text bundles~~. It also adds the requirement that fields ignored with `#[reflect(ignore)]` implement `Default`, since we need to initialize them somehow.
Co-authored-by: Carter Anderson <mcanders1@gmail.com>
2021-12-26 18:49:01 +00:00
|
|
|
impl_from_reflect_value!(Mat3);
|
|
|
|
impl_from_reflect_value!(Mat4);
|
|
|
|
impl_from_reflect_value!(Quat);
|