# core/node/worldnode (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/core/node/worldnode/

계층적 영역 객체.

## WorldNode extends TransformNode

계층적 영역 객체.
- Anchor, Pivot, ConetentSize 기능.

예제: 부모 안에 사각형으로 놓고 맞는지 보기

```js
import { WorldNode, Vector2 } from "./build/vanilla.js";

const panel = new WorldNode();
panel.setPivot(Vector2.create(0, 0));
panel.setContentSize(Vector2.create(400, 300));
const button = new WorldNode();
button.setRect(20, 20, 120, 40);
button.setInteractable(true);
panel.addChild(button);
const corners = button.getWorldCorners();
console.log(corners[0].x, corners[0].y, corners[2].x, corners[2].y);
const touchPosition = Vector2.create(50, 30);
const isInside = button.contains(touchPosition);
const localPosition = button.convertViewToLocal(touchPosition);
console.log(isInside, localPosition.x, localPosition.y);
```

- `constructor()` (constructor, since 0.1.0): 생성.
- `pushTransform(graphic)` (method, since 0.1.0): 출력 상태 시작.
  - `graphic` Graphic: 변환을 걸 그래픽.
- `draw(graphic)` (method, since 0.2.1): 출력. (오버라이드: Mask 컴포넌트가 부착돼 있으면 자식을 그 영역으로 크롭)
  - `graphic` Graphic: 그릴 그래픽.
- `calculateDrawnRect(rect)` (method, since 0.4.0): 노드 스스로(컴포넌트와 자식 밖에) 그리는 사각형을 rect 에 적기. (내용 영역의 왼쪽 위가 원점, 알면 true)
  - draw 를 덮어쓰지 않은 노드는 스스로 그리는 것이 없으므로 크기 0 을 적고 true.
  - draw 를 덮어쓴 노드는 어디에 그리는지 모르므로 false. (그 노드의 조상은 하위 트리 그리기를 건너뛰지 않는다)
    덮어쓴 노드가 이 함수도 덮어써 그리는 사각형을 알리면 건너뛸 수 있다. 사각형이 바뀌면 markDrawBoundsDirty 를 부른다.
  - `rect` Rect: 그리는 사각형을 적을 곳. (노드 내용 영역의 왼쪽 위가 원점)
  - returns boolean: 알면 참.

  예제: 스스로 그리는 노드가 그리는 곳 알리기

  ```js
  import { WorldNode, Rect, Vector2, Colors } from "./build/vanilla.js";
  
  class Badge extends WorldNode {
  	draw(graphic) {
  		super.draw(graphic);
  		const contentSize = this.getContentSize();
  		graphic.setFillColor(Colors.white);
  		graphic.drawRect(Rect.create(contentSize.x - 6, -6, 12, 12));
  	}
  	calculateDrawnRect(rect) {
  		const contentSize = this.getContentSize();
  		rect.position.set(contentSize.x - 6, -6);
  		rect.size.set(12, 12);
  		return true;
  	}
  }
  
  const badge = new Badge();
  badge.setContentSize(Vector2.create(40, 40));
  const drawnRect = Rect.zero();
  console.log(badge.calculateDrawnRect(drawnRect), drawnRect.position.x);
  ```

- `transformDrawBoundsToParent(rect)` (method, since 0.4.0): 하위 트리 그리기 경계를 부모의 내용 좌표로 옮기기. (오버라이드: 위치, 회전, 배율, 피봇으로, pushTransform 과 같은 차례)
  - pushTransform 이나 위치, 배율, 회전, 피봇, 내용 크기 읽기를 덮어쓴 노드는 어디에 놓이는지 모르므로 false. (부모는 건너뛰지 않고 이 노드 아래는 저마다 건너뛸 수 있다)
  - `rect` Rect: 옮길 경계. (이 노드의 내용 좌표, 부모의 내용 좌표로 바뀐 경계 상자를 적는다)
  - returns boolean: 놓이는 곳을 알면 참.

  예제: 자식의 그리기 경계를 부모 좌표로 옮기기

  ```js
  import { WorldNode, Rect, Vector2, Pivot } from "./build/vanilla.js";
  
  const node = new WorldNode();
  node.setPivot(Pivot.topLeft);
  node.setLocalPosition(Vector2.create(100, 20));
  node.setLocalScale(Vector2.create(2, 2));
  const drawBounds = Rect.create(0, 0, 10, 10);
  node.transformDrawBoundsToParent(drawBounds);
  console.log(drawBounds.position.x, drawBounds.size.x);
  ```

- `drawGizmos(graphic)` (method, since 0.1.0): 출력.
  - `graphic` Graphic: 그릴 그래픽.
- `setPivot(pivot)` (method, since 0.1.0): 기준점 설정.
  - `pivot` Vector2: 기준점. (내용 크기에 대한 비, 0 ~ 1 로 자르고, (0, 0) 이 왼쪽 위)
- `getPivot()` (method, since 0.1.0): 기준점 반환.
  - returns Vector2: 기준점의 사본.
- `readPivot(out)` (method, since 0.4.0): 기준점을 주어진 벡터에 쓰기. (getPivot 과 같은 값, 복제하지 않는다)
  - `out` Vector2: 쓸 벡터.
  - returns Vector2: out.

  예제: 기준점 읽기

  ```js
  import { WorldNode, Vector2 } from "./vanilla.js";
  const node = new WorldNode();
  node.setPivot(Vector2.create(0.5, 1));
  const pivot = node.readPivot(Vector2.create(0, 0));
  if (pivot.x !== 0.5 || pivot.y !== 1) {
  	throw new Error("pivot failed");
  }
  ```

- `setContentSize(contentSize)` (method, since 0.1.0): 실제 내용 크기 설정.
  - `contentSize` Vector2: 내용 크기. (값을 옮겨 적는다)
- `setContentSizeXY(width, height)` (method, since 0.4.0): 실제 내용 크기 설정. (너비, 높이를 바로 받는다, Vector2 를 만들지 않는다)
  - setContentSize() 를 덮어쓴 하위 클래스면 setContentSize 로 넘긴다.
  - `width` number: 내용 너비.
  - `height` number: 내용 높이.
- `setRect(x, y, width, height)` (method, since 0.4.0): 부모 영역 안의 사각형에 놓기. (x, y 는 이 노드의 왼쪽 위, 부모 내용 영역의 왼쪽 위가 원점)
  - 내용 크기를 width, height 로 두고, 피봇이 어디든 왼쪽 위가 (x, y) 에 오게 로컬 위치를 정한다. (x + width * pivot.x, y + height * pivot.y)
  - 앵커는 바꾸지 않는다. 로컬 위치는 앵커와 무관하게 부모 왼쪽 위 기준이라 앵커가 어디든 같은 자리다. (getAnchoredPosition 은 그 앵커 기준으로 다시 셈해 준다)
  - 로컬 배율과 회전은 보지 않는다. (배율 1, 회전 0 일 때 화면에서도 그 사각형이다)
  - `x` number: 왼쪽 위 X. (부모 내용 영역의 왼쪽 위가 원점)
  - `y` number: 왼쪽 위 Y. (부모 내용 영역의 왼쪽 위가 원점)
  - `width` number: 내용 너비.
  - `height` number: 내용 높이.
- `getContentSize()` (method, since 0.1.0): 실제 내용 크기 반환.
  - 부모가 없고(루트 노드) 명시적으로 설정된 적 없으면 현재 뷰 크기를 반환한다.
  - returns Vector2: 내용 크기의 사본.
- `readContentSize(out)` (method, since 0.4.0): 내용 크기를 주어진 벡터에 쓰기. (getContentSize 와 같은 값, 복제하지 않는다)
  - `out` Vector2: 쓸 벡터.
  - returns Vector2: out.

  예제: 내용 크기 읽기

  ```js
  import { WorldNode, Vector2 } from "./vanilla.js";
  const node = new WorldNode();
  node.setContentSize(Vector2.create(120, 40));
  const contentSize = node.readContentSize(Vector2.create(0, 0));
  if (contentSize.x !== 120 || contentSize.y !== 40) {
  	throw new Error("content size failed");
  }
  ```

- `copyContentSizeTo(target)` (method, since 0.4.0): 실제 내용 크기를 target 에 적는다. (복제본을 만들지 않는다, Paint 가 그릴 때마다 쓴다)
  - getContentSize() 를 덮어쓴 하위 클래스면 그 함수가 주는 값을 적는다.
  - `target` Vector2: 내용 크기를 받을 벡터.
- `setAnchor(anchor)` (method, since 0.1.0): 앵커 설정.
  - `anchor` Vector2: 앵커. (부모 내용 크기에 대한 비, 0 ~ 1 로 자른다)
- `getAnchor()` (method, since 0.1.0): 앵커 반환.
  - returns Vector2: 앵커. (안의 벡터 그대로)
- `setAnchoredPosition(anchoredPosition)` (method, since 0.2.0): 앵커 기준 위치 설정.
  - anchoredPosition: 부모 영역 내 anchor 기준점(= parentContentSize * anchor)에서의 상대 위치.
  - 내부적으로 localPosition = anchorRef + anchoredPosition 으로 변환해 저장한다.
    (localPosition 은 anchor 와 무관하게 부모 영역 내 자기 중심점 좌표를 나타냄)
  - `anchoredPosition` Vector2: 앵커 기준 자리. (부모가 WorldNode 가 아니면 로컬 위치로 쓰고, null 이면 아무것도 하지 않는다)

  예제: 부모의 오른쪽 위 귀퉁이에서 띄워 놓기

  ```js
  import { WorldNode, Vector2 } from "./build/vanilla.js";
  
  const parent = new WorldNode();
  parent.setContentSize(Vector2.create(800, 600));
  const closeButton = new WorldNode();
  parent.addChild(closeButton);
  closeButton.setAnchor(Vector2.create(1, 0));
  closeButton.setAnchoredPosition(Vector2.create(-50, 40));
  const localPosition = closeButton.getLocalPosition();
  const anchoredPosition = closeButton.getAnchoredPosition();
  console.log(localPosition.x, localPosition.y, anchoredPosition.x, anchoredPosition.y);
  ```

- `getAnchoredPosition()` (method, since 0.2.0): 앵커 기준 위치 반환.
  - localPosition - anchorRef 로 역산해서 반환한다.
  - returns Vector2: 앵커 기준 자리. (부모가 WorldNode 가 아니면 로컬 위치)
- `getPosition()` (method, since 0.1.0): 글로벌 위치 반환.
  - localPosition 은 부모 content top-left 기준 자기 객체 중심 좌표.
  - 부모의 pivot 점은 (parentContentSize * parentPivot) 위치이므로
    parent.getPosition() (= 부모 pivot 점의 월드 좌표) 기준 오프셋으로 변환 후
    부모 회전/스케일을 적용한다. (anchor 는 위치 계산에 직접 영향 없음)
  - returns Vector2: 월드 자리. (기준점의 자리)
- `readPosition(out)` (method, since 0.4.0): 글로벌 위치를 주어진 벡터에 쓰기. (getPosition 과 같은 셈과 차례, 새 벡터를 짓지 않는다)
  - getPosition 이나 부모의 내용 크기, 기준점 읽기를 덮어쓴 하위 클래스면 그 함수로 읽는다.
  - `out` Vector2: 쓸 벡터.
  - returns Vector2: out. (기준점의 자리)
- `setPosition(position)` (method, since 0.4.0): 글로벌 위치 설정. (getPosition 의 거꾸로)
  - parent.getPosition() (= 부모 pivot 점의 월드 좌표) 에서 본 차이를 부모 회전/스케일을 풀어 부모 pivot 점 기준 오프셋으로 되돌린 뒤
    부모 content top-left 기준(localPosition)으로 옮긴다. 부모 스케일이 0 인 축은 부모 pivot 점에 놓는다.
  - `position` Vector2: 월드 자리. (기준점의 자리, 부모의 회전과 배율을 풀어 로컬 위치로 바꾼다)
- `getWorldCorners()` (method, since 0.1.0): 실제 화면에 그려지는 영역 반환. (OBB)
  - returns Vector2[]: 네 귀퉁이. (왼쪽 위, 오른쪽 위, 오른쪽 아래, 왼쪽 아래 차례, 뷰 좌표)
- `getWorldBounds()` (method, since 0.1.0): getWorldCorners()를 기반으로 최소, 최대위치를 만들어 바운딩박스를 형성.
  - returns Rect: 네 귀퉁이를 감싸는 축 정렬 사각형.
- `contains(viewPosition)` (method, since 0.1.0): getWorldCorners() 를 통한 충돌 검출.
  - `viewPosition` Vector2: 볼 뷰 좌표.
  - returns boolean: 네 귀퉁이 안이면 참. (null 이면 거짓)
- `convertViewToLocal(viewPosition)` (method, since 0.4.0): 뷰 좌표 → 이 노드의 로컬 좌표. (내용 영역의 왼쪽 위가 원점, 오른쪽 아래가 내용 크기)
  - 부모들의 위치, 배율, 회전을 모두 거친 네 귀퉁이(getWorldCorners)로 셈하므로 contains 와 같은 판정이다.
  - 내용 크기가 0 인 축은 0 을 돌려준다.
  - `viewPosition` Vector2: 바꿀 뷰 좌표.
  - returns Vector2: 로컬 좌표. (내용 영역의 왼쪽 위가 원점)
- `convertNodePositionToViewPosition(nodePosition)` (method, since 0.4.0): 노드 영역 안의 점 → 뷰 좌표. (내용 영역의 왼쪽 위가 원점, 부모들의 변환을 모두 거친다, 러스트 판 NodeTree::convert_node_position_to_view_position)
  - `nodePosition` Vector2: 이 노드 내용 좌표의 점.
  - returns Vector2: 뷰 좌표의 점.

  예제: 단추 오른쪽 아래 귀퉁이의 뷰 자리

  ```js
  import { Vector2, WorldNode } from "./build/vanilla.js";
  
  const rootNode = new WorldNode();
  const buttonNode = new WorldNode();
  rootNode.addChild(buttonNode);
  buttonNode.setLocalPosition(Vector2.create(100, 100));
  buttonNode.setContentSize(Vector2.create(40, 20));
  const corner = buttonNode.convertNodePositionToViewPosition(Vector2.create(40, 20));
  console.log(corner.x, corner.y);
  ```

- `convertViewPositionToNodePosition(viewPosition)` (method, since 0.4.0): 뷰 좌표 → 노드 영역 안의 점. (행렬을 뒤집을 수 없으면(배율 0) null, 러스트 판 NodeTree::convert_view_position_to_node_position)
  - `viewPosition` Vector2: 뷰 좌표의 점.
  - returns Vector2 | null: 이 노드 내용 좌표의 점. (배율이 0 이라 뒤집을 수 없으면 null)

  예제: 뷰 자리를 노드 안 자리로 바꾸고 되돌리기

  ```js
  import { Vector2, WorldNode } from "./build/vanilla.js";
  
  const rootNode = new WorldNode();
  const panelNode = new WorldNode();
  rootNode.addChild(panelNode);
  panelNode.setLocalPosition(Vector2.create(200, 100));
  panelNode.setContentSize(Vector2.create(100, 50));
  panelNode.setLocalScale(Vector2.create(2, 2));
  const nodePosition = panelNode.convertViewPositionToNodePosition(Vector2.create(200, 100));
  console.log(nodePosition.x, nodePosition.y);
  panelNode.setLocalScale(Vector2.create(0, 1));
  console.log(panelNode.convertViewPositionToNodePosition(Vector2.create(200, 100)));
  ```

- `setInteractable(isInteractable)` (method, since 0.2.0): 터치 인터랙션 활성화 설정.
  - `isInteractable` boolean: 참이면 TouchRaycaster 가 이 노드를 맞힌다.
- `isInteractable()` (method, since 0.2.0): 터치 인터랙션 활성화 여부 반환.
  - returns boolean: 누를 수 있으면 참.
- `touchPress(viewInputPosition)` (method, since 0.2.0): 터치 누름. (TouchRaycaster에 의해 호출, 컴포넌트에 전달)
  - `viewInputPosition` Vector2: 뷰 좌표의 입력 위치.
- `touchMove(viewInputPosition)` (method, since 0.2.0): 터치 이동. (TouchRaycaster에 의해 호출, 컴포넌트에 전달)
  - `viewInputPosition` Vector2: 뷰 좌표의 입력 위치.
- `touchRelease(viewInputPosition)` (method, since 0.2.0): 터치 뗌. (TouchRaycaster에 의해 호출, 컴포넌트에 전달)
  - `viewInputPosition` Vector2: 뷰 좌표의 입력 위치.
- `touchCancel(viewInputPosition)` (method, since 0.2.0): 터치 취소. (TouchRaycaster에 의해 호출, 컴포넌트에 전달)
  - `viewInputPosition` Vector2: 뷰 좌표의 입력 위치.
