From 15807a3dc13d41e11a2b1c3c4b08bd75a51d78d1 Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Sat, 27 Apr 2024 00:28:49 -0400 Subject: [PATCH] resource: impl Reflect for a bunch of types, add some utility methods to handles --- lyra-resource/src/gltf/material.rs | 6 ++-- lyra-resource/src/gltf/mesh.rs | 4 +-- lyra-resource/src/gltf/mod.rs | 7 +++-- lyra-resource/src/resource.rs | 49 ++++++++++++++++++++++++------ lyra-resource/src/texture.rs | 2 +- 5 files changed, 50 insertions(+), 18 deletions(-) diff --git a/lyra-resource/src/gltf/material.rs b/lyra-resource/src/gltf/material.rs index 96d69a5..3a24d9d 100644 --- a/lyra-resource/src/gltf/material.rs +++ b/lyra-resource/src/gltf/material.rs @@ -31,7 +31,7 @@ impl From> for PbrRoughness { } } -#[derive(Clone, Debug, Default)] +#[derive(Clone, Debug, Default, Reflect)] pub struct PbrGlossiness { /// The rgba diffuse color of the material pub diffuse_color: glam::Vec4, @@ -95,7 +95,7 @@ impl From for AlphaMode { } } -#[derive(Clone)] +#[derive(Clone, Reflect)] pub struct Specular { /// The strength of the specular reflection, default of 1.0 pub factor: f32, @@ -131,7 +131,7 @@ impl Specular { } } -#[derive(Clone, Default)] +#[derive(Clone, Default, Reflect)] pub struct Material { pub shader_uuid: Option, pub name: Option, diff --git a/lyra-resource/src/gltf/mesh.rs b/lyra-resource/src/gltf/mesh.rs index f4552f7..a23affe 100644 --- a/lyra-resource/src/gltf/mesh.rs +++ b/lyra-resource/src/gltf/mesh.rs @@ -48,7 +48,7 @@ impl From> for MeshIndices { } #[repr(C)] -#[derive(Clone, Debug, PartialEq)] +#[derive(Clone, Debug, PartialEq, Reflect)] pub enum VertexAttributeData { Vec2(Vec), Vec3(Vec), @@ -87,7 +87,7 @@ pub enum MeshVertexAttribute { Other(String), } -#[derive(Clone, Default, lyra_ecs::Component)] +#[derive(Clone, Default, lyra_ecs::Component, Reflect)] pub struct Mesh { pub attributes: HashMap, pub indices: Option, diff --git a/lyra-resource/src/gltf/mod.rs b/lyra-resource/src/gltf/mod.rs index 84910c4..8a40454 100644 --- a/lyra-resource/src/gltf/mod.rs +++ b/lyra-resource/src/gltf/mod.rs @@ -2,6 +2,7 @@ pub mod loader; pub use loader::*; pub mod material; +use lyra_reflect::Reflect; use lyra_scene::SceneGraph; use crate::ResourceData; pub use material::*; @@ -14,8 +15,10 @@ pub use scene::*; use crate::ResHandle; +use crate::lyra_engine; + /// A loaded Gltf file -#[derive(Clone, Default)] +#[derive(Clone, Default, Reflect)] pub struct Gltf { pub scenes: Vec>, pub materials: Vec>, @@ -48,6 +51,4 @@ impl ResourceData for Gltf { fn as_any_mut(&mut self) -> &mut dyn std::any::Any { self } - - } \ No newline at end of file diff --git a/lyra-resource/src/resource.rs b/lyra-resource/src/resource.rs index f20a8ac..5e16d01 100644 --- a/lyra-resource/src/resource.rs +++ b/lyra-resource/src/resource.rs @@ -1,6 +1,7 @@ use std::{any::{Any, TypeId}, marker::PhantomData, ops::{Deref, DerefMut}, sync::{Arc, Condvar, Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard}}; use lyra_ecs::Component; +use lyra_reflect::Reflect; use crate::{loader::LoaderError, lyra_engine, DependencyState}; use uuid::Uuid; @@ -90,16 +91,16 @@ pub struct UntypedResource { pub(crate) version: usize, pub(crate) state: ResourceState, uuid: Uuid, - path: Option, + pub(crate) path: Option, pub(crate) is_watched: bool, /// can be used to wait for the resource to load. pub(crate) condvar: Arc<(Mutex, Condvar)>, } -#[derive(Clone, Component)] +#[derive(Clone, Component, Reflect)] pub struct UntypedResHandle{ + #[reflect(skip)] pub(crate) res: Arc>, - #[allow(dead_code)] tyid: TypeId, } @@ -131,6 +132,15 @@ impl UntypedResHandle { matches!(d.state, ResourceState::Ready(_)) } + pub fn get_error(&self) -> Option> { + let d = self.read(); + + match &d.state { + ResourceState::Error(e) => Some(e.clone()), + _ => None, + } + } + /// Returns the uuid of the resource. pub fn uuid(&self) -> Uuid { let d = self.read(); @@ -199,11 +209,28 @@ impl UntypedResHandle { DependencyState::from_res_recurse(self) } - pub fn as_typed(&self) -> ResHandle { - ResHandle { - handle: self.clone(), - _marker: PhantomData::, - } + /// Retrieve a typed handle to the resource. + /// + /// Returns `None` if the types do not match + pub fn as_typed(&self) -> Option> { + self.clone().into_typed() + } + + /// Convert `self` into a typed handle. + /// + /// Returns `None` if the types do not match + pub fn into_typed(self) -> Option> { + if self.tyid == TypeId::of::() { + Some(ResHandle { + handle: self, + _marker: PhantomData::, + }) + } else { None } + } + + /// Retrieve the type id of the resource in the handle. + pub fn resource_type_id(&self) -> TypeId { + self.tyid } } @@ -213,7 +240,7 @@ impl UntypedResHandle { /// This struct has an inner [`RwLock`] to the resource data, so most methods may be blocking. /// However, the only times it will be blocking is if another thread is reloading the resource /// and has a write lock on the data. This means that most of the time, it is not blocking. -#[derive(Component)] +#[derive(Component, Reflect)] pub struct ResHandle { pub(crate) handle: UntypedResHandle, _marker: PhantomData, @@ -331,6 +358,10 @@ impl ResourceStorage for ResHandle { let mut d = self.handle.write(); d.state = new; } + + fn clone_untyped(&self) -> UntypedResHandle { + self.handle.clone() + } } #[cfg(test)] diff --git a/lyra-resource/src/texture.rs b/lyra-resource/src/texture.rs index 6663c56..cd58a4f 100644 --- a/lyra-resource/src/texture.rs +++ b/lyra-resource/src/texture.rs @@ -75,7 +75,7 @@ impl From for Image { } } -#[derive(Clone)] +#[derive(Clone, Reflect)] pub struct Texture { pub image: ResHandle, pub sampler: Option,