# misc/toucheffect (vanilla-engine 0.4.0)

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

터치 효과 컴포넌트.
- 원본 misc/toucheffect.js 를 옮긴 것이다. 누른 자리에 하늘색 빛 입자를 흩뿌리고 0.4 초 동안 흐려지며 퍼지게 한다.
- 입자는 흰색 방사형 그라디언트 그림(64x64, 한 번만 만든다)을 하늘색(rgb 100, 200, 255)으로 틴트하고 더하기(lighter) 블렌드로 그린다.
- 사용:
  ```text
    let touch_effect_id = node_tree.create_child(root_id, "TouchEffect");
    node_tree[touch_effect_id].set_pivot(Pivot::TOP_LEFT);
    node_tree[touch_effect_id].add_component(TouchEffect::new());
    // 누를 때: node_tree[touch_effect_id].get_component_mut::<TouchEffect>().unwrap().create_touch_particle(x, y);
  ```
- 원본과 다른 점:
  원본은 WorldNode 를 상속한 노드이고, 러스트 판은 노드에 붙는 컴포넌트다. 블렌드는 원본처럼 push_transform 에서 더하기로 바꾸고
  pop_transform 에서 앞의 블렌드로 되돌려 자식까지 더하기로 그린다. (컴포넌트의 노드 출력 상태 앞뒤 훅)
  그라디언트는 원본처럼 오프스크린 캔버스(엔진 Canvas2D)의 방사 그라디언트로 구운 Image 다.

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

터치 입자.
- TouchEffect 가 목록으로 든다. 값만 담는 구조체다.

예제: 입자 하나의 값

```rust
use vanilla::{TouchParticle, Vector2};

let mut particle = TouchParticle::new();
particle.position = Vector2::create(40.0, 30.0);
particle.radius = 20.0;
assert_eq!(particle.life, particle.max_life);
assert_eq!(particle.velocity, Vector2::zero());
```

- `pub position: Vector2` (field, since 0.3.2): 가운데 위치. (노드 영역 좌표, 픽셀)
  - returns Vector2
- `pub velocity: Vector2` (field, since 0.3.2): 속도. (픽셀/초)
  - returns Vector2
- `pub life: f64` (field, since 0.3.2): 남은 수명. (TouchEffect 에서 1 로 시작해 초당 2.5 씩 줄고, 0 이하면 빠진다)
  - returns f64
- `pub max_life: f64` (field, since 0.3.2): 처음 수명. (그릴 때 투명도는 life / max_life)
  - returns f64
- `pub radius: f64` (field, since 0.3.2): 반지름. (픽셀)
  - returns f64
- `pub fn new() -> TouchParticle` (constructor, since 0.3.2): 생성. (제자리, 수명 1, 반지름 0)
  - returns TouchParticle: 위치와 속도가 0, 수명과 처음 수명이 1, 반지름이 0 인 입자.

## TouchEffect (struct) (Default, Component, UIDocumentNode)

터치 효과 컴포넌트.
- 누를 때 create_touch_particle 을 부르면 tick 에서 입자를 줄이고 draw 에서 더하기 블렌드로 그린다.

예제: 노드에 붙이고 누른 자리에 입자 만들기

```rust
use vanilla::{NodeTree, Pivot, TouchEffect};

let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let touch_effect_id = node_tree.create_child(root_id, "TouchEffect");
node_tree[touch_effect_id].set_pivot(Pivot::TOP_LEFT);
node_tree[touch_effect_id].add_component(TouchEffect::new());

let touch_effect = node_tree[touch_effect_id].get_component_mut::<TouchEffect>().unwrap();
touch_effect.create_touch_particle(120.0, 80.0);
assert_eq!(touch_effect.get_particle_count(), 1);
touch_effect.update_touch_particles(0.5);
assert_eq!(touch_effect.get_particle_count(), 0);
```

- `pub fn new() -> TouchEffect` (constructor, since 0.3.2): 생성.
  - returns TouchEffect: 입자가 없는 새 터치 효과.
- `pub fn create_touch_particle(&mut self, x: f64, y: f64)` (method, since 0.3.2): 터치 입자 만들기. (노드 영역 좌표, ±5 픽셀 흩뿌림, ±60 픽셀/초 속도, 반지름 10 ~ 35)
  - `x` f64: 누른 자리 x. (노드 영역 좌표, 픽셀)
  - `y` f64: 누른 자리 y. (노드 영역 좌표, 픽셀)
- `pub fn update_touch_particles(&mut self, time_delta: f64)` (method, since 0.3.2): 터치 입자 갱신. (수명이 초당 2.5 씩 줄고, 다 되면 뺀다)
  - `time_delta` f64: 지난 시간. (초, 위치는 속도 * time_delta 만큼 움직인다)
- `pub fn draw_touch_particles(&self, graphic: &mut Graphic)` (method, since 0.3.2): 터치 입자 출력. (흰색 그라디언트를 입자 색으로 틴트하고, 남은 수명만큼 흐리게)
  - 블렌드는 바꾸지 않는다. (컴포넌트로 그릴 때는 push_transform 이 더하기로 바꾼다)
  - `graphic` &mut Graphic: 그릴 그래픽. (이미지 틴트와 전역 알파는 그린 뒤 되돌린다)
- `pub fn get_particle_count(&self) -> usize` (method, since 0.3.2): 살아 있는 입자 수. (러스트 판에서 더한 것)
  - returns usize: 수명이 남은 입자 수.
