Struct Spritesheet

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

An helper to obtain frame indices from a spritesheet.

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

let clip = Clip::from_frames([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);

// Create a clip from all the frames in row 2

let clip1 = Clip::from_frames(spritesheet.row(2));

// Create another clip with the vertical strip starting at (0, 1)
// (will wrap to the next columns)

let clip2 = Clip::from_frames(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 = Clip::from_frames(spritesheet.all());

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

assert_eq!(clip.frames(), vec![0, 1, 2, 3]);
Source

pub fn positions( &self, positions: impl IntoIterator<Item = (usize, usize)>, ) -> 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 = Clip::from_frames(spritesheet.positions([(1, 0), (0, 1)]));

// This clip will play frames B → C

assert_eq!(clip.frames(), 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 = Clip::from_frames(spritesheet.row(1));

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

assert_eq!(clip.frames(), 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);

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

let clip1 = Clip::from_frames(spritesheet.row_partial(1, 1..=3));

assert_eq!(clip1.frames(), vec![6, 7, 8]);

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

let clip2 = Clip::from_frames(spritesheet.row_partial(0, 2..));

assert_eq!(clip2.frames(), vec![2, 3, 4]);

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

let clip3 = Clip::from_frames(spritesheet.row_partial(1, ..4));

assert_eq!(clip3.frames(), 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 = Clip::from_frames(spritesheet.column(1));

// This clip will play frames B → E

assert_eq!(clip.frames(), 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 = Clip::from_frames(spritesheet.column_partial(1, 1..));

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

assert_eq!(clip.frames(), 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 = Clip::from_frames(spritesheet.horizontal_strip(2, 0, 3));

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

assert_eq!(clip.frames(), 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 = Clip::from_frames(spritesheet.vertical_strip(1, 0, 3));

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

assert_eq!(clip.frames(), vec![1, 4, 2]);
Source

pub fn atlas_layout( &self, frame_width: u32, frame_height: u32, ) -> TextureAtlasLayout

Creates a TextureAtlasLayout from the spritesheet.

§Arguments
  • frame_width - the width of a single frame
  • frame_height - the height of a single frame
§Example
fn setup(
    mut commands: Commands,
    mut library: ResMut<AnimationLibrary>,
    mut atlas_layouts: ResMut<Assets<TextureAtlasLayout>>,
    assets: Res<AssetServer>,
) {
    let spritesheet = Spritesheet::new(8, 8);

    // ... omitted: create an animation ...

    let image = assets.load("character.png");

    let atlas = TextureAtlas {
        layout: atlas_layouts.add(spritesheet.atlas_layout(100, 200)),
        ..default()
    };

    commands.spawn((
        Sprite::from_atlas_image(image, atlas),
        SpritesheetAnimation::from_id(animation_id),
    ));
}

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<GpuImage>) -> 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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>

Converts Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Converts Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

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

Converts &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> DowncastSend for T
where T: Any + Send,

Source§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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

Source§

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

Source§

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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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> ConditionalSend for T
where T: Send,

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,