# core/touchraycaster (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/core/touchraycaster/

Touch raycaster.

## TouchRaycaster extends Object

Touch raycaster.
- Receives the scene's touches (Scene.tickTouch passes them to the scene's raycaster, Scene.setTouchRaycaster),
  traverses the root node tree in draw() order,
  and among active + isInteractable() nodes, delivers the touch event
  to the frontmost (highest number) node.
- Node types are not restricted. A node with isInteractable and contains can be hit even if it is not a WorldNode (UINode, etc.).

Example: Pass scene presses to the frontmost node

```js
import { Engine, EngineConfiguration, Scene, TouchRaycaster, WorldNode, Vector2 } from "./build/vanilla.js";

class ButtonNode extends WorldNode {
	touchPress(viewInputPosition) {
		console.log("pressed", viewInputPosition.x, viewInputPosition.y);
	}
}

class MainScene extends Scene {
	initialize(engine) {
		const root = this.getRoot();
		const buttonNode = new ButtonNode();
		root.addChild(buttonNode);
		buttonNode.setRect(100, 100, 200, 80);
		buttonNode.setInteractable(true);
		const touchRaycaster = this.getTouchRaycaster();
		touchRaycaster.setFirstTouchCallback(() => {
			console.log("first touch");
		});
		const hitNode = touchRaycaster.raycast(Vector2.create(150, 120));
		console.log(hitNode === buttonNode, touchRaycaster instanceof TouchRaycaster);
	}
}

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
engine.run(new MainScene());
```

- `constructor()` (constructor, since 0.1.0): Constructor.
- `touchPress(viewInputPosition)` (method, since 0.1.0): Touch press.
  - Determines the target with raycast and delivers touchPress.
  - `viewInputPosition` Vector2: Input position in view coordinates.
- `touchMove(viewInputPosition)` (method, since 0.1.0): Touch move.
  - Delivers touchMove to the target determined at press.
  - `viewInputPosition` Vector2: Input position in view coordinates.
- `touchRelease(viewInputPosition)` (method, since 0.1.0): Touch release.
  - Delivers touchRelease to the target determined at press and releases the target.
  - `viewInputPosition` Vector2: Input position in view coordinates.
- `touchCancel(viewInputPosition)` (method, since 0.1.0): Touch cancel.
  - Delivers touchCancel to the target determined at press and releases the target.
  - `viewInputPosition` Vector2: Input position in view coordinates.
- `touchWheel(viewInputPosition, wheelDelta)` (method, since 0.2.1): Mouse wheel. (Regardless of touchPress, each time finds the scroll view that receives the wheel at viewInputPosition and delivers wheel.)
  - If the frontmost in draw order among the things covering that point is a scroll view (whose node contains that point), that view receives it.
    It receives it even when the wheel is rolled over non-pressable things inside it, like text or images.
  - If the frontmost is a pressable node, the first scroll view in that node and its parent chain receives it. (Overlays or windows covering a scroll view block the wheel)
  - true if a view received it.
  - `viewInputPosition` Vector2: Input position in view coordinates.
  - `wheelDelta` Vector2: Wheel amount. (passed as is to the scroll view's wheel)
  - returns boolean: True if a scroll view received the wheel.
- `findWheelScrollView(viewInputPosition)` (method, since 0.4.0): Finds the scroll view that receives the wheel at that point. (touchWheel's computation, null if none)
  - Iterates in the same order as raycast and picks the frontmost among pressable nodes and scroll view nodes.
  - `viewInputPosition` Vector2: Input position in view coordinates.
  - returns object | null: The scroll view component to receive the wheel. (null if none)
- `findScrollViewComponent(node)` (method, since 0.4.0): Returns the scroll view component attached to the node. (Enabled ones only, null if none, duck typing)
  - `node` WorldNode: Node to look at.
  - returns object | null: The enabled scroll view component. (null if none)
- `findAncestorScrollViewForWheel(target)` (method, since 0.2.1): Finds the nearest UIScrollView in the parent chain, including hitNode itself.
  (Difference from TouchRecognizer's findAncestorScrollView: target itself is also a candidate)
  - To avoid importing UIScrollView, the component type is checked by duck typing.
  - `target` WorldNode: Node to start the search from. (itself is also a candidate)
  - returns object | null: The found scroll view component. (null if none)
- `raycast(viewInputPosition)` (method, since 0.1.0): Raycast.
  - Traverses from the root node, numbering in draw() call order.
  - Returns the highest-numbered node among the nodes that are active, whose isInteractable() is true and that contain
    the touch coordinates. (node types are not restricted)
  - Descendants of a node with a Mask hit only within that node's area. (Clipped, invisible areas are not pressed)
  - `viewInputPosition` Vector2: Input position in view coordinates.
  - returns WorldNode | null: The frontmost interactable node containing that position. (null if none)
- `isMaskedOut(node, viewInputPosition)` (method, since 0.3.2): Whether the node has a Mask and the coordinates are outside its area.
  - `node` WorldNode: Node to look at.
  - `viewInputPosition` Vector2: Input position in view coordinates.
  - returns boolean: True if an enabled Mask is attached and the position is outside the node.
- `setRootNode(rootNode)` (method, since 0.1.0): Sets the root node.
  - `rootNode` *: Root node to traverse. (usually the scene root)
- `getRootNode()` (method, since 0.1.0): Returns the root node.
  - returns *: The root node being traversed. (null if not set)
- `getTouchTarget()` (method, since 0.1.0): Returns the current touch target.
  - returns WorldNode | null: The node hit on press. (null after release or if none)
- `hasEverTouched()` (method, since 0.2.0): Returns whether it has ever been touched.
  - returns boolean: True if pressed at least once.
- `setFirstTouchCallback(callback)` (method, since 0.2.0): Sets the first touch callback.
  - `callback` Function: Function called once on the first press. (no arguments)
