# ui/touchrecognizer (vanilla.js 0.4.0)

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

Distance (px) at which it is judged to have switched to a drag.

## TouchRecognizer extends TouchRaycaster

Touch recognizer.
- Extends TouchRaycaster to do UIKit-style gesture recognition.
- Finds the node with a UIScrollView in the pressed node's parent chain in advance,
  and when the drag threshold is exceeded:
    1) sends touchCancel to the pressed node to cancel the click
    2) sends touchPress (initial press position) → touchMove (current) to the ScrollView above,
       in order, to hand over the scroll.
- Within the threshold, sends touch* only to the pressed node. (Behaves like a basic button)
- If the ScrollView has setPassesCrossAxisDrag(true), a finger dragged more along the non-scrolling axis
  is not handed over. The pending press is sent right away to the hit node, and the following moves are also
  sent to the hit node. (When dragging a slider inside a scroll horizontally)
- When a parent has a ScrollView, delivery of touchPress to the hit node is delayed by
  DELAY_PRESS_MS. If it turns out to be a drag meanwhile, press is never delivered, so
  "cancel during press transition" does not occur. For a short tap, at release time
  the pending press is fired immediately and release is sent right after to compensate.

Example: Pass touches to widgets in a plain scene

```js
import { Engine, EngineConfiguration, Scene, TouchRecognizer, UIButton, Paint, WorldNode, Pivot, Vector2, Color } from "./build/vanilla.js";

class MainScene extends Scene {
	initialize(engine) {
		super.initialize(engine);
		this.setTouchRaycaster(new TouchRecognizer());

		const root = this.getRoot();
		const buttonNode = new WorldNode();
		buttonNode.setPivot(Pivot.topLeft);
		buttonNode.setAnchor(Pivot.topLeft);
		buttonNode.setLocalPosition(Vector2.create(40, 40));
		buttonNode.setContentSize(Vector2.create(160, 56));
		const paint = buttonNode.addComponent(Paint);
		paint.setColor(new Color(0.2, 0.45, 0.85, 1));
		const button = buttonNode.addComponent(UIButton);
		button.setClickedEvent(() => {
			console.log("clicked");
		});
		root.addChild(buttonNode);
	}
}

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.1): Constructor.
- `touchPress(viewInputPosition)` (method, since 0.2.1): Touch press.
  - Finds the hit node and the node holding a ScrollView in its parent chain in advance.
  - If no parent has a ScrollView, delivers touchPress to the hit node immediately.
  - If a parent has a ScrollView, schedules delivery after DELAY_PRESS_MS.
    If a drag starts meanwhile, the scheduled press is cancelled.
  - `viewInputPosition` Vector2: Press position. (view coordinates)
- `touchMove(viewInputPosition)` (method, since 0.2.1): Touch move.
  - In drag mode, delivers only to the ScrollView.
  - If not in drag mode, there is a ScrollView, and the threshold is exceeded, switches modes:
    (1) if a delayed press is pending, cancels it (not delivered at all),
        if press was already delivered, touchCancel to the hit node,
    (2) touchPress (initial position) → touchMove (current position) to the ScrollView.
  - Otherwise touchMove to the hit node. However, if press has not been delivered yet,
    touchMove is not delivered either.
  - `viewInputPosition` Vector2: Moved position. (view coordinates)
- `touchRelease(viewInputPosition)` (method, since 0.2.1): Touch release.
  - In drag mode, release to the ScrollView.
  - Otherwise release to the hit node.
    However, if it is a short tap and the delayed press has not fired yet, fires the pending press immediately
    and then sends release right after, so the visual effect shows for at least one frame.
  - `viewInputPosition` Vector2: Release position. (view coordinates)
- `touchCancel(viewInputPosition)` (method, since 0.2.1): Touch cancel.
  - If a delayed press is pending, does not send cancel to the hit node (since press itself
    was not delivered).
  - `viewInputPosition` Vector2: Position at cancel. (view coordinates)
- `getTouchTarget()` (method, since 0.4.0): The node currently receiving the press. (The scroll view node if the drag was handed over, null if none)
  - returns *: The node currently receiving the press. (null if none)
- `isTouching()` (method, since 0.4.0): Whether it is being held down. (From press until release or cancel)
  - returns boolean: True from press until release or cancel.
- `reset()` (method, since 0.2.1): Resets the tracking state.
- `findAncestorScrollView(target)` (method, since 0.2.1): In the node's parent chain, finds the nearest node with a UIScrollView component and
  returns its ScrollView component. null if none.
  - However, if target is a widget (has getScrollView) of the ScrollView it controls,
    that ScrollView is excluded from the search. (Because the scrollbar itself must not be handed over to
    the drag handler of its own parent ScrollView.)
  - `target` *: The pressed node. (returns null if none)
  - returns UIScrollView | null: The nearest ancestor UIScrollView. (null if none)
