# misc/nodelayout (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/misc/nodelayout/

노드 레이아웃.
- 원본 misc/nodelayout.js 를 옮긴 것이다. 지역 변수 없이 노드 계층을 선언하듯 짜는 플루언트 빌더다.
- Vector2 없이 숫자쌍(x, y)으로 위치와 크기를 정한다.
- 사용:
  ```text
    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 를 모두 뗀다.

## NodeLayoutFanOptions (struct) (Clone, Copy, Debug, PartialEq, Default)

부채꼴 배치 설정. (원본 options = { center_x, center_y, radius, angle_step_radian = 0.12, base_angle_radian = -PI/2, rotate_nodes = true })

예제: 중심과 반지름만 정하고 나머지는 기본값

```rust
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);
```

- `pub center_x: f64` (field, since 0.3.2): 호의 중심.
  - returns f64
- `pub center_y: f64` (field, since 0.3.2): 호의 중심 y. (center_x 와 짝, 부모 영역 기준 로컬 좌표)
  - returns f64
- `pub radius: f64` (field, since 0.3.2): 호의 반지름.
  - returns f64
- `pub angle_step_radian: f64` (field, since 0.3.2): 이웃한 항목 사이 각도. (라디안)
  - returns f64
- `pub base_angle_radian: f64` (field, since 0.3.2): 가운데 항목이 바라보는 방향. (라디안, 기본 위쪽)
  - returns f64
- `pub rotate_nodes: bool` (field, since 0.3.2): 항목 회전도 호의 접선에 맞출지.
  - returns bool

## NodeLayout (struct) (Default)

노드 레이아웃.
- 설정 메서드는 self 를 받아 돌려주므로 사슬로 잇고, 끝에 build 로 노드를 만든다.

예제: 패널과 그 안의 글 노드를 한 번에 짓기

```rust
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");
```

- `pub fn new() -> NodeLayout` (constructor, since 0.3.2): 생성. (빈 설정)
  - returns NodeLayout: 설정도 자식도 없는 새 레이아웃.
- `pub fn create() -> NodeLayout` (static, since 0.3.2): 레이아웃 만들기. (원본 NodeLayout.create(WorldNode))
  - returns NodeLayout: 설정도 자식도 없는 새 레이아웃. (new 와 같다)
- `pub fn arrange_fan(node_tree: &mut NodeTree, node_ids: &[NodeId], options: NodeLayoutFanOptions)` (static, since 0.3.2): 부채꼴 배치. (손패 카드처럼 노드들을 호 위에 펼친다)
  - 가운데 항목이 base_angle_radian 방향을 보도록 좌우 대칭으로 벌린다.
  - rotate_nodes 면 각 노드의 회전(도)도 호의 접선에 맞춘다.
  - 노드의 로컬 위치를 바로 바꾼다. (빌더와 따로 쓰는 정적 함수)
  - `node_tree` &mut NodeTree: 노드가 든 트리.
  - `node_ids` &[NodeId]: 펼칠 노드 목록. (이 차례로 왼쪽에서 오른쪽, 트리에 없는 노드는 건너뛴다)
  - `options` NodeLayoutFanOptions: 호의 중심, 반지름, 간격, 방향, 회전 여부.

  예제: 카드 세 장을 위쪽 호에 펼치기

  ```rust
  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);
  ```

- `pub fn apply(self, callback: impl FnOnce(&mut WorldNode) + 'static) -> NodeLayout` (method, since 0.3.2): 노드 직접 접근. (빌더에 없는 속성을 정할 때)
  - `callback` impl FnOnce(&mut WorldNode) + 'static: build 때 만든 노드를 받아 고칠 함수. (앞뒤 설정과 붙인 순서대로 불린다)
  - returns NodeLayout: 설정을 하나 더 붙인 레이아웃.

  예제: 누를 수 있는 노드로 만들기

  ```rust
  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());
  ```

- `pub fn active(self, is_active: bool) -> NodeLayout` (method, since 0.3.2): 활성화 설정.
  - `is_active` bool: 노드를 켤지.
  - returns NodeLayout: 설정을 하나 더 붙인 레이아웃.
- `pub fn name(self, name: &str) -> NodeLayout` (method, since 0.3.2): 이름 설정.
  - `name` &str: 노드 이름. (빌드한 노드의 처음 이름은 빈 글)
  - returns NodeLayout: 설정을 하나 더 붙인 레이아웃.
- `pub fn component<T: Component + 'static>(self, component: T) -> NodeLayout` (method, since 0.3.2): 컴포넌트 추가. (설정은 넘기기 전에 값에 해 둔다)
  - `component` T: build 때 node_tree.add_component 로 붙일 컴포넌트.
  - returns NodeLayout: 설정을 하나 더 붙인 레이아웃.
- `pub fn component_with<T: Component + 'static>(self, component: T, callback: impl FnOnce(&mut T) + 'static) -> NodeLayout` (method, since 0.3.2): 컴포넌트 추가 후 콜백으로 설정. (원본 component(type, callback))
  - `component` T: build 때 붙일 컴포넌트.
  - `callback` impl FnOnce(&mut T) + 'static: 붙인 뒤 그 컴포넌트를 받아 설정할 함수. (붙이지 못하면 부르지 않는다)
  - returns NodeLayout: 설정을 하나 더 붙인 레이아웃.
- `pub fn children(mut self, layouts: impl IntoIterator<Item = NodeLayout>) -> NodeLayout` (method, since 0.3.2): 자식 레이아웃 추가. (여러 개를 한 번에, 붙인 순서대로 그려진다)
  - `layouts` impl IntoIterator<Item = NodeLayout>: 자식 레이아웃 배열이나 Vec. (build 때 이 노드를 부모로 재귀 빌드한다)
  - returns NodeLayout: 자식을 더한 레이아웃.
- `pub fn local_position(self, x: f64, y: f64) -> NodeLayout` (method, since 0.3.2): 로컬 위치 설정.
  - `x` f64: 로컬 위치 x. (부모 영역 기준, 픽셀)
  - `y` f64: 로컬 위치 y. (픽셀)
  - returns NodeLayout: 설정을 하나 더 붙인 레이아웃.
- `pub fn pivot(self, pivot: Vector2) -> NodeLayout` (method, since 0.3.2): 피봇 설정.
  - `pivot` Vector2: 피봇. (0 ~ 1, Pivot::MIDDLE_CENTER 같은 상수)
  - returns NodeLayout: 설정을 하나 더 붙인 레이아웃.
- `pub fn content_size(self, x: f64, y: f64) -> NodeLayout` (method, since 0.3.2): 컨텐츠 크기 설정.
  - `x` f64: 너비. (픽셀)
  - `y` f64: 높이. (픽셀)
  - returns NodeLayout: 설정을 하나 더 붙인 레이아웃.
- `pub fn local_scale(self, x: f64, y: f64) -> NodeLayout` (method, since 0.3.2): 로컬 크기 설정.
  - `x` f64: 가로 배율. (1 이 원래 크기)
  - `y` f64: 세로 배율.
  - returns NodeLayout: 설정을 하나 더 붙인 레이아웃.
- `pub fn parent(mut self, parent_id: NodeId) -> NodeLayout` (method, since 0.3.2): 부모 노드 설정. (build 에 부모를 넘기지 않으면 이 부모의 자식으로 붙는다)
  - `parent_id` NodeId: 붙일 부모 노드.
  - returns NodeLayout: 부모를 정한 레이아웃.
- `pub fn anchor(self, x: f64, y: f64) -> NodeLayout` (method, since 0.3.2): 앵커 설정.
  - `x` f64: 가로 앵커. (0 ~ 1, 부모 content_size 의 비율)
  - `y` f64: 세로 앵커. (0 ~ 1)
  - returns NodeLayout: 설정을 하나 더 붙인 레이아웃.
- `pub fn anchored_position(self, x: f64, y: f64) -> NodeLayout` (method, since 0.3.2): 앵커 기준 위치 설정. (부모가 있으면 부모 content_size 의 앵커 점 기준, 없으면 로컬 위치 그대로)
  - build 때 거는 순간의 앵커와 부모 크기로 셈한다. 그러니 anchor 를 이것보다 먼저 붙인다.
  - `x` f64: 앵커 점에서 가로 거리. (픽셀)
  - `y` f64: 앵커 점에서 세로 거리. (픽셀)
  - returns NodeLayout: 설정을 하나 더 붙인 레이아웃.

  예제: 부모 오른쪽 아래 모서리에서 안쪽으로

  ```rust
  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));
  ```

- `pub fn mask_enabled(self, is_enabled: bool) -> NodeLayout` (method, since 0.3.2): 마스크 켜기 / 끄기. (켜면 Mask 가 없을 때만 붙이고, 끄면 뗀다)
  - `is_enabled` bool: true 면 Mask 를 붙이고, false 면 그 노드의 Mask 를 모두 뗀다.
  - returns NodeLayout: 설정을 하나 더 붙인 레이아웃.
- `pub fn build(self, node_tree: &mut NodeTree, parent_id: Option<NodeId>) -> NodeId` (method, since 0.3.2): 빌드. (노드를 만들어 부모에 붙이고, 모은 설정을 차례대로 건 뒤, 자식 레이아웃을 재귀로 빌드한다)
  - parent_id 를 넘기면 그 부모에, 없으면 parent() 로 정한 부모에 붙인다. 둘 다 없으면 어디에도 붙지 않는다.
  - 만든 노드의 핸들을 돌려준다.
  - `node_tree` &mut NodeTree: 노드를 만들 트리.
  - `parent_id` Option<NodeId>: 붙일 부모. None 이면 parent() 로 정한 부모.
  - returns NodeId: 만든 노드. (자식 레이아웃의 노드는 그 아래에 있다)

  예제: parent() 로 부모를 미리 정하고 None 으로 빌드

  ```rust
  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);
  ```

