# misc/pointergesture (vanilla-engine 0.4.0)

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

Pointer gesture recognizer. (without nodes)
- A port of the original misc/pointergesture.js. Splits tap / drag / long press by thresholds. It is not tied to nodes, so it can be used anywhere.
- Usage: pass press(x, y), move(x, y), tick(time_delta), release() in that order,
  and receive the results through callbacks (set_tap_event / set_drag_event / set_drag_end_event / set_long_press_event).
- Differences from the original:
  Movement is move_pointer in both engines. (original movePointer; the reserved word move is not used as a name)
  The options object is a PointerGestureOptions struct. Callbacks are FnMut taking a Vector2.

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

Gesture settings. (original options = { drag_threshold = 12, long_press_seconds = 0.6 })

Example: Change only the long press time

```rust
use vanilla::{PointerGesture, PointerGestureOptions};

let pointer_gesture_options = PointerGestureOptions {
	long_press_seconds: 1.0,
	..PointerGestureOptions::default()
};
assert_eq!(pointer_gesture_options.drag_threshold, 12.0);
let pointer_gesture = PointerGesture::new(pointer_gesture_options);
assert!(!pointer_gesture.is_pressed());
```

- `pub drag_threshold: f64` (field, since 0.3.2): Moving this far from the press point counts as a drag. (pixels)
  - returns f64
- `pub long_press_seconds: f64` (field, since 0.3.2): Holding this long without moving is a long press. (seconds)
  - returns f64

## PointerGesture (struct) (Default)

Pointer gesture recognizer.
- Pass press, move_pointer, tick, release in order and receive tap, drag, drag end, and long press through callbacks.

Example: Tell a tap from a drag

```rust
use std::cell::RefCell;
use std::rc::Rc;
use vanilla::{PointerGesture, PointerGestureOptions, Vector2};

let event_list = Rc::new(RefCell::new(Vec::new()));
let mut pointer_gesture = PointerGesture::new(PointerGestureOptions::default());
let tap_event_list = event_list.clone();
pointer_gesture.set_tap_event(move |position| {
	tap_event_list.borrow_mut().push(("tap", position));
});
let drag_end_event_list = event_list.clone();
pointer_gesture.set_drag_end_event(move |position, _press_position| {
	drag_end_event_list.borrow_mut().push(("drag_end", position));
});

pointer_gesture.press(10.0, 10.0);
pointer_gesture.move_pointer(14.0, 10.0);
pointer_gesture.tick(0.1);
pointer_gesture.release();

pointer_gesture.press(10.0, 10.0);
pointer_gesture.move_pointer(40.0, 10.0);
assert!(pointer_gesture.is_dragging());
pointer_gesture.release();

assert_eq!(*event_list.borrow(), vec![("tap", Vector2::create(14.0, 10.0)), ("drag_end", Vector2::create(40.0, 10.0))]);
```

- `pub fn new(options: PointerGestureOptions) -> PointerGesture` (constructor, since 0.3.2): Constructor.
  - `options` PointerGestureOptions: Drag threshold and long press time.
  - returns PointerGesture: A new recognizer, not pressed and with no callbacks.
- `pub fn press(&mut self, x: f64, y: f64)` (method, since 0.3.2): Press.
  - Clears the drag and long press states and the press time, and remembers the press position.
  - `x` f64: Press position x. (pixels, the caller decides the coordinate system)
  - `y` f64: Press position y. (pixels)
- `pub fn move_pointer(&mut self, x: f64, y: f64)` (method, since 0.4.0): Move. (becomes a drag once it passes the threshold from the press point, and calls the drag notification on every move after that)
  - Does nothing if not pressed. It is a drag if the distance from the press point is at least the threshold.
  - `x` f64: Current position x. (pixels, same coordinate system as press)
  - `y` f64: Current position y. (pixels)
- `pub fn tick(&mut self, time_delta: f64)` (method, since 0.3.2): Update. (notifies a long press once when long_press_seconds passes without being a drag)
  - `time_delta` f64: Elapsed time. (seconds, added to the press time)

  Example: Holding for 0.6 seconds is a long press, and there is no tap on release

  ```rust
  use std::cell::Cell;
  use std::rc::Rc;
  use vanilla::{PointerGesture, PointerGestureOptions};
  
  let long_press_count = Rc::new(Cell::new(0));
  let tap_count = Rc::new(Cell::new(0));
  let mut pointer_gesture = PointerGesture::new(PointerGestureOptions::default());
  let long_press_count_for_event = long_press_count.clone();
  pointer_gesture.set_long_press_event(move |_position| {
  	long_press_count_for_event.set(long_press_count_for_event.get() + 1);
  });
  let tap_count_for_event = tap_count.clone();
  pointer_gesture.set_tap_event(move |_position| {
  	tap_count_for_event.set(tap_count_for_event.get() + 1);
  });
  
  pointer_gesture.press(50.0, 50.0);
  pointer_gesture.tick(0.3);
  assert!((pointer_gesture.get_long_press_ratio() - 0.5).abs() < 0.0001);
  pointer_gesture.tick(0.3);
  pointer_gesture.tick(0.3);
  pointer_gesture.release();
  assert_eq!(long_press_count.get(), 1);
  assert_eq!(tap_count.get(), 0);
  ```

- `pub fn release(&mut self)` (method, since 0.3.2): Release. (notifies a drag end if it was a drag, nothing if it was a long press, otherwise a tap)
- `pub fn cancel(&mut self)` (method, since 0.3.2): Cancels. (notifies nothing and only resets the state)
- `pub fn get_long_press_ratio(&self) -> f64` (method, since 0.3.2): Long press progress ratio. (for showing a gauge, 0 ~ 1)
  - returns f64: Press time / long press time. (clamped at 1) 0 if not pressed, dragging, or the long press time is 0 or less.
- `pub fn is_pressed(&self) -> bool` (method, since 0.3.2): Whether it is pressed.
  - returns bool: true after press and before release or cancel.
- `pub fn is_dragging(&self) -> bool` (method, since 0.3.2): Whether it is dragging.
  - returns bool: true if it moved past the threshold during this press.
- `pub fn set_tap_event(&mut self, tap_event: impl FnMut(Vector2) + 'static)` (method, since 0.3.2): Sets the tap notification. (release position)
  - `tap_event` impl FnMut(Vector2) + 'static: Function to call on a tap. Receives the last position. (replaces the previous notification)
- `pub fn set_drag_event(&mut self, drag_event: impl FnMut(Vector2, Vector2) + 'static)` (method, since 0.3.2): Sets the drag notification. (current position, press position)
  - `drag_event` impl FnMut(Vector2, Vector2) + 'static: Function to call on every move while dragging. Receives the current position and the press position.
- `pub fn set_drag_end_event(&mut self, drag_end_event: impl FnMut(Vector2, Vector2) + 'static)` (method, since 0.3.2): Sets the drag end notification. (current position, press position)
  - `drag_end_event` impl FnMut(Vector2, Vector2) + 'static: Function to call on release while dragging. Receives the last position and the press position.
- `pub fn set_long_press_event(&mut self, long_press_event: impl FnMut(Vector2) + 'static)` (method, since 0.3.2): Sets the long press notification. (press position)
  - `long_press_event` impl FnMut(Vector2) + 'static: Function to call once at the moment it becomes a long press. Receives the press position.
- `pub fn set_drag_threshold(&mut self, drag_threshold: f64)` (method, since 0.3.2): Sets the drag threshold. (pixels)
  - `drag_threshold` f64: Distance that counts as a drag. (pixels)
- `pub fn set_long_press_seconds(&mut self, long_press_seconds: f64)` (method, since 0.3.2): Sets the long press time. (seconds)
  - `long_press_seconds` f64: Press time that counts as a long press. (seconds)
