# ui/toastmotion (vanilla-engine 0.4.0)

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

알림이 나오고 사라지는 모션. (vanilla.js ui/toastmotion.js 의 ToastMotion, ToastPhase, readToastSeconds, attachToastMotion)
- 알림 노드에 붙여 show 로 띄운다. 바깥쪽(위 알림은 위, 아래 알림은 아래)에서 미끄러져 들어오며 보이고 살짝 커졌다 돌아오고,
  머무는 시간이 지나면 떠오르며 사라진다.
- 떠 있는 동안 다른 글이 오면 앞 것이 빨리 비키고 새 글이 들어오며, 같은 글이 또 오면 시간만 다시 채우고 통 튄다.
- 노드의 자리는 놓인 자리를 기준으로 그 둘레에서만 움직인다. (그 사이 다시 놓으면 그 자리가 새 기준)
- 원본과 다른 것: show 는 요청만 받아 두고 다음 tick 에서 노드를 움직인다. (장면 코드는 노드 문맥 없이 부르므로)
  글은 show 에 준 함수가 나오기 시작할 때 노드에 넣는다. (원본 show(text, seconds, applyText) 와 같다) 숨은 동안 노드는 끄지 않고 보이지 않게만 한다. (tick 이 계속 돌게)
- UIToast 는 줄 세운 알약 알림을 스스로 만들고, 이것은 게임이 만든 알림 노드를 움직인다.

## ToastApplyText (type)

노드에 글과 색을 넣는 함수. (원본 show 의 applyText, 나오기 시작할 때 한 번 부른다, 같은 노드의 UILabel 같은 컴포넌트는 문맥의 노드에서 찾는다)

```rust
pub type ToastApplyText = Box<dyn FnOnce(&mut ComponentContext)>
```

## ToastMotion (struct) (Component)

알림 모션. (게임이 만든 알림 노드에 붙어, show 로 띄우면 나오고 머물다 사라지는 움직임을 맡는다)

예제: 게임이 만든 알림 노드 띄우기

```rust
use vanilla::{attach_toast_motion, ComponentContext, Engine, EngineConfiguration, NodeTree, Text, ToastMotion, ToastPhase, Vector2};

let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let toast_id = node_tree.create_child(root_id, "Toast");
node_tree[toast_id].set_local_position(Vector2::create(640.0, 80.0));
node_tree[toast_id].set_content_size(Vector2::create(320.0, 48.0));
node_tree[toast_id].add_component(Text::create_with_text("", 24.0));
node_tree[toast_id].set_visible(false);
let toast_motion = attach_toast_motion(&mut node_tree, toast_id, -1.0).expect("toast motion");
toast_motion.show("Saved", 1.0, |context: &mut ComponentContext| {
	if let Some(text_component) = context.get_node_mut().get_component_mut::<Text>() {
		text_component.set_text("Saved");
	}
});
assert!(toast_motion.is_showing());

node_tree.tick(&engine, 0.016);
assert!(node_tree[toast_id].is_visible());
assert_eq!(node_tree[toast_id].get_component::<Text>().expect("text").get_text(), "Saved");
let toast_motion = node_tree[toast_id].get_component::<ToastMotion>().expect("toast motion");
assert_eq!(toast_motion.get_phase(), ToastPhase::Enter);

for _frame_index in 0..120 {
	node_tree.tick(&engine, 1.0 / 60.0);
}
assert!(!node_tree[toast_id].is_visible());
assert_eq!(node_tree[toast_id].get_local_position(), Vector2::create(640.0, 80.0));
```

- `pub fn new() -> ToastMotion` (constructor, since 0.4.0): 생성. (원본 new ToastMotion(), 위에서 들어온다, 들어오는 쪽은 set_enter_side 로 바꾼다)
  - returns ToastMotion: 숨은 새 모션. (노드에 붙일 때는 attach_toast_motion 을 쓴다)
- `pub fn show(&mut self, text: &str, seconds: f64, apply_text: impl FnOnce(&mut ComponentContext) + 'static)` (method, since 0.4.0): 띄우기 요청. (다음 tick 에서 띄운다, 떠 있는 다른 글은 빨리 비킨 뒤 띄운다)
  - `text` &str: 띄울 글. (떠 있는 글과 같고 사라지는 중이 아니면 머무는 시간만 다시 채우고 통 튄다)
  - `seconds` f64: 머무는 시간. (초, 나오는 시간과 사라지는 시간은 빼고, read_toast_seconds 로 글 길이에 맞출 수 있다)
  - `apply_text` impl FnOnce(&mut ComponentContext) + 'static: 노드에 글과 색을 넣는 함수. (나오기 시작할 때 문맥을 받아 한 번 부른다, 원본 applyText)
- `pub fn hide_now(&mut self)` (method, since 0.4.0): 곧바로 숨기기 요청. (판이 바뀔 때)
- `pub fn is_showing(&self) -> bool` (method, since 0.4.0): 떠 있는지. (나오는 중, 머무는 중, 사라지는 중, 띄우기 요청이 있음)
  - returns bool: 숨음 단계가 아니거나 띄우기 요청이 있으면 true.
- `pub fn get_phase(&self) -> ToastPhase` (method, since 0.4.0): 단계 반환.
  - returns ToastPhase: 지금 단계.
- `pub fn set_enter_side(&mut self, enter_side: f64)` (method, since 0.4.0): 들어오는 쪽 설정.
  - `enter_side` f64: 들어오는 쪽. (-1 이면 위에서, 1 이면 아래에서)

## Functions and constants

- `pub fn read_toast_seconds(text: &str, base_seconds: f64) -> f64` (method, since 0.4.0): 알림 머무는 시간. (긴 글이면 20 자마다 0.5 초를 더하고 4 초까지)
  - `text` &str: 알림 글. (글자 수는 char 로 센다)
  - `base_seconds` f64: 기본 머무는 시간. (초)
  - returns f64: 머무는 시간. (초, 20 자를 넘은 글자를 20 자마다 올림해 0.5 초씩 더하고, 4 초를 넘지 않는다. base_seconds 가 4 초보다 길어도 4)

  예제: 글 길이에 따른 머무는 초

  ```rust
  use vanilla::read_toast_seconds;
  
  assert_eq!(read_toast_seconds("Saved", 2.0), 2.0);
  assert_eq!(read_toast_seconds(&"a".repeat(45), 2.0), 3.0);
  assert_eq!(read_toast_seconds(&"a".repeat(400), 2.0), 4.0);
  ```

- `pub fn attach_toast_motion(node_tree: &mut NodeTree, node_id: NodeId, enter_side: f64) -> Option<&mut ToastMotion>` (method, since 0.4.0): 알림 노드에 모션 붙이기. (들어오는 쪽을 정하고 붙인 ToastMotion 을 돌려준다, 원본 attachToastMotion)
  - `node_tree` &mut NodeTree: 노드 트리.
  - `node_id` NodeId: 알림 노드.
  - `enter_side` f64: 들어오는 쪽. (-1 이면 위에서, 1 이면 아래에서)
  - returns Option<&mut ToastMotion>: 붙인 ToastMotion. 노드가 없으면 None.

## ToastPhase (enum) (Clone, Copy, PartialEq, Eq, Debug)

알림의 단계.

- `Hidden`: 숨음. (노드는 끄지 않고 보이지 않게만 둔다)
- `Enter`: 나오는 중. (미끄러져 들어오며 보이고 살짝 커졌다 돌아온다)
- `Stay`: 머무는 중. (머무는 초를 센다)
- `Exit`: 사라지는 중. (떠오르며 투명해진다)
