# ui/uihitarea (vanilla-engine 0.4.0)

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

누르는 자리 넓히기. (vanilla.js ui/uihitarea.js 의 UIHitArea)
- 보이는 단추는 그대로 두고, 단추 바로 뒤 형제로 투명한 누름 자리를 두어 가로세로를 가장 작은 크기 이상으로 넓힌다. (손가락으로 누르기 좋게)
- 그리는 차례가 늦은 노드가 누름을 받으므로, 단추 위는 단추가 받고 단추 둘레의 넓힌 몫만 누름 자리가 받는다.
- 누름 자리를 누르면 단추의 클릭 알림(Clicked, Click)을 그대로 부른다. 단추가 꺼지거나 누를 수 없으면 누름 자리도 따른다.
- 단추의 자리, 크기, 피봇을 프레임마다 따라가므로 화면이 단추를 다시 놓아도 맞는다.
- 자르는 노드를 정하면 넓힌 몫이 그 노드의 사각형 밖으로 나가지 않게 자른다. (단추 자신의 자리는 그대로, 창 밖이나 스크롤 칸 밖이 눌리지 않게)
- 사용: UIHitArea::attach(node_tree, button_id, minimum_size) (단추가 부모에 붙은 뒤에 부른다)

## UIHitArea (struct) (Default, Component)

누르는 자리 넓히기. (단추 바로 뒤 형제 노드에 붙어 단추를 따라가는 투명한 누름 자리)

예제: 작은 닫기 단추의 누르는 자리 넓히기

```rust
use std::cell::Cell;
use std::rc::Rc;
use vanilla::{ComponentContext, Engine, EngineConfiguration, NodeTree, Paint, Pivot, TouchKind, UIButton, UIHitArea, Vector2};

let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let button_id = node_tree.create_child(root_id, "CloseButton");
node_tree[button_id].set_pivot(Pivot::TOP_LEFT);
node_tree[button_id].set_local_position(Vector2::create(300.0, 20.0));
node_tree[button_id].set_content_size(Vector2::create(24.0, 24.0));
node_tree[button_id].add_component(Paint::create_with_color("#d94d4d"));
let click_count = Rc::new(Cell::new(0));
let click_count_for_event = click_count.clone();
let button = node_tree.add_component(button_id, UIButton::new()).expect("button");
button.set_click_event(Box::new(move |_button: &mut UIButton, _context: &mut ComponentContext| {
	click_count_for_event.set(click_count_for_event.get() + 1);
}));

let hit_id = UIHitArea::attach(&mut node_tree, button_id, 48.0).expect("hit area");
node_tree.tick(&engine, 0.016);
assert_eq!(node_tree[hit_id].get_local_position(), Vector2::create(288.0, 8.0));
assert_eq!(node_tree[hit_id].get_content_size(), Vector2::create(48.0, 48.0));
let hit_area = node_tree[hit_id].get_component::<UIHitArea>().expect("hit area");
assert!(hit_area.get_target_button(&node_tree).is_some());

let beside_button = Vector2::create(295.0, 15.0);
node_tree.dispatch_touch(hit_id, &engine, TouchKind::Press, beside_button);
node_tree.dispatch_touch(hit_id, &engine, TouchKind::Release, beside_button);
assert_eq!(click_count.get(), 1);
```

- `pub fn new() -> UIHitArea` (constructor, since 0.4.0): 생성.
  - returns UIHitArea: 대상이 없고 가장 작은 가로세로가 0 인 누름 자리. (단추에 달 때는 attach 를 쓴다)
- `pub fn attach(node_tree: &mut NodeTree, button_id: NodeId, minimum_size: f64) -> Option<NodeId>` (static, since 0.4.0): 단추에 넓힌 누름 자리 달기. (단추의 바로 뒤 형제로 끼우고 누름 자리 노드를 돌려준다, 부모가 없거나 UIButton 이 없으면 None)
  - 누름 자리 노드는 부모의 자식 목록에서 단추 바로 앞에 끼워 단추보다 먼저 그려진다. 피봇은 왼쪽 위다.
  - 누름 자리 노드에도 UIButton 을 붙이고, 그 클릭이 단추의 fire_click_events 를 부른다.
  - `node_tree` &mut NodeTree: 단추가 든 노드 트리.
  - `button_id` NodeId: 넓힐 단추 노드. (UIButton 이 붙어 있고 부모에 붙어 있어야 한다)
  - `minimum_size` f64: 누르는 자리의 가장 작은 가로세로. (단추의 content_size 와 같은 단위)
  - returns Option<NodeId>: 만든 누름 자리 노드. (이름은 단추 이름 뒤에 "HitArea") 단추에 부모가 없거나 UIButton 이 없으면 None 이고 노드를 만들지 않는다.

  예제: 단추가 아니면 달지 않고, 단추면 바로 앞에 끼운다

  ```rust
  use vanilla::{NodeTree, UIButton, UIHitArea};
  
  let mut node_tree = NodeTree::new();
  let root_id = node_tree.get_root_id();
  let plain_id = node_tree.create_child(root_id, "Plain");
  assert!(UIHitArea::attach(&mut node_tree, plain_id, 48.0).is_none());
  
  let button_id = node_tree.create_child(root_id, "Ok");
  node_tree.add_component(button_id, UIButton::new());
  let hit_id = UIHitArea::attach(&mut node_tree, button_id, 48.0).expect("hit area");
  assert_eq!(node_tree.get_child_ids(root_id), &[plain_id, hit_id, button_id]);
  assert_eq!(node_tree[hit_id].get_name(), "OkHitArea");
  ```

- `pub fn set_target(&mut self, target_id: NodeId)` (method, since 0.4.0): 넓혀 줄 단추의 노드 설정.
  - `target_id` NodeId: 넓혀 줄 단추 노드. (UIButton 이 붙은 노드, tick 이 그 자리와 크기를 따라간다)
- `pub fn get_target(&self) -> Option<NodeId>` (method, since 0.4.0): 넓혀 줄 단추의 노드 반환.
  - returns Option<NodeId>: 넓혀 줄 단추 노드. 정하지 않았으면 None.
- `pub fn get_target_button<'a>(&self, node_tree: &'a NodeTree) -> Option<&'a UIButton>` (method, since 0.4.0): 넓혀 줄 단추. (원본 getTargetButton, 원본은 단추 컴포넌트를 쥐고 러스트 판은 대상 노드에서 꺼내므로 트리를 받는다)
  - `node_tree` &'a NodeTree: 대상 노드를 찾을 노드 트리.
  - returns Option<&'a UIButton>: 대상 노드의 UIButton. 대상을 정하지 않았거나, 대상 노드가 없거나, UIButton 이 없으면 None.
- `pub fn set_minimum_size(&mut self, minimum_size: f64)` (method, since 0.4.0): 누르는 자리의 가장 작은 가로세로 설정.
  - `minimum_size` f64: 가장 작은 가로세로. (단추의 content_size 와 같은 단위, 단추가 이보다 크면 그 쪽은 단추 크기)
- `pub fn get_minimum_size(&self) -> f64` (method, since 0.4.0): 누르는 자리의 가장 작은 가로세로 반환.
  - returns f64: 가장 작은 가로세로. (기본 0)
- `pub fn set_clip_node(&mut self, clip_id: Option<NodeId>)` (method, since 0.4.0): 넓힌 몫을 자르는 노드 설정. (None 이면 자르지 않는다)
  - `clip_id` Option<NodeId>: 넓힌 몫이 그 content_size 사각형 밖으로 나가지 않게 할 노드. (창이나 스크롤 칸, None 이면 자르지 않는다)
- `pub fn get_clip_node(&self) -> Option<NodeId>` (method, since 0.4.0): 넓힌 몫을 자르는 노드 반환.
  - returns Option<NodeId>: 자르는 노드. 정하지 않았으면 None.
