# core/animation (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/core/animation/

Animation handler.
- A port of the original core/animation.js. Advances a list of frames (Frame) at a frames-per-second rate (animation_speed), and
  if not looping, stops at the last frame and calls the completion callback.
- In the original it is also a plain object, not a component. It is not attached to a node; the holder (a component or such) calls tick and
  takes the current frame out with get_current_image / get_current_rect to put it in a Sprite.
- The original's set_frames family simply returns if given a non-array or an empty one. In the Rust version the types are fixed, so only empty lists are filtered.
  (set_frames accepts an empty list as in the original)
- Frame numbers are usize. The type takes care of clamping negatives to 0, which the original gotoAndPlay(-1) does.

## Animation (struct) (Default)

Animation handler.
- Advances a list of frames (Frame) at a frames-per-second rate. It is not attached to a node, so the holder calls tick and sets the current frame on a Sprite.
- Starts with no frames, 10 frames per second, looping, stopped.

Example: Cycle four cells of a sprite sheet and set them on a Sprite

```rust
use vanilla::{Animation, Image, Rect, Sprite};

let sheet = Image::create_from_pixels(64, 16, vec![255u8; 64 * 16 * 4]);
let rects: Vec<Rect> = (0..4).map(|frame_index| Rect::create(16.0 * frame_index as f64, 0.0, 16.0, 16.0)).collect();
let mut animation = Animation::new();
animation.set_frames_from_rects(sheet, &rects);
animation.set_animation_speed(8.0);
animation.play();
animation.tick(0.25);
assert_eq!(animation.get_current_frame_index(), 2);

let mut sprite = Sprite::new();
sprite.set_frame(animation.get_current_frame());
assert_eq!(sprite.get_image_rect(), Rect::create(32.0, 0.0, 16.0, 16.0));
```

- `pub fn new() -> Animation` (constructor, since 0.3.2): Constructor. (10 frames per second, looping, stopped)
  - returns Animation: A new handler with no frames.
- `pub fn tick(&mut self, time_delta: f64)` (method, since 0.3.2): Update. (advances frames by the accumulated time. Can advance several frames at once)
  - Does nothing if not playing, if there are no frames, or if the frames per second is 0 or less. Time short of one frame carries over to the next tick.
  - When passing the end, goes back to the start if looping; otherwise stops at the last frame and calls the completion callback.
  - `time_delta` f64: Elapsed time. (seconds)

  Example: Advance two frames at once and wrap to the start

  ```rust
  use vanilla::{Animation, Image, Rect};
  
  let sheet = Image::create_from_pixels(48, 16, vec![255u8; 48 * 16 * 4]);
  let rects = [Rect::create(0.0, 0.0, 16.0, 16.0), Rect::create(16.0, 0.0, 16.0, 16.0), Rect::create(32.0, 0.0, 16.0, 16.0)];
  let mut animation = Animation::new();
  animation.set_frames_from_rects(sheet, &rects);
  animation.set_animation_speed(4.0);
  animation.play();
  animation.tick(0.125);
  assert_eq!(animation.get_current_frame_index(), 0);
  animation.tick(0.375);
  assert_eq!(animation.get_current_frame_index(), 2);
  animation.tick(0.25);
  assert_eq!(animation.get_current_frame_index(), 0);
  ```

- `pub fn play(&mut self)` (method, since 0.3.2): Plays. (does nothing if already playing)
- `pub fn pause(&mut self)` (method, since 0.3.2): Pause. (keeps the current frame and the accumulated time as they are)
- `pub fn resume(&mut self)` (method, since 0.3.2): Resume. (continues from the paused frame)
- `pub fn stop(&mut self)` (method, since 0.3.2): Stop. (returns to the first frame. Returns to it even when already stopped)
- `pub fn set_frames(&mut self, frames: Vec<Frame>)` (method, since 0.3.2): Sets the frames. (stops after setting)
  - `frames` Vec<Frame>: List of frames shown in order. (an empty list is also accepted)
- `pub fn set_frames_from_images(&mut self, images: &[Rc<Image>])` (method, since 0.3.2): Sets the frames. (a list of single sprite images, ignored if empty)
  - `images` &[Rc<Image>]: List of images used per frame. (each frame's region is the whole image)
- `pub fn set_frames_from_rects(&mut self, image: Rc<Image>, rects: &[Rect])` (method, since 0.3.2): Sets the frames. (one sprite sheet and a list of cell regions, ignored if empty)
  - `image` Rc<Image>: Sprite sheet image.
  - `rects` &[Rect]: List of cell regions in the sheet used per frame. (pixels, relative to the sheet's top left)
- `pub fn set_animation_speed(&mut self, animation_speed: f64)` (method, since 0.3.2): Sets the frames per second. (e.g. 60 changes the image 60 times per second. Does not advance if 0 or less)
  - `animation_speed` f64: Frames per second. (initially 10)
- `pub fn set_loop(&mut self, is_loop: bool)` (method, since 0.3.2): Sets whether to loop.
  - `is_loop` bool: If true, wraps from the end to the start; if false, stops at the last frame. (initially true)
- `pub fn set_on_complete(&mut self, callback: impl FnMut() + 'static)` (method, since 0.3.2): Sets the animation completion callback. (called when reaching the last frame while not looping)
  - Calling again replaces the previous callback. After stopping, it is not called until playing again reaches the end.
  - `callback` impl FnMut() + 'static: Function called when reaching the end. (called inside tick)

  Example: Know when it has played through once

  ```rust
  use vanilla::{Animation, Image};
  use std::cell::Cell;
  use std::rc::Rc;
  
  let images = [Image::create_from_pixels(8, 8, vec![0u8; 8 * 8 * 4]), Image::create_from_pixels(8, 8, vec![0u8; 8 * 8 * 4])];
  let mut animation = Animation::new();
  animation.set_frames_from_images(&images);
  animation.set_loop(false);
  let complete_count = Rc::new(Cell::new(0));
  let complete_count_in_callback = complete_count.clone();
  animation.set_on_complete(move || {
  	complete_count_in_callback.set(complete_count_in_callback.get() + 1);
  });
  animation.play();
  animation.tick(1.0);
  assert_eq!(complete_count.get(), 1);
  assert!(!animation.is_playing());
  assert_eq!(animation.get_current_frame_index(), 1);
  ```

- `pub fn goto_and_play(&mut self, index: usize)` (method, since 0.3.2): Moves to a specific frame and plays. (the index is clamped to the frame count)
  - `index` usize: Frame index to move to. (from 0; the last one if greater than frame count - 1, 0 if there are no frames)
- `pub fn goto_and_stop(&mut self, index: usize)` (method, since 0.3.2): Moves to a specific frame and stops. (the index is clamped to the frame count)
  - `index` usize: Frame index to move to. (from 0; the last one if greater than frame count - 1, 0 if there are no frames)
- `pub fn is_playing(&self) -> bool` (method, since 0.3.2): Returns whether playing.
  - returns bool: true if playing.
- `pub fn get_current_frame(&self) -> Option<&Frame>` (method, since 0.3.2): Returns the current frame. (None if none)
  - returns Option<&Frame>: The current frame. None if there are no frames.
- `pub fn get_current_image(&self) -> Option<&Rc<Image>>` (method, since 0.3.2): Returns the current image.
  - returns Option<&Rc<Image>>: Image of the current frame. None if there are no frames.
- `pub fn get_current_rect(&self) -> Option<Rect>` (method, since 0.3.2): Returns the current region.
  - returns Option<Rect>: Region within the image of the current frame. (pixels) None if there are no frames.
- `pub fn get_current_frame_index(&self) -> usize` (method, since 0.3.2): Returns the current frame number.
  - returns usize: Current frame index. (from 0)
- `pub fn get_total_frame_count(&self) -> usize` (method, since 0.3.2): Returns the total number of frames.
  - returns usize: Frame count.
