# ui/uitoast (vanilla-engine 0.4.0)

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

토스트. (원본 ui/uitoast.js 의 UIToast)
- show(message) 로 알약 모양 알림이 아래에서 떠올라(0.24 초) 잠시 머문 뒤(기본 1.8 초) 위로 빠지며 사라진다(0.2 초).
- 여러 번 부르면 줄을 세워 하나씩 보여 준다.
- 씬 뿌리의 맨 위(마지막 자식)에 두고 쓴다. 가로 자리를 정하지 않으면 부모 가로 가운데에 선다. (부모 크기를 모르면 480)
- 원본은 WorldNode 를 상속한 노드이고 노드 자신이 알약이다. 러스트 판은 노드(호스트)에 붙는 컴포넌트이고, 붙을 때 원본 생성자처럼
  호스트를 꾸미고(가운데 피봇, 크기) Paint(둥근 알약)와 UILabel 을 호스트에 붙인다. UIToast::create(node_tree, parent_id, options) 가 new + add_child 자리다.
- 사용:
  ```text
    let toast_id = UIToast::create(node_tree, root_id, UIToastOptions::default());
    node_tree[toast_id].get_component_mut::<UIToast>().unwrap().show("저장되었습니다.");
  ```
- 원본과 다른 것:
  - show 는 줄에 넣기만 하고, 꺼내서 보이는 것은 다음 tick 이 한다. (같은 tick 에 첫 걸음도 가므로 시간 흐름은 원본과 같다)
    그래서 원본은 숨을 때 set_active(false) 하지만 러스트 판은 tick 이 돌아야 해서 set_visible(false) 로 숨긴다.
    UI 문서는 원본처럼 숨은 토스트를 active false 로 쓰고 읽는다. (uidocument.rs 의 UIDocumentNode)
  - 등장 / 머묾 / 퇴장 시간과 곡선은 원본 그대로 이 안에서 센다. (Tween 은 쓰지 않는다)
  - 가로 자리 rest_x 는 `Option<f64>` 다. (원본 null = 부모 가운데) options 객체는 UIToastOptions 구조체다.

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

토스트 설정. (원본 options = { width = 400, height = 48, rest_y = 560, rest_x = null, hold_seconds = 1.8, font_size = 15, background_color?, text_color? })

예제: 위쪽에 오래 머무는 좁은 토스트

```rust
use vanilla::{NodeTree, UIToast, UIToastOptions, Vector2};

let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let options = UIToastOptions {
	width: 320.0,
	rest_y: 80.0,
	hold_seconds: 3.0,
	..UIToastOptions::default()
};
let toast_id = UIToast::create(&mut node_tree, root_id, options);
assert_eq!(node_tree[toast_id].get_content_size(), Vector2::create(320.0, 48.0));
assert!(!node_tree[toast_id].is_visible());
```

- `pub width: f64` (field, since 0.3.2): 알약 가로. (기본 400)
  - returns f64
- `pub height: f64` (field, since 0.3.2): 알약 세로. (기본 48, 둥근 정도는 그 반)
  - returns f64
- `pub rest_y: f64` (field, since 0.3.2): 머무는 자리의 가운데 세로 좌표.
  - returns f64
- `pub rest_x: Option<f64>` (field, since 0.3.2): 머무는 자리의 가운데 가로 좌표. (None 이면 부모 가로 가운데)
  - returns Option<f64>
- `pub hold_seconds: f64` (field, since 0.3.2): 머무는 시간. (초)
  - returns f64
- `pub font_size: f64` (field, since 0.3.2): 글 크기. (기본 15)
  - returns f64
- `pub background_color: Color` (field, since 0.3.2): 알약 색.
  - returns Color
- `pub text_color: Color` (field, since 0.3.2): 글 색.
  - returns Color

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

토스트.

예제: 저장 알림 띄우기

```rust
use vanilla::{Engine, EngineConfiguration, NodeTree, UILabel, UIToast, UIToastOptions, Vector2};

let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
node_tree[root_id].set_content_size(Vector2::create(800.0, 600.0));
let toast_id = UIToast::create(&mut node_tree, root_id, UIToastOptions::default());
node_tree[toast_id].get_component_mut::<UIToast>().expect("toast").show("Saved.");
node_tree.tick(&engine, 0.24);
assert!(node_tree[toast_id].is_visible());
assert_eq!(node_tree[toast_id].get_component::<UILabel>().expect("label").get_text(), "Saved.");
assert_eq!(node_tree[toast_id].get_local_position(), Vector2::create(400.0, 560.0));
```

- `pub fn new() -> UIToast` (constructor, since 0.3.2): 생성. (기본 설정)
  - returns UIToast: 붙기 전의 토스트. (UIToastOptions::default)
- `pub fn create_with_options(options: UIToastOptions) -> UIToast` (static, since 0.3.2): 설정으로 생성. (노드에는 node_tree.add_component 로 붙인다)
  - `options` UIToastOptions: 토스트 설정.
  - returns UIToast: 붙기 전의 토스트.
- `pub fn create(node_tree: &mut NodeTree, parent_id: NodeId, options: UIToastOptions) -> NodeId` (static, since 0.3.2): 노드 만들기. (parent_id 밑에 "UIToast" 노드를 만들어 붙인다. 원본 new UIToast(options) + add_child)
  - `node_tree` &mut NodeTree: 노드 트리.
  - `parent_id` NodeId: 부모 노드. (보통 씬 뿌리, 가로 자리를 정하지 않으면 그 가로 가운데에 선다)
  - `options` UIToastOptions: 토스트 설정.
  - returns NodeId: 만든 토스트 노드. (보이기 전이라 숨어 있다)
- `pub fn show(&mut self, message_text: &str)` (method, since 0.3.2): 토스트 표시. (줄에 넣는다. 보이는 중이면 차례를 기다린다)
  - `message_text` &str: 보일 글. (다음 tick 부터 보인다)

  예제: 두 번 부르면 앞의 것이 사라진 뒤 다음 것이 나온다

  ```rust
  use vanilla::{Engine, EngineConfiguration, NodeTree, UILabel, UIToast, UIToastOptions};
  
  let engine = Engine::new(EngineConfiguration::new());
  let mut node_tree = NodeTree::new();
  let root_id = node_tree.get_root_id();
  let toast_id = UIToast::create(&mut node_tree, root_id, UIToastOptions::default());
  if let Some(toast) = node_tree[toast_id].get_component_mut::<UIToast>() {
  	toast.show("First");
  	toast.show("Second");
  }
  node_tree.tick(&engine, 0.24);
  node_tree.tick(&engine, 1.8);
  node_tree.tick(&engine, 0.2);
  assert_eq!(node_tree[toast_id].get_component::<UILabel>().expect("label").get_text(), "Second");
  ```

- `pub fn set_rest_y(&mut self, rest_y: f64)` (method, since 0.3.2): 머무는 자리(가운데 세로 좌표) 설정.
  - `rest_y` f64: 머무는 자리의 가운데 세로 좌표. (부모 로컬 좌표, 등장할 때는 28 아래에서 떠오른다, 다음 자리 셈부터)
- `pub fn set_rest_x(&mut self, rest_x: Option<f64>)` (method, since 0.3.2): 머무는 자리(가운데 가로 좌표) 설정. (None 이면 부모 가로 가운데)
  - `rest_x` Option<f64>: 머무는 자리의 가운데 가로 좌표. (부모 로컬 좌표, None 이면 부모 가로 가운데, 부모 크기를 모르면 480)
- `pub fn set_hold_seconds(&mut self, hold_seconds: f64)` (method, since 0.3.2): 머무는 시간 설정. (초)
  - `hold_seconds` f64: 다 떠오른 뒤 머무는 시간. (초, 기본 1.8)
- `pub fn is_showing(&self) -> bool` (method, since 0.3.2): 보이는 중인지. (줄에 남은 메시지가 있어도 참)
  - returns bool: 등장, 머묾, 퇴장 중이거나 줄에 기다리는 메시지가 있으면 true.
