# resource/animationclip (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/resource/animationclip/

Animation clip. (list of sprite sheet frame areas + whether it loops + duration)
- A port of the original resource/animationclip.js. Animator's AnimationState holds it.
- The original comment calls the frame list Frame[], but what from_atlas puts in is Rect. The Rust version settles on a list of Rect.
  (a Frame must hold an image, so it cannot hold an atlas area without an image)
- Differences from the original:
  - get_frame is None when out of range. (the original undefined)
  - Rust has no default arguments for from_atlas (fps = 10, is_loop = true), so they are always passed.
    frame_ids accepts anything that implements Display (strings, integers). Array atlases read that text as an index.
  - It has the same name as the 3D AnimationClip (graphics3d::skinnedmodel), so it is not re-exported from lib.rs.
    Use it as vanilla::resource::animationclip::AnimationClip.

## AnimationClip (struct) (Clone, Debug, Default, PartialEq)

Animation clip.
- Holds a list of sprite sheet frame areas (Rect), whether it loops, and its duration (seconds).

Example: Make it by putting frame areas in directly

```rust
use vanilla::Rect;
use vanilla::resource::animationclip::AnimationClip;

let mut clip = AnimationClip::new();
clip.set_frames(vec![Rect::create(0.0, 0.0, 32.0, 32.0), Rect::create(32.0, 0.0, 32.0, 32.0)]);
clip.set_loop(true);
clip.set_duration(0.2);
assert_eq!(clip.get_frame_count(), 2);
assert_eq!(clip.get_frame(1), Some(&Rect::create(32.0, 0.0, 32.0, 32.0)));
assert_eq!(clip.get_frame(2), None);
assert!(clip.is_loop());
assert_eq!(clip.get_duration(), 0.2);
```

- `pub fn new() -> AnimationClip` (constructor, since 0.3.2): Creates one. (no frames, duration 0, no looping)
  - returns AnimationClip: Empty clip.
- `pub fn from_atlas<T: Display>(atlas_json: &Value, frame_ids: &[T], fps: f64, is_loop: bool) -> AnimationClip` (static, since 0.3.2): Creates a clip from atlas JSON. (static)
  - Accepts both the TexturePacker format ({ frames: { name: { frame: {x,y,w,h} } } }) and the array format ([{x,y,w,h}, ...]).
  - Picks frames in frame_ids order and makes duration = frame count / fps. Missing names are skipped.
  - For the area, reads the frame field if present, otherwise the entry itself. For width and height, reads width, height if w, h are missing, and missing values are 0.
  - `atlas_json` &Value: Atlas JSON. (TexturePacker object format or array format)
  - `frame_ids` &[T]: Order of frame names or indices to pick. (anything that implements Display, the array format reads that text as an index)
  - `fps` f64: Frames per second. (duration 0 if 0 or less)
  - `is_loop` bool: Whether to loop playback.
  - returns AnimationClip: Clip holding only the frames found, in order.

  Example: Pick in name order from the TexturePacker format (missing names are skipped)

  ```rust
  use vanilla::Rect;
  use vanilla::resource::animationclip::AnimationClip;
  
  let atlas_json = serde_json::json!({ "frames": {
  	"run0": { "frame": { "x": 0, "y": 0, "w": 32, "h": 48 } },
  	"run1": { "frame": { "x": 32, "y": 0, "w": 32, "h": 48 } },
  } });
  let clip = AnimationClip::from_atlas(&atlas_json, &["run0", "missing", "run1"], 4.0, true);
  assert_eq!(clip.get_frame_count(), 2);
  assert_eq!(clip.get_frame(1), Some(&Rect::create(32.0, 0.0, 32.0, 48.0)));
  assert_eq!(clip.get_duration(), 0.5);
  ```

  Example: The array format looks up by index

  ```rust
  use vanilla::Rect;
  use vanilla::resource::animationclip::AnimationClip;
  
  let atlas_json = serde_json::json!([{ "x": 0, "y": 0, "width": 16, "height": 16 }, { "x": 16, "y": 0, "w": 16, "h": 16 }]);
  let clip = AnimationClip::from_atlas(&atlas_json, &[1, 0], 10.0, false);
  assert_eq!(clip.get_frames(), &[Rect::create(16.0, 0.0, 16.0, 16.0), Rect::create(0.0, 0.0, 16.0, 16.0)]);
  assert!(!clip.is_loop());
  ```

- `pub fn set_frames(&mut self, frames: Vec<Rect>)` (method, since 0.3.2): Sets the frame list.
  - `frames` Vec<Rect>: List of frame areas. (sprite sheet pixel coordinates, playback order)
- `pub fn set_loop(&mut self, is_loop: bool)` (method, since 0.3.2): Sets whether to loop.
  - `is_loop` bool: Whether to loop playback.
- `pub fn set_duration(&mut self, duration: f64)` (method, since 0.3.2): Sets the animation duration. (seconds)
  - `duration` f64: Playback time for one loop. (seconds)
- `pub fn is_loop(&self) -> bool` (method, since 0.3.2): Returns whether it loops.
  - returns bool: true to loop playback. (default false)
- `pub fn get_frame(&self, index: usize) -> Option<&Rect>` (method, since 0.3.2): Returns the frame at the target index. (None if out of range)
  - `index` usize: Frame index. (from 0)
  - returns Option<&Rect>: Frame area at that index. None if out of range.
- `pub fn get_frames(&self) -> &[Rect]` (method, since 0.3.2): Returns the frame list.
  - returns &[Rect]: List of frame areas. (playback order)
- `pub fn get_frame_count(&self) -> usize` (method, since 0.3.2): Returns the total frame count.
  - returns usize: Frame count.
- `pub fn get_duration(&self) -> f64` (method, since 0.3.2): Returns the animation duration. (seconds)
  - returns f64: Playback time for one loop. (seconds, default 0)
