vanilladocs
vanillapunch.com
API / core/component/sprite

core/component/sprite

스프라이트 출력자 컴포넌트.

  • 이미지가 없으면 Paint 처럼 색 사각형을 그린다. (원본은 Paint 를 상속, 여기서는 Paint 를 품는다)
  • 색의 알파가 0 보다 크면 이미지 실루엣 안쪽에만 그 색을 덮는다. (틴트)
  • 그림 경로(image_path)는 원본이 sprite.imagePath 로 덧붙여 쓰는 값이다. UI 문서(UIDocument)가 그림 대신 이 경로를 적고 읽는다. 그리기에는 쓰지 않는다.

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.

SpriteBlendModetype

짝Counterpart: vanilla.js SpriteBlendMode소스source

원본의 SpriteBlendMode 는 Graphic 의 블렌드 모드와 같은 값이다.

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

pub type SpriteBlendMode = BlendMode

Spritestruct traits Default, Component

짝Counterpart: vanilla.js Sprite소스source

스프라이트 출력자 컴포넌트. (그림을 노드 content_size 에 그린다)

  • 그림이 없으면 Paint 처럼 색 사각형을 칠한다. 그림이 있고 색의 알파가 0 보다 크면 그림 실루엣 안쪽을 그 색으로 덮는다. (틴트)
  • 출력 방식(Simple, Sliced, Tiled), 블렌드 모드, 좌우 / 상하 뒤집기를 고른다.
  • 처음은 그림 없음, 색 투명, Simple, Normal, 뒤집기 없음, 영역과 나인슬라이스는 빈 사각형.

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
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
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"));
생성자constructorpub fn new() -> Sprite#0.3.2 부터since 0.3.2소스source

생성.

Constructor.

반환ReturnsSprite 그림이 없고 색이 투명인 새 컴포넌트. (Simple, Normal)New component with no image and a transparent color. (Simple, Normal)

짝Counterpart: vanilla.js Sprite.constructor

정적staticpub fn create_with_image(image: Rc<Image>) -> Sprite#0.3.2 부터since 0.3.2소스source

이미지와 함께 생성.

Constructor with an image.

imageRc<Image>
그릴 그림.Image to draw.

반환ReturnsSprite 그 그림 전체를 그리는 새 컴포넌트. (영역은 빈 사각형이라 그림 전체)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
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());
메서드methodpub fn set_sprite_draw_mode(&mut self, sprite_draw_mode: SpriteDrawMode)#0.3.2 부터since 0.3.2소스source

출력 방식 설정.

Sets the draw mode.

sprite_draw_modeSpriteDrawMode
출력 방식. (처음 Simple)Draw mode. (initially Simple)

짝Counterpart: vanilla.js Sprite.setSpriteDrawMode

메서드methodpub fn get_sprite_draw_mode(&self) -> SpriteDrawMode#0.3.2 부터since 0.3.2소스source

출력 방식 반환.

Returns the draw mode.

반환ReturnsSpriteDrawMode 출력 방식.Draw mode.

짝Counterpart: vanilla.js Sprite.getSpriteDrawMode

메서드methodpub fn set_sprite_blend_mode(&mut self, sprite_blend_mode: SpriteBlendMode)#0.3.2 부터since 0.3.2소스source

블렌드 모드 설정.

Sets the blend mode.

sprite_blend_modeSpriteBlendMode
그림을 그릴 때 걸 블렌드 모드. (처음 Normal, 그린 뒤 Normal 로 되돌린다, 그림이 없을 때는 쓰지 않는다)Blend mode applied when drawing the image. (initially Normal, restored to Normal after drawing, not used when there is no image)

짝Counterpart: vanilla.js Sprite.setSpriteBlendMode

메서드methodpub fn get_sprite_blend_mode(&self) -> SpriteBlendMode#0.3.2 부터since 0.3.2소스source

블렌드 모드 반환.

Returns the blend mode.

반환ReturnsSpriteBlendMode 그림을 그릴 때 거는 블렌드 모드.Blend mode applied when drawing the image.

짝Counterpart: vanilla.js Sprite.getSpriteBlendMode

메서드methodpub fn set_nine_slice(&mut self, nine_slice: Rect)#0.3.2 부터since 0.3.2소스source

나인슬라이스 영역 설정. (position.x / position.y / size.x / size.y = 왼쪽 / 위 / 오른쪽 / 아래)

Sets the nine-slice area. (position.x / position.y / size.x / size.y = left / top / right / bottom)

nine_sliceRect
그림 가장자리에서 늘이지 않을 두께. (픽셀, Sliced 일 때만 쓴다)Thickness from the image edges that is not stretched. (pixels, used only for Sliced)

짝Counterpart: vanilla.js Sprite.setNineSlice

예제Example가장자리 16 픽셀을 지키는 판A panel that preserves 16 pixels at the edges
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);
메서드methodpub fn get_nine_slice(&self) -> Rect#0.3.2 부터since 0.3.2소스source

나인슬라이스 영역 반환.

Returns the nine-slice region.

반환ReturnsRect 왼쪽 / 위 / 오른쪽 / 아래 두께. (position.x / position.y / size.x / size.y, 픽셀)Left / top / right / bottom thickness. (position.x / position.y / size.x / size.y, pixels)

짝Counterpart: vanilla.js Sprite.getNineSlice

메서드methodpub fn set_image(&mut self, image: Option<Rc<Image>>)#0.3.2 부터since 0.3.2소스source

이미지 설정.

Sets the image.

imageOption<Rc<Image>>
그릴 그림. (None 이면 색 사각형을 칠한다, 영역은 그대로 둔다)Image to draw. (paints a color rectangle if None, the area is left as is)

짝Counterpart: vanilla.js Sprite.setImage

메서드methodpub fn set_frame(&mut self, frame: Option<&Frame>)#0.4.0 부터since 0.4.0소스source

조각 걸기. (Frame 의 그림과 그 안의 영역을 함께 건다, None 이면 그림을 비운다, 원본 setFrame, SpriteAtlas.read_frame)

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)

frameOption<&Frame>
걸 조각. (None 이면 그림만 비우고 영역은 그대로 둔다)Frame to set. (if None, clears only the image and leaves the area as is)

짝Counterpart: vanilla.js Sprite.setFrame

예제Example시트의 둘째 칸 걸기Set the second cell of a sheet
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());
메서드methodpub fn get_image(&self) -> Option<&Rc<Image>>#0.3.2 부터since 0.3.2소스source

이미지 반환.

Returns the image.

반환ReturnsOption<&Rc<Image>> 그리는 그림. 없으면 None.The image being drawn. None if none.

짝Counterpart: vanilla.js Sprite.getImage

메서드methodpub fn set_image_path(&mut self, image_path: &str)#0.4.0 부터since 0.4.0소스source

그림 경로 설정. (원본 sprite.imagePath, UI 문서가 적고 읽는다)

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)

짝Counterpart: vanilla.js Sprite.setImagePath

메서드methodpub fn get_image_path(&self) -> &str#0.4.0 부터since 0.4.0소스source

그림 경로 반환. (정하지 않았으면 빈 글)

Returns the image path. (empty string if not set)

반환Returns&str 그림 경로. (정하지 않았으면 빈 글)Image path. (empty string if not set)

짝Counterpart: vanilla.js Sprite.getImagePath

메서드methodpub fn set_image_rect(&mut self, image_rect: Rect)#0.3.2 부터since 0.3.2소스source

이미지 영역 설정. (비우면 전체)

Sets the image area. (the whole image if empty)

image_rectRect
그림 안의 영역. (픽셀, 그림 왼쪽 위 기준, Rect::zero() 면 그림 전체, Sliced 는 쓰지 않는다)Area within the image. (pixels, relative to the image's top left, the whole image if Rect::zero(), not used by Sliced)

짝Counterpart: vanilla.js Sprite.setImageRect

메서드methodpub fn get_image_rect(&self) -> Rect#0.3.2 부터since 0.3.2소스source

이미지 영역 반환.

Returns the image region.

반환ReturnsRect 그림 안의 영역. (픽셀, 빈 사각형이면 그림 전체)Area within the image. (pixels, the whole image if an empty rectangle)

짝Counterpart: vanilla.js Sprite.getImageRect

메서드methodpub fn set_horizontal_flip(&mut self, flip: bool)#0.3.2 부터since 0.3.2소스source

좌우 뒤집기 설정.

Sets the horizontal flip.

flipbool
좌우로 뒤집어 그릴지. (처음 false)Whether to draw flipped horizontally. (initially false)

짝Counterpart: vanilla.js Sprite.setHorizontalFlip

메서드methodpub fn set_vertical_flip(&mut self, flip: bool)#0.3.2 부터since 0.3.2소스source

상하 뒤집기 설정.

Sets the vertical flip.

flipbool
상하로 뒤집어 그릴지. (처음 false)Whether to draw flipped vertically. (initially false)

짝Counterpart: vanilla.js Sprite.setVerticalFlip

메서드methodpub fn is_horizontal_flip(&self) -> bool#0.3.2 부터since 0.3.2소스source

좌우 뒤집기 여부.

Whether flipped horizontally.

반환Returnsbool 좌우로 뒤집어 그리면 true.true if drawn flipped horizontally.

짝Counterpart: vanilla.js Sprite.isHorizontalFlip

메서드methodpub fn is_vertical_flip(&self) -> bool#0.3.2 부터since 0.3.2소스source

상하 뒤집기 여부.

Whether flipped vertically.

반환Returnsbool 상하로 뒤집어 그리면 true.true if drawn flipped vertically.

짝Counterpart: vanilla.js Sprite.isVerticalFlip

메서드methodpub fn get_flip(&self) -> Vector2#0.3.2 부터since 0.3.2소스source

뒤집히는 값. (뒤집힌 축은 -1)

Flip values. (-1 on flipped axes)

반환ReturnsVector2 축마다 뒤집혔으면 -1, 아니면 1 인 값. (x 는 좌우, y 는 상하, 그릴 크기에 곱한다)Value that is -1 on each flipped axis and 1 otherwise. (x is horizontal, y is vertical, multiplied by the draw size)

짝Counterpart: vanilla.js Sprite.getFlip

예제Example상하만 뒤집기Flip vertically only
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));
메서드methodpub fn set_color(&mut self, color: impl Into<Color>)#0.3.2 부터since 0.3.2소스source

색상 설정. (이미지가 있으면 틴트, 없으면 채우기 색)

Sets the color. (a tint if there is an image, otherwise the fill color)

colorimpl Into<Color>
틴트 또는 채우기 색. (Color 또는 "#rrggbb" 같은 문자열, 처음 투명, 틴트는 알파가 0 보다 클 때만 건다)Tint or fill color. (a Color or a string such as "#rrggbb", initially transparent, the tint applies only when alpha is greater than 0)

짝Counterpart: vanilla.js Paint.setColor

메서드methodpub fn get_color(&self) -> Color#0.3.2 부터since 0.3.2소스source

색상 반환.

Returns the color.

반환ReturnsColor 틴트 또는 채우기 색.Tint or fill color.

짝Counterpart: vanilla.js Paint.getColor

메서드methodpub fn set_round_size(&mut self, round_size: f64)#0.4.0 부터since 0.4.0소스source

둥근 모서리 크기 설정. (이미지가 없을 때 칠하는 사각형, 원본은 Paint 를 상속해 같은 함수를 쓴다)

Sets the round corner size. (for the rectangle painted when there is no image, the original inherits Paint and uses the same function)

round_sizef64
귀퉁이 둥근 크기. (노드 좌표, 0 이하면 곧은 사각형, 처음 0)Corner round size. (node coordinates, a sharp rectangle if 0 or less, initially 0)

짝Counterpart: vanilla.js Paint.setRoundSize

메서드methodpub fn get_round_size(&self) -> f64#0.4.0 부터since 0.4.0소스source

둥근 모서리 크기 반환.

Returns the round corner size.

반환Returnsf64 그림이 없을 때 칠하는 사각형의 귀퉁이 둥근 크기. (노드 좌표)Corner round size of the rectangle painted when there is no image. (node coordinates)

짝Counterpart: vanilla.js Paint.getRoundSize

SpriteDrawModeenum traits Clone, Copy, Debug, PartialEq, Eq

짝Counterpart: vanilla.js SpriteDrawMode소스source

스프라이트 출력 방식.

Sprite draw mode.

이름Name설명Description
Simple그림(image_rect 조각, 비면 그림 전체)을 노드 content_size 에 늘려 그린다.Draws the image (the image_rect piece, or the whole image if empty) stretched to the node's content_size.
Sliced나인슬라이스. 네 귀퉁이는 그대로 두고 변과 가운데를 늘려 그림 전체를 content_size 에 그린다. (image_rect 는 쓰지 않는다, content_size 가 왼쪽 + 오른쪽이나 위 + 아래보다 작으면 그리지 않는다)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)
Tiledimage_rect 조각(비면 그림 전체)을 제 크기로 content_size 영역에 되풀이해 깔고, 영역 밖은 자른다.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.