# ui/uidraggable (vanilla-engine 0.4.0)

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

Draggable component. (UIDraggable of the original ui/uidraggable.js)
- Attached to a node, it can be pressed and dragged. On release it notifies with the end event, and with set_snap_back_enabled(true) it slides back to where it was before being grabbed.
- The movement adds the view coordinate difference as is to the local coordinates. (sensitivity differs if an ancestor node has a scale. same as the original)
- The original inherits UIControl. The Rust version embeds UIControl (ui_control field) and passes through content and the background color.
  attach_to_tree turns the host node interactable. Attach with node_tree.add_component(node_id, UIDraggable::new()).
- Usage:
  ```text
    let draggable = node_tree.add_component(card_id, UIDraggable::new()).expect("draggable");
    draggable.set_snap_back_enabled(true);
    draggable.set_drag_end_event(Box::new(|draggable, context, view_position| { if is_drop_spot { draggable.settle_here(context.get_node_tree_mut()); } }));
  ```
- Differences from the original:
  - Events take the drag component and the context. (move / end also the view coordinates) Original callbacks take only the component (and the view coordinates).
  - settle_here / snap_home need to read and write the node position, so they take the tree. (context.get_node_tree_mut() inside the end event)

## UIDraggableEvent (type)

Drag event. (takes the drag component and the context. start event)

```rust
pub type UIDraggableEvent = Box<dyn FnMut(&mut UIDraggable, &mut ComponentContext)>
```

## UIDraggablePositionEvent (type)

Drag position event. (drag component, context, view coordinates. move / end events)

```rust
pub type UIDraggablePositionEvent = Box<dyn FnMut(&mut UIDraggable, &mut ComponentContext, Vector2)>
```

## UIDraggable (struct) (Default, Component)

Draggable component.

Example: A card that settles when dropped on a drop spot and returns to its place otherwise

```rust
use vanilla::{ComponentContext, Engine, EngineConfiguration, NodeTree, TouchKind, UIDraggable, Vector2};

let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let card_id = node_tree.create_child(root_id, "Card");
node_tree[card_id].set_local_position(Vector2::create(100.0, 100.0));
node_tree[card_id].set_content_size(Vector2::create(50.0, 50.0));
let draggable = node_tree.add_component(card_id, UIDraggable::new()).expect("draggable");
draggable.set_snap_back_enabled(true);
draggable.set_drag_end_event(Box::new(|draggable: &mut UIDraggable, context: &mut ComponentContext, view_position: Vector2| {
	if view_position.x > 300.0 {
		draggable.settle_here(context.get_node_tree_mut());
	}
}));
node_tree.dispatch_touch(card_id, &engine, TouchKind::Press, Vector2::create(100.0, 100.0));
node_tree.dispatch_touch(card_id, &engine, TouchKind::Moved, Vector2::create(400.0, 100.0));
node_tree.dispatch_touch(card_id, &engine, TouchKind::Release, Vector2::create(400.0, 100.0));
node_tree.tick(&engine, 0.5);
assert_eq!(node_tree[card_id].get_local_position(), Vector2::create(400.0, 100.0));
assert_eq!(node_tree[card_id].get_component::<UIDraggable>().expect("draggable").get_home_position(), Some(Vector2::create(400.0, 100.0)));
```

- `pub fn new() -> UIDraggable` (constructor, since 0.3.2): Constructor. (snap back off, snap back speed 14)
  - returns UIDraggable: Drag component before being attached.
- `pub fn get_uicontrol(&self) -> &UIControl` (method, since 0.3.2): Returns the embedded control.
  - returns &UIControl: The embedded UIControl.
- `pub fn get_uicontrol_mut(&mut self) -> &mut UIControl` (method, since 0.3.2): Returns the embedded control. (mutable)
  - returns &mut UIControl: The embedded UIControl.
- `pub fn get_content(&self) -> Option<NodeId>` (method, since 0.3.2): Content node handle.
  - returns Option<NodeId>: Content node. (None before being attached to a tree)
- `pub fn set_background_color(&mut self, background_color: impl Into<Color>)` (method, since 0.3.2): Sets the background color.
  - `background_color` impl Into<Color>: Background color. (drawn by the embedded UIControl)
- `pub fn settle_here(&mut self, node_tree: &mut NodeTree)` (method, since 0.3.2): Settles at the current position. (called when the drop succeeds in the end event. stops the snap back)
  - `node_tree` &mut NodeTree: Node tree that holds the drag node. (makes the current local position the home position, context.get_node_tree_mut() inside the end event)
- `pub fn snap_home(&mut self, node_tree: &mut NodeTree)` (method, since 0.3.2): Returns to the home position immediately.
  - `node_tree` &mut NodeTree: Node tree that holds the drag node. (if never grabbed, the position stays and only the snap back stops)
- `pub fn set_snap_back_enabled(&mut self, is_snap_back_enabled: bool)` (method, since 0.3.2): Sets whether to return to the home position on release.
  - `is_snap_back_enabled` bool: true to slide back to the home position on release. (default false)
- `pub fn set_snap_back_speed(&mut self, snap_back_speed: f64)` (method, since 0.3.2): Sets the snap back speed. (rate factor that reduces the remaining distance per second)
  - `snap_back_speed` f64: Snap back speed. (each tick moves closer by speed x seconds of the remaining distance, and arrives immediately if that fraction exceeds 1. default 14)

  Example: At speed 10, covers half the remaining distance in 0.05 seconds

  ```rust
  use vanilla::{Engine, EngineConfiguration, NodeTree, TouchKind, UIDraggable, Vector2};
  
  let engine = Engine::new(EngineConfiguration::new());
  let mut node_tree = NodeTree::new();
  let root_id = node_tree.get_root_id();
  let card_id = node_tree.create_child(root_id, "Card");
  node_tree[card_id].set_local_position(Vector2::create(100.0, 100.0));
  if let Some(draggable) = node_tree.add_component(card_id, UIDraggable::new()) {
  	draggable.set_snap_back_enabled(true);
  	draggable.set_snap_back_speed(10.0);
  }
  node_tree.dispatch_touch(card_id, &engine, TouchKind::Press, Vector2::create(100.0, 100.0));
  node_tree.dispatch_touch(card_id, &engine, TouchKind::Moved, Vector2::create(200.0, 100.0));
  node_tree.dispatch_touch(card_id, &engine, TouchKind::Release, Vector2::create(200.0, 100.0));
  node_tree.tick(&engine, 0.05);
  assert!((node_tree[card_id].get_local_position().x - 150.0).abs() < 0.001);
  ```

- `pub fn set_drag_start_event(&mut self, drag_start_event: UIDraggableEvent)` (method, since 0.3.2): Sets the start event.
  - `drag_start_event` UIDraggableEvent: Event called when pressed and grabbed. (after remembering the position before the grab)
- `pub fn set_drag_move_event(&mut self, drag_move_event: UIDraggablePositionEvent)` (method, since 0.3.2): Sets the move event.
  - `drag_move_event` UIDraggablePositionEvent: Event called on every move while dragging. (after moving the node, takes the touch's view coordinates)
- `pub fn set_drag_end_event(&mut self, drag_end_event: UIDraggablePositionEvent)` (method, since 0.3.2): Sets the end event.
  - `drag_end_event` UIDraggablePositionEvent: Event called on release or when the touch is canceled. (takes the touch's view coordinates, if snap back is on it has already started returning, so stop it with settle_here)
- `pub fn is_dragging(&self) -> bool` (method, since 0.3.2): Whether it is being dragged.
  - returns bool: true if after pressing and grabbing and before release.
- `pub fn is_returning(&self) -> bool` (method, since 0.3.2): Whether it is returning.
  - returns bool: true if sliding back to the home position. (false once within 0.5 pixels)
- `pub fn get_home_position(&self) -> Option<Vector2>` (method, since 0.3.2): Local position before the grab. (None if never grabbed)
  - returns Option<Vector2>: Home position. (the local position before the last grab, the settled position after settle_here, None if never grabbed)
