# ui/uidialog (vanilla.js 0.4.0)

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

Confirmation dialog.

## UIDialog extends WorldNode

Confirmation dialog.
- Pops up a darkening overlay + a centered card + confirm / cancel buttons with a single show().
  Entrance / exit animate with PopupMotion (bounce scale up / shrink).
- Pressing the overlay counts as cancel. If onCancel is not passed, it becomes a confirm-only alert.
- addChild it at the top (last child) of the scene root before use.
- Usage:
    const dialog = new UIDialog();
    scene.getRoot().addChild(dialog);
    dialog.show("Really delete?", { onConfirm: () => remove(), onCancel: () => {} });

Example: A dialog asking to confirm or cancel

```js
import { Engine, EngineConfiguration, UIScene, UIDialog } from "./build/vanilla.js";

class DialogScene extends UIScene {
	initialize(engine) {
		super.initialize(engine);
		const dialog = new UIDialog({ width: 360, height: 200, confirmText: "Delete", cancelText: "Keep" });
		const root = this.getRoot();
		root.addChild(dialog);
		dialog.show("Delete this save file?", {
			onConfirm: () => {
				console.log("deleted");
			},
			onCancel: () => {
				console.log("kept");
			},
		});
	}
}

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

- `constructor(options = {})` (constructor, since 0.3.0): Constructor.
  - `options` object: { width = 420, height = 240, confirmText = "확인", cancelText = "취소",
                            panelColor?, accentColor?, textColor? }
- `show(messageText, options = {})` (method, since 0.3.0): Opens the dialog.
  - If onCancel is not passed, hides the cancel button and widens the confirm button to the center.
  - `messageText` string: Body text. (wrapped to the card width)
  - `options` object: { onConfirm?, onCancel?, confirmText?, cancelText? }
- `dismiss(isConfirmed)` (method, since 0.3.0): Close. (calls the confirm / cancel handler depending on isConfirmed)
  - `isConfirmed` boolean: If true, calls onConfirm, if false, calls onCancel. (does nothing before it has fully opened)
- `requestDismiss(isConfirmed)` (method, since 0.4.0): Closes on the next tick. (to defer closing from outside tick)
  - `isConfirmed` boolean: If true, closes as confirmed, if false, as cancelled.
- `tick(timeDelta)` (method, since 0.3.0): Update.
  - `timeDelta` number: Time since the last frame. (Seconds)
- `isShowing()` (method, since 0.3.0): Whether it is showing.
  - returns boolean: True unless in the closed state. (also true while opening or closing)
- `setInstantEnabled(isInstantEnabled)` (method, since 0.3.0): Sets whether to switch instantly. (UI tone without effects)
  - `isInstantEnabled` boolean: If true, opens and closes immediately without animation.
- `getPopupMotion()` (method, since 0.4.0): Returns the motion.
  - returns PopupMotion: Open and close motion.
- `getOverlayNode()` (method, since 0.4.0): Returns the overlay node that covers what is behind.
  - returns WorldNode: Overlay node.
- `getPanelNode()` (method, since 0.4.0): Returns the panel node.
  - returns WorldNode: Panel node.
- `getConfirmNode()` (method, since 0.4.0): Returns the confirm button node.
  - returns WorldNode: Confirm button node.

  Example: Finding the confirm button node and reading its name and size

  ```js
  import { UIDialog } from "./build/vanilla.js";
  
  const dialog = new UIDialog({ confirmText: "OK", cancelText: "Cancel" });
  const confirmNode = dialog.getConfirmNode();
  const cancelNode = dialog.getCancelNode();
  console.log(confirmNode.getName(), cancelNode.getName(), dialog.getPanelNode().getName());
  ```

- `getCancelNode()` (method, since 0.4.0): Returns the cancel button node.
  - returns WorldNode: Cancel button node.
