-
Notifications
You must be signed in to change notification settings - Fork 85
/
Gesture.as
681 lines (557 loc) · 16.1 KB
/
Gesture.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
package org.gestouch.gestures
{
import org.gestouch.core.Gestouch;
import org.gestouch.core.GestureState;
import org.gestouch.core.GesturesManager;
import org.gestouch.core.IGestureTargetAdapter;
import org.gestouch.core.Touch;
import org.gestouch.core.gestouch_internal;
import org.gestouch.events.GestureEvent;
import flash.errors.IllegalOperationError;
import flash.events.EventDispatcher;
import flash.geom.Point;
import flash.system.Capabilities;
import flash.utils.Dictionary;
use namespace gestouch_internal;
/**
* Dispatched when the state of the gesture changes.
*
* @eventType org.gestouch.events.GestureEvent
* @see #state
*/
[Event(name="gestureStateChange", type="org.gestouch.events.GestureEvent")]
/**
* Dispatched when the state of the gesture changes to GestureState.POSSIBLE.
*
* @eventType org.gestouch.events.GestureEvent
* @see #state
*/
[Event(name="gesturePossible", type="org.gestouch.events.GestureEvent")]
/**
* Dispatched when the state of the gesture changes to GestureState.FAILED.
*
* @eventType org.gestouch.events.GestureEvent
* @see #state
*/
[Event(name="gestureFailed", type="org.gestouch.events.GestureEvent")]
/**
* Base class for all gestures. Gesture is essentially a detector that tracks touch points
* in order detect specific gesture motion and form gesture event on target.
*
* @author Pavel fljot
*/
public class Gesture extends EventDispatcher
{
/**
* Threshold for screen distance they must move to count as valid input
* (not an accidental offset on touch),
* based on 20 pixels on a 252ppi device.
*/
public static var DEFAULT_SLOP:uint = Math.round(20 / 252 * flash.system.Capabilities.screenDPI);
/**
* If a gesture should receive a touch.
* Callback signature: function(gesture:Gesture, touch:Touch):Boolean
*
* @see Touch
*/
public var gestureShouldReceiveTouchCallback:Function;
/**
* If a gesture should be recognized (transition from state POSSIBLE to state RECOGNIZED or BEGAN).
* Returning <code>false</code> causes the gesture to transition to the FAILED state.
*
* Callback signature: function(gesture:Gesture):Boolean
*
* @see state
* @see GestureState
*/
public var gestureShouldBeginCallback:Function;
/**
* If two gestures should be allowed to recognize simultaneously.
*
* Callback signature: function(gesture:Gesture, otherGesture:Gesture):Boolean
*/
public var gesturesShouldRecognizeSimultaneouslyCallback:Function;
protected const _gesturesManager:GesturesManager = Gestouch.gesturesManager;
/**
* Map (generic object) of tracking touch points, where keys are touch points IDs.
*/
protected var _touchesMap:Object = {};
protected var _centralPoint:Point = new Point();
/**
* List of gesture we require to fail.
* @see requireGestureToFail()
*/
protected var _gesturesToFail:Dictionary = new Dictionary(true);
protected var _pendingRecognizedState:GestureState;
public function Gesture(target:Object = null)
{
super();
preinit();
this.target = target;
}
/** @private */
protected var _targetAdapter:IGestureTargetAdapter;
/**
*
*/
gestouch_internal function get targetAdapter():IGestureTargetAdapter
{
return _targetAdapter;
}
protected function get targetAdapter():IGestureTargetAdapter
{
return _targetAdapter;
}
/**
* FIXME
* InteractiveObject (DisplayObject) which this gesture is tracking the actual gesture motion on.
*
* <p>Could be some image, component (like map) or the larger view like Stage.</p>
*
* <p>You can change the target in the runtime, e.g. you have a gallery
* where only one item is visible at the moment, so use one gesture instance
* and change the target to the currently visible item.</p>
*
* @see http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/InteractiveObject.html
*/
public function get target():Object
{
return _targetAdapter ? _targetAdapter.target : null;
}
public function set target(value:Object):void
{
var target:Object = this.target;
if (target == value)
return;
uninstallTarget(target);
_targetAdapter = value ? Gestouch.createGestureTargetAdapter(value) : null;
installTarget(value);
}
/** @private */
protected var _enabled:Boolean = true;
/**
* @default true
*/
public function get enabled():Boolean
{
return _enabled;
}
public function set enabled(value:Boolean):void
{
if (_enabled == value)
return;
_enabled = value;
if (!_enabled)
{
if (state == GestureState.POSSIBLE)
{
setState(GestureState.FAILED);
}
else
if (state == GestureState.BEGAN || state == GestureState.CHANGED)
{
setState(GestureState.CANCELLED);
}
}
}
protected var _state:GestureState = GestureState.POSSIBLE;
public function get state():GestureState
{
return _state;
}
protected var _idle:Boolean = true;
gestouch_internal function get idle():Boolean
{
return _idle;
}
protected var _touchesCount:uint = 0;
/**
* Amount of currently tracked touch points.
*
* @see #_touches
*/
public function get touchesCount():uint
{
return _touchesCount;
}
protected var _location:Point = new Point();
/**
* Virtual central touch point among all tracking touch points (geometrical center).
*/
public function get location():Point
{
return _location.clone();
}
//--------------------------------------------------------------------------
//
// Public methods
//
//--------------------------------------------------------------------------
[Abstract]
/**
* Reflects gesture class (for better perfomance).
*
* <p><b>NB!</b> This is abstract method and must be overridden.</p>
*
* @see performance optimization tips
*/
public function reflect():Class
{
throw Error("reflect() is abstract method and must be overridden.");
}
public function isTrackingTouch(touchID:uint):Boolean
{
return (_touchesMap[touchID] != undefined);
}
/**
* Cancels current tracking (interaction) cycle.
*
* <p>Could be useful to "stop" gesture for the current interaction cycle.</p>
*/
public function reset():void
{
if (idle)
return;// Do nothing as we are idle and there is nothing to reset
const state:GestureState = this.state;//caching getter
_location.x = 0;
_location.y = 0;
_touchesMap = {};
_touchesCount = 0;
_idle = true;
for (var key:* in _gesturesToFail)
{
var gestureToFail:Gesture = key as Gesture;
gestureToFail.removeEventListener(GestureEvent.GESTURE_STATE_CHANGE, gestureToFail_stateChangeHandler);
}
_pendingRecognizedState = null;
if (state == GestureState.POSSIBLE)
{
// manual reset() call. Set to FAILED to keep our State Machine clean and stable
setState(GestureState.FAILED);
}
else if (state == GestureState.BEGAN || state == GestureState.CHANGED)
{
// manual reset() call. Set to CANCELLED to keep our State Machine clean and stable
setState(GestureState.CANCELLED);
}
else
{
// reset from GesturesManager after reaching one of the 4 final states:
// (state == GestureState.RECOGNIZED ||
// state == GestureState.ENDED ||
// state == GestureState.FAILED ||
// state == GestureState.CANCELLED)
setState(GestureState.POSSIBLE);
}
}
/**
* Remove gesture and prepare it for GC.
*
* <p>The gesture is not able to use after calling this method.</p>
*/
public function dispose():void
{
//TODO
reset();
target = null;
gestureShouldReceiveTouchCallback = null;
gestureShouldBeginCallback = null;
gesturesShouldRecognizeSimultaneouslyCallback = null;
_gesturesToFail = null;
}
gestouch_internal function canBePreventedByGesture(preventingGesture:Gesture):Boolean
{
return true;
}
gestouch_internal function canPreventGesture(preventedGesture:Gesture):Boolean
{
return true;
}
/**
* <b>NB! Current implementation is highly experimental!</b> See examples for more info.
*/
public function requireGestureToFail(gesture:Gesture):void
{
//TODO
if (!gesture)
{
throw new ArgumentError();
}
_gesturesToFail[gesture] = true;
}
// --------------------------------------------------------------------------
//
// Protected methods
//
// --------------------------------------------------------------------------
/**
* First method, called in constructor.
*/
protected function preinit():void
{
}
/**
* Called internally when changing the target.
*
* @see http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/InteractiveObject.html
*/
protected function installTarget(target:Object):void
{
if (target)
{
_gesturesManager.addGesture(this);
}
}
/**
* Called internally when changing the target.
*
* <p>You should remove all listeners from target here.</p>
*
* @see http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/InteractiveObject.html
*/
protected function uninstallTarget(target:Object):void
{
if (target)
{
_gesturesManager.removeGesture(this);
}
}
/**
* TODO: clarify usage. For now it's supported to call this method in onTouchBegin with return.
*/
protected function ignoreTouch(touch:Touch):void
{
if (touch.id in _touchesMap)
{
delete _touchesMap[touch.id];
_touchesCount--;
}
}
protected function failOrIgnoreTouch(touch:Touch):void
{
if (state == GestureState.POSSIBLE)
{
setState(GestureState.FAILED);
}
else
{
ignoreTouch(touch);
}
}
[Abstract]
/**
* <p><b>NB!</b> This is abstract method and must be overridden.</p>
*/
protected function onTouchBegin(touch:Touch):void
{
}
[Abstract]
/**
* <p><b>NB!</b> This is abstract method and must be overridden.</p>
*/
protected function onTouchMove(touch:Touch):void
{
}
[Abstract]
/**
* <p><b>NB!</b> This is abstract method and must be overridden.</p>
*/
protected function onTouchEnd(touch:Touch):void
{
}
/**
*
*/
protected function onTouchCancel(touch:Touch):void
{
}
protected function setState(newState:GestureState):Boolean
{
if (_state == newState && _state == GestureState.CHANGED)
{
// shortcut for better performance
if (hasEventListener(GestureEvent.GESTURE_STATE_CHANGE))
{
dispatchEvent(new GestureEvent(GestureEvent.GESTURE_STATE_CHANGE, _state, _state));
}
if (hasEventListener(GestureEvent.GESTURE_CHANGED))
{
dispatchEvent(new GestureEvent(GestureEvent.GESTURE_CHANGED, _state, _state));
}
resetNotificationProperties();
return true;
}
if (!_state.canTransitionTo(newState))
{
throw new IllegalOperationError("You cannot change from state " +
_state + " to state " + newState + ".");
}
if (newState != GestureState.POSSIBLE)
{
// in case instantly switch state in touchBeganHandler()
_idle = false;
}
if (newState == GestureState.BEGAN || newState == GestureState.RECOGNIZED)
{
var gestureToFail:Gesture;
var key:*;
// first we check if other required-to-fail gestures recognized
// TODO: is this really necessary? using "requireGestureToFail" API assume that
// required-to-fail gesture always recognizes AFTER this one.
for (key in _gesturesToFail)
{
gestureToFail = key as Gesture;
if (!gestureToFail.idle &&
gestureToFail.state != GestureState.POSSIBLE &&
gestureToFail.state != GestureState.FAILED)
{
// Looks like other gesture won't fail,
// which means the required condition will not happen, so we must fail
setState(GestureState.FAILED);
return false;
}
}
// then we check if other required-to-fail gestures are actually tracked (not IDLE)
// and not still not recognized (e.g. POSSIBLE state)
for (key in _gesturesToFail)
{
gestureToFail = key as Gesture;
if (gestureToFail.state == GestureState.POSSIBLE)
{
// Other gesture might fail soon, so we postpone state change
_pendingRecognizedState = newState;
for (key in _gesturesToFail)
{
gestureToFail = key as Gesture;
gestureToFail.addEventListener(GestureEvent.GESTURE_STATE_CHANGE, gestureToFail_stateChangeHandler, false, 0, true);
}
return false;
}
// else if gesture is in IDLE state it means it doesn't track anything,
// so we simply ignore it as it doesn't seem like conflict from this perspective
// (perspective of using "requireGestureToFail" API)
}
if (gestureShouldBeginCallback != null && !gestureShouldBeginCallback(this))
{
setState(GestureState.FAILED);
return false;
}
}
var oldState:GestureState = _state;
_state = newState;
if (_state.isEndState)
{
_gesturesManager.scheduleGestureStateReset(this);
}
//TODO: what if RTE happens in event handlers?
if (hasEventListener(GestureEvent.GESTURE_STATE_CHANGE))
{
dispatchEvent(new GestureEvent(GestureEvent.GESTURE_STATE_CHANGE, _state, oldState));
}
if (hasEventListener(_state.toEventType()))
{
dispatchEvent(new GestureEvent(_state.toEventType(), _state, oldState));
}
resetNotificationProperties();
if (_state == GestureState.BEGAN || _state == GestureState.RECOGNIZED)
{
_gesturesManager.onGestureRecognized(this);
}
return true;
}
gestouch_internal function setState_internal(state:GestureState):void
{
setState(state);
}
protected function updateCentralPoint():void
{
var touchLocation:Point;
var x:Number = 0;
var y:Number = 0;
for (var touchID:String in _touchesMap)
{
touchLocation = (_touchesMap[int(touchID)] as Touch).location;
x += touchLocation.x;
y += touchLocation.y;
}
_centralPoint.x = x / _touchesCount;
_centralPoint.y = y / _touchesCount;
}
protected function updateLocation():void
{
updateCentralPoint();
_location.x = _centralPoint.x;
_location.y = _centralPoint.y;
}
protected function resetNotificationProperties():void
{
}
//--------------------------------------------------------------------------
//
// Event handlers
//
//--------------------------------------------------------------------------
gestouch_internal function touchBeginHandler(touch:Touch):void
{
_touchesMap[touch.id] = touch;
_touchesCount++;
onTouchBegin(touch);
if (_touchesCount == 1 && state == GestureState.POSSIBLE)
{
_idle = false;
}
}
gestouch_internal function touchMoveHandler(touch:Touch):void
{
_touchesMap[touch.id] = touch;
onTouchMove(touch);
}
gestouch_internal function touchEndHandler(touch:Touch):void
{
delete _touchesMap[touch.id];
_touchesCount--;
onTouchEnd(touch);
}
gestouch_internal function touchCancelHandler(touch:Touch):void
{
delete _touchesMap[touch.id];
_touchesCount--;
onTouchCancel(touch);
if (!state.isEndState)
{
if (state == GestureState.BEGAN || state == GestureState.CHANGED)
{
setState(GestureState.CANCELLED);
}
else
{
setState(GestureState.FAILED);
}
}
}
protected function gestureToFail_stateChangeHandler(event:GestureEvent):void
{
if (!_pendingRecognizedState || state != GestureState.POSSIBLE)
return;
if (event.newState == GestureState.FAILED)
{
for (var key:* in _gesturesToFail)
{
var gestureToFail:Gesture = key as Gesture;
if (gestureToFail.state == GestureState.POSSIBLE)
{
// we're still waiting for some gesture to fail
return;
}
}
// at this point all gestures-to-fail are either in IDLE or in FAILED states
setState(_pendingRecognizedState);
}
else if (event.newState != GestureState.POSSIBLE)
{
//TODO: need to re-think this over
setState(GestureState.FAILED);
}
}
}
}