# ui/toastmotion (vanilla.js 0.4.0)

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

How a notification appears and disappears. (a component attached to a toast node, UIToast builds queued pill notifications itself, while this moves a notification node made by the game)

## ToastMotion extends Component

How a notification appears and disappears. (a component attached to a toast node, UIToast builds queued pill notifications itself, while this moves a notification node made by the game)

Attach it to a notification node and pop it up with show. It slides in from the outside (top notifications from above, bottom ones from below) as it becomes visible, grows slightly and settles back,
and after the stay time it floats up, disappears, and turns the node off.
If another text arrives while it is up, the previous one quickly gets out of the way and the new text comes in, and if the same text arrives again it only refills the time and gives a bounce.
The node's position moves only around the position the screen placed it at. (if the screen places it again meanwhile, that position becomes the new reference)

Example: Showing a notification node made by the game

```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): Constructor.
- `setEnterSide(enterSide)` (method, since 0.4.0): Sets the entry side. (-1 from above, 1 from below)
  - `enterSide` number: Entering side. (-1 enters from above, 1 from below, multiplied by the 10 pixel entering distance)
- `show(text, seconds, applyText)` (method, since 0.4.0): Pops it up. (applyText puts the text and color into the node, any other text showing quickly gets out of the way first)
  - `text` string: Notification text. (if it is the same as the shown text, only the stay seconds are refilled and it pops)
  - `seconds` number: Seconds to stay.
  - `applyText` function(): void: Function that puts the text and colors on the node. (called when it starts entering)
- `isShowing()` (method, since 0.4.0): Whether it is up. (appearing, staying, disappearing)
  - returns boolean: True unless in the hidden phase.
- `getPhase()` (method, since 0.4.0): Returns the phase. (ToastPhase)
  - returns string: Phase. (a ToastPhase value)
- `hideNow()` (method, since 0.4.0): Turns it off immediately. (when the round changes)
- `startEnter(text, seconds, applyText)` (method, since 0.4.0): Starts appearing.
  - `text` string: Notification text.
  - `seconds` number: Seconds to stay.
  - `applyText` function(): void: Function that puts the text and colors on the node. (called right away)
- `startExit(exitLength)` (method, since 0.4.0): Starts disappearing.
  - `exitLength` number: Seconds to exit. (normally 0.25, 0.12 when making way for a new notification)
- `tick(timeDelta)` (method, since 0.4.0): Update. (position, size and opacity per phase)
  - `timeDelta` number: Time since the last frame. (Seconds)
- `placeAround(node, scale, offsetY, opacity)` (method, since 0.4.0): Places it centered on the position the screen placed, with that size, vertical offset and opacity.
  - `node` WorldNode: Notification node.
  - `scale` number: Scale. (1 is the normal size, grows around the center)
  - `offsetY` number: Vertical offset from the base position. (pixels, down is +)
  - `opacity` number: Opacity. (0 ~ 1)

## Functions and constants

- `readToastSeconds(text, baseSeconds)` (method, since 0.4.0): Notification stay duration. (for long text, adds LONG_TEXT_EXTRA_SECONDS to the table's time for every LONG_TEXT_CHARACTERS characters, up to MAXIMUM_STAY_SECONDS)
  - `text` string: Notification text. (characters are counted as code points)
  - `baseSeconds` number: Base stay seconds from the table.
  - returns number: Seconds to stay. (adds 0.5 seconds for every 20 characters past 20, never more than 4 seconds)

  Example: Stay seconds by text length

  ```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): Attaches appearing and disappearing to a notification node. (returns a ToastMotion)
  - `node` WorldNode: Notification node.
  - `enterSide` number: -1 means it enters from above, 1 means from below.
  - returns ToastMotion: The attached component.

## ToastPhase (enum)

Notification phases.

- `hidden` = `hidden`: Hidden. (the node is turned off)
- `enter` = `enter`: Entering. (slides in while appearing, grows slightly and settles)
- `stay` = `stay`: Staying. (counts down the stay seconds)
- `exit` = `exit`: Exiting. (rises while fading out)
