# ui/uiview (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/ui/uiview/

뷰 컴포넌트.

## UIView extends Component

뷰 컴포넌트.
- ScrollView, SnapScrollView 등 모든 뷰 컴포넌트의 기반 클래스.
- 타입으로 하위 뷰 컴포넌트를 한번에 조회할 수 있다.
  예: node.getComponent(View)
- 컨테이너 역할을 하므로 UIControl 을 상속하지 않고 Component 직접 상속.
  대신 UIControl 이 갖던 setEngine/getEngine 를 자체적으로 보유한다.

예제: 배경색이 있는 판에 라벨 담기

```js
import { Engine, EngineConfiguration, UIScene, UIView, UILabel, WorldNode, Pivot, Vector2, Color } from "./build/vanilla.js";

class MainScene extends UIScene {
	constructor() {
		super();
		this.setLoadingMinDurationMs(0);
	}

	initialize(engine) {
		super.initialize(engine);
		const panelNode = new WorldNode();
		panelNode.setPivot(Pivot.topLeft);
		panelNode.setAnchor(Pivot.topLeft);
		panelNode.setLocalPosition(Vector2.create(40, 40));
		panelNode.setContentSize(Vector2.create(240, 160));
		const view = panelNode.addComponent(UIView);
		view.setBackgroundColor(new Color(0.1, 0.12, 0.16, 1));

		const labelNode = new WorldNode();
		labelNode.setPivot(Pivot.topLeft);
		labelNode.setAnchor(Pivot.topLeft);
		labelNode.setLocalPosition(Vector2.create(16, 16));
		labelNode.setContentSize(Vector2.create(208, 32));
		const label = labelNode.addComponent(UILabel);
		label.setText("Panel");
		label.setFontSize(20);
		const content = view.getContent();
		content.addChild(labelNode);

		const root = this.getRoot();
		root.addChild(panelNode);
	}
}

const engineConfiguration = new EngineConfiguration();
engineConfiguration.canvasId = "mainCanvas";
engineConfiguration.referenceResolutionSize = Vector2.create(640, 360);
engineConfiguration.autoResizeOnWindowResize = true;
const engine = new Engine(engineConfiguration);
engine.run(new MainScene());
```

- `constructor()` (constructor, since 0.2.0): 생성.
- `setEngine(engine)` (method, since 0.2.1): 엔진 설정. (구 UIComponent 에서 이관)
  - `engine` Engine: 엔진.
- `getEngine()` (method, since 0.2.1): 엔진 반환. (구 UIComponent 에서 이관)
  - returns Engine | null: setEngine 으로 준 엔진. (주지 않았으면 null)
- `attach(node)` (method, since 0.2.0): 노드에 붙음.
  - `node` ComponentNode: 붙을 노드.
- `detach(node)` (method, since 0.2.0): 노드에서 떨어짐.
  - `node` ComponentNode: 떨어질 노드.
- `isDrawnInsideContent()` (method, since 0.4.0): 내용 영역 안에서만 그리는지 반환. (배경은 내용 크기의 사각형만 칠한다, 화면 밖과 자르기 밖 거르기가 쓴다)
  - draw 를 덮어쓴 하위 클래스는 어디에 그리는지 모르므로 false. (그 클래스가 이 함수도 덮어써 알린다)
  - returns boolean: 내용 영역 안에서만 그리면 참.
- `writeGeometryKey(geometryKey)` (method, since 0.4.0): 그리기 입력을 기하 캐시 열쇠에 적기. (배경색, 내용 크기)
  - draw 를 덮어쓴 하위 클래스는 무엇을 읽는지 모르므로 false. (그 클래스가 이 함수도 덮어써 알린다)
  - `geometryKey` GeometryKey: 입력을 적을 열쇠.
  - returns boolean: 입력을 적었으면 참.
- `draw(graphic)` (method, since 0.2.0): 출력. (배경색이 설정된 경우 스크롤뷰 영역에 배경을 그린다)
  - `graphic` Graphic: 그래픽.
- `setBackdropBlur(blurRadius)` (method, since 0.4.0): 뒤 화면 흐림 설정. (CSS backdrop-filter: blur(), 언리얼 UMG BackgroundBlur 와 같은 뜻)
  - 켜면 이 뷰가 그려질 때, 그보다 먼저 그려진 화면을 이 뷰의 내용 영역만 흐리고 그 위에 배경색(반투명)을 얹는다.
  - 인자는 CSS backdrop-filter: blur(Npx) 의 N 과 같은 표준편차다. (노드 좌표 단위, 화면에서는 변환 배율을 곱한 기기 픽셀)
  - 흐림은 ScreenEffect 의 gaussianBlur(분리형 가우스, 선형 샘플링, 3 시그마까지, 모서리는 거울 접기)다. 크롬의 backdrop-filter 와 같다.
  - 화면에 그릴 때만 된다. (렌더 타겟에 그리는 중이면 흐리지 않는다) 켜진 동안은 기하 캐시를 쓰지 않는다. (매 그리기마다 화면을 읽는다)
  - `blurRadius` number: 흐림 표준편차. (CSS blur(Npx) 의 N, 노드 좌표 단위, 0 이하면 끈다, 처음은 0)

  예제: 반투명 패널 뒤를 흐리기

  ```js
  import { UIView, Color } from "./build/vanilla.js";
  
  const view = new UIView();
  view.setBackgroundColor(new Color(1, 1, 1, 0.25));
  view.setBackdropBlur(6);
  console.log(view.getBackdropBlur());
  ```

- `setBackdropCornerRadius(cornerRadius)` (method, since 0.4.0): 뒤 화면 흐림과 바탕색의 둥근 모서리 설정. (CSS border-radius 와 같은 뜻)
  - 흐림이 켜져 있을 때 흐림 영역과 바탕색이 이 반지름의 둥근 사각형 안에만 깔린다. (모서리 바깥은 뒤 화면 그대로)
  - `cornerRadius` number: 모서리 반지름. (노드 좌표 단위, 0 이하면 사각형, 처음은 0)

  예제: 둥근 단추 모양으로 뒤 흐리기

  ```js
  import { UIView, Color } from "./build/vanilla.js";
  
  const view = new UIView();
  view.setBackgroundColor(new Color(1, 1, 1, 0.25));
  view.setBackdropBlur(6);
  view.setBackdropCornerRadius(12);
  console.log(view.getBackdropCornerRadius());
  ```

- `getBackdropCornerRadius()` (method, since 0.4.0): 뒤 화면 흐림의 둥근 모서리 반지름 반환.
  - returns number: 모서리 반지름. (노드 좌표 단위, 0 이면 사각형)
- `getBackdropBlur()` (method, since 0.4.0): 뒤 화면 흐림 반지름 반환.
  - returns number: 흐림 표준편차. (CSS blur(Npx) 의 N, 0 이면 꺼짐)
- `touchPress(viewInputPosition)` (method, since 0.2.0): 터치 누름. (TouchRaycaster → WorldNode)
  - `viewInputPosition` Vector2: 누른 자리. (뷰 좌표)
- `touchMove(viewInputPosition)` (method, since 0.2.0): 터치 이동. (TouchRaycaster → WorldNode)
  - `viewInputPosition` Vector2: 옮긴 자리. (뷰 좌표)
- `touchRelease(viewInputPosition)` (method, since 0.2.0): 터치 뗌. (TouchRaycaster → WorldNode)
  - `viewInputPosition` Vector2: 뗀 자리. (뷰 좌표)
- `touchCancel(viewInputPosition)` (method, since 0.2.0): 터치 취소. (TouchRaycaster → WorldNode)
  - `viewInputPosition` Vector2: 취소된 때의 자리. (뷰 좌표)
- `getContent()` (method, since 0.2.0): 콘텐츠 노드 반환. (자식 노드를 이 노드에 추가하면 스크롤 대상이 됨)
  - returns WorldNode: 컨텐트 노드. (노드에 붙기 전이면 null)
- `setBackgroundColor(backgroundColor)` (method, since 0.2.0): 배경색 설정. (null 이면 배경을 지운다)
  - `backgroundColor` Color | null: 배경색. (사본을 담는다, 알파가 0 이면 그리지 않는다, null 이나 undefined 면 처음 값인 알파 0 흰색으로 돌려 그리지 않는다)

  예제: 배경을 칠했다가 지우기

  ```js
  import { UIView, Color } from "./build/vanilla.js";
  
  const view = new UIView();
  view.setBackgroundColor(new Color(0.1, 0.12, 0.16, 1));
  const paintedColor = view.getBackgroundColor();
  view.setBackgroundColor(null);
  const clearedColor = view.getBackgroundColor();
  console.log(paintedColor.alpha, clearedColor.alpha);
  ```

- `getBackgroundColor()` (method, since 0.2.0): 배경색 반환.
  - returns Color: 배경색. (기본은 알파가 0 인 흰색)

## Functions and constants

- `isViewPositionInsideNode(node, viewPosition)` (method, since 0.4.0): 뷰 좌표의 점이 노드 내용 영역 안인지. (부모들의 변환을 모두 거친 네 귀퉁이로 본다, 러스트 판 ui::uiview::is_view_position_inside_node)
  - `node` WorldNode | null: 볼 노드. (없으면 거짓)
  - `viewPosition` Vector2: 뷰 좌표의 점. (픽셀)
  - returns boolean: 노드 contentSize 영역 안이면 참.

  예제: 가운데 피봇(기본)인 40 x 20 노드의 안과 밖

  ```js
  import { isViewPositionInsideNode, 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));
  console.log(isViewPositionInsideNode(buttonNode, Vector2.create(100, 100)));
  console.log(isViewPositionInsideNode(buttonNode, Vector2.create(81, 91)));
  console.log(isViewPositionInsideNode(buttonNode, Vector2.create(79, 100)));
  ```

