Wiki
Clone wikihydro / Gestures
Gesture types
The gestures defined for the Hydro interface are:
TAP
: a single touch on the screenSWIPE
: a slide with a start point and an end pointSPRINCH
: a double touch, that will become either aPINCH
or aSPREAD
PINCH
: double touch that ends with the fingers closerSPREAD
: 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 frameprogress
: the gesture started in an older frame but it is not ended yetend
: 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 thanMinDragSpan
OnDrag
: the mouse has been dragged by a distance greater thanMinDragSpan
OnZoomIn
: triggered when the left shift key is released while theW
key is downOnZoomOut
: same asOnZoomIn
but with theS
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 tap
s, swipe
s, pinch
es and spread
s, 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