Wiki

Clone wiki

hydro / Gestures

Gesture types

The gestures defined for the Hydro interface are:

  • TAP: a single touch on the screen
  • SWIPE: a slide with a start point and an end point
  • SPRINCH: a double touch, that will become either a PINCH or a SPREAD
  • PINCH: double touch that ends with the fingers closer
  • SPREAD: double touch that ends with the fingers widened

Every gesture can be in one of the following states:

  • start: the gesture is recognised in the current frame
  • progress: the gesture started in an older frame but it is not ended yet
  • end: the user stopped touching the device and the final event is fired

A gesture can be canceled by the user, usually by ending a gesture moving the touch position to its starting point. Gesture cancelation can be intercepted as a gesture progress event with the canceled flag set to true (for events that support cancelation).

Event sources

The GestureRecognizer script listens actively for touch events and fires them to the registered handlers. The events it fires follow the description above.

The GesturesEmulator listens to mouse and keyboard events and fires a subset of event types:

  • OnClick: the mouse button has been clicked and the mouse has not moved by a distance greater than MinDragSpan
  • OnDrag: the mouse has been dragged by a distance greater than MinDragSpan
  • OnZoomIn: triggered when the left shift key is released while the W key is down
  • OnZoomOut: same as OnZoomIn but with the S key

To make scripts agnostic of the kind of the gesture subsystem currently in use, the GesturesDispatcher hides it by defining a set of common event types (the same described in the "Gesture types" section). The GesturesEmulator events are automatically converted to taps, swipes, pinches and spreads, but fired only with the end state.

Gestures subsystem setup

To use the GesturesDispatcher script, it must be added to the scene with the GestureRecognizer and/or the GesturesEmulator scripts. Then from any script an event handler can be added to the dispatcher's events.

#!c#

public GameObject gestures;    // assigned from Unity inspector

void Awake() {
    var dispatcher = gestures.GetComponent<GesturesDispatcher>();
    // specific gesture type
    dispatcher.OnTapEnd += tap => DoSomethingWithEnded(tap);
    // any gesture type
    dispatcher.OnGestureProgress += gesture => DoSomethingWithAnyOngoing(gesture);
}

The prefab Assets/Gestures/GestureSystem must be added to the scene, and passed to the script above through the Unity inspector.

Updated