# ui/uidialog (vanilla-engine 0.4.0)

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

확인 대화 상자. (원본 ui/uidialog.js 의 UIDialog)
- 어둡게 가리는 막 + 가운데 카드 + 확인 / 취소 단추를 show 한 번으로 띄운다. 등장 / 퇴장은 PopupMotion(되튕김 확대 / 수축)으로 움직인다.
- 막을 누르면 취소로 처리한다. on_cancel 을 넘기지 않으면 취소 단추를 감추고 확인 단추만 넓게 둔 알림이 된다.
- 씬 뿌리의 맨 위(마지막 자식)에 두고 쓴다. 부모 content_size 를 막 크기로 쓴다. (없으면 960 x 640)
- 원본은 WorldNode 를 상속한 노드다. 러스트 판은 노드(호스트)에 붙는 컴포넌트이고, 붙을 때 원본 생성자처럼 호스트를 꾸미고
  (왼쪽 위 피봇, 비활성) 막, 카드, 메시지, 단추 노드를 자식으로 만든다. UIDialog::create(node_tree, parent_id, options) 가 new + add_child 자리다.
- 사용:
  ```text
    let dialog_id = UIDialog::create(node_tree, root_id, UIDialogOptions::default());
    node_tree.with_component_mut::<UIDialog, _>(dialog_id, |dialog, tree| {
        dialog.show(tree, "정말 삭제할까요?", UIDialogShowOptions { on_confirm: Some(Box::new(|dialog, context| { ... })), on_cancel: Some(Box::new(|_, _| {})), ..UIDialogShowOptions::default() });
    });
  ```
- 원본과 다른 것:
  - show 는 노드를 바꾸므로 트리를 받는다. 씬 코드는 node_tree.with_component_mut 로, 콜백에서는 context.get_node_tree_mut() 로 부른다.
  - 확인 / 취소 처리기는 대화 상자와 그 호스트의 문맥을 받는 한 번짜리 함수(FnOnce)다. dismiss 는 처리기에 문맥을 넘기려고 문맥을 받는다.
    문맥이 없는 씬 코드에서는 원본에도 있는 request_dismiss 를 부르면 다음 tick 에 dismiss 한다.
  - 원본은 PopupMotion 닫힘 알림에서 set_active(false) 한다. 러스트 판은 tick 에서 보이던 것이 닫힘이 되면(즉시 전환이면 dismiss 에서) 끈다.
  - 원본 tick 은 자식 tick 다음에 전환기를 돌린다. 러스트 판은 컴포넌트가 자식보다 먼저 돈다. (보이는 차이는 없다)
  - 전환 시간은 PopupMotion 그대로다. (등장 0.32 초, 퇴장 0.22 초. Tween 은 쓰지 않는다)

## UIDialogOptions (struct) (Clone, Debug, PartialEq, Default)

대화 상자 설정. (원본 options = { width = 420, height = 240, confirm_text = "확인", cancel_text = "취소", panel_color?, accent_color?, text_color? })

예제: 작은 카드와 바꾼 확인 단추 글

```rust
use vanilla::{NodeTree, UIDialog, UIDialogOptions, Vector2};

let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let options = UIDialogOptions {
	width: 360.0,
	height: 200.0,
	confirm_text: String::from("OK"),
	..UIDialogOptions::default()
};
let dialog_id = UIDialog::create(&mut node_tree, root_id, options);
let panel_id = node_tree[dialog_id].get_component::<UIDialog>().expect("dialog").get_panel_node().expect("panel");
assert_eq!(node_tree[panel_id].get_content_size(), Vector2::create(360.0, 200.0));
```

- `pub width: f64` (field, since 0.3.2): 카드 가로. (기본 420)
  - returns f64
- `pub height: f64` (field, since 0.3.2): 카드 세로. (기본 240, 단추 윗변은 카드 아래에서 76 위)
  - returns f64
- `pub confirm_text: String` (field, since 0.3.2): 확인 단추 글. (기본 "확인")
  - returns String
- `pub cancel_text: String` (field, since 0.3.2): 취소 단추 글. (기본 "취소")
  - returns String
- `pub panel_color: Color` (field, since 0.3.2): 카드 바탕색.
  - returns Color
- `pub accent_color: Color` (field, since 0.3.2): 확인 단추 색.
  - returns Color
- `pub text_color: Color` (field, since 0.3.2): 메시지와 단추 글의 색.
  - returns Color

## UIDialogHandler (type)

확인 / 취소 처리기. (대화 상자와 그 호스트의 문맥을 받는다. 한 번만 불린다)

```rust
pub type UIDialogHandler = Box<dyn FnOnce(&mut UIDialog, &mut ComponentContext)>
```

## UIDialogShowOptions (struct) (Default)

열기 설정. (원본 show 의 options = { on_confirm?, on_cancel?, confirm_text?, cancel_text? })

예제: 취소 처리기 없이 연 알림은 취소 단추를 감춘다

```rust
use vanilla::{NodeTree, UIDialog, UIDialogOptions, UIDialogShowOptions};

let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let dialog_id = UIDialog::create(&mut node_tree, root_id, UIDialogOptions::default());
let show_options = UIDialogShowOptions {
	confirm_text: Some(String::from("OK")),
	..UIDialogShowOptions::default()
};
node_tree.with_component_mut::<UIDialog, _>(dialog_id, |dialog, tree| {
	dialog.show(tree, "Saved.", show_options);
});
let cancel_id = node_tree[dialog_id].get_component::<UIDialog>().expect("dialog").get_cancel_node().expect("cancel");
assert!(!node_tree[cancel_id].is_active());
```

- `pub on_confirm: Option<UIDialogHandler>` (field, since 0.3.2): 확인으로 닫힐 때 부르는 처리기. (None 이면 닫기만 한다)
  - returns Option<UIDialogHandler>
- `pub on_cancel: Option<UIDialogHandler>` (field, since 0.3.2): 없으면 취소 단추를 감춘 알림이 된다.
  - returns Option<UIDialogHandler>
- `pub confirm_text: Option<String>` (field, since 0.3.2): 확인 단추에 새로 쓸 글. (None 이면 지금 글 그대로)
  - returns Option<String>
- `pub cancel_text: Option<String>` (field, since 0.3.2): 취소 단추에 새로 쓸 글. (None 이면 지금 글 그대로)
  - returns Option<String>

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

확인 대화 상자.

예제: 묻고, 다 열린 뒤 확인으로 닫기

```rust
use std::cell::Cell;
use std::rc::Rc;
use vanilla::{ComponentContext, Engine, EngineConfiguration, NodeTree, UIDialog, UIDialogOptions, UIDialogShowOptions};

let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let dialog_id = UIDialog::create(&mut node_tree, root_id, UIDialogOptions::default());
let is_deleted = Rc::new(Cell::new(false));
let is_deleted_handler = is_deleted.clone();
let show_options = UIDialogShowOptions {
	on_confirm: Some(Box::new(move |_dialog: &mut UIDialog, _context: &mut ComponentContext| {
		is_deleted_handler.set(true);
	})),
	on_cancel: Some(Box::new(|_dialog: &mut UIDialog, _context: &mut ComponentContext| {})),
	..UIDialogShowOptions::default()
};
node_tree.with_component_mut::<UIDialog, _>(dialog_id, |dialog, tree| {
	dialog.show(tree, "Delete this item?", show_options);
});
node_tree.tick(&engine, 0.4);
node_tree[dialog_id].get_component_mut::<UIDialog>().expect("dialog").request_dismiss(true);
node_tree.tick(&engine, 0.016);
assert!(is_deleted.get());
node_tree.tick(&engine, 0.3);
assert!(!node_tree[dialog_id].is_active());
```

- `pub fn new() -> UIDialog` (constructor, since 0.3.2): 생성. (기본 설정)
  - returns UIDialog: 붙기 전의 대화 상자. (UIDialogOptions::default)
- `pub fn create_with_options(options: UIDialogOptions) -> UIDialog` (static, since 0.3.2): 설정으로 생성. (노드에는 node_tree.add_component 로 붙인다)
  - `options` UIDialogOptions: 대화 상자 설정.
  - returns UIDialog: 붙기 전의 대화 상자.
- `pub fn create(node_tree: &mut NodeTree, parent_id: NodeId, options: UIDialogOptions) -> NodeId` (static, since 0.3.2): 노드 만들기. (parent_id 밑에 "UIDialog" 노드를 만들어 붙인다. 원본 new UIDialog(options) + add_child)
  - `node_tree` &mut NodeTree: 노드 트리.
  - `parent_id` NodeId: 부모 노드. (보통 씬 뿌리, 그 content_size 를 막 크기로 쓴다)
  - `options` UIDialogOptions: 대화 상자 설정.
  - returns NodeId: 만든 호스트 노드. (열기 전이라 꺼져 있다)
- `pub fn show(&mut self, node_tree: &mut NodeTree, message_text: &str, options: UIDialogShowOptions)` (method, since 0.3.2): 대화 상자 열기. (on_cancel 이 없으면 취소 단추를 감추고 확인 단추를 가운데로 넓힌다)
  - 트리에 붙기 전이면 아무 일도 없다. 열면 호스트를 켜고 부모 크기에 막을 맞춘 뒤 등장 움직임을 시작한다.
  - `node_tree` &mut NodeTree: 대화 상자가 든 노드 트리. (콜백에서는 context.get_node_tree_mut())
  - `message_text` &str: 보일 글. (카드 가로 - 56 에서 줄을 바꾼다)
  - `options` UIDialogShowOptions: 처리기와 이번에 쓸 단추 글. (처리기는 지난번 것을 바꾼다)

  예제: 확인 처리기에서 문맥으로 노드 지우기

  ```rust
  use vanilla::{ComponentContext, NodeId, NodeTree, UIDialog, UIDialogOptions, UIDialogShowOptions};
  
  fn ask_delete(node_tree: &mut NodeTree, dialog_id: NodeId, item_id: NodeId) {
  	let show_options = UIDialogShowOptions {
  		on_confirm: Some(Box::new(move |_dialog: &mut UIDialog, context: &mut ComponentContext| {
  			context.get_node_tree_mut().destroy_node(item_id);
  		})),
  		on_cancel: Some(Box::new(|_dialog: &mut UIDialog, _context: &mut ComponentContext| {})),
  		..UIDialogShowOptions::default()
  	};
  	node_tree.with_component_mut::<UIDialog, _>(dialog_id, |dialog, tree| {
  		dialog.show(tree, "Delete this item?", show_options);
  	});
  }
  
  let mut node_tree = NodeTree::new();
  let root_id = node_tree.get_root_id();
  let item_id = node_tree.create_child(root_id, "Item");
  let dialog_id = UIDialog::create(&mut node_tree, root_id, UIDialogOptions::default());
  ask_delete(&mut node_tree, dialog_id, item_id);
  assert!(node_tree[dialog_id].get_component::<UIDialog>().expect("dialog").is_showing());
  ```

- `pub fn dismiss(&mut self, context: &mut ComponentContext, is_confirmed: bool)` (method, since 0.3.2): 닫기. (다 열린 뒤에만. is_confirmed 에 따라 확인 / 취소 처리기를 부른다. 문맥은 대화 상자 호스트의 것)
  - 퇴장 움직임을 시작한 뒤 처리기를 부르고, 부르지 않은 쪽 처리기는 버린다.
  - `context` &mut ComponentContext: 대화 상자 호스트 노드의 문맥. (처리기에 그대로 넘긴다)
  - `is_confirmed` bool: true 면 확인, false 면 취소.

  예제: 다른 단추에서 취소로 닫기

  ```rust
  use vanilla::{ComponentContext, NodeId, UIButton, UIDialog};
  
  fn bind_skip_button(button: &mut UIButton, dialog_id: NodeId) {
  	button.set_clicked_event(Box::new(move |_button: &mut UIButton, context: &mut ComponentContext| {
  		context.with_component_mut::<UIDialog, _>(dialog_id, |dialog, dialog_context| {
  			dialog.dismiss(dialog_context, false);
  		});
  	}));
  }
  ```

- `pub fn request_dismiss(&mut self, is_confirmed: bool)` (method, since 0.3.2): 다음 tick 에 닫기. (문맥이 없는 씬 코드용)
  - `is_confirmed` bool: true 면 확인, false 면 취소. (다음 tick 에 dismiss 로 넘긴다, 그때 다 열려 있지 않으면 닫히지 않는다)
- `pub fn is_showing(&self) -> bool` (method, since 0.3.2): 보이는 중인지. (등장 / 열림 / 퇴장 중이면 참)
  - returns bool: 닫힘 상태가 아니면 true.
- `pub fn set_instant_enabled(&mut self, is_instant_enabled: bool)` (method, since 0.3.2): 즉시 전환 설정. (연출 없는 UI 톤)
  - `is_instant_enabled` bool: true 면 움직임 없이 바로 열리고 닫힌다. (기본 false)
- `pub fn get_popup_motion(&self) -> &PopupMotion` (method, since 0.3.2): 전환기 반환.
  - returns &PopupMotion: 등장과 퇴장 움직임. (등장 0.32 초, 퇴장 0.22 초)
- `pub fn get_overlay_node(&self) -> Option<NodeId>` (method, since 0.3.2): 막 노드 핸들.
  - returns Option<NodeId>: 어둡게 가리는 막 노드. (트리에 붙기 전이면 None)
- `pub fn get_panel_node(&self) -> Option<NodeId>` (method, since 0.3.2): 카드 노드 핸들.
  - returns Option<NodeId>: 카드 노드. (가운데 피봇, 트리에 붙기 전이면 None)
- `pub fn get_confirm_node(&self) -> Option<NodeId>` (method, since 0.3.2): 확인 단추 노드 핸들.
  - returns Option<NodeId>: 확인 단추 노드. (트리에 붙기 전이면 None)
- `pub fn get_cancel_node(&self) -> Option<NodeId>` (method, since 0.3.2): 취소 단추 노드 핸들.
  - returns Option<NodeId>: 취소 단추 노드. (트리에 붙기 전이면 None, 알림 꼴로 열면 꺼져 있다)
