# core/component/sprite (vanilla-engine 0.4.0)

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

Sprite renderer component.
- Without an image it draws a color rectangle like Paint. (the original inherits Paint, here it contains a Paint)
- If the color's alpha is greater than 0, it covers only the inside of the image silhouette with that color. (tint)
- The image path (image_path) is the value the original attaches as sprite.imagePath. The UI document (UIDocument) writes and reads this path in place of the image. It is not used for drawing.

## SpriteBlendMode (type)

The original's SpriteBlendMode has the same values as the Graphic's blend mode.

```rust
pub type SpriteBlendMode = BlendMode
```

## Sprite (struct) (Default, Component)

Sprite renderer component. (draws an image in the node's content_size)
- Without an image it paints a color rectangle like Paint. With an image and a color alpha greater than 0, it covers the inside of the image silhouette with that color. (tint)
- Choose the draw mode (Simple, Sliced, Tiled), blend mode and horizontal / vertical flip.
- Initially no image, transparent color, Simple, Normal, no flip, and empty rectangles for the area and nine-slice.

Example: Attach an image and flip it to face left

```rust
use vanilla::{Image, NodeTree, Sprite, Vector2};

let image = Image::create_from_pixels(16, 16, vec![255u8; 16 * 16 * 4]);
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let player_id = node_tree.create_child(root_id, "Player");
node_tree[player_id].set_content_size(Vector2::create(64.0, 64.0));
let sprite = node_tree.add_component(player_id, Sprite::create_with_image(image)).unwrap();
sprite.set_horizontal_flip(true);

let sprite = node_tree[player_id].get_component::<Sprite>().unwrap();
assert!(sprite.get_image().is_some());
assert_eq!(sprite.get_flip(), Vector2::create(-1.0, 1.0));
```

Example: A round color rectangle without an image

```rust
use vanilla::{Color, Sprite};

let mut sprite = Sprite::new();
sprite.set_color("#44aa66");
sprite.set_round_size(8.0);
assert!(sprite.get_image().is_none());
assert_eq!(sprite.get_color(), Color::create_from_hex("#44aa66"));
```

- `pub fn new() -> Sprite` (constructor, since 0.3.2): Constructor.
  - returns Sprite: New component with no image and a transparent color. (Simple, Normal)
- `pub fn create_with_image(image: Rc<Image>) -> Sprite` (static, since 0.3.2): Constructor with an image.
  - `image` Rc<Image>: Image to draw.
  - returns Sprite: New component that draws the whole image. (the area is an empty rectangle, so the whole image)

  Example: A sprite that draws the whole image

  ```rust
  use vanilla::{Image, Sprite};
  
  let image = Image::create_from_pixels(32, 16, vec![255u8; 32 * 16 * 4]);
  let sprite = Sprite::create_with_image(image);
  assert_eq!(sprite.get_image().unwrap().get_width(), 32);
  assert!(sprite.get_image_rect().is_zero());
  ```

- `pub fn set_sprite_draw_mode(&mut self, sprite_draw_mode: SpriteDrawMode)` (method, since 0.3.2): Sets the draw mode.
  - `sprite_draw_mode` SpriteDrawMode: Draw mode. (initially Simple)
- `pub fn get_sprite_draw_mode(&self) -> SpriteDrawMode` (method, since 0.3.2): Returns the draw mode.
  - returns SpriteDrawMode: Draw mode.
- `pub fn set_sprite_blend_mode(&mut self, sprite_blend_mode: SpriteBlendMode)` (method, since 0.3.2): Sets the blend mode.
  - `sprite_blend_mode` SpriteBlendMode: Blend mode applied when drawing the image. (initially Normal, restored to Normal after drawing, not used when there is no image)
- `pub fn get_sprite_blend_mode(&self) -> SpriteBlendMode` (method, since 0.3.2): Returns the blend mode.
  - returns SpriteBlendMode: Blend mode applied when drawing the image.
- `pub fn set_nine_slice(&mut self, nine_slice: Rect)` (method, since 0.3.2): Sets the nine-slice area. (position.x / position.y / size.x / size.y = left / top / right / bottom)
  - `nine_slice` Rect: Thickness from the image edges that is not stretched. (pixels, used only for Sliced)

  Example: A panel that preserves 16 pixels at the edges

  ```rust
  use vanilla::{Image, Rect, Sprite, SpriteDrawMode};
  
  let panel_image = Image::create_from_pixels(48, 48, vec![255u8; 48 * 48 * 4]);
  let mut sprite = Sprite::create_with_image(panel_image);
  sprite.set_sprite_draw_mode(SpriteDrawMode::Sliced);
  sprite.set_nine_slice(Rect::create(16.0, 16.0, 16.0, 16.0));
  assert_eq!(sprite.get_sprite_draw_mode(), SpriteDrawMode::Sliced);
  assert_eq!(sprite.get_nine_slice().size.x, 16.0);
  ```

- `pub fn get_nine_slice(&self) -> Rect` (method, since 0.3.2): Returns the nine-slice region.
  - returns Rect: Left / top / right / bottom thickness. (position.x / position.y / size.x / size.y, pixels)
- `pub fn set_image(&mut self, image: Option<Rc<Image>>)` (method, since 0.3.2): Sets the image.
  - `image` Option<Rc<Image>>: Image to draw. (paints a color rectangle if None, the area is left as is)
- `pub fn set_frame(&mut self, frame: Option<&Frame>)` (method, since 0.4.0): Sets a frame. (sets the Frame's image and the area within it together, clears the image if None, the original setFrame, SpriteAtlas.read_frame)
  - `frame` Option<&Frame>: Frame to set. (if None, clears only the image and leaves the area as is)

  Example: Set the second cell of a sheet

  ```rust
  use vanilla::{Frame, Image, Rect, Sprite};
  
  let sheet = Image::create_from_pixels(64, 16, vec![255u8; 64 * 16 * 4]);
  let frame = Frame::new(sheet, Some(Rect::create(16.0, 0.0, 16.0, 16.0)));
  let mut sprite = Sprite::new();
  sprite.set_frame(Some(&frame));
  assert_eq!(sprite.get_image_rect(), Rect::create(16.0, 0.0, 16.0, 16.0));
  
  sprite.set_frame(None);
  assert!(sprite.get_image().is_none());
  ```

- `pub fn get_image(&self) -> Option<&Rc<Image>>` (method, since 0.3.2): Returns the image.
  - returns Option<&Rc<Image>>: The image being drawn. None if none.
- `pub fn set_image_path(&mut self, image_path: &str)` (method, since 0.4.0): Sets the image path. (the original sprite.imagePath, written and read by the UI document)
  - `image_path` &str: Image path. (only recorded, the image is not loaded)
- `pub fn get_image_path(&self) -> &str` (method, since 0.4.0): Returns the image path. (empty string if not set)
  - returns &str: Image path. (empty string if not set)
- `pub fn set_image_rect(&mut self, image_rect: Rect)` (method, since 0.3.2): Sets the image area. (the whole image if empty)
  - `image_rect` Rect: Area within the image. (pixels, relative to the image's top left, the whole image if Rect::zero(), not used by Sliced)
- `pub fn get_image_rect(&self) -> Rect` (method, since 0.3.2): Returns the image region.
  - returns Rect: Area within the image. (pixels, the whole image if an empty rectangle)
- `pub fn set_horizontal_flip(&mut self, flip: bool)` (method, since 0.3.2): Sets the horizontal flip.
  - `flip` bool: Whether to draw flipped horizontally. (initially false)
- `pub fn set_vertical_flip(&mut self, flip: bool)` (method, since 0.3.2): Sets the vertical flip.
  - `flip` bool: Whether to draw flipped vertically. (initially false)
- `pub fn is_horizontal_flip(&self) -> bool` (method, since 0.3.2): Whether flipped horizontally.
  - returns bool: true if drawn flipped horizontally.
- `pub fn is_vertical_flip(&self) -> bool` (method, since 0.3.2): Whether flipped vertically.
  - returns bool: true if drawn flipped vertically.
- `pub fn get_flip(&self) -> Vector2` (method, since 0.3.2): Flip values. (-1 on flipped axes)
  - returns Vector2: Value that is -1 on each flipped axis and 1 otherwise. (x is horizontal, y is vertical, multiplied by the draw size)

  Example: Flip vertically only

  ```rust
  use vanilla::{Sprite, Vector2};
  
  let mut sprite = Sprite::new();
  sprite.set_vertical_flip(true);
  assert_eq!(sprite.get_flip(), Vector2::create(1.0, -1.0));
  ```

- `pub fn set_color(&mut self, color: impl Into<Color>)` (method, since 0.3.2): Sets the color. (a tint if there is an image, otherwise the fill color)
  - `color` impl Into<Color>: Tint or fill color. (a Color or a string such as "#rrggbb", initially transparent, the tint applies only when alpha is greater than 0)
- `pub fn get_color(&self) -> Color` (method, since 0.3.2): Returns the color.
  - returns Color: Tint or fill color.
- `pub fn set_round_size(&mut self, round_size: f64)` (method, since 0.4.0): Sets the round corner size. (for the rectangle painted when there is no image, the original inherits Paint and uses the same function)
  - `round_size` f64: Corner round size. (node coordinates, a sharp rectangle if 0 or less, initially 0)
- `pub fn get_round_size(&self) -> f64` (method, since 0.4.0): Returns the round corner size.
  - returns f64: Corner round size of the rectangle painted when there is no image. (node coordinates)

## SpriteDrawMode (enum) (Clone, Copy, Debug, PartialEq, Eq)

Sprite draw mode.

- `Simple`: Draws the image (the image_rect piece, or the whole image if empty) stretched to the node's content_size.
- `Sliced`: Nine-slice. Keeps the four corners as is and stretches the edges and center to draw the whole image in content_size. (image_rect is not used, does not draw if content_size is smaller than left + right or top + bottom)
- `Tiled`: Tiles the image_rect piece (the whole image if empty) at its own size repeatedly over the content_size area, and clips outside the area.
