# ui/toastmotion (vanilla.js 0.4.0)

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

알림이 나오고 사라지는 모습. (토스트 노드에 붙이는 컴포넌트, UIToast 는 줄 세운 알약 알림을 스스로 만들고 이것은 게임이 만든 알림 노드를 움직인다)

## ToastMotion extends Component

알림이 나오고 사라지는 모습. (토스트 노드에 붙이는 컴포넌트, UIToast 는 줄 세운 알약 알림을 스스로 만들고 이것은 게임이 만든 알림 노드를 움직인다)

알림 노드에 붙여 show 로 띄운다. 바깥쪽(위 알림은 위, 아래 알림은 아래)에서 미끄러져 들어오며 보이고 살짝 커졌다 돌아오고,
머무는 시간이 지나면 떠오르며 사라진 뒤 노드를 끈다.
떠 있는 동안 다른 글이 오면 앞 것이 빨리 비키고 새 글이 들어오며, 같은 글이 또 오면 시간만 다시 채우고 통 튄다.
노드의 자리는 화면이 놓은 자리를 기준으로 그 둘레에서만 움직인다. (그 사이 화면이 다시 놓으면 그 자리가 새 기준)

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

```js
import { Engine, EngineConfiguration, UIScene, UILabel, WorldNode, Paint, Color, Pivot, Vector2, attachToastMotion, readToastSeconds } from "./build/vanilla.js";

class ToastScene extends UIScene {
	create() {
		super.create();
		const toastNode = new WorldNode();
		toastNode.setPivot(Pivot.topLeft.clone());
		toastNode.setAnchor(Pivot.topLeft.clone());
		toastNode.setContentSize(Vector2.create(240, 40));
		toastNode.setLocalPosition(Vector2.create(200, 24));
		toastNode.setActive(false);
		const toastPaint = toastNode.addComponent(Paint);
		toastPaint.setColor(new Color(0.08, 0.1, 0.14, 0.95));
		toastPaint.setRoundSize(20);
		const toastLabel = toastNode.addComponent(UILabel);
		const toastMotion = attachToastMotion(toastNode, -1);
		const root = this.getRoot();
		root.addChild(toastNode);

		const text = "Game saved";
		const seconds = readToastSeconds(text, 1.5);
		toastMotion.show(text, seconds, () => {
			toastLabel.setText(text);
		});
	}
}

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
const scene = new ToastScene();
scene.setLoadingMinDurationMs(0);
engine.run(scene);
```

- `constructor()` (constructor, since 0.4.0): 생성.
- `setEnterSide(enterSide)` (method, since 0.4.0): 들어오는 쪽 정하기. (-1 이면 위에서, 1 이면 아래에서)
  - `enterSide` number: 들어오는 쪽. (-1 이면 위에서, 1 이면 아래에서, 들어오는 거리 10 픽셀에 곱한다)
- `show(text, seconds, applyText)` (method, since 0.4.0): 띄우기. (applyText 가 노드에 글과 색을 넣음, 떠 있는 다른 글은 빨리 비킨 뒤 띄움)
  - `text` string: 알림 글. (떠 있는 글과 같으면 머무는 초만 다시 채우고 통 튄다)
  - `seconds` number: 머무는 초.
  - `applyText` function(): void: 노드에 글과 색을 넣는 함수. (나오기 시작할 때 부른다)
- `isShowing()` (method, since 0.4.0): 떠 있는지. (나오는 중, 머무는 중, 사라지는 중)
  - returns boolean: 숨음 단계가 아니면 참.
- `getPhase()` (method, since 0.4.0): 단계 반환. (ToastPhase)
  - returns string: 단계. (ToastPhase 의 값)
- `hideNow()` (method, since 0.4.0): 곧바로 끄기. (판이 바뀔 때)
- `startEnter(text, seconds, applyText)` (method, since 0.4.0): 나오기 시작.
  - `text` string: 알림 글.
  - `seconds` number: 머무는 초.
  - `applyText` function(): void: 노드에 글과 색을 넣는 함수. (곧바로 부른다)
- `startExit(exitLength)` (method, since 0.4.0): 사라지기 시작.
  - `exitLength` number: 사라지는 초. (보통 0.25, 새 알림에 비킬 때 0.12)
- `tick(timeDelta)` (method, since 0.4.0): 갱신. (단계마다 자리와 크기와 불투명도)
  - `timeDelta` number: 지난 프레임부터의 시간. (초)
- `placeAround(node, scale, offsetY, opacity)` (method, since 0.4.0): 화면이 놓은 자리를 기준으로 가운데를 두고 그 크기와 위아래 비킴, 불투명도로 놓기.
  - `node` WorldNode: 알림 노드.
  - `scale` number: 배율. (1 이 보통 크기, 가운데를 두고 키운다)
  - `offsetY` number: 기준 자리에서 세로로 비킬 거리. (픽셀, 아래가 +)
  - `opacity` number: 불투명도. (0 ~ 1)

## Functions and constants

- `readToastSeconds(text, baseSeconds)` (method, since 0.4.0): 알림 머무는 시간. (표의 시간에 긴 글이면 LONG_TEXT_CHARACTERS 자마다 LONG_TEXT_EXTRA_SECONDS 를 더하고 MAXIMUM_STAY_SECONDS 까지)
  - `text` string: 알림 글. (글자 수는 코드 포인트로 센다)
  - `baseSeconds` number: 표의 머무는 초.
  - returns number: 머무는 초. (20 자를 넘으면 20 자마다 0.5 초를 더하고, 4 초를 넘지 않는다)

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

  ```js
  import { readToastSeconds } from "./build/vanilla.js";
  
  const shortSeconds = readToastSeconds("Saved", 1.5);
  const longSeconds = readToastSeconds("The cloud save finished uploading just now", 1.5);
  const cappedSeconds = readToastSeconds("x".repeat(200), 1.5);
  console.log(shortSeconds, longSeconds, cappedSeconds);
  ```

- `attachToastMotion(node, enterSide)` (method, since 0.4.0): 알림 노드에 나옴과 사라짐 붙이기. (ToastMotion 을 돌려줌)
  - `node` WorldNode: 알림 노드.
  - `enterSide` number: -1 이면 위에서, 1 이면 아래에서 들어온다.
  - returns ToastMotion: 붙인 컴포넌트.

## ToastPhase (enum)

알림의 단계.

- `hidden` = `hidden`: 숨음. (노드가 꺼져 있다)
- `enter` = `enter`: 나오는 중. (미끄러져 들어오며 보이고 살짝 커졌다 돌아온다)
- `stay` = `stay`: 머무는 중. (머무는 초를 센다)
- `exit` = `exit`: 사라지는 중. (떠오르며 투명해진다)
