-
Notifications
You must be signed in to change notification settings - Fork 84
/
GesturesManager.as
364 lines (304 loc) · 9.88 KB
/
GesturesManager.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
package org.gestouch.core
{
import flash.display.DisplayObject;
import flash.display.Shape;
import flash.display.Stage;
import flash.errors.IllegalOperationError;
import flash.events.Event;
import flash.events.IEventDispatcher;
import flash.utils.Dictionary;
import flash.utils.getQualifiedClassName;
import org.gestouch.extensions.native.NativeTouchHitTester;
import org.gestouch.gestures.Gesture;
import org.gestouch.input.NativeInputAdapter;
/**
* @author Pavel fljot
*/
public class GesturesManager
{
protected const _frameTickerShape:Shape = new Shape();
protected var _gesturesMap:Dictionary = new Dictionary(true);
protected var _gesturesForTouchMap:Dictionary = new Dictionary();
protected var _gesturesForTargetMap:Dictionary = new Dictionary(true);
protected var _dirtyGesturesCount:uint = 0;
protected var _dirtyGesturesMap:Dictionary = new Dictionary(true);
protected var _stage:Stage;
use namespace gestouch_internal;
public function GesturesManager()
{
}
//--------------------------------------------------------------------------
//
// Private methods
//
//--------------------------------------------------------------------------
protected function onStageAvailable(stage:Stage):void
{
_stage = stage;
Gestouch.inputAdapter ||= new NativeInputAdapter(stage);
Gestouch.addTouchHitTester(new NativeTouchHitTester(stage));
}
protected function resetDirtyGestures():void
{
for (var gesture:Object in _dirtyGesturesMap)
{
(gesture as Gesture).reset();
}
_dirtyGesturesCount = 0;
_dirtyGesturesMap = new Dictionary(true);
_frameTickerShape.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
gestouch_internal function addGesture(gesture:Gesture):void
{
if (!gesture)
{
throw new ArgumentError("Argument 'gesture' must be not null.");
}
const target:Object = gesture.target;
if (!target)
{
throw new IllegalOperationError("Gesture must have target.");
}
var targetGestures:Vector.<Gesture> = _gesturesForTargetMap[target] as Vector.<Gesture>;
if (targetGestures)
{
if (targetGestures.indexOf(gesture) == -1)
{
targetGestures.push(gesture);
}
}
else
{
targetGestures = _gesturesForTargetMap[target] = new Vector.<Gesture>();
targetGestures[0] = gesture;
}
_gesturesMap[gesture] = true;
if (!_stage)
{
var targetAsDO:DisplayObject = target as DisplayObject;
if (targetAsDO)
{
if (targetAsDO.stage)
{
onStageAvailable(targetAsDO.stage);
}
else
{
targetAsDO.addEventListener(Event.ADDED_TO_STAGE, gestureTarget_addedToStageHandler, false,0, true);
}
}
}
}
gestouch_internal function removeGesture(gesture:Gesture):void
{
if (!gesture)
{
throw new ArgumentError("Argument 'gesture' must be not null.");
}
var target:Object = gesture.target;
// check for target because it could be already GC-ed (since target reference is weak)
if (target)
{
var targetGestures:Vector.<Gesture> = _gesturesForTargetMap[target] as Vector.<Gesture>;
if (targetGestures.length > 1)
{
targetGestures.splice(targetGestures.indexOf(gesture), 1);
}
else
{
delete _gesturesForTargetMap[target];
if (target is IEventDispatcher)
{
(target as IEventDispatcher).removeEventListener(Event.ADDED_TO_STAGE, gestureTarget_addedToStageHandler);
}
}
}
delete _gesturesMap[gesture];
gesture.reset();
}
gestouch_internal function scheduleGestureStateReset(gesture:Gesture):void
{
if (!_dirtyGesturesMap[gesture])
{
_dirtyGesturesMap[gesture] = true;
_dirtyGesturesCount++;
_frameTickerShape.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
}
gestouch_internal function onGestureRecognized(gesture:Gesture):void
{
const target:Object = gesture.target;
for (var key:Object in _gesturesMap)
{
var otherGesture:Gesture = key as Gesture;
var otherTarget:Object = otherGesture.target;
// conditions for otherGesture "own properties"
if (otherGesture != gesture &&
target && otherTarget &&//in case GC worked half way through
otherGesture.enabled &&
otherGesture.state == GestureState.POSSIBLE)
{
if (otherTarget == target ||
gesture.targetAdapter.contains(otherTarget) ||
otherGesture.targetAdapter.contains(target)
)
{
// conditions for gestures relations
if (gesture.canPreventGesture(otherGesture) &&
otherGesture.canBePreventedByGesture(gesture) &&
(gesture.gesturesShouldRecognizeSimultaneouslyCallback == null ||
!gesture.gesturesShouldRecognizeSimultaneouslyCallback(gesture, otherGesture)) &&
(otherGesture.gesturesShouldRecognizeSimultaneouslyCallback == null ||
!otherGesture.gesturesShouldRecognizeSimultaneouslyCallback(otherGesture, gesture)))
{
otherGesture.setState_internal(GestureState.FAILED);
}
}
}
}
}
gestouch_internal function onTouchBegin(touch:Touch):void
{
var gesture:Gesture;
var i:uint;
// This vector will contain active gestures for specific touch during all touch session.
var gesturesForTouch:Vector.<Gesture> = _gesturesForTouchMap[touch] as Vector.<Gesture>;
if (!gesturesForTouch)
{
gesturesForTouch = new Vector.<Gesture>();
_gesturesForTouchMap[touch] = gesturesForTouch;
}
else
{
// touch object may be pooled in the future
gesturesForTouch.length = 0;
}
var target:Object = touch.target;
const displayListAdapter:IDisplayListAdapter = Gestouch.gestouch_internal::getDisplayListAdapter(target);
if (!displayListAdapter)
{
throw new Error("Display list adapter not found for target of type '" + getQualifiedClassName(target) + "'.");
}
const hierarchy:Vector.<Object> = displayListAdapter.getHierarchy(target);
const hierarchyLength:uint = hierarchy.length;
if (hierarchyLength == 0)
{
throw new Error("No hierarchy build for target '" + target +"'. Something is wrong with that IDisplayListAdapter.");
}
if (_stage && !(hierarchy[hierarchyLength - 1] is Stage))
{
// Looks like some non-native (non DisplayList) hierarchy
// but we must always handle gestures with Stage target
// since Stage is anyway the top-most parent
hierarchy[hierarchyLength] = _stage;
}
// Create a sorted(!) list of gestures which are interested in this touch.
// Sorting priority: deeper target has higher priority, recently added gesture has higher priority.
var gesturesForTarget:Vector.<Gesture>;
for each (target in hierarchy)
{
gesturesForTarget = _gesturesForTargetMap[target] as Vector.<Gesture>;
if (gesturesForTarget)
{
i = gesturesForTarget.length;
while (i-- > 0)
{
gesture = gesturesForTarget[i];
if (gesture.enabled &&
(gesture.gestureShouldReceiveTouchCallback == null ||
gesture.gestureShouldReceiveTouchCallback(gesture, touch)))
{
//TODO: optimize performance! decide between unshift() vs [i++] = gesture + reverse()
gesturesForTouch.unshift(gesture);
}
}
}
}
// Then we populate them with this touch and event.
// They might start tracking this touch or ignore it (via Gesture#ignoreTouch())
i = gesturesForTouch.length;
while (i-- > 0)
{
gesture = gesturesForTouch[i];
// Check for state because previous (i+1) gesture may already abort current (i) one
if (!_dirtyGesturesMap[gesture])
{
gesture.touchBeginHandler(touch);
}
else
{
gesturesForTouch.splice(i, 1);
}
}
}
gestouch_internal function onTouchMove(touch:Touch):void
{
var gesturesForTouch:Vector.<Gesture> = _gesturesForTouchMap[touch] as Vector.<Gesture>;
var gesture:Gesture;
var i:uint = gesturesForTouch.length;
while (i-- > 0)
{
gesture = gesturesForTouch[i];
if (!_dirtyGesturesMap[gesture] && gesture.isTrackingTouch(touch.id))
{
gesture.touchMoveHandler(touch);
}
else
{
// gesture is no more interested in this touch (e.g. ignoreTouch was called)
gesturesForTouch.splice(i, 1);
}
}
}
gestouch_internal function onTouchEnd(touch:Touch):void
{
var gesturesForTouch:Vector.<Gesture> = _gesturesForTouchMap[touch] as Vector.<Gesture>;
var gesture:Gesture;
var i:uint = gesturesForTouch.length;
while (i-- > 0)
{
gesture = gesturesForTouch[i];
if (!_dirtyGesturesMap[gesture] && gesture.isTrackingTouch(touch.id))
{
gesture.touchEndHandler(touch);
}
}
gesturesForTouch.length = 0;// release for GC
delete _gesturesForTouchMap[touch];//TODO: remove this once Touch objects are pooled
}
gestouch_internal function onTouchCancel(touch:Touch):void
{
var gesturesForTouch:Vector.<Gesture> = _gesturesForTouchMap[touch] as Vector.<Gesture>;
var gesture:Gesture;
var i:uint = gesturesForTouch.length;
while (i-- > 0)
{
gesture = gesturesForTouch[i];
if (!_dirtyGesturesMap[gesture] && gesture.isTrackingTouch(touch.id))
{
gesture.touchCancelHandler(touch);
}
}
gesturesForTouch.length = 0;// release for GC
delete _gesturesForTouchMap[touch];//TODO: remove this once Touch objects are pooled
}
//--------------------------------------------------------------------------
//
// Event handlers
//
//--------------------------------------------------------------------------
protected function gestureTarget_addedToStageHandler(event:Event):void
{
var target:DisplayObject = event.target as DisplayObject;
target.removeEventListener(Event.ADDED_TO_STAGE, gestureTarget_addedToStageHandler);
if (!_stage)
{
onStageAvailable(target.stage);
}
}
private function enterFrameHandler(event:Event):void
{
resetDirtyGestures();
}
}
}