# misc/floatingtext (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/misc/floatingtext/

떠오르는 텍스트 관리 컴포넌트. (데미지 숫자 / +코인 등)
- 원본 misc/floatingtext.js 를 옮긴 것이다. spawn 으로 띄우면 위로 떠오르며 사라지고, 끝난 라벨 노드는 다시 쓴다.
- 씬 뿌리 밑에 노드 하나를 만들어 두고 월드 좌표로 띄운다.
- 사용:
  ```text
    let floating_text_id = FloatingText::create(node_tree, root_id);
    FloatingText::spawn(node_tree, floating_text_id, "-120", position, FloatingTextOptions { color: Color::create(1.0, 0.4, 0.4, 1.0), ..FloatingTextOptions::default() });
  ```
- 원본과 다른 점:
  원본은 WorldNode 를 상속한 노드다. 러스트 판은 노드에 붙는 컴포넌트이고, create 가 노드를 만들어 붙여 준다.
  spawn / clear 는 라벨 노드를 만들고 바꿔야 해서 트리와 노드 핸들을 받는 연관 함수다. (컴포넌트가 없으면 아무것도 하지 않는다)
  options 객체는 FloatingTextOptions 구조체다.

## FloatingTextOptions (struct) (Clone, Copy, Debug, PartialEq, Default)

띄우기 설정. (원본 options = { color?, font_size = 22, rise = 46, duration = 0.8, jitter = 10, bold = true })

예제: 기본값에서 색과 높이만 바꾸기

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

let options = FloatingTextOptions {
	color: Color::create(1.0, 0.4, 0.4, 1.0),
	rise: 60.0,
	..FloatingTextOptions::default()
};
assert_eq!(options.font_size, 22.0);
assert_eq!(options.duration, 0.8);
assert!(options.is_bold);
```

- `pub color: Color` (field, since 0.3.2): 글자 색. (기본 흰색)
  - returns Color
- `pub font_size: f64` (field, since 0.3.2): 글자 크기. (픽셀, 기본 22)
  - returns f64
- `pub rise: f64` (field, since 0.3.2): 떠오르는 높이. (픽셀)
  - returns f64
- `pub duration: f64` (field, since 0.3.2): 사라질 때까지 걸리는 시간. (초)
  - returns f64
- `pub jitter: f64` (field, since 0.3.2): 가로로 흩뿌리는 폭. (±픽셀)
  - returns f64
- `pub is_bold: bool` (field, since 0.3.2): 굵게 그릴지. (기본 true)
  - returns bool

## FloatingText (struct) (Default, Component, UIDocumentNode)

떠오르는 텍스트 관리 컴포넌트.
- 띄운 라벨은 duration 동안 rise 만큼 감속하며 떠오르고 흐려진다. 다 끝난 라벨 노드는 꺼 두었다가 다음 spawn 이 다시 쓴다.

예제: 데미지 숫자 띄우기

```rust
use vanilla::{Color, Engine, EngineConfiguration, FloatingText, FloatingTextOptions, NodeTree, UILabel, Vector2};

let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let floating_text_id = FloatingText::create(&mut node_tree, root_id);
let options = FloatingTextOptions {
	color: Color::create(1.0, 0.4, 0.4, 1.0),
	..FloatingTextOptions::default()
};
let label_id = FloatingText::spawn(&mut node_tree, floating_text_id, "-120", Vector2::create(100.0, 200.0), options).unwrap();
assert_eq!(node_tree[label_id].get_component::<UILabel>().unwrap().get_text(), "-120");

node_tree.tick(&engine, 1.0);
assert!(!node_tree[label_id].is_active());
assert_eq!(node_tree[floating_text_id].get_component::<FloatingText>().unwrap().get_active_count(), 0);
```

- `pub fn new() -> FloatingText` (constructor, since 0.3.2): 생성.
  - 노드에 붙이지 않은 컴포넌트다. 보통은 노드까지 만들어 붙이는 create 를 쓴다.
  - returns FloatingText: 떠 있는 항목이 없는 컴포넌트.
- `pub fn create(node_tree: &mut NodeTree, parent_id: NodeId) -> NodeId` (static, since 0.3.2): 노드 만들기. (parent_id 밑에 "FloatingText" 노드를 만들고 컴포넌트를 붙인다. 원본 new FloatingText() + add_child)
  - 원본 생성자처럼 노드의 피벗과 앵커는 왼쪽 위다. (spawn 의 월드 좌표가 부모 왼쪽 위 기준)
  - `node_tree` &mut NodeTree: 노드를 만들 트리.
  - `parent_id` NodeId: 부모 노드. (보통 씬 뿌리)
  - returns NodeId: 만든 노드. (spawn, clear 에 넘긴다)
- `pub fn spawn(node_tree: &mut NodeTree, floating_text_node_id: NodeId, text: &str, world_position: Vector2, options: FloatingTextOptions) -> Option<NodeId>` (static, since 0.3.2): 텍스트 띄우기. (쉬는 라벨 노드가 있으면 다시 쓰고, 없으면 만든다. 띄운 라벨 노드를 돌려준다)
  - 새 라벨 노드는 FloatingText 노드의 자식이고, 가운데 피봇, 240x40, Text 컴포넌트를 단다.
  - `node_tree` &mut NodeTree: 라벨 노드를 만들거나 다시 쓸 트리.
  - `floating_text_node_id` NodeId: FloatingText 컴포넌트가 붙은 노드. (create 가 돌려준 것)
  - `text` &str: 띄울 글.
  - `world_position` Vector2: 띄울 자리. (FloatingText 노드 안의 지역 좌표, 가로는 ±jitter 안에서 무작위로 흩뿌린다)
  - `options` FloatingTextOptions: 색, 크기, 높이, 시간, 흩뿌림, 굵게.
  - returns Option<NodeId>: 띄운 라벨 노드. 노드가 없거나 FloatingText 컴포넌트가 없으면 None.
- `pub fn get_active_count(&self) -> usize` (method, since 0.3.2): 떠 있는 항목 수 반환.
  - returns usize: 아직 떠오르는 중인 라벨 수.
- `pub fn clear(node_tree: &mut NodeTree, floating_text_node_id: NodeId)` (static, since 0.3.2): 모두 지우기. (떠 있는 라벨을 끄고 쉬는 목록으로 돌린다)
  - 노드를 지우지 않는다. 밖에서 지운 라벨은 목록에서만 뺀다. 컴포넌트가 없으면 아무것도 하지 않는다.
  - `node_tree` &mut NodeTree: 라벨 노드가 든 트리.
  - `floating_text_node_id` NodeId: FloatingText 컴포넌트가 붙은 노드.
