diff --git a/fyrox-core/src/dyntype.rs b/fyrox-core/src/dyntype.rs index ca98b4920..d93263336 100644 --- a/fyrox-core/src/dyntype.rs +++ b/fyrox-core/src/dyntype.rs @@ -30,6 +30,7 @@ use parking_lot::{Mutex, MutexGuard}; use std::{ any::{type_name, Any, TypeId}, fmt::{Debug, Display, Formatter}, + ops::{Deref, DerefMut}, }; use uuid::Uuid; @@ -145,9 +146,9 @@ impl dyn DynType { } /// A container for a boxed dyntype. -#[derive(Debug, TypeUuidProvider)] +#[derive(Debug, TypeUuidProvider, Reflect)] #[type_uuid(id = "87d9ef74-09a9-4228-a2d1-df270b50fddb")] -pub struct DynTypeWrapper(pub Box); +pub struct DynTypeWrapper(#[reflect(deref, display_name = "Data")] pub Box); impl Clone for DynTypeWrapper { fn clone(&self) -> Self { @@ -155,84 +156,6 @@ impl Clone for DynTypeWrapper { } } -static CONTENT_METADATA: FieldMetadata = FieldMetadata { - name: "Content", - display_name: "Content", - tag: "", - read_only: false, - immutable_collection: false, - min_value: None, - max_value: None, - step: None, - precision: None, - doc: "", -}; - -impl Reflect for DynTypeWrapper { - fn type_info() -> TypeInfo { - TypeInfo { - source_path: file!(), - type_name: type_name::(), - assembly_name: env!("CARGO_PKG_NAME"), - doc_comment: "", - derived_types: &[], - } - } - - fn type_info_ref(&self) -> TypeInfo { - Self::type_info() - } - - fn fields_ref(&self, func: &mut dyn FnMut(&[FieldRef])) { - func(&[{ - FieldRef { - metadata: &CONTENT_METADATA, - value: &*self.0, - } - }]) - } - - fn fields_mut(&mut self, func: &mut dyn FnMut(&mut [FieldMut])) { - func(&mut [{ - FieldMut { - metadata: &CONTENT_METADATA, - value: &mut *self.0, - } - }]) - } - - fn set(&mut self, value: Box) -> Result, Box> { - let this = std::mem::replace(self, value.take()?); - Ok(Box::new(this)) - } - - fn try_clone_box(&self) -> Option> { - Some(Box::new(self.clone())) - } - - fn field_direct_ref(&self, index: usize) -> Option { - if index == 0 { - Some(FieldRef { - metadata: &CONTENT_METADATA, - value: &*self.0, - }) - } else { - None - } - } - - fn field_direct_mut(&mut self, index: usize) -> Option { - if index == 0 { - Some(FieldMut { - metadata: &CONTENT_METADATA, - value: &mut *self.0, - }) - } else { - None - } - } -} - /// "Nullable" container for a dyntype. This container is essentially a wrapper for [`Option`] that /// supports additional functionality and handles serialization for you. #[derive(Default, Reflect, Clone, Debug)] diff --git a/fyrox-graph/src/lib.rs b/fyrox-graph/src/lib.rs index 96da653d3..ebdb23d6d 100644 --- a/fyrox-graph/src/lib.rs +++ b/fyrox-graph/src/lib.rs @@ -1677,8 +1677,8 @@ mod test { } } - #[derive(Debug)] - pub struct Node(Box); + #[derive(Debug, Reflect)] + pub struct Node(#[reflect(deref, display_name = "Node")] Box); impl Clone for Node { fn clone(&self) -> Self { @@ -1698,67 +1698,6 @@ mod test { } } - impl Reflect for Node { - fn type_info() -> TypeInfo { - TypeInfo { - source_path: file!(), - type_name: type_name::(), - assembly_name: env!("CARGO_PKG_NAME"), - doc_comment: "", - derived_types: &[], - } - } - - fn type_info_ref(&self) -> TypeInfo { - Self::type_info() - } - - fn fields_ref(&self, func: &mut dyn FnMut(&[FieldRef])) { - self.0.deref().fields_ref(func) - } - - fn fields_mut(&mut self, func: &mut dyn FnMut(&mut [FieldMut])) { - self.0.deref_mut().fields_mut(func) - } - - fn set(&mut self, value: Box) -> Result, Box> { - self.0.deref_mut().set(value) - } - - fn set_field( - &mut self, - field: &str, - value: Box, - func: &mut dyn FnMut(Result, SetFieldError>), - ) { - self.0.deref_mut().set_field(field, value, func) - } - - fn find_field(&self, name: &str, func: &mut dyn FnMut(Option<&dyn Reflect>)) { - self.0.deref().find_field(name, func) - } - - fn find_field_mut(&mut self, name: &str, func: &mut dyn FnMut(Option<&mut dyn Reflect>)) { - self.0.deref_mut().find_field_mut(name, func) - } - - fn try_clone_box(&self) -> Option> { - Some(Box::new(self.clone())) - } - - fn field_direct_ref(&self, index: usize) -> Option> { - self.0.deref().field_direct_ref(index) - } - - fn field_direct_mut(&mut self, index: usize) -> Option> { - self.0.deref_mut().field_direct_mut(index) - } - - fn fields_count(&self) -> usize { - self.0.fields_count() - } - } - impl NameProvider for Node { fn name(&self) -> &str { &self.name diff --git a/fyrox-resource/src/state.rs b/fyrox-resource/src/state.rs index 3913b4935..8eb2343bb 100644 --- a/fyrox-resource/src/state.rs +++ b/fyrox-resource/src/state.rs @@ -21,7 +21,7 @@ //! A module that handles resource states. use crate::{core::reflect::prelude::*, ResourceData, ResourceLoadError, TypedResourceData}; -use std::any::{type_name, Any}; +use std::any::Any; use std::fmt::{Debug, Display}; use std::path::PathBuf; use std::{ @@ -82,87 +82,11 @@ impl Display for LoadError { } } -static CONTENT_METADATA: FieldMetadata = FieldMetadata { - name: "Content", - display_name: "Content", - tag: "", - read_only: false, - immutable_collection: false, - min_value: None, - max_value: None, - step: None, - precision: None, - doc: "", -}; - #[doc(hidden)] -#[derive(Debug)] -pub struct ResourceDataWrapper(pub Box); - -impl Reflect for ResourceDataWrapper { - fn type_info() -> TypeInfo { - TypeInfo { - source_path: file!(), - type_name: type_name::(), - assembly_name: env!("CARGO_PKG_NAME"), - doc_comment: "", - derived_types: &[], - } - } - - fn type_info_ref(&self) -> TypeInfo { - Self::type_info() - } - - fn try_clone_box(&self) -> Option> { - Some(Box::new(self.clone())) - } - - fn fields_ref(&self, func: &mut dyn FnMut(&[FieldRef])) { - func(&[{ - FieldRef { - metadata: &CONTENT_METADATA, - value: &*self.0, - } - }]) - } - - fn fields_mut(&mut self, func: &mut dyn FnMut(&mut [FieldMut])) { - func(&mut [{ - FieldMut { - metadata: &CONTENT_METADATA, - value: &mut *self.0, - } - }]) - } - - fn set(&mut self, value: Box) -> Result, Box> { - let this = std::mem::replace(self, value.take()?); - Ok(Box::new(this)) - } - - fn field_direct_ref(&self, index: usize) -> Option> { - if index == 0 { - Some(FieldRef { - metadata: &CONTENT_METADATA, - value: &*self.0, - }) - } else { - None - } - } - - fn field_direct_mut(&mut self, index: usize) -> Option> { - if index == 0 { - Some(FieldMut { - metadata: &CONTENT_METADATA, - value: &mut *self.0, - }) - } else { - None - } - } -} +#[derive(Debug, Reflect)] +pub struct ResourceDataWrapper( + #[reflect(deref, display_name = "Resource Data")] pub Box, +); impl Deref for ResourceDataWrapper { type Target = dyn ResourceData; diff --git a/fyrox-ui/src/node/mod.rs b/fyrox-ui/src/node/mod.rs index a12f61033..d6dd9702d 100644 --- a/fyrox-ui/src/node/mod.rs +++ b/fyrox-ui/src/node/mod.rs @@ -31,7 +31,6 @@ use crate::{ use fyrox_graph::SceneGraphNode; use fyrox_resource::{untyped::UntypedResource, Resource}; -use std::any::type_name; use std::{ any::TypeId, fmt::{Debug, Formatter}, @@ -47,7 +46,8 @@ pub mod container; /// `Box` everywhere, just `UiNode`) and to provide some useful methods such as type /// casting, field fetching, etc. You could also be interested in [`Control`] docs, since it /// contains all the interesting stuff and detailed description for each method. -pub struct UiNode(pub Box); +#[derive(Reflect)] +pub struct UiNode(#[reflect(deref, display_name = "UiNode")] pub Box); impl From for UiNode { fn from(value: T) -> Self { @@ -270,60 +270,3 @@ impl Visit for UiNode { self.0.visit(name, visitor) } } - -impl Reflect for UiNode { - fn type_info() -> TypeInfo { - TypeInfo { - source_path: file!(), - type_name: type_name::(), - assembly_name: env!("CARGO_PKG_NAME"), - doc_comment: "", - derived_types: &[], - } - } - - fn type_info_ref(&self) -> TypeInfo { - Self::type_info() - } - - fn try_clone_box(&self) -> Option> { - Some(Box::new(self.clone())) - } - - fn fields_ref(&self, func: &mut dyn FnMut(&[FieldRef])) { - self.0.deref().fields_ref(func) - } - - fn fields_mut(&mut self, func: &mut dyn FnMut(&mut [FieldMut])) { - self.0.deref_mut().fields_mut(func) - } - - fn set(&mut self, value: Box) -> Result, Box> { - self.0.deref_mut().set(value) - } - - fn field_direct_ref(&self, index: usize) -> Option { - self.0.deref().field_direct_ref(index) - } - - fn field_direct_mut(&mut self, index: usize) -> Option { - self.0.deref_mut().field_direct_mut(index) - } - - fn set_field( - &mut self, - field: &str, - value: Box, - func: &mut dyn FnMut(Result, SetFieldError>), - ) { - self.0.deref_mut().set_field(field, value, func) - } - - fn find_field(&self, name: &str, func: &mut dyn FnMut(Option<&dyn Reflect>)) { - self.0.deref().find_field(name, func) - } - - fn find_field_mut(&mut self, name: &str, func: &mut dyn FnMut(Option<&mut dyn Reflect>)) { - self.0.deref_mut().find_field_mut(name, func) - } -}