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 자리다.
- 사용:
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 은 쓰지 않는다)
Confirmation dialog. (UIDialog of the original ui/uidialog.js)
- Pops up a dimming blocker + a centered card + confirm / cancel buttons with a single show. Appearing / disappearing moves with PopupMotion (bounce scale up / shrink).
- Pressing the blocker counts as cancel. If on_cancel is not passed, the cancel button is hidden and it becomes an alert with only a wide confirm button.
- Place it at the top of the scene root (the last child). The parent's content_size is used as the blocker size. (960 x 640 if none)
- The original is a node that inherits WorldNode. The Rust version is a component attached to a node (host), and on attach, like the original constructor, it sets up the host (top-left pivot, inactive) and creates the blocker, card, message, and button nodes as children. UIDialog::create(node_tree, parent_id, options) takes the place of new + add_child.
- Usage:
let dialog_id = UIDialog::create(node_tree, root_id, UIDialogOptions::default());
node_tree.with_component_mut::<UIDialog, _>(dialog_id, |dialog, tree| {
dialog.show(tree, "Really delete?", UIDialogShowOptions { on_confirm: Some(Box::new(|dialog, context| { ... })), on_cancel: Some(Box::new(|_, _| {})), ..UIDialogShowOptions::default() });
});- Differences from the original:
- show changes nodes, so it takes the tree. Scene code calls it with node_tree.with_component_mut, and callbacks with context.get_node_tree_mut().
- Confirm / cancel handlers are one-shot functions (FnOnce) that take the dialog and its host's context. dismiss takes the context to pass it to the handlers. Scene code without a context can call request_dismiss, which the original also has, to dismiss on the next tick.
- The original calls set_active(false) on the PopupMotion closed notification. The Rust version turns it off in tick when what was showing becomes closed (in dismiss for an instant transition).
- The original tick runs the transitioner after the children's tick. In the Rust version, components run before children. (no visible difference)
- Transition times are the same as PopupMotion. (appear 0.32 seconds, disappear 0.22 seconds. Tween is not used)
UIDialogOptionsstruct traits Clone, Debug, PartialEq, Default
대화 상자 설정. (원본 options = { width = 420, height = 240, confirm_text = "확인", cancel_text = "취소", panel_color?, accent_color?, text_color? })
Dialog settings. (original options = { width = 420, height = 240, confirm_text = "OK", cancel_text = "Cancel", panel_color?, accent_color?, text_color? })
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));카드 세로. (기본 240, 단추 윗변은 카드 아래에서 76 위)
Card height. (default 240, the top edge of the buttons is 76 above the card's bottom)
반환Returnsf64
확인 단추 글. (기본 "확인")
Confirm button text. (default "OK")
반환ReturnsString
취소 단추 글. (기본 "취소")
Cancel button text. (default "Cancel")
반환ReturnsString
UIDialogHandlertype
확인 / 취소 처리기. (대화 상자와 그 호스트의 문맥을 받는다. 한 번만 불린다)
Confirm / cancel handler. (takes the dialog and its host's context. called only once)
pub type UIDialogHandler = Box<dyn FnOnce(&mut UIDialog, &mut ComponentContext)>UIDialogShowOptionsstruct traits Default
열기 설정. (원본 show 의 options = { on_confirm?, on_cancel?, confirm_text?, cancel_text? })
Open settings. (options of the original show = { on_confirm?, on_cancel?, confirm_text?, cancel_text? })
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());확인으로 닫힐 때 부르는 처리기. (None 이면 닫기만 한다)
Handler called when closed by confirm. (None only closes)
반환ReturnsOption<UIDialogHandler>
없으면 취소 단추를 감춘 알림이 된다.
If none, it becomes an alert with the cancel button hidden.
반환ReturnsOption<UIDialogHandler>
확인 단추에 새로 쓸 글. (None 이면 지금 글 그대로)
New text for the confirm button. (None keeps the current text)
반환ReturnsOption<String>
취소 단추에 새로 쓸 글. (None 이면 지금 글 그대로)
New text for the cancel button. (None keeps the current text)
반환ReturnsOption<String>
UIDialogstruct traits Default, Component, UIDocumentNode
확인 대화 상자.
Confirmation dialog.
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());생성. (기본 설정)
Constructor. (default settings)
반환ReturnsUIDialog 붙기 전의 대화 상자. (UIDialogOptions::default)Dialog before being attached. (UIDialogOptions::default)
짝Counterpart: vanilla.js UIDialog.constructor
설정으로 생성. (노드에는 node_tree.add_component 로 붙인다)
Creates with settings. (attach to a node with node_tree.add_component)
- optionsUIDialogOptions
- 대화 상자 설정.Dialog settings.
반환ReturnsUIDialog 붙기 전의 대화 상자.Dialog before being attached.
pub fn create(node_tree: &mut NodeTree, parent_id: NodeId, options: UIDialogOptions) -> NodeId#노드 만들기. (parent_id 밑에 "UIDialog" 노드를 만들어 붙인다. 원본 new UIDialog(options) + add_child)
Creates the node. (creates and attaches a "UIDialog" node under parent_id. original new UIDialog(options) + add_child)
- node_tree&mut NodeTree
- 노드 트리.Node tree.
- parent_idNodeId
- 부모 노드. (보통 씬 뿌리, 그 content_size 를 막 크기로 쓴다)Parent node. (usually the scene root, its content_size is used as the blocker size)
- optionsUIDialogOptions
- 대화 상자 설정.Dialog settings.
반환ReturnsNodeId 만든 호스트 노드. (열기 전이라 꺼져 있다)The created host node. (turned off because it has not been opened yet)
pub fn show(&mut self, node_tree: &mut NodeTree, message_text: &str, options: UIDialogShowOptions)#대화 상자 열기. (on_cancel 이 없으면 취소 단추를 감추고 확인 단추를 가운데로 넓힌다)
- 트리에 붙기 전이면 아무 일도 없다. 열면 호스트를 켜고 부모 크기에 막을 맞춘 뒤 등장 움직임을 시작한다.
Opens the dialog. (without on_cancel, hides the cancel button and widens the confirm button to the center)
- Does nothing before being attached to a tree. On open, turns on the host, fits the blocker to the parent size, and starts the appear movement.
- node_tree&mut NodeTree
- 대화 상자가 든 노드 트리. (콜백에서는 context.get_node_tree_mut())Node tree that holds the dialog. (context.get_node_tree_mut() in callbacks)
- message_text&str
- 보일 글. (카드 가로 - 56 에서 줄을 바꾼다)Text to show. (wraps at card width - 56)
- optionsUIDialogShowOptions
- 처리기와 이번에 쓸 단추 글. (처리기는 지난번 것을 바꾼다)Handlers and the button text to use this time. (handlers replace the previous ones)
짝Counterpart: vanilla.js UIDialog.show
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());닫기. (다 열린 뒤에만. is_confirmed 에 따라 확인 / 취소 처리기를 부른다. 문맥은 대화 상자 호스트의 것)
- 퇴장 움직임을 시작한 뒤 처리기를 부르고, 부르지 않은 쪽 처리기는 버린다.
Closes. (only once fully open. calls the confirm / cancel handler depending on is_confirmed. the context is the dialog host's)
- Starts the disappear movement, then calls the handler, and discards the handler of the side not called.
- context&mut ComponentContext
- 대화 상자 호스트 노드의 문맥. (처리기에 그대로 넘긴다)Context of the dialog host node. (passed straight to the handler)
- is_confirmedbool
- true 면 확인, false 면 취소.true for confirm, false for cancel.
짝Counterpart: vanilla.js UIDialog.dismiss
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);
});
}));
}다음 tick 에 닫기. (문맥이 없는 씬 코드용)
Closes on the next tick. (for scene code without a context)
- is_confirmedbool
- true 면 확인, false 면 취소. (다음 tick 에 dismiss 로 넘긴다, 그때 다 열려 있지 않으면 닫히지 않는다)true for confirm, false for cancel. (passed to dismiss on the next tick, does not close if not fully open by then)
짝Counterpart: vanilla.js UIDialog.requestDismiss
보이는 중인지. (등장 / 열림 / 퇴장 중이면 참)
Whether it is showing. (true while appearing / open / disappearing)
반환Returnsbool 닫힘 상태가 아니면 true.true unless in the closed state.
짝Counterpart: vanilla.js UIDialog.isShowing
즉시 전환 설정. (연출 없는 UI 톤)
Sets instant transitions. (UI tone without effects)
- is_instant_enabledbool
- true 면 움직임 없이 바로 열리고 닫힌다. (기본 false)If true, opens and closes instantly without movement. (default false)
짝Counterpart: vanilla.js UIDialog.setInstantEnabled
전환기 반환.
Returns the transitioner.
반환Returns&PopupMotion 등장과 퇴장 움직임. (등장 0.32 초, 퇴장 0.22 초)Appear and disappear movement. (appear 0.32 seconds, disappear 0.22 seconds)
짝Counterpart: vanilla.js UIDialog.getPopupMotion
막 노드 핸들.
Blocker node handle.
반환ReturnsOption<NodeId> 어둡게 가리는 막 노드. (트리에 붙기 전이면 None)Dimming blocker node. (None before being attached to a tree)
짝Counterpart: vanilla.js UIDialog.getOverlayNode
카드 노드 핸들.
Card node handle.
반환ReturnsOption<NodeId> 카드 노드. (가운데 피봇, 트리에 붙기 전이면 None)Card node. (center pivot, None before being attached to a tree)
짝Counterpart: vanilla.js UIDialog.getPanelNode
확인 단추 노드 핸들.
Confirm button node handle.
반환ReturnsOption<NodeId> 확인 단추 노드. (트리에 붙기 전이면 None)Confirm button node. (None before being attached to a tree)
짝Counterpart: vanilla.js UIDialog.getConfirmNode
취소 단추 노드 핸들.
Cancel button node handle.
반환ReturnsOption<NodeId> 취소 단추 노드. (트리에 붙기 전이면 None, 알림 꼴로 열면 꺼져 있다)Cancel button node. (None before being attached to a tree, turned off when opened as an alert)
짝Counterpart: vanilla.js UIDialog.getCancelNode