# ui/uiimageview (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/ui/uiimageview/

UI image view. (UIImageView of the original ui/uiimageview.js)
- A display-only widget. Exposes the sprite's image / color / flip / draw mode / blend / nine-slice.
- The original attaches a Sprite to the host on attach and holds a reference. The Rust version embeds the Sprite. (for the same reason UILabel embeds Text)
  The embedded Sprite is taken out with get_sprite / get_sprite_mut as in the original. `get_component::<Sprite>()` on the image view node is None.
  The UI document writes and reads the embedded Sprite as a Sprite after UIImageView, as in the original. (uidocument.rs)
- The image path (get_image_path) is held separately from the embedded Sprite's path (the original Sprite's imagePath), like the original imageView.imagePath.
- The original setImage takes both an image element and an ImageAsset. The Rust version splits it into set_image(`Option<Rc<Image>>`) and set_image_asset.
- The background color (UIView) is drawn before the sprite. (in the original too, UIImageView is attached first and attach adds the Sprite after, so the order is the same)

## UIImageView (struct) (Default, Component)

UI image view.
- Embeds a Sprite and passes through the image, color, flip, draw mode, blend, and nine-slice. Draws the background color first and the sprite on top.

Example: Stretch an image made in code with nine-slice

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

let image = Image::create_from_pixels(32, 32, vec![255u8; 32 * 32 * 4]);
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let frame_id = node_tree.create_child(root_id, "frame");
node_tree[frame_id].set_content_size(Vector2::create(160.0, 96.0));
let image_view = node_tree.add_component(frame_id, UIImageView::create_with_image(image)).unwrap();
image_view.set_draw_mode(SpriteDrawMode::Sliced);
image_view.set_nine_slice(Rect::create(8.0, 8.0, 8.0, 8.0));
assert!(image_view.get_image().is_some());
assert_eq!(image_view.get_sprite().get_sprite_draw_mode(), SpriteDrawMode::Sliced);
assert!(node_tree[frame_id].get_component::<Sprite>().is_none());
```

- `pub fn new() -> UIImageView` (constructor, since 0.3.2): Constructor.
  - returns UIImageView: New image view with no image. (the content node is created when attached to a tree)
- `pub fn create_with_image(image: Rc<Image>) -> UIImageView` (static, since 0.3.2): Constructor with an image.
  - `image` Rc<Image>: Image to show.
  - returns UIImageView: New image view with that image set.
- `pub fn get_uiview(&self) -> &UIView` (method, since 0.3.2): Returns the embedded view.
  - returns &UIView: Reference to the embedded UIView.
- `pub fn get_uiview_mut(&mut self) -> &mut UIView` (method, since 0.3.2): Returns the embedded view. (mutable)
  - returns &mut UIView: Mutable reference to the embedded UIView.
- `pub fn get_content(&self) -> Option<NodeId>` (method, since 0.3.2): Content node handle.
  - returns Option<NodeId>: content node. None before being attached to a tree.
- `pub fn set_background_color(&mut self, background_color: impl Into<Color>)` (method, since 0.3.2): Sets the background color.
  - `background_color` impl Into<Color>: Background color. (drawn before the sprite, not painted if alpha is 0)
- `pub fn get_background_color(&self) -> Color` (method, since 0.3.2): Returns the background color.
  - returns Color: Background color. (default is white with alpha 0)
- `pub fn get_sprite(&self) -> &Sprite` (method, since 0.3.2): Returns the embedded Sprite. (when fine control is needed)
  - returns &Sprite: Reference to the contained Sprite.
- `pub fn get_sprite_mut(&mut self) -> &mut Sprite` (method, since 0.3.2): Returns the contained Sprite. (mutable)
  - returns &mut Sprite: Mutable reference to the embedded Sprite.
- `pub fn set_image(&mut self, image: Option<Rc<Image>>)` (method, since 0.3.2): Sets the image.
  - `image` Option<Rc<Image>>: Image to show. None clears it.
- `pub fn set_image_asset(&mut self, image_asset: &mut ImageAsset)` (method, since 0.3.2): Sets the image from an image asset. (clears it if not loaded yet)
  - `image_asset` &mut ImageAsset: Asset to take the image from. (the image becomes None if not fully loaded or decoding failed)
- `pub fn get_image(&self) -> Option<&Rc<Image>>` (method, since 0.3.2): Returns the image.
  - returns Option<&Rc<Image>>: The set image. None if none.
- `pub fn set_image_rect(&mut self, image_rect: Rect)` (method, since 0.3.2): Sets the image sub-area.
  - `image_rect` Rect: Area of the image to draw. (pixels, the whole image if an empty rectangle. not used in the Sliced draw mode)
- `pub fn set_color(&mut self, color: impl Into<Color>)` (method, since 0.3.2): Sets the color. (sprite tint)
  - `color` impl Into<Color>: Sprite color. (with an image, a tint that covers the inside of the image silhouette when alpha is greater than 0, otherwise the fill color)
- `pub fn get_color(&self) -> Color` (method, since 0.3.2): Returns the color.
  - returns Color: Sprite color.
- `pub fn set_horizontal_flip(&mut self, is_horizontal_flip: bool)` (method, since 0.3.2): Sets the horizontal flip.
  - `is_horizontal_flip` bool: If true, draws flipped horizontally.
- `pub fn set_vertical_flip(&mut self, is_vertical_flip: bool)` (method, since 0.3.2): Sets the vertical flip.
  - `is_vertical_flip` bool: If true, draws flipped vertically.
- `pub fn set_draw_mode(&mut self, sprite_draw_mode: SpriteDrawMode)` (method, since 0.3.2): Sets the draw mode. (simple / sliced / tiled)
  - `sprite_draw_mode` SpriteDrawMode: Draw mode. (Simple, Sliced, Tiled)
- `pub fn set_blend_mode(&mut self, sprite_blend_mode: SpriteBlendMode)` (method, since 0.3.2): Sets the blend mode.
  - `sprite_blend_mode` SpriteBlendMode: Blend mode.
- `pub fn set_nine_slice(&mut self, nine_slice: Rect)` (method, since 0.3.2): Sets the nine-slice region.
  - `nine_slice` Rect: Nine-slice border. (position.x, position.y, size.x, size.y are the left, top, right, bottom thickness, pixels. used when the draw mode is Sliced, not drawn if the node's content size is smaller than the sum of the borders)
- `pub fn set_image_path(&mut self, image_path: &str)` (method, since 0.4.0): Sets the image path. (original imageView.imagePath, written and read by the UI document)
  - `image_path` &str: Image path. (does not load the image, held separately from the embedded Sprite's path)
- `pub fn get_image_path(&self) -> &str` (method, since 0.4.0): Returns the image path.
  - returns &str: Image path. (empty string if not set)
