pub struct SpritesheetLibrary { /* private fields */ }
Expand description
The library is the global store for clips and animations.
When the SpritesheetAnimationPlugin is added to the app, the SpritesheetLibrary becomes available as a resource.
You can then create new clips, new animations and new markers from any system.
§Example
fn my_system(mut library: ResMut<SpritesheetLibrary>) {
let marker_id = library.new_marker();
let clip_id = library.new_clip(|clip| {
// Configure your clip here...
// Let's use the marker created above
clip.add_marker(marker_id, 5);
});
let animation_id = library.new_animation(|animation| {
// Configure your animation here...
// Let's use the clip created above
animation.add_stage(clip_id.into());
});
// ... Assign the animation to a SpritesheetAnimation component ...
}
Implementations§
source§impl SpritesheetLibrary
impl SpritesheetLibrary
sourcepub fn new_clip<F: Fn(&mut AnimationClip)>(
&mut self,
builder: F
) -> AnimationClipId
pub fn new_clip<F: Fn(&mut AnimationClip)>( &mut self, builder: F ) -> AnimationClipId
Creates a clip and returns a unique ID to refer to it.
The clip can then be referenced in one or several AnimationStages.
§Arguments
builder
- a builder function that takes the new clip as an argument so that you can configure it
§Example
let clip_id = library.new_clip(|clip| {
clip
.push_frame_indices([0, 4, 5])
.set_default_repeat(10);
});
let stage = AnimationStage::from_clip(clip_id);
sourcepub fn name_clip(
&mut self,
clip_id: AnimationClipId,
name: impl Into<String>
) -> Result<(), LibraryError>
pub fn name_clip( &mut self, clip_id: AnimationClipId, name: impl Into<String> ) -> Result<(), LibraryError>
Associates a unique name to a clip.
The clip ID can then later be queried from that name with SpritesheetLibrary::clip_with_name.
Returns a LibraryError::NameAlreadyTaken error if the name is already in use.
§Arguments
clip_id
- the ID of the clip to namename
- the name to assign
§Example
let clip_id = library.new_clip(|clip| {
// ...
});
library.name_clip(clip_id, "jump");
assert_eq!(library.clip_with_name("jump"), Some(clip_id));
assert!(library.is_clip_name(clip_id, "jump"));
sourcepub fn clip_with_name(&self, name: impl Into<String>) -> Option<AnimationClipId>
pub fn clip_with_name(&self, name: impl Into<String>) -> Option<AnimationClipId>
sourcepub fn is_clip_name(
&self,
clip_id: AnimationClipId,
name: impl Into<String>
) -> bool
pub fn is_clip_name( &self, clip_id: AnimationClipId, name: impl Into<String> ) -> bool
Returns true if a clip has the given name.
§Arguments
clip_id
- the ID of the clip to check the name ofname
- the name to check
sourcepub fn clips(&self) -> &HashMap<AnimationClipId, AnimationClip>
pub fn clips(&self) -> &HashMap<AnimationClipId, AnimationClip>
Returns all the clips registered in the library.
sourcepub fn new_animation<F: Fn(&mut Animation)>(
&mut self,
builder: F
) -> AnimationId
pub fn new_animation<F: Fn(&mut Animation)>( &mut self, builder: F ) -> AnimationId
Creates an animation and returns a unique ID to refer to it.
The animation can then be referenced in crate::prelude::SpritesheetAnimation components.
§Arguments
builder
- a builder function that takes the new animation as an argument so that you can configure it
§Example
fn f(
mut commands: Commands,
mut library: SpritesheetLibrary,
// ...
) {
let animation_id = library.new_animation(|animation| {
animation
.add_stage(some_clip_id.into())
.set_duration(AnimationDuration::PerCycle(1500));
});
// The animation can then be assigned to an entity
// ... Load a texture and create an atlas layout for the sprite ...
commands.spawn((
SpriteSheetBundle {
texture: texture.clone(),
atlas: TextureAtlas {
layout: layout.clone(),
..default()
},
..default()
},
SpritesheetAnimation::from_id(animation_id)
));
}
sourcepub fn name_animation(
&mut self,
animation_id: AnimationId,
name: impl Into<String>
) -> Result<(), LibraryError>
pub fn name_animation( &mut self, animation_id: AnimationId, name: impl Into<String> ) -> Result<(), LibraryError>
Associates a unique name to an animation.
The animation ID can then later be queried from that name with SpritesheetLibrary::animation_with_name.
Returns a LibraryError::NameAlreadyTaken error if the name is already in use.
§Arguments
animation_id
- the ID of the animation to namename
- the name to assign
§Example
let animation_id = library.new_animation(|animation| {
// ...
});
library.name_animation(animation_id, "crouch");
assert_eq!(library.animation_with_name("crouch"), Some(animation_id));
assert!(library.is_animation_name(animation_id, "crouch"));
sourcepub fn animation_with_name(
&self,
name: impl Into<String>
) -> Option<AnimationId>
pub fn animation_with_name( &self, name: impl Into<String> ) -> Option<AnimationId>
Returns the ID of the animation with the given name if it exists.
§Arguments
name
- the animation name
sourcepub fn is_animation_name(
&self,
animation_id: AnimationId,
name: impl Into<String>
) -> bool
pub fn is_animation_name( &self, animation_id: AnimationId, name: impl Into<String> ) -> bool
Returns true if an animation has the given name.
§Arguments
animation_id
- the ID of the animation to check the name ofname
- the name to check
sourcepub fn animations(&self) -> &HashMap<AnimationId, Animation>
pub fn animations(&self) -> &HashMap<AnimationId, Animation>
Returns all the animations registered in the library.
sourcepub fn new_marker(&mut self) -> AnimationMarkerId
pub fn new_marker(&mut self) -> AnimationMarkerId
Creates a new animation marker and returns a unique ID to refer to it.
The marker can then be inserted into AnimationClips and an AnimationEvent::MarkerHit event will be emitted whenever an animation reaches it.
For more details, see the documentation of AnimationEvent.
§Example
let marker = library.new_marker();
let clip_id = library.new_clip(|clip| {
clip
.push_frame_indices([7, 8, 9, 10, 11, 12])
.add_marker(marker, 3);
});
sourcepub fn name_marker(
&mut self,
marker_id: AnimationMarkerId,
name: impl Into<String>
) -> Result<(), LibraryError>
pub fn name_marker( &mut self, marker_id: AnimationMarkerId, name: impl Into<String> ) -> Result<(), LibraryError>
Associates a unique name to an animation marker.
The marker ID can then later be queried from that name with SpritesheetLibrary::marker_with_name.
Returns a LibraryError::NameAlreadyTaken error if the name is already in use.
§Arguments
marker_id
- the ID of the marker to namename
- the name to assign
§Example
let marker_id = library.new_marker();
library.name_marker(marker_id, "raise sword");
assert_eq!(library.marker_with_name("raise sword"), Some(marker_id));
assert!(library.is_marker_name(marker_id, "raise sword"));
sourcepub fn marker_with_name(
&self,
name: impl Into<String>
) -> Option<AnimationMarkerId>
pub fn marker_with_name( &self, name: impl Into<String> ) -> Option<AnimationMarkerId>
sourcepub fn is_marker_name(
&self,
marker_id: AnimationMarkerId,
name: impl Into<String>
) -> bool
pub fn is_marker_name( &self, marker_id: AnimationMarkerId, name: impl Into<String> ) -> bool
Returns true if an animation marker has the given name.
§Arguments
marker_id
- the ID of the marker to check the name ofname
- the name to check
sourcepub fn markers(&self) -> &HashSet<AnimationMarkerId>
pub fn markers(&self) -> &HashSet<AnimationMarkerId>
Returns all the animation markers registered in the library.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for SpritesheetLibrary
impl RefUnwindSafe for SpritesheetLibrary
impl Send for SpritesheetLibrary
impl Sync for SpritesheetLibrary
impl Unpin for SpritesheetLibrary
impl UnwindSafe for SpritesheetLibrary
Blanket Implementations§
source§impl<T, U> AsBindGroupShaderType<U> for T
impl<T, U> AsBindGroupShaderType<U> for T
source§fn as_bind_group_shader_type(&self, _images: &RenderAssets<Image>) -> U
fn as_bind_group_shader_type(&self, _images: &RenderAssets<Image>) -> U
T
ShaderType
for self
. When used in AsBindGroup
derives, it is safe to assume that all images in self
exist.source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.