# ui/uiview (vanilla-engine 0.4.0)

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

뷰 컴포넌트. (원본 ui/uiview.js 의 UIView)
- 모든 UI 위젯의 바탕. 노드(호스트)에 붙으면 호스트 아래에 "content" 자식 노드를 만든다. (피봇 왼쪽 위, 앵커 0,0, 자리 0,0)
- 배경색의 알파가 0 보다 크면 호스트 content_size 영역을 그 색으로 칠한다.
- 원본은 UILabel, UIButton 같은 위젯이 UIView 를 상속한다. 러스트 판은 위젯이 UIView 를 품고(ui_view 필드) content, 배경색을 넘겨준다.
  그래서 원본의 node.get_component(UIView) 로 하위 위젯까지 한 번에 찾는 쓰임은 없다. (`get_component::<UIButton>()` 처럼 구체 타입으로 찾는다)
- content 노드는 attach_to_tree 에서 만든다. node_tree.add_component(node_id, UIView::new()) 로 붙여야 한다.
- 원본의 setEngine / get_engine 은 옮기지 않았다. 엔진은 tick / 터치 때 ComponentContext::get_engine 으로 얻는다.
- 원본 setBackgroundColor(null) 은 배경을 지운다. (처음 값인 알파 0 흰색으로 돌린다) 러스트 판은 None 자리가 없어 알파 0 색(처음 값이나 Color::transparent())을 준다.

## UIView (struct) (Default, Component)

뷰 컴포넌트.
- 노드에 붙이면 호스트 아래 content 자식 노드를 만들고, 배경색으로 호스트 content_size 영역을 칠한다.

예제: 반투명 배경 판과 그 content 노드

```rust
use vanilla::{Color, NodeTree, UIView, Vector2};

let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let panel_id = node_tree.create_child(root_id, "panel");
node_tree[panel_id].set_content_size(Vector2::create(200.0, 100.0));
let panel_view = node_tree.add_component(panel_id, UIView::new()).unwrap();
panel_view.set_background_color(Color::create(0.1, 0.1, 0.2, 0.8));
let content_id = panel_view.get_content().unwrap();
assert_eq!(node_tree.get_parent_id(content_id), Some(panel_id));
assert_eq!(node_tree[content_id].get_name(), "content");
```

- `pub fn new() -> UIView` (constructor, since 0.3.2): 생성. (배경은 투명한 흰색)
  - returns UIView: 새 뷰. (content 노드는 트리에 붙일 때 만든다)
- `pub fn create_content_node(&mut self, node_tree: &mut NodeTree, node_id: NodeId)` (method, since 0.3.2): 컨텐트 노드 만들기. (호스트의 자식, 왼쪽 위 정렬. 이미 있으면 그대로 둔다)
  - `node_tree` &mut NodeTree: 호스트가 든 트리.
  - `node_id` NodeId: 호스트 노드. (content 노드가 이 노드의 자식이 된다)
- `pub fn set_backdrop_blur(&mut self, blur_radius: f64)` (method, since 0.4.0): 뒤 화면 흐림 설정. (CSS backdrop-filter: blur(), 언리얼 UMG BackgroundBlur 와 같은 뜻, 원본 setBackdropBlur)
  - 켜면 이 뷰가 그려질 때, 그보다 먼저 그려진 화면을 이 뷰의 내용 영역만 흐리고 그 위에 배경색(반투명)을 얹는다.
  - 인자는 CSS backdrop-filter: blur(Npx) 의 N 과 같은 표준편차다. (노드 좌표 단위, 화면에서는 변환 배율을 곱한 기기 픽셀)
  - 흐림은 ScreenEffect 의 GaussianBlur(분리형 가우스, 선형 샘플링, 3 시그마까지, 모서리는 거울 접기)다. 크롬의 backdrop-filter 와 같다.
  - 화면에 그릴 때만 된다. (렌더 타겟에 그리는 중이면 흐리지 않는다) 켜진 동안은 기하 캐시를 쓰지 않는다. (매 그리기마다 화면을 읽는다)
  - `blur_radius` f64: 흐림 표준편차. (CSS blur(Npx) 의 N, 노드 좌표 단위, 0 이하면 끈다, 기본 0)

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

  ```rust
  use vanilla::{Color, UIView};
  
  let mut view = UIView::new();
  view.set_background_color(Color::create(1.0, 1.0, 1.0, 0.25));
  view.set_backdrop_blur(6.0);
  assert_eq!(view.get_backdrop_blur(), 6.0);
  ```

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

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

  ```rust
  use vanilla::{Color, UIView};
  
  let mut view = UIView::new();
  view.set_background_color(Color::create(1.0, 1.0, 1.0, 0.25));
  view.set_backdrop_blur(6.0);
  view.set_backdrop_corner_radius(12.0);
  assert_eq!(view.get_backdrop_corner_radius(), 12.0);
  ```

- `pub fn get_backdrop_corner_radius(&self) -> f64` (method, since 0.4.0): 뒤 화면 흐림의 둥근 모서리 반지름 반환.
  - returns f64: 모서리 반지름. (노드 좌표 단위, 0 이면 사각형)
- `pub fn get_backdrop_blur(&self) -> f64` (method, since 0.4.0): 뒤 화면 흐림 반지름 반환.
  - returns f64: 흐림 표준편차. (CSS blur(Npx) 의 N, 0 이면 꺼짐)
- `pub fn get_content(&self) -> Option<NodeId>` (method, since 0.3.2): 컨텐트 노드 핸들. (자식 노드를 여기에 붙이면 스크롤 대상이 된다. 트리에 붙기 전이면 None)
  - returns Option<NodeId>: content 노드. 트리에 붙기 전이면 None.
- `pub fn set_background_color(&mut self, background_color: impl Into<Color>)` (method, since 0.3.2): 배경색 설정. (알파가 0 이면 그리지 않는다. 원본 null 처럼 배경을 지우려면 알파 0 색을 준다)
  - `background_color` impl Into<Color>: 배경색. (Color 나 "#rrggbb" 같은 글, 알파가 0 이면 칠하지 않는다)
- `pub fn get_background_color(&self) -> Color` (method, since 0.3.2): 배경색 반환.
  - returns Color: 배경색. (기본은 알파 0 인 흰색)
- `pub fn get_engine(&self) -> Result<&'static crate::core::engine::Engine, crate::core::globalengine::EngineAccessError>` (method, since 0.4.0): 엔진 반환. (원본 getEngine, 원본 setEngine 으로 준 엔진 자리. 러스트 판은 엔진이 하나라 전역 엔진을 준다)
  - Engine::get_engine 과 같다. 엔진을 만든 직후부터 어디서나 꺼낸다.
  - returns Result<&'static crate::core::engine::Engine, crate::core::globalengine::EngineAccessError>: 전역 엔진, 못 꺼내면 그 까닭.
- `pub fn draw_background(&self, transform: &NodeTransform, graphic: &mut Graphic)` (method, since 0.3.2): 배경 출력. (투명한 바탕은 그리지 않는다. 보이지 않는 드로우가 묶어 그리기를 끊지 않게)
  - `transform` &NodeTransform: 호스트 노드의 변환. (content_size 영역 (0, 0) ~ (너비, 높이) 를 칠한다)
  - `graphic` &mut Graphic: 그릴 그래픽.

## Functions and constants

- `pub fn is_view_position_inside_node(node_tree: &NodeTree, node_id: NodeId, view_position: Vector2) -> bool` (method, since 0.3.2): 뷰 좌표의 점이 노드 content_size 영역 안에 있는지. (원본 WorldNode.contains)
  - `node_tree` &NodeTree: 노드가 든 트리.
  - `node_id` NodeId: 볼 노드.
  - `view_position` Vector2: 뷰 좌표의 점. (픽셀)
  - returns bool: 노드까지 누적한 월드 행렬로 본 노드 content_size 영역 안이면 true. 노드가 없으면 false.

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

  ```rust
  use vanilla::ui::uiview::is_view_position_inside_node;
  use vanilla::{NodeTree, Vector2};
  
  let mut node_tree = NodeTree::new();
  let root_id = node_tree.get_root_id();
  let button_id = node_tree.create_child(root_id, "button");
  node_tree[button_id].set_local_position(Vector2::create(100.0, 100.0));
  node_tree[button_id].set_content_size(Vector2::create(40.0, 20.0));
  assert!(is_view_position_inside_node(&node_tree, button_id, Vector2::create(100.0, 100.0)));
  assert!(is_view_position_inside_node(&node_tree, button_id, Vector2::create(81.0, 91.0)));
  assert!(!is_view_position_inside_node(&node_tree, button_id, Vector2::create(79.0, 100.0)));
  ```

