vanilladocs
vanillapunch.com
API / ui/uicontextmenu

ui/uicontextmenu

컨텍스트 메뉴. (원본 ui/uicontextmenu.js 의 UIContextMenu, 우클릭 / 길게 누름 메뉴)

  • show(node_tree, position, item_list) 로 항목 목록을 그 자리에 띄운다. 바깥을 누르면 닫히고, 항목을 누르면 닫힌 뒤 처리기를 부른다.
  • 메뉴가 부모 영역 밖으로 나가지 않게 오른쪽 / 아래 끝에 붙인다. (부모 크기를 모르면 960 x 640)
  • 씬 뿌리의 맨 위(마지막 자식)에 두고 쓴다.
  • 원본은 WorldNode 를 상속한 노드다. 러스트 판은 노드(호스트)에 붙는 컴포넌트이고, 붙을 때 원본 생성자처럼 호스트를 꾸미고(왼쪽 위 피봇, 비활성) 바깥 누름 막(투명 UIButton)과 메뉴 판(둥근 Paint)을 자식으로 만든다. UIContextMenu::create(node_tree, parent_id, options) 가 원본 new UIContextMenu(options) + add_child 자리다.
  • 사용:
let menu_id = UIContextMenu::create(node_tree, root_id, UIContextMenuOptions::default());
node_tree.with_component_mut::<UIContextMenu, _>(menu_id, |context_menu, tree| {
    context_menu.show(tree, view_position, vec![UIContextMenuItem::create("복사", Box::new(|context| { ... })), UIContextMenuItem::create_disabled("삭제")]);
});
  • 원본과 다른 것:
  • show / close 는 노드를 바꾸므로 트리를 받는다. 씬 코드는 node_tree.with_component_mut 로 부른다.
  • 항목은 UIContextMenuItem 구조체({ text, handler?, is_disabled }), 처리기는 누른 행 노드의 문맥을 받는 한 번짜리 함수(FnOnce)다.
  • is_showing 은 노드 활성 대신 show / close 가 고치는 값을 돌려준다.
  • 이전 행은 떼기(removeChild) 대신 지운다. 행 노드 이름은 원본처럼 "ContextMenuItem{번호}" 다.

Context menu. (UIContextMenu of the original ui/uicontextmenu.js, a right click / long press menu)

  • show(node_tree, position, item_list) pops up the item list at that position. Pressing outside closes it, and pressing an item closes it and then calls the handler.
  • The menu is pinned to the right / bottom edge so it does not go outside the parent area. (960 x 640 if the parent size is unknown)
  • Place it at the top of the scene root (the last child).
  • 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 an outside-press blocker (transparent UIButton) and a menu panel (rounded Paint) as children. UIContextMenu::create(node_tree, parent_id, options) takes the place of the original new UIContextMenu(options) + add_child.
  • Usage:
let menu_id = UIContextMenu::create(node_tree, root_id, UIContextMenuOptions::default());
node_tree.with_component_mut::<UIContextMenu, _>(menu_id, |context_menu, tree| {
    context_menu.show(tree, view_position, vec![UIContextMenuItem::create("Copy", Box::new(|context| { ... })), UIContextMenuItem::create_disabled("Delete")]);
});
  • Differences from the original:
  • show / close change nodes, so they take the tree. Scene code calls them with node_tree.with_component_mut.
  • Items are a UIContextMenuItem struct ({ text, handler?, is_disabled }), and a handler is a one-shot function (FnOnce) that takes the context of the pressed row node.
  • is_showing returns the value changed by show / close instead of the node's active state.
  • Previous rows are deleted instead of detached (removeChild). Row node names are "ContextMenuItem{index}" as in the original.

UIContextMenuOptionsstruct traits Clone, Copy, Debug, PartialEq, Default

소스source

컨텍스트 메뉴 설정. (원본 options = { menu_width = 168, item_height = 28, font_size = 12, panel_color?, text_color? })

Context menu settings. (original options = { menu_width = 168, item_height = 28, font_size = 12, panel_color?, text_color? })

예제Example넓은 메뉴, 높은 행Wide menu, tall rows
use vanilla::{NodeTree, UIContextMenu, UIContextMenuItem, UIContextMenuOptions, Vector2};

let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
node_tree[root_id].set_content_size(Vector2::create(800.0, 600.0));
let options = UIContextMenuOptions {
	menu_width: 200.0,
	item_height: 32.0,
	..UIContextMenuOptions::default()
};
let menu_id = UIContextMenu::create(&mut node_tree, root_id, options);
node_tree.with_component_mut::<UIContextMenu, _>(menu_id, |context_menu, tree| {
	context_menu.show(tree, Vector2::create(100.0, 100.0), vec![UIContextMenuItem::create_disabled("Paste")]);
});
let panel_id = node_tree[menu_id].get_component::<UIContextMenu>().expect("menu").get_panel_node().expect("panel");
assert_eq!(node_tree[panel_id].get_content_size(), Vector2::create(200.0, 40.0));
필드fieldpub menu_width: f64#0.3.2 부터since 0.3.2소스source

메뉴 판 가로. (기본 168)

Menu panel width. (default 168)

반환Returnsf64

필드fieldpub item_height: f64#0.3.2 부터since 0.3.2소스source

항목 행 높이. (기본 28, 판 세로는 항목 수 x 행 높이 + 8)

Item row height. (default 28, the panel height is item count x row height + 8)

반환Returnsf64

필드fieldpub font_size: f64#0.3.2 부터since 0.3.2소스source

항목 글 크기. (기본 12)

Item text size. (default 12)

반환Returnsf64

필드fieldpub panel_color: Color#0.3.2 부터since 0.3.2소스source

메뉴 판 색.

Menu panel color.

반환ReturnsColor

필드fieldpub text_color: Color#0.3.2 부터since 0.3.2소스source

누를 수 있는 항목의 글 색. (누를 수 없는 항목은 회색)

Text color of pressable items. (items that cannot be pressed are gray)

반환ReturnsColor

UIContextMenuHandlertype

소스source

항목 처리기. (누른 행 노드의 문맥을 받는다. 한 번만 불린다)

Item handler. (takes the context of the pressed row node. called only once)

pub type UIContextMenuHandler = Box<dyn FnOnce(&mut ComponentContext)>

UIContextMenuItemstruct

소스source

메뉴 항목. (원본 { text, handler?, is_disabled? })

Menu item. (original { text, handler?, is_disabled? })

예제Example누를 수 있는 항목과 누를 수 없는 항목A pressable item and an item that cannot be pressed
use vanilla::{ComponentContext, UIContextMenuItem};

let copy_item = UIContextMenuItem::create("Copy", Box::new(|_context: &mut ComponentContext| {}));
let delete_item = UIContextMenuItem::create_disabled("Delete");
assert!(copy_item.handler.is_some());
assert!(!copy_item.is_disabled);
assert!(delete_item.is_disabled);
assert_eq!(delete_item.text, "Delete");
정적staticpub fn create(text: &str, handler: UIContextMenuHandler) -> UIContextMenuItem#0.3.2 부터since 0.3.2소스source

처리기가 있는 항목 생성.

Creates an item with a handler.

text&str
항목 글.Item text.
handlerUIContextMenuHandler
누르면 메뉴를 닫은 뒤 부르는 처리기. (누른 행 노드의 문맥을 받는다)Handler called after closing the menu when pressed. (takes the context of the pressed row node)

반환ReturnsUIContextMenuItem 누를 수 있는 항목.A pressable item.

정적staticpub fn create_disabled(text: &str) -> UIContextMenuItem#0.3.2 부터since 0.3.2소스source

누를 수 없는 항목 생성. (회색 글자)

Creates an item that cannot be pressed. (gray text)

text&str
항목 글.Item text.

반환ReturnsUIContextMenuItem 처리기 없이 누를 수 없는 항목.An item with no handler that cannot be pressed.

필드fieldpub text: String#0.3.2 부터since 0.3.2소스source

항목 글.

Item text.

반환ReturnsString

필드fieldpub handler: Option<UIContextMenuHandler>#0.3.2 부터since 0.3.2소스source

누르면 메뉴를 닫은 뒤 부르는 처리기. (None 이면 닫기만 한다)

Handler called after closing the menu when pressed. (None only closes)

반환ReturnsOption<UIContextMenuHandler>

필드fieldpub is_disabled: bool#0.3.2 부터since 0.3.2소스source

true 면 누를 수 없고 회색 글로 보인다.

If true, it cannot be pressed and is shown in gray text.

반환Returnsbool

UIContextMenustruct traits Default, Component, UIDocumentNode

짝Counterpart: vanilla.js UIContextMenu소스source

컨텍스트 메뉴.

Context menu.

예제Example항목을 누르면 처리기를 부르고 닫힌다Pressing an item calls the handler and closes the menu
use std::cell::Cell;
use std::rc::Rc;
use vanilla::{ComponentContext, Engine, EngineConfiguration, NodeTree, TouchKind, UIContextMenu, UIContextMenuItem, UIContextMenuOptions, Vector2};

let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
node_tree[root_id].set_content_size(Vector2::create(800.0, 600.0));
let menu_id = UIContextMenu::create(&mut node_tree, root_id, UIContextMenuOptions::default());
let copy_count = Rc::new(Cell::new(0));
let copy_counter = copy_count.clone();
let item_list = vec![
	UIContextMenuItem::create("Copy", Box::new(move |_context: &mut ComponentContext| {
		copy_counter.set(copy_counter.get() + 1);
	})),
	UIContextMenuItem::create_disabled("Delete"),
];
node_tree.with_component_mut::<UIContextMenu, _>(menu_id, |context_menu, tree| {
	context_menu.show(tree, Vector2::create(100.0, 100.0), item_list);
});
let copy_item_id = node_tree[menu_id].get_component::<UIContextMenu>().expect("menu").get_item_nodes()[0];
let item_center = node_tree.convert_node_position_to_view_position(copy_item_id, Vector2::create(80.0, 14.0));
node_tree.dispatch_touch(copy_item_id, &engine, TouchKind::Press, item_center);
node_tree.dispatch_touch(copy_item_id, &engine, TouchKind::Release, item_center);
assert_eq!(copy_count.get(), 1);
assert!(!node_tree[menu_id].get_component::<UIContextMenu>().expect("menu").is_showing());
생성자constructorpub fn new() -> UIContextMenu#0.3.2 부터since 0.3.2소스source

생성. (기본 설정)

Constructor. (default settings)

반환ReturnsUIContextMenu 붙기 전의 메뉴. (UIContextMenuOptions::default)Menu before being attached. (UIContextMenuOptions::default)

짝Counterpart: vanilla.js UIContextMenu.constructor

정적staticpub fn create_with_options(options: UIContextMenuOptions) -> UIContextMenu#0.3.2 부터since 0.3.2소스source

설정으로 생성. (노드에는 node_tree.add_component 로 붙인다)

Creates with settings. (attach to a node with node_tree.add_component)

optionsUIContextMenuOptions
메뉴 설정.Menu settings.

반환ReturnsUIContextMenu 붙기 전의 메뉴.Menu before being attached.

정적staticpub fn create(node_tree: &mut NodeTree, parent_id: NodeId, options: UIContextMenuOptions) -> NodeId#0.3.2 부터since 0.3.2소스source

노드 만들기. (parent_id 밑에 "UIContextMenu" 노드를 만들어 붙인다. 원본 new UIContextMenu(options) + add_child)

Creates the node. (creates and attaches a "UIContextMenu" node under parent_id. original new UIContextMenu(options) + add_child)

node_tree&mut NodeTree
노드 트리.Node tree.
parent_idNodeId
부모 노드. (보통 씬 뿌리, 그 content_size 가 메뉴를 가두는 영역이다)Parent node. (usually the scene root, its content_size is the area that confines the menu)
optionsUIContextMenuOptions
메뉴 설정.Menu settings.

반환ReturnsNodeId 만든 호스트 노드. (열기 전이라 꺼져 있다)The created host node. (turned off because it has not been opened yet)

메서드methodpub fn show(&mut self, node_tree: &mut NodeTree, position: Vector2, item_list: Vec<UIContextMenuItem>)#0.3.2 부터since 0.3.2소스source

메뉴 열기. (position 은 메뉴 왼쪽 위의 자리, 메뉴 노드(부모 크기의 영역) 밑 로컬 좌표. 행을 다시 만들고 영역 안에 붙여 띄운다)

Opens the menu. (position is the top-left of the menu in local coordinates under the menu node (an area the size of the parent). Rebuilds the rows and shows it kept inside the area)

node_tree&mut NodeTree
메뉴가 든 노드 트리.Node tree that holds the menu.
positionVector2
메뉴 판 왼쪽 위 자리. (판의 로컬 자리로 쓰고, 영역 오른쪽, 아래 끝에서 4 안쪽을 넘지 않게 자른다)Top-left position of the menu panel. (used as the panel's local position, clamped so it does not go past 4 inside the right and bottom edges of the area)
item_listVec<UIContextMenuItem>
항목 목록. (차례대로 위에서 아래로 놓는다)Item list. (placed in order from top to bottom)

짝Counterpart: vanilla.js UIContextMenu.show

예제Example오른쪽 아래 끝에서 연 메뉴는 영역 안으로 붙는다A menu opened at the bottom-right edge is pinned inside the area
use vanilla::{NodeTree, UIContextMenu, UIContextMenuItem, UIContextMenuOptions, Vector2};

let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
node_tree[root_id].set_content_size(Vector2::create(800.0, 600.0));
let menu_id = UIContextMenu::create(&mut node_tree, root_id, UIContextMenuOptions::default());
let item_list = vec![UIContextMenuItem::create_disabled("Undo"), UIContextMenuItem::create_disabled("Redo")];
node_tree.with_component_mut::<UIContextMenu, _>(menu_id, |context_menu, tree| {
	context_menu.show(tree, Vector2::create(780.0, 590.0), item_list);
});
let panel_id = node_tree[menu_id].get_component::<UIContextMenu>().expect("menu").get_panel_node().expect("panel");
assert_eq!(node_tree[panel_id].get_local_position(), Vector2::create(628.0, 532.0));
메서드methodpub fn close(&mut self, node_tree: &mut NodeTree)#0.3.2 부터since 0.3.2소스source

닫기.

Close.

node_tree&mut NodeTree
메뉴가 든 노드 트리. (호스트 노드를 끈다, 행 노드는 다음 show 까지 남는다)Node tree that holds the menu. (turns off the host node, row nodes remain until the next show)

짝Counterpart: vanilla.js UIContextMenu.close

메서드methodpub fn is_showing(&self) -> bool#0.3.2 부터since 0.3.2소스source

보이는 중인지.

Whether it is showing.

반환Returnsbool show 뒤 close 전이면 true.true if after show and before close.

짝Counterpart: vanilla.js UIContextMenu.isShowing

메서드methodpub fn get_overlay_node(&self) -> Option<NodeId>#0.3.2 부터since 0.3.2소스source

바깥 누름 막 노드 핸들.

Outside-press blocker node handle.

반환ReturnsOption<NodeId> 투명한 막 노드. (트리에 붙기 전이면 None)Transparent blocker node. (None before being attached to a tree)

메서드methodpub fn get_panel_node(&self) -> Option<NodeId>#0.3.2 부터since 0.3.2소스source

메뉴 판 노드 핸들.

Menu panel node handle.

반환ReturnsOption<NodeId> 메뉴 판 노드. (트리에 붙기 전이면 None)Menu panel node. (None before being attached to a tree)

메서드methodpub fn get_item_nodes(&self) -> &[NodeId]#0.3.2 부터since 0.3.2소스source

항목 행 노드 핸들 목록.

List of item row node handles.

반환Returns&[NodeId] 마지막 show 가 만든 행 노드. (항목 차례, show 마다 다시 만든다)Row nodes created by the last show. (in item order, rebuilt on every show)