OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of touch; | 5 part of touch; |
6 | 6 |
7 /** | 7 /** |
8 * Touch Handler. Class that handles all touch events and | 8 * Touch Handler. Class that handles all touch events and |
9 * uses them to interpret higher level gestures and behaviors. TouchEvent is a | 9 * uses them to interpret higher level gestures and behaviors. TouchEvent is a |
10 * built in mobile safari type: | 10 * built in mobile safari type: |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 return absVelocity * (velocity < 0 ? -1 : 1); | 160 return absVelocity * (velocity < 0 ? -1 : 1); |
161 } | 161 } |
162 | 162 |
163 /** | 163 /** |
164 * Start listenting for events. | 164 * Start listenting for events. |
165 * If [capture] is True the TouchHandler should listen during the capture | 165 * If [capture] is True the TouchHandler should listen during the capture |
166 * phase. | 166 * phase. |
167 */ | 167 */ |
168 void enable([bool capture = false]) { | 168 void enable([bool capture = false]) { |
169 Function onEnd = (e) { | 169 Function onEnd = (e) { |
170 _onEnd(e.timeStamp, e); | 170 _onEnd(e.timeStamp.toInt(), e); |
171 }; | 171 }; |
172 _addEventListeners(_element, (e) { | 172 _addEventListeners(_element, (e) { |
173 _onStart(e); | 173 _onStart(e); |
174 }, (e) { | 174 }, (e) { |
175 _onMove(e); | 175 _onMove(e); |
176 }, onEnd, onEnd, capture); | 176 }, onEnd, onEnd, capture); |
177 } | 177 } |
178 | 178 |
179 /** | 179 /** |
180 * Get the current horizontal drag delta. Drag delta is defined as the deltaX | 180 * Get the current horizontal drag delta. Drag delta is defined as the deltaX |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 */ | 265 */ |
266 void _onMove(TouchEvent e) { | 266 void _onMove(TouchEvent e) { |
267 if (!_tracking || _draggable == null) { | 267 if (!_tracking || _draggable == null) { |
268 return; | 268 return; |
269 } | 269 } |
270 final touch = e.touches[0]; | 270 final touch = e.touches[0]; |
271 int clientX = touch.client.x; | 271 int clientX = touch.client.x; |
272 int clientY = touch.client.y; | 272 int clientY = touch.client.y; |
273 int moveX = _lastTouchX - clientX; | 273 int moveX = _lastTouchX - clientX; |
274 int moveY = _lastTouchY - clientY; | 274 int moveY = _lastTouchY - clientY; |
| 275 int timeStamp = e.timeStamp.toInt(); |
275 _totalMoveX += moveX.abs(); | 276 _totalMoveX += moveX.abs(); |
276 _totalMoveY += moveY.abs(); | 277 _totalMoveY += moveY.abs(); |
277 _lastTouchX = clientX; | 278 _lastTouchX = clientX; |
278 _lastTouchY = clientY; | 279 _lastTouchY = clientY; |
279 if (!_dragging && | 280 if (!_dragging && |
280 ((_totalMoveY > _MIN_TRACKING_FOR_DRAG && _draggable.verticalEnabled) || | 281 ((_totalMoveY > _MIN_TRACKING_FOR_DRAG && _draggable.verticalEnabled) || |
281 (_totalMoveX > _MIN_TRACKING_FOR_DRAG && | 282 (_totalMoveX > _MIN_TRACKING_FOR_DRAG && |
282 _draggable.horizontalEnabled))) { | 283 _draggable.horizontalEnabled))) { |
283 _dragging = _draggable.onDragStart(e); | 284 _dragging = _draggable.onDragStart(e); |
284 if (!_dragging) { | 285 if (!_dragging) { |
285 _endTracking(); | 286 _endTracking(); |
286 } else { | 287 } else { |
287 _startTouchX = clientX; | 288 _startTouchX = clientX; |
288 _startTouchY = clientY; | 289 _startTouchY = clientY; |
289 _startTime = e.timeStamp; | 290 _startTime = timeStamp; |
290 } | 291 } |
291 } | 292 } |
292 if (_dragging) { | 293 if (_dragging) { |
293 _draggable.onDragMove(); | 294 _draggable.onDragMove(); |
294 _lastEvent = e; | 295 _lastEvent = e; |
295 e.preventDefault(); | 296 e.preventDefault(); |
296 _recentTouchesX = | 297 _recentTouchesX = |
297 _removeTouchesInWrongDirection(_recentTouchesX, _lastMoveX, moveX); | 298 _removeTouchesInWrongDirection(_recentTouchesX, _lastMoveX, moveX); |
298 _recentTouchesY = | 299 _recentTouchesY = |
299 _removeTouchesInWrongDirection(_recentTouchesY, _lastMoveY, moveY); | 300 _removeTouchesInWrongDirection(_recentTouchesY, _lastMoveY, moveY); |
300 _recentTouchesX = _removeOldTouches(_recentTouchesX, e.timeStamp); | 301 _recentTouchesX = _removeOldTouches(_recentTouchesX, timeStamp); |
301 _recentTouchesY = _removeOldTouches(_recentTouchesY, e.timeStamp); | 302 _recentTouchesY = _removeOldTouches(_recentTouchesY, timeStamp); |
302 _recentTouchesX.add(clientX); | 303 _recentTouchesX.add(clientX); |
303 _recentTouchesX.add(e.timeStamp); | 304 _recentTouchesX.add(timeStamp); |
304 _recentTouchesY.add(clientY); | 305 _recentTouchesY.add(clientY); |
305 _recentTouchesY.add(e.timeStamp); | 306 _recentTouchesY.add(timeStamp); |
306 } | 307 } |
307 _lastMoveX = moveX; | 308 _lastMoveX = moveX; |
308 _lastMoveY = moveY; | 309 _lastMoveY = moveY; |
309 } | 310 } |
310 | 311 |
311 /** | 312 /** |
312 * Touch start handler. | 313 * Touch start handler. |
313 */ | 314 */ |
314 void _onStart(TouchEvent e) { | 315 void _onStart(TouchEvent e) { |
315 if (_touching) { | 316 if (_touching) { |
316 return; | 317 return; |
317 } | 318 } |
318 _touching = true; | 319 _touching = true; |
319 if (!_touchable.onTouchStart(e) || _draggable == null) { | 320 if (!_touchable.onTouchStart(e) || _draggable == null) { |
320 return; | 321 return; |
321 } | 322 } |
322 final touch = e.touches[0]; | 323 final touch = e.touches[0]; |
323 _startTouchX = _lastTouchX = touch.client.x; | 324 _startTouchX = _lastTouchX = touch.client.x; |
324 _startTouchY = _lastTouchY = touch.client.y; | 325 _startTouchY = _lastTouchY = touch.client.y; |
325 _startTime = e.timeStamp; | 326 int timeStamp = e.timeStamp.toInt(); |
| 327 _startTime = timeStamp; |
326 // TODO(jacobr): why don't we just clear the lists? | 328 // TODO(jacobr): why don't we just clear the lists? |
327 _recentTouchesX = new List<int>(); | 329 _recentTouchesX = new List<int>(); |
328 _recentTouchesY = new List<int>(); | 330 _recentTouchesY = new List<int>(); |
329 _recentTouchesX.add(touch.client.x); | 331 _recentTouchesX.add(touch.client.x); |
330 _recentTouchesX.add(e.timeStamp); | 332 _recentTouchesX.add(timeStamp); |
331 _recentTouchesY.add(touch.client.y); | 333 _recentTouchesY.add(touch.client.y); |
332 _recentTouchesY.add(e.timeStamp); | 334 _recentTouchesY.add(timeStamp); |
333 _lastEvent = e; | 335 _lastEvent = e; |
334 _beginTracking(); | 336 _beginTracking(); |
335 } | 337 } |
336 | 338 |
337 /** | 339 /** |
338 * Filters the provided recent touches list to remove all touches older than | 340 * Filters the provided recent touches list to remove all touches older than |
339 * the max tracking time or the 5th most recent touch. | 341 * the max tracking time or the 5th most recent touch. |
340 * [recentTouches] specifies a list of tuples where the first item is the x | 342 * [recentTouches] specifies a list of tuples where the first item is the x |
341 * or y component of the recent touch and the second item is the touch time | 343 * or y component of the recent touch and the second item is the touch time |
342 * stamp. The time of the most recent event is specified by [recentTime]. | 344 * stamp. The time of the most recent event is specified by [recentTime]. |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
389 | 391 |
390 /** | 392 /** |
391 * Call this method to enable drag behavior on a draggable delegate. | 393 * Call this method to enable drag behavior on a draggable delegate. |
392 * The [draggable] object can be the same as the [_touchable] object, they are | 394 * The [draggable] object can be the same as the [_touchable] object, they are |
393 * assigned to different members to allow for strong typing with interfaces. | 395 * assigned to different members to allow for strong typing with interfaces. |
394 */ | 396 */ |
395 void setDraggable(Draggable draggable) { | 397 void setDraggable(Draggable draggable) { |
396 _draggable = draggable; | 398 _draggable = draggable; |
397 } | 399 } |
398 } | 400 } |
OLD | NEW |