diff --git a/src/animation.rs b/src/animation.rs index 56a6ee7..44a37c9 100644 --- a/src/animation.rs +++ b/src/animation.rs @@ -1,11 +1,14 @@ use std::fmt; +use bevy::reflect::prelude::*; + use crate::{clip::ClipId, easing::Easing}; /// An opaque identifier that references an [Animation]. /// /// Returned by [AnimationLibrary::register_animation](crate::prelude::AnimationLibrary::register_animation). -#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] +#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Reflect)] +#[reflect(Debug, PartialEq, Hash)] pub struct AnimationId { pub(crate) value: usize, } @@ -19,7 +22,8 @@ impl fmt::Display for AnimationId { /// Specifies the duration of an [Animation]. /// /// Defaults to `PerFrame(100)`. -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone, Copy, Reflect)] +#[reflect(Debug)] pub enum AnimationDuration { /// Specifies the duration of each frame in milliseconds PerFrame(u32), @@ -36,7 +40,8 @@ impl Default for AnimationDuration { /// Specifies how many times an [Animation] repeats. /// /// Defaults to `AnimationRepeat::Loop`. -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Reflect)] +#[reflect(Debug, PartialEq, Hash)] pub enum AnimationRepeat { /// Loops indefinitely Loop, @@ -53,7 +58,8 @@ impl Default for AnimationRepeat { /// Specifies the direction of an [Animation]. /// /// Defaults to `AnimationDirection::Forwards`. -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Reflect)] +#[reflect(Debug, PartialEq, Hash)] pub enum AnimationDirection { /// Frames play from left to right Forwards, @@ -98,7 +104,8 @@ impl Default for AnimationDirection { /// /// let animation_id = library.register_animation(animation); /// ``` -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Reflect)] +#[reflect(Debug)] pub struct Animation { /// The IDs of the [Clip](crate::prelude::Clip)s that compose this animation clip_ids: Vec, diff --git a/src/animator.rs b/src/animator.rs index d7aa33c..3613885 100644 --- a/src/animator.rs +++ b/src/animator.rs @@ -5,8 +5,10 @@ use bevy::{ ecs::{ entity::Entity, event::EventWriter, + reflect::*, system::{Query, Resource}, }, + reflect::prelude::*, sprite::TextureAtlas, time::Time, }; @@ -22,6 +24,8 @@ use crate::{ use self::{iterator::AnimationIterator, iterator::IteratorFrame}; +#[derive(Debug, Reflect)] +#[reflect(Debug)] /// An instance of an animation that is currently being played struct AnimationInstance { animation_id: AnimationId, @@ -35,7 +39,8 @@ struct AnimationInstance { } /// The animator is responsible for playing animations as time advances. -#[derive(Resource, Default)] +#[derive(Resource, Debug, Default, Reflect)] +#[reflect(Resource, Debug, Default)] pub struct Animator { /// Instances of animations currently being played. /// Each animation instance is associated to an entity with a [SpritesheetAnimation] component. diff --git a/src/animator/cache.rs b/src/animator/cache.rs index 81b3b8e..00dc165 100644 --- a/src/animator/cache.rs +++ b/src/animator/cache.rs @@ -1,4 +1,4 @@ -use bevy::log::warn; +use bevy::{log::warn, reflect::prelude::*}; use crate::{ animation::{AnimationDirection, AnimationDuration, AnimationId, AnimationRepeat}, @@ -10,7 +10,8 @@ use crate::{ }; /// A pre-computed frame of animation, ready to be played back. -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Reflect)] +#[reflect(Debug)] pub struct CacheFrame { pub atlas_index: usize, pub duration: u32, @@ -27,7 +28,8 @@ pub struct CacheFrame { /// The iterator & animator will also generate extra events that cannot be cached: /// - ClipRepetitionEnd, ClipEnd, AnimationRepetitionEnd on the first frame of the repetitions /// - AnimationEnd after the last frame -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, Eq, Hash, Reflect)] +#[reflect(Debug, PartialEq, Hash)] pub enum AnimationCacheEvent { MarkerHit { marker_id: AnimationMarkerId, @@ -43,6 +45,8 @@ pub enum AnimationCacheEvent { }, } +#[derive(Debug, Reflect)] +#[reflect(Debug)] /// The [AnimationCache] contains pre-computed frames for an animation. /// /// The idea is to cache for each frame its atlas index, duration and emitted events diff --git a/src/animator/iterator.rs b/src/animator/iterator.rs index c0267de..299c60c 100644 --- a/src/animator/iterator.rs +++ b/src/animator/iterator.rs @@ -1,6 +1,6 @@ use std::sync::Arc; -use bevy::log::warn; +use bevy::{log::warn, reflect::prelude::*}; use crate::{ animation::AnimationDirection, clip::ClipId, @@ -10,7 +10,8 @@ use crate::{ use super::cache::{AnimationCache, AnimationCacheEvent, CacheFrame}; /// Same as [CacheFrame] but with `animation_repetition` -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Reflect)] +#[reflect(Debug)] pub struct IteratorFrame { pub atlas_index: usize, pub duration: u32, @@ -23,7 +24,8 @@ pub struct IteratorFrame { /// A partial version of AnimationEvent. /// /// The animation will promote them to regular AnimationEvents and add the information available at its level. -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, Eq, Hash, Reflect)] +#[reflect(Debug, PartialEq, Hash)] pub enum AnimationIteratorEvent { MarkerHit { marker_id: AnimationMarkerId, @@ -43,6 +45,8 @@ pub enum AnimationIteratorEvent { }, } +#[derive(Debug, Reflect)] +#[reflect(Debug)] /// An iterator that advances an animation frame by frame. /// /// `next()` will produce frames until the end of the animation. diff --git a/src/clip.rs b/src/clip.rs index e509d1c..014258d 100644 --- a/src/clip.rs +++ b/src/clip.rs @@ -1,5 +1,7 @@ use std::{collections::HashMap, fmt}; +use bevy::reflect::prelude::*; + use crate::{ animation::{AnimationDirection, AnimationDuration}, easing::Easing, @@ -9,7 +11,8 @@ use crate::{ /// An opaque identifier that references a [Clip]. /// /// Returned by [AnimationLibrary::register_clip](crate::prelude::AnimationLibrary::register_clip). -#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] +#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Reflect)] +#[reflect(Debug, PartialEq, Hash)] pub struct ClipId { pub(crate) value: usize, } @@ -72,7 +75,8 @@ impl fmt::Display for ClipId { /// /// let composite_animation = Animation::from_clips([slow_clip_id, fast_clip_id]); /// ``` -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Reflect)] +#[reflect(Debug)] pub struct Clip { /// Indices into the layout of a TextureAtlas component atlas_indices: Vec, diff --git a/src/components/sprite3d.rs b/src/components/sprite3d.rs index ec0e6df..6428f2e 100644 --- a/src/components/sprite3d.rs +++ b/src/components/sprite3d.rs @@ -1,8 +1,9 @@ use bevy::{ asset::Handle, color::Color, - ecs::{bundle::Bundle, component::Component}, + ecs::prelude::*, math::{Rect, Vec2}, + reflect::prelude::*, render::{ texture::Image, view::{InheritedVisibility, ViewVisibility, Visibility}, @@ -16,7 +17,8 @@ use bevy::{ /// This contains similar fields as Bevy's [Sprite](bevy::sprite::Sprite). /// /// This is commonly used as a component within [Sprite3dBundle]. -#[derive(Component)] +#[derive(Component, Debug, Reflect)] +#[reflect(Component, Debug)] pub struct Sprite3d { /// A color to tint the sprite with. /// diff --git a/src/components/spritesheet_animation.rs b/src/components/spritesheet_animation.rs index 5262ef9..eb9cb8f 100644 --- a/src/components/spritesheet_animation.rs +++ b/src/components/spritesheet_animation.rs @@ -1,9 +1,10 @@ -use bevy::ecs::component::Component; +use bevy::{ecs::prelude::*, reflect::prelude::*}; use crate::animation::AnimationId; // The progress of an animation being played. -#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)] +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, Reflect)] +#[reflect(Debug, Default, PartialEq, Hash)] pub struct AnimationProgress { /// The index of the active frame of the animation /// @@ -61,7 +62,8 @@ pub struct AnimationProgress { /// )); /// } /// ``` -#[derive(Component, Debug, Clone)] +#[derive(Component, Debug, Clone, Reflect)] +#[reflect(Component, Debug)] pub struct SpritesheetAnimation { /// The ID of the animation to play /// diff --git a/src/easing.rs b/src/easing.rs index 054b2df..a5f2d46 100644 --- a/src/easing.rs +++ b/src/easing.rs @@ -1,7 +1,10 @@ use std::f32::consts::PI; +use bevy::reflect::prelude::*; + /// Variety to associate with [Easing]s to tune the acceleration. -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Reflect)] +#[reflect(Debug, PartialEq, Hash)] pub enum EasingVariety { Quadratic, Cubic, @@ -39,7 +42,8 @@ pub enum EasingVariety { /// /// - /// - -#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)] +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, Reflect)] +#[reflect(Debug, Default, PartialEq, Hash)] pub enum Easing { /// Linear interpolation #[default] diff --git a/src/events.rs b/src/events.rs index f9b6780..3f96001 100644 --- a/src/events.rs +++ b/src/events.rs @@ -1,13 +1,17 @@ use std::fmt; -use bevy::ecs::{entity::Entity, event::Event}; +use bevy::{ + ecs::{entity::Entity, event::Event}, + reflect::prelude::*, +}; use crate::{animation::AnimationId, clip::ClipId}; /// An opaque identifier that references an animation marker. /// /// Returned by [AnimationLibrary::new_marker](crate::prelude::AnimationLibrary::new_marker). -#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] +#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Reflect)] +#[reflect(Debug, PartialEq, Hash)] pub struct AnimationMarkerId { pub(crate) value: usize, } diff --git a/src/library.rs b/src/library.rs index 9ee15bc..0e3a28b 100644 --- a/src/library.rs +++ b/src/library.rs @@ -3,7 +3,7 @@ use std::{ sync::Arc, }; -use bevy::prelude::Resource; +use bevy::{ecs::reflect::*, prelude::Resource, reflect::prelude::*}; use crate::{ animator::cache::AnimationCache, @@ -51,7 +51,8 @@ pub enum LibraryError { /// // ... Assign the animation to a SpritesheetAnimation component ... /// } /// ``` -#[derive(Resource, Default)] +#[derive(Resource, Default, Reflect)] +#[reflect(Resource, Default)] pub struct AnimationLibrary { /// All the clips clips: HashMap, diff --git a/src/plugin.rs b/src/plugin.rs index 4a6cdfb..1c7905f 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -57,10 +57,13 @@ impl Plugin for SpritesheetAnimationPlugin { app // The animation library, for creating clips, animations and markers .init_resource::() + .register_type::() // The animator responsible for running animations .init_resource::() + .register_type::() // Cache for 3D sprites .init_resource::() + .register_type::() // Animations events .add_event::() // Systems diff --git a/src/systems/sprite3d.rs b/src/systems/sprite3d.rs index 364298f..57bd119 100644 --- a/src/systems/sprite3d.rs +++ b/src/systems/sprite3d.rs @@ -21,7 +21,8 @@ use bevy::{ use crate::components::sprite3d::Sprite3d; /// Cached data for the 3D sprites -#[derive(Resource, Default)] +#[derive(Resource, Debug, Default, Reflect)] +#[reflect(Resource, Debug, Default)] pub struct Cache { /// Materials used by 3D sprites. /// @@ -35,7 +36,8 @@ pub struct Cache { } /// Uniquely identifies a sprite material -#[derive(Hash, PartialEq, Eq)] +#[derive(Debug, Hash, PartialEq, Eq, Reflect)] +#[reflect(Debug, Hash, PartialEq)] struct MaterialId { image: Handle, color: u32, @@ -51,7 +53,8 @@ impl MaterialId { } /// Uniquely identifies a sprite mesh -#[derive(Hash, PartialEq, Eq)] +#[derive(Debug, Hash, PartialEq, Eq, Reflect)] +#[reflect(Debug, Hash, PartialEq)] struct MeshId { sprite_custom_size: [u32; 2], sprite_anchor: [u32; 2],