misc/nodelayout
노드 레이아웃.
- 원본 misc/nodelayout.js 를 옮긴 것이다. 지역 변수 없이 노드 계층을 선언하듯 짜는 플루언트 빌더다.
- Vector2 없이 숫자쌍(x, y)으로 위치와 크기를 정한다.
- 사용:
let panel_id = NodeLayout::create()
.pivot(Pivot::MIDDLE_CENTER)
.content_size(680.0, 900.0)
.component_with(Paint::new(), |paint| { paint.set_color(Color::create(0.1, 0.1, 0.1, 1.0)); })
.children([
NodeLayout::create()
.content_size(120.0, 70.0)
.component_with(Text::new(), |text| { text.set_text("확인"); }),
])
.build(node_tree, Some(parent_id));- 원본과 다른 점: 러스트 판은 노드가 트리(NodeTree) 안에만 있어서, 빌더는 설정을 차례대로 모아 두었다가 build(node_tree, parent_id) 때 노드를 만들고 그 순서대로 건다. 그래서 컴포넌트도 build 때 node_tree.add_component 로 붙는다. (attach_to_tree 가 불려 자식 노드를 만드는 컴포넌트도 된다) 노드 클래스가 없으므로 create(nodeClass) 대신 create() 다. component(type, callback) 은 component(값) 과 component_with(값, 콜백) 둘로 나눴다. (러스트는 생략 인자가 없다) children(...layouts) 의 가변 인자는 배열이나 Vec 하나로 받는다. anchored_position 은 원본처럼 build 때 부모에 붙인 뒤 부모 content_size 의 앵커 점 기준으로 건다. (앵커는 그 설정을 걸 때의 앵커, 나중에 건 local_position 이 이긴다) mask_enabled(false) 는 원본처럼 첫 마스크만이 아니라 그 노드의 Mask 를 모두 뗀다.
Node layout.
- A port of the original misc/nodelayout.js. A fluent builder that lays out node hierarchies declaratively without local variables.
- Positions and sizes are set with number pairs (x, y) without Vector2.
- Usage:
let panel_id = NodeLayout::create()
.pivot(Pivot::MIDDLE_CENTER)
.content_size(680.0, 900.0)
.component_with(Paint::new(), |paint| { paint.set_color(Color::create(0.1, 0.1, 0.1, 1.0)); })
.children([
NodeLayout::create()
.content_size(120.0, 70.0)
.component_with(Text::new(), |text| { text.set_text("OK"); }),
])
.build(node_tree, Some(parent_id));- Differences from the original: In the Rust version nodes live only inside the tree (NodeTree), so the builder collects settings in order and, at build(node_tree, parent_id), creates the nodes and applies them in that order. So components are also attached with node_tree.add_component at build. (components whose attach_to_tree creates child nodes work too) There is no node class, so it is create() instead of create(nodeClass). component(type, callback) is split into component(value) and component_with(value, callback). (Rust has no optional arguments) The variadic arguments of children(...layouts) are taken as one array or Vec. Like the original, anchored_position is applied at build after attaching to the parent, relative to the anchor point of the parent content_size. (the anchor is the one at the time the setting is applied, a local_position set later wins) Like the original, mask_enabled(false) removes all Masks of that node, not only the first mask.
NodeLayoutFanOptionsstruct traits Clone, Copy, Debug, PartialEq, Default
부채꼴 배치 설정. (원본 options = { center_x, center_y, radius, angle_step_radian = 0.12, base_angle_radian = -PI/2, rotate_nodes = true })
Fan layout settings. (original options = { center_x, center_y, radius, angle_step_radian = 0.12, base_angle_radian = -PI/2, rotate_nodes = true })
use vanilla::NodeLayoutFanOptions;
let fan_options = NodeLayoutFanOptions {
center_x: 640.0,
center_y: 900.0,
radius: 400.0,
..NodeLayoutFanOptions::default()
};
assert_eq!(fan_options.angle_step_radian, 0.12);
assert!(fan_options.rotate_nodes);호의 중심 y. (center_x 와 짝, 부모 영역 기준 로컬 좌표)
Center y of the arc. (paired with center_x, local coordinates relative to the parent area)
반환Returnsf64
이웃한 항목 사이 각도. (라디안)
Angle between neighboring items. (radians)
반환Returnsf64
가운데 항목이 바라보는 방향. (라디안, 기본 위쪽)
Direction the middle item faces. (radians, up by default)
반환Returnsf64
항목 회전도 호의 접선에 맞출지.
Whether to also align item rotation to the arc tangent.
반환Returnsbool
NodeLayoutstruct traits Default
노드 레이아웃.
- 설정 메서드는 self 를 받아 돌려주므로 사슬로 잇고, 끝에 build 로 노드를 만든다.
Node layout.
- Setting methods take and return self so they chain, and build at the end creates the node.
use vanilla::{Color, NodeLayout, NodeTree, Paint, Pivot, Text, Vector2};
let mut node_tree = NodeTree::new();
let parent_id = node_tree.get_root_id();
let panel_id = NodeLayout::create()
.pivot(Pivot::MIDDLE_CENTER)
.content_size(680.0, 900.0)
.component_with(Paint::new(), |paint| {
paint.set_color(Color::create(0.1, 0.1, 0.1, 1.0));
})
.children([
NodeLayout::create()
.name("label")
.content_size(120.0, 70.0)
.component_with(Text::new(), |text| {
text.set_text("OK");
}),
])
.build(&mut node_tree, Some(parent_id));
assert_eq!(node_tree[panel_id].get_content_size(), Vector2::create(680.0, 900.0));
let label_id = node_tree.find_child_by_name(panel_id, "label").unwrap();
assert_eq!(node_tree[label_id].get_component::<Text>().unwrap().get_text(), "OK");생성. (빈 설정)
Constructor. (empty settings)
반환ReturnsNodeLayout 설정도 자식도 없는 새 레이아웃.A new layout with no settings and no children.
짝Counterpart: vanilla.js NodeLayout.constructor
레이아웃 만들기. (원본 NodeLayout.create(WorldNode))
Creates a layout. (original NodeLayout.create(WorldNode))
반환ReturnsNodeLayout 설정도 자식도 없는 새 레이아웃. (new 와 같다)A new layout with no settings and no children. (same as new)
짝Counterpart: vanilla.js NodeLayout.create
pub fn arrange_fan(node_tree: &mut NodeTree, node_ids: &[NodeId], options: NodeLayoutFanOptions)#부채꼴 배치. (손패 카드처럼 노드들을 호 위에 펼친다)
- 가운데 항목이 base_angle_radian 방향을 보도록 좌우 대칭으로 벌린다.
- rotate_nodes 면 각 노드의 회전(도)도 호의 접선에 맞춘다.
- 노드의 로컬 위치를 바로 바꾼다. (빌더와 따로 쓰는 정적 함수)
Fan layout. (spreads nodes on an arc, like cards in a hand)
- Spreads symmetrically left and right so the middle item faces the base_angle_radian direction.
- If rotate_nodes, also aligns each node's rotation (degrees) to the arc tangent.
- Changes the nodes' local positions directly. (a static function used separately from the builder)
- node_tree&mut NodeTree
- 노드가 든 트리.Tree holding the node.
- node_ids&[NodeId]
- 펼칠 노드 목록. (이 차례로 왼쪽에서 오른쪽, 트리에 없는 노드는 건너뛴다)List of nodes to spread. (left to right in this order, nodes not in the tree are skipped)
- optionsNodeLayoutFanOptions
- 호의 중심, 반지름, 간격, 방향, 회전 여부.Arc center, radius, spacing, direction, whether to rotate.
짝Counterpart: vanilla.js NodeLayout.arrangeFan
use vanilla::{NodeId, NodeLayout, NodeLayoutFanOptions, NodeTree};
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let card_ids: Vec<NodeId> = (0..3).map(|_| node_tree.create_child(root_id, "card")).collect();
NodeLayout::arrange_fan(&mut node_tree, &card_ids, NodeLayoutFanOptions {
center_x: 100.0,
center_y: 500.0,
radius: 200.0,
..NodeLayoutFanOptions::default()
});
let middle_position = node_tree[card_ids[1]].get_local_position();
assert!((middle_position.x - 100.0).abs() < 0.001);
assert!((middle_position.y - 300.0).abs() < 0.001);
assert_eq!(node_tree[card_ids[1]].get_local_rotation(), 0.0);
assert!(node_tree[card_ids[2]].get_local_rotation() > 0.0);노드 직접 접근. (빌더에 없는 속성을 정할 때)
Direct node access. (for setting properties the builder lacks)
- callbackimpl FnOnce(&mut WorldNode) + 'static
- build 때 만든 노드를 받아 고칠 함수. (앞뒤 설정과 붙인 순서대로 불린다)Function that receives and modifies the node created at build. (called in the order it was attached among the other settings)
반환ReturnsNodeLayout 설정을 하나 더 붙인 레이아웃.The layout with one more setting attached.
짝Counterpart: vanilla.js NodeLayout.apply
use vanilla::{NodeLayout, NodeTree};
let mut node_tree = NodeTree::new();
let button_id = NodeLayout::create()
.apply(|node| {
node.set_interactable(true);
})
.build(&mut node_tree, None);
assert!(node_tree[button_id].is_interactable());활성화 설정.
Sets active.
- is_activebool
- 노드를 켤지.Whether to enable the node.
반환ReturnsNodeLayout 설정을 하나 더 붙인 레이아웃.The layout with one more setting attached.
짝Counterpart: vanilla.js NodeLayout.active
이름 설정.
Sets the name.
- name&str
- 노드 이름. (빌드한 노드의 처음 이름은 빈 글)Node name. (the initial name of a built node is an empty string)
반환ReturnsNodeLayout 설정을 하나 더 붙인 레이아웃.The layout with one more setting attached.
짝Counterpart: vanilla.js NodeLayout.name
컴포넌트 추가. (설정은 넘기기 전에 값에 해 둔다)
Adds a component. (configure the value before passing it)
- componentT
- build 때 node_tree.add_component 로 붙일 컴포넌트.Component to attach with node_tree.add_component at build.
반환ReturnsNodeLayout 설정을 하나 더 붙인 레이아웃.The layout with one more setting attached.
짝Counterpart: vanilla.js NodeLayout.component
pub fn component_with<T: Component + 'static>(self, component: T, callback: impl FnOnce(&mut T) + 'static) -> NodeLayout#컴포넌트 추가 후 콜백으로 설정. (원본 component(type, callback))
Adds a component and configures it with a callback. (original component(type, callback))
- componentT
- build 때 붙일 컴포넌트.Component to attach at build.
- callbackimpl FnOnce(&mut T) + 'static
- 붙인 뒤 그 컴포넌트를 받아 설정할 함수. (붙이지 못하면 부르지 않는다)Function that receives and configures that component after attaching. (not called if it could not be attached)
반환ReturnsNodeLayout 설정을 하나 더 붙인 레이아웃.The layout with one more setting attached.
자식 레이아웃 추가. (여러 개를 한 번에, 붙인 순서대로 그려진다)
Adds child layouts. (several at once, drawn in the order attached)
- layoutsimpl IntoIterator<Item = NodeLayout>
- 자식 레이아웃 배열이나 Vec. (build 때 이 노드를 부모로 재귀 빌드한다)Array or Vec of child layouts. (recursively built with this node as the parent at build)
반환ReturnsNodeLayout 자식을 더한 레이아웃.The layout with the children added.
짝Counterpart: vanilla.js NodeLayout.children
로컬 위치 설정.
Sets the local position.
- xf64
- 로컬 위치 x. (부모 영역 기준, 픽셀)Local position x. (relative to the parent area, pixels)
- yf64
- 로컬 위치 y. (픽셀)Local position y. (pixels)
반환ReturnsNodeLayout 설정을 하나 더 붙인 레이아웃.The layout with one more setting attached.
짝Counterpart: vanilla.js NodeLayout.localPosition
피봇 설정.
Sets the pivot.
- pivotVector2
- 피봇. (0 ~ 1, Pivot::MIDDLE_CENTER 같은 상수)Pivot. (0 ~ 1, a constant like Pivot::MIDDLE_CENTER)
반환ReturnsNodeLayout 설정을 하나 더 붙인 레이아웃.The layout with one more setting attached.
짝Counterpart: vanilla.js NodeLayout.pivot
컨텐츠 크기 설정.
Sets the content size.
- xf64
- 너비. (픽셀)Width. (pixels)
- yf64
- 높이. (픽셀)Height. (pixels)
반환ReturnsNodeLayout 설정을 하나 더 붙인 레이아웃.The layout with one more setting attached.
짝Counterpart: vanilla.js NodeLayout.contentSize
로컬 크기 설정.
Sets the local scale.
- xf64
- 가로 배율. (1 이 원래 크기)Horizontal scale. (1 is the original size)
- yf64
- 세로 배율.Vertical scale.
반환ReturnsNodeLayout 설정을 하나 더 붙인 레이아웃.The layout with one more setting attached.
짝Counterpart: vanilla.js NodeLayout.localScale
부모 노드 설정. (build 에 부모를 넘기지 않으면 이 부모의 자식으로 붙는다)
Sets the parent node. (if no parent is passed to build, attaches as a child of this parent)
- parent_idNodeId
- 붙일 부모 노드.Parent node to attach to.
반환ReturnsNodeLayout 부모를 정한 레이아웃.The layout with the parent set.
짝Counterpart: vanilla.js NodeLayout.parent
앵커 설정.
Sets the anchor.
- xf64
- 가로 앵커. (0 ~ 1, 부모 content_size 의 비율)Horizontal anchor. (0 ~ 1, ratio of the parent content_size)
- yf64
- 세로 앵커. (0 ~ 1)Vertical anchor. (0 ~ 1)
반환ReturnsNodeLayout 설정을 하나 더 붙인 레이아웃.The layout with one more setting attached.
짝Counterpart: vanilla.js NodeLayout.anchor
앵커 기준 위치 설정. (부모가 있으면 부모 content_size 의 앵커 점 기준, 없으면 로컬 위치 그대로)
- build 때 거는 순간의 앵커와 부모 크기로 셈한다. 그러니 anchor 를 이것보다 먼저 붙인다.
Sets the anchored position. (relative to the anchor point of the parent content_size if there is a parent, otherwise the local position as is)
- Computed at build from the anchor and parent size at the moment it is applied. So attach anchor before this.
- xf64
- 앵커 점에서 가로 거리. (픽셀)Horizontal distance from the anchor point. (pixels)
- yf64
- 앵커 점에서 세로 거리. (픽셀)Vertical distance from the anchor point. (pixels)
반환ReturnsNodeLayout 설정을 하나 더 붙인 레이아웃.The layout with one more setting attached.
짝Counterpart: vanilla.js NodeLayout.anchoredPosition
use vanilla::{NodeLayout, NodeTree, Pivot, Vector2};
let mut node_tree = NodeTree::new();
let panel_id = NodeLayout::create()
.pivot(Pivot::TOP_LEFT)
.content_size(680.0, 900.0)
.children([
NodeLayout::create().name("corner").anchor(1.0, 1.0).anchored_position(-10.0, -20.0),
])
.build(&mut node_tree, None);
let corner_id = node_tree.find_child_by_name(panel_id, "corner").unwrap();
assert_eq!(node_tree[corner_id].get_local_position(), Vector2::create(670.0, 880.0));마스크 켜기 / 끄기. (켜면 Mask 가 없을 때만 붙이고, 끄면 뗀다)
Turns the mask on / off. (on attaches a Mask only if there is none, off removes it)
- is_enabledbool
- true 면 Mask 를 붙이고, false 면 그 노드의 Mask 를 모두 뗀다.true attaches a Mask, false removes all of the node's Masks.
반환ReturnsNodeLayout 설정을 하나 더 붙인 레이아웃.The layout with one more setting attached.
짝Counterpart: vanilla.js NodeLayout.maskEnabled
빌드. (노드를 만들어 부모에 붙이고, 모은 설정을 차례대로 건 뒤, 자식 레이아웃을 재귀로 빌드한다)
- parent_id 를 넘기면 그 부모에, 없으면 parent() 로 정한 부모에 붙인다. 둘 다 없으면 어디에도 붙지 않는다.
- 만든 노드의 핸들을 돌려준다.
Build. (creates the node and attaches it to the parent, applies the collected settings in order, then recursively builds the child layouts)
- Attaches to parent_id if passed, otherwise to the parent set with parent(). If neither, it is attached nowhere.
- Returns the handle of the created node.
- node_tree&mut NodeTree
- 노드를 만들 트리.Tree to create the node in.
- parent_idOption<NodeId>
- 붙일 부모. None 이면 parent() 로 정한 부모.Parent to attach to. None uses the parent set with parent().
반환ReturnsNodeId 만든 노드. (자식 레이아웃의 노드는 그 아래에 있다)The created node. (the child layouts' nodes are beneath it)
짝Counterpart: vanilla.js NodeLayout.build
use vanilla::{NodeLayout, NodeTree};
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let node_id = NodeLayout::create().name("hud").parent(root_id).build(&mut node_tree, None);
assert_eq!(node_tree.get_parent_id(node_id), Some(root_id));
let detached_id = NodeLayout::create().build(&mut node_tree, None);
assert_eq!(node_tree.get_parent_id(detached_id), None);