# misc/floatingtext (vanilla-engine 0.4.0)

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

Floating text manager component. (damage numbers / +coins, etc.)
- A port of the original misc/floatingtext.js. Text spawned with spawn rises and fades out, and finished label nodes are reused.
- Create one node under the scene root and spawn in world coordinates.
- Usage:
  ```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() });
  ```
- Differences from the original:
  The original is a node that inherits WorldNode. The Rust version is a component attached to a node, and create makes the node and attaches it.
  spawn / clear are associated functions that take the tree and a node handle because they must create and change label nodes. (they do nothing if the component is missing)
  The options object is the FloatingTextOptions struct.

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

Spawn settings. (original options = { color?, font_size = 22, rise = 46, duration = 0.8, jitter = 10, bold = true })

Example: Change only the color and height from the defaults

```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): Text color. (default white)
  - returns Color
- `pub font_size: f64` (field, since 0.3.2): Font size. (pixels, default 22)
  - returns f64
- `pub rise: f64` (field, since 0.3.2): Rise height. (pixels)
  - returns f64
- `pub duration: f64` (field, since 0.3.2): Time until it disappears. (seconds)
  - returns f64
- `pub jitter: f64` (field, since 0.3.2): Horizontal scatter width. (±pixels)
  - returns f64
- `pub is_bold: bool` (field, since 0.3.2): Whether to draw bold. (default true)
  - returns bool

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

Floating text manager component.
- A spawned label rises by rise while decelerating and fades over duration. Finished label nodes are turned off and reused by the next spawn.

Example: Spawn a damage number

```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): Constructor.
  - A component not attached to a node. Usually use create, which also makes the node and attaches it.
  - returns FloatingText: A component with no floating items.
- `pub fn create(node_tree: &mut NodeTree, parent_id: NodeId) -> NodeId` (static, since 0.3.2): Creates the node. (creates a "FloatingText" node under parent_id and attaches the component; the original new FloatingText() + add_child)
  - Like the original constructor, the node's pivot and anchor are top-left. (spawn's world position is relative to the parent's top-left)
  - `node_tree` &mut NodeTree: Tree to create the node in.
  - `parent_id` NodeId: Parent node. (usually the scene root)
  - returns NodeId: The created node. (pass it to 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): Spawns text. (reuses an idle label node if any, otherwise creates one. Returns the spawned label node)
  - A new label node is a child of the FloatingText node, with a center pivot, 240x40, and a Text component.
  - `node_tree` &mut NodeTree: Tree to create or reuse label nodes in.
  - `floating_text_node_id` NodeId: Node with the FloatingText component attached. (the one returned by create)
  - `text` &str: Text to show.
  - `world_position` Vector2: Spawn position. (local coordinates within the FloatingText node, scattered randomly within ±jitter horizontally)
  - `options` FloatingTextOptions: Color, size, height, duration, scatter, bold.
  - returns Option<NodeId>: The spawned label node. None if the node or the FloatingText component is missing.
- `pub fn get_active_count(&self) -> usize` (method, since 0.3.2): Returns the number of floating items.
  - returns usize: Number of labels still rising.
- `pub fn clear(node_tree: &mut NodeTree, floating_text_node_id: NodeId)` (static, since 0.3.2): Clears all. (turns off the floating labels and returns them to the idle list)
  - Does not delete nodes. Labels deleted from outside are only removed from the list. Does nothing if the component is missing.
  - `node_tree` &mut NodeTree: Tree containing the label nodes.
  - `floating_text_node_id` NodeId: Node with the FloatingText component attached.
