# ui/uidraggable (vanilla.js 0.4.0)

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

Draggable component.

## UIDraggable extends UIControl

Draggable component.
- Attached to a node, it can be pressed and dragged. On release it notifies via dragEndEvent, and
  if setSnapBackEnabled(true), it slides back to where it was before being grabbed.
- The movement adds the view coordinate delta directly to the local position.
  (sensitivity may differ if an ancestor node has a scale)
- Usage:
    const draggable = cardNode.addComponent(UIDraggable);
    draggable.setSnapBackEnabled(true);
    draggable.setDragEndEvent((self, viewPosition) => { ... });

Example: A card that stays when dropped in the slot and returns when dropped outside

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

class DragScene extends UIScene {
	create() {
		super.create();
		const root = this.getRoot();

		const slotNode = new WorldNode();
		slotNode.setPivot(Pivot.topLeft.clone());
		slotNode.setAnchor(Pivot.topLeft.clone());
		slotNode.setContentSize(Vector2.create(140, 100));
		slotNode.setLocalPosition(Vector2.create(420, 130));
		const slotPaint = slotNode.addComponent(Paint);
		slotPaint.setColor(new Color(1, 1, 1, 0.1));
		root.addChild(slotNode);

		const cardNode = new WorldNode();
		cardNode.setPivot(Pivot.topLeft.clone());
		cardNode.setAnchor(Pivot.topLeft.clone());
		cardNode.setContentSize(Vector2.create(120, 80));
		cardNode.setLocalPosition(Vector2.create(80, 140));
		const cardPaint = cardNode.addComponent(Paint);
		cardPaint.setColor(new Color(0.36, 0.42, 1, 1));
		cardPaint.setRoundSize(8);
		const draggable = cardNode.addComponent(UIDraggable);
		draggable.setSnapBackEnabled(true);
		draggable.setDragEndEvent((self, viewPosition) => {
			if (slotNode.contains(viewPosition)) {
				self.settleHere();
			}
		});
		root.addChild(cardNode);
	}
}

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
const scene = new DragScene();
scene.setLoadingMinDurationMs(0);
engine.run(scene);
```

- `constructor()` (constructor, since 0.3.0): Constructor.
- `attach(node)` (method, since 0.3.0): Attached to a node. (enables interaction to receive touches)
  - `node` import("../core/node/componentnode.js").ComponentNode: The node to attach to. (setInteractable(true) if it is a WorldNode)
- `touchPress(viewInputPosition)` (method, since 0.3.0): Touch pressed.
  - `viewInputPosition` Vector2: Input position. (view coordinates)
- `touchMove(viewInputPosition)` (method, since 0.3.0): Touch move.
  - `viewInputPosition` Vector2: Input position. (view coordinates)
- `touchRelease(viewInputPosition)` (method, since 0.3.0): Touch release.
  - `viewInputPosition` Vector2: Input position. (view coordinates)
- `touchCancel(viewInputPosition)` (method, since 0.3.0): Touch cancel.
  - `viewInputPosition` Vector2: Input position. (view coordinates)
- `tick(timeDelta)` (method, since 0.3.0): Update. (snap-back slide)
  - `timeDelta` number: Time since the last frame. (Seconds)
- `settleHere()` (method, since 0.3.0): Settles at the current position. (call on a successful drop in the dragEnd handler, stops the snap-back)
- `snapHome()` (method, since 0.3.0): Returns to the original position immediately.
- `setSnapBackEnabled(isSnapBackEnabled)` (method, since 0.3.0): Sets whether it returns to where it was before being grabbed on release.
  - `isSnapBackEnabled` boolean: If true, it slides back to where it was before being grabbed after release.
- `setSnapBackSpeed(snapBackSpeed)` (method, since 0.3.0): Sets the return speed. (default 14)
  - `snapBackSpeed` number: Return rate coefficient per second. (each frame it closes in by this value times the frame seconds share of the remaining distance, the share does not exceed 1)
- `setDragStartEvent(dragStartEvent)` (method, since 0.3.0): Sets the drag start handler. (once, at the moment of pressing)
  - `dragStartEvent` Function: (self)
- `setDragMoveEvent(dragMoveEvent)` (method, since 0.3.0): Sets the dragging handler. (on each move, after moving the node)
  - `dragMoveEvent` Function: (self, viewPosition)
- `setDragEndEvent(dragEndEvent)` (method, since 0.3.0): Sets the drag end handler. (called when released or cancelled, after the return is turned on)
  - Calling settleHere inside the handler makes it stay in place.
  - `dragEndEvent` Function: (self, viewPosition)

  Example: Staying only when dropped on the right half

  ```js
  import { UIDraggable, WorldNode, Vector2 } from "./build/vanilla.js";
  
  const cardNode = new WorldNode();
  cardNode.setLocalPosition(Vector2.create(100, 100));
  const draggable = cardNode.addComponent(UIDraggable);
  draggable.setSnapBackEnabled(true);
  draggable.setDragEndEvent((self, viewPosition) => {
  	if (viewPosition.x > 320) {
  		self.settleHere();
  	}
  });
  draggable.touchPress(Vector2.create(110, 110));
  draggable.touchMove(Vector2.create(160, 120));
  draggable.touchRelease(Vector2.create(160, 120));
  draggable.tick(1);
  const localPosition = cardNode.getLocalPosition();
  console.log(localPosition.x, localPosition.y);
  ```

- `isDragging()` (method, since 0.3.0): Returns whether it is being dragged.
  - returns boolean: True while being dragged. (false while returning)
- `getHomePosition()` (method, since 0.3.0): Returns the position before being grabbed. (that position if settleHere was called)
  - returns Vector2 | null: Copy of the local position. (null if never pressed)
