pub struct Spritesheet { /* private fields */ }
Expand description

An helper to obtain frame indices from a spritesheet with a given layout.

When creating an animation clip, you might specify its frames by using raw indices:

let clip_id = library.new_clip(|clip| {
    clip.push_frame_indices([6, 7, 8, 9, 10, 11]);
});

However, a clearer and less error-prone approach is to use a Spritesheet to retrieve indices with layout queries:

// We're working with a spritesheet with 8 columns and 4 rows
let spritesheet = Spritesheet::new(8, 4);

let clip1_id = library.new_clip(|clip| {
    // Use all the frames in row 2
    clip.push_frame_indices(spritesheet.row(2));
});

let clip2_id = library.new_clip(|clip| {
    // Pick 12 frames vertically starting at (0, 1), will wrap to the next columns
    clip.push_frame_indices(spritesheet.vertical_strip(0, 1, 12));
});

Implementations§

source§

impl Spritesheet

source

pub fn new(columns: usize, rows: usize) -> Self

Creates a new spritesheet helper with the given layout.

§Arguments
  • columns - the number of columns in the spritesheet
  • rows - the number of rows in the spritesheet
source

pub fn all(&self) -> Vec<usize>

Returns the frame indices for all of the spritesheet.

This is convenient if the whole spritesheet represents a single animation.

§Example
// ┌───┐
// │A B│
// │C D│
// └───┘

let spritesheet = Spritesheet::new(2, 2);

let clip_id = library.new_clip(|clip| {
    clip.push_frame_indices(spritesheet.all());

    // This clip will play frames A → B → C → D

    assert_eq!(clip.frame_indices(), vec![0, 1, 2, 3]);
});
source

pub fn positions<I: IntoIterator<Item = (usize, usize)>>( &self, positions: I ) -> Vec<usize>

Returns the frame indices corresponding to the given positions in the spritesheet.

This is convenient if the frames that you’re interested in are scattered all over the spritesheet.

§Arguments
  • positions - the list of (x, y) positions for each frame
§Example
// ┌───┐
// │A B│
// │C D│
// └───┘

let spritesheet = Spritesheet::new(2, 2);

let clip_id = library.new_clip(|clip| {
    clip.push_frame_indices(spritesheet.positions([(1, 0), (0, 1)]));

    // This clip will play frames B → C

    assert_eq!(clip.frame_indices(), vec![1, 2]);
});
source

pub fn row(&self, row: usize) -> Vec<usize>

Returns the frame indices for a whole row of the spritesheet.

This is convenient if some spritesheet row contains a single animation.

§Arguments
  • row - the index of the spritesheet row to add frames for
§Example
// ┌─────┐
// │A B C│
// │D E F│
// └─────┘

let spritesheet = Spritesheet::new(3, 2);

let clip_id = library.new_clip(|clip| {
    clip.push_frame_indices(spritesheet.row(1));

    // This clip will play frames D → E → F

    assert_eq!(clip.frame_indices(), vec![3, 4, 5]);
});
source

pub fn row_partial<R: RangeBounds<usize>>( &self, row: usize, column_range: R ) -> Vec<usize>

Returns the frame indices for a section of a row of the spritesheet.

This is convenient if some spritesheet row contains an animation next to other unrelated frames.

§Arguments
  • row - the index of the spritesheet row to add frames for
  • column_range - the range of columns to add frames for
§Example
// ┌─────────┐
// │A B C D E│
// │F G H I J│
// └─────────┘

let spritesheet = Spritesheet::new(5, 2);

let clip1_id = library.new_clip(|clip| {
    clip.push_frame_indices(spritesheet.row_partial(1, 1..=3));

    // This clip will play frames G → H → I

    assert_eq!(clip.frame_indices(), vec![6, 7, 8]);
});

let clip2_id = library.new_clip(|clip| {
    clip.push_frame_indices(spritesheet.row_partial(0, 2..));

    // This clip will play frames C → D → E

    assert_eq!(clip.frame_indices(), vec![2, 3, 4]);
});

let clip3_id = library.new_clip(|clip| {
    clip.push_frame_indices(spritesheet.row_partial(1, ..4));

    // This clip will play frames F → G → H → I

    assert_eq!(clip.frame_indices(), vec![5, 6, 7, 8]);
});
source

pub fn column(&self, column: usize) -> Vec<usize>

Returns the frame indices for a whole column of the spritesheet.

This is convenient if some spritesheet column contains a single animation.

§Arguments
  • column - the index of the spritesheet column to add frames for
§Example
// ┌─────┐
// │A B C│
// │D E F│
// └─────┘

let spritesheet = Spritesheet::new(3, 2);

let clip_id = library.new_clip(|clip| {
    clip.push_frame_indices(spritesheet.column(1));

    // This clip will play frames C → F

    assert_eq!(clip.frame_indices(), vec![1, 4]);
});
source

pub fn column_partial<R: RangeBounds<usize>>( &self, column: usize, row_range: R ) -> Vec<usize>

Returns the frame indices for a section of a column of the spritesheet.

This is convenient if some spritesheet column contains an animation among other unrelated frames.

§Arguments
  • column - the index of the spritesheet column to add frames for
  • row_range - the range of rows to add frames for
§Example
// ┌─────┐
// │A B C│
// │D E F│
// │G H I│
// │J K L│
// └─────┘

let spritesheet = Spritesheet::new(3, 4);

let clip_id = library.new_clip(|clip| {
    clip.push_frame_indices(spritesheet.column_partial(1, 1..));

    // This clip will play frames E → H → K

    assert_eq!(clip.frame_indices(), vec![4, 7, 10]);
});
source

pub fn horizontal_strip(&self, x: usize, y: usize, count: usize) -> Vec<usize>

Returns the frame indices for an horizontal strip in the spritesheet, wrapping from row to row.

This is convenient if some animations span several rows of a spritesheet.

§Arguments
  • x - the x position of the beginning of the strip
  • y - the y position of the beginning of the strip
  • count - the number of frames to add
§Example
// ┌─────┐
// │A B C│
// │D E F│
// └─────┘

let spritesheet = Spritesheet::new(3, 2);

let clip_id = library.new_clip(|clip| {
    clip.push_frame_indices(spritesheet.horizontal_strip(2, 0, 3));

    // This clip will play frames C → D → E

    assert_eq!(clip.frame_indices(), vec![2, 3, 4]);
});
source

pub fn vertical_strip(&self, x: usize, y: usize, count: usize) -> Vec<usize>

Returns the frame indices for a vertical strip in the spritesheet, wrapping from column to column.

This is convenient if some animations span several columns of a spritesheet.

§Arguments
  • x - the x position of the beginning of the strip
  • y - the y position of the beginning of the strip
  • count - the number of frames to add
§Example
// ┌─────┐
// │A B C│
// │D E F│
// └─────┘

let spritesheet = Spritesheet::new(3, 2);

let clip_id = library.new_clip(|clip| {
    clip.push_frame_indices(spritesheet.vertical_strip(1, 0, 3));

    // This clip will play frames B → E → C

    assert_eq!(clip.frame_indices(), vec![1, 4, 2]);
});

Trait Implementations§

source§

impl Clone for Spritesheet

source§

fn clone(&self) -> Spritesheet

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Spritesheet

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Copy for Spritesheet

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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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> TypeData for T
where T: 'static + Send + Sync + Clone,

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,