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

source

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);
source

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 name
  • name - 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"));
source

pub fn clip_with_name(&self, name: impl Into<String>) -> Option<AnimationClipId>

Returns the ID of the clip with the given name if it exists.

§Arguments
  • name - the clip name
source

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 of
  • name - the name to check
source

pub fn clips(&self) -> &HashMap<AnimationClipId, AnimationClip>

Returns all the clips registered in the library.

source

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)
    ));
}
source

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 name
  • name - 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"));
source

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
source

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 of
  • name - the name to check
source

pub fn animations(&self) -> &HashMap<AnimationId, Animation>

Returns all the animations registered in the library.

source

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);
});
source

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 name
  • name - 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"));
source

pub fn marker_with_name( &self, name: impl Into<String> ) -> Option<AnimationMarkerId>

Returns the ID of the marker with the given name if it exists.

§Arguments
  • name - the marker name
source

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 of
  • name - the name to check
source

pub fn markers(&self) -> &HashSet<AnimationMarkerId>

Returns all the animation markers registered in the library.

Trait Implementations§

source§

impl Resource for SpritesheetLibrary
where Self: Send + Sync + 'static,

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T, U> AsBindGroupShaderType<U> for T
where U: ShaderType, &'a T: for<'a> Into<U>,

source§

fn as_bind_group_shader_type(&self, _images: &RenderAssets<Image>) -> U

Return the T ShaderType for self. When used in AsBindGroup derives, it is safe to assume that all images in self exist.
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> Downcast<T> for T

source§

fn downcast(&self) -> &T

source§

impl<T> Downcast for T
where T: Any,

source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert 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>

Convert 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)

Convert &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)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> Upcast<T> for T

source§

fn upcast(&self) -> Option<&T>

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<T> Settings for T
where T: 'static + Send + Sync,

source§

impl<T> WasmNotSend for T
where T: Send,

source§

impl<T> WasmNotSendSync for T

source§

impl<T> WasmNotSync for T
where T: Sync,