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 /** | 5 /** |
6 * Implementations can be used to simulate the deceleration of an element within | 6 * Implementations can be used to simulate the deceleration of an element within |
7 * a certain region. To use this behavior you need to provide an initial | 7 * a certain region. To use this behavior you need to provide an initial |
8 * velocity that is meant to represent the gesture that is initiating this | 8 * velocity that is meant to represent the gesture that is initiating this |
9 * deceleration. You also provide the bounds of the region that the element | 9 * deceleration. You also provide the bounds of the region that the element |
10 * exists in, and the current offset of the element within that region. The | 10 * exists in, and the current offset of the element within that region. The |
(...skipping 437 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
448 assert (_stepTimeout === null); | 448 assert (_stepTimeout === null); |
449 assert(minCoord.x <= maxCoord.x); | 449 assert(minCoord.x <= maxCoord.x); |
450 assert(minCoord.y <= maxCoord.y); | 450 assert(minCoord.y <= maxCoord.y); |
451 _previousOffset = initialOffset.clone(); | 451 _previousOffset = initialOffset.clone(); |
452 physicsX.configure(minCoord.x, maxCoord.x, initialOffset.x, | 452 physicsX.configure(minCoord.x, maxCoord.x, initialOffset.x, |
453 _customDecelerationFactor, velocity.x); | 453 _customDecelerationFactor, velocity.x); |
454 physicsY.configure(minCoord.y, maxCoord.y, initialOffset.y, | 454 physicsY.configure(minCoord.y, maxCoord.y, initialOffset.y, |
455 _customDecelerationFactor, velocity.y); | 455 _customDecelerationFactor, velocity.y); |
456 if (!physicsX.isDone() || !physicsY.isDone()) { | 456 if (!physicsX.isDone() || !physicsY.isDone()) { |
457 _calculateMoves(); | 457 _calculateMoves(); |
458 if (!_moves.isEmpty()) { | 458 if (!_moves.isEmpty) { |
459 num firstTime = _moves.first().time; | 459 num firstTime = _moves.first().time; |
460 _stepTimeout = Env.requestAnimationFrame( | 460 _stepTimeout = Env.requestAnimationFrame( |
461 _step, null, firstTime); | 461 _step, null, firstTime); |
462 _decelerating = true; | 462 _decelerating = true; |
463 return true; | 463 return true; |
464 } | 464 } |
465 } | 465 } |
466 _decelerating = false; | 466 _decelerating = false; |
467 return false; | 467 return false; |
468 } | 468 } |
(...skipping 14 matching lines...) Expand all Loading... |
483 | 483 |
484 /** | 484 /** |
485 * Calculate the next offset of the element and animate it to that position. | 485 * Calculate the next offset of the element and animate it to that position. |
486 */ | 486 */ |
487 void _step(num timestamp) { | 487 void _step(num timestamp) { |
488 _stepTimeout = null; | 488 _stepTimeout = null; |
489 | 489 |
490 // Prune moves that are more than 1 frame behind when we have more | 490 // Prune moves that are more than 1 frame behind when we have more |
491 // available moves. | 491 // available moves. |
492 num lastEpoch = timestamp - SingleDimensionPhysics._MS_PER_FRAME; | 492 num lastEpoch = timestamp - SingleDimensionPhysics._MS_PER_FRAME; |
493 while (!_moves.isEmpty() && _moves.first() !== _moves.last() | 493 while (!_moves.isEmpty && _moves.first() !== _moves.last() |
494 && _moves.first().time < lastEpoch) { | 494 && _moves.first().time < lastEpoch) { |
495 _moves.removeFirst(); | 495 _moves.removeFirst(); |
496 } | 496 } |
497 | 497 |
498 if (!_moves.isEmpty()) { | 498 if (!_moves.isEmpty) { |
499 final move = _moves.removeFirst(); | 499 final move = _moves.removeFirst(); |
500 _delegate.onDecelerate(move.x, move.y); | 500 _delegate.onDecelerate(move.x, move.y); |
501 if (!_moves.isEmpty()) { | 501 if (!_moves.isEmpty) { |
502 num nextTime = _moves.first().time; | 502 num nextTime = _moves.first().time; |
503 assert(_stepTimeout === null); | 503 assert(_stepTimeout === null); |
504 _stepTimeout = Env.requestAnimationFrame(_step, null, nextTime); | 504 _stepTimeout = Env.requestAnimationFrame(_step, null, nextTime); |
505 } else { | 505 } else { |
506 stop(); | 506 stop(); |
507 } | 507 } |
508 } | 508 } |
509 } | 509 } |
510 | 510 |
511 void abort() { | 511 void abort() { |
512 _decelerating = false; | 512 _decelerating = false; |
513 _moves.clear(); | 513 _moves.clear(); |
514 if (_stepTimeout !== null) { | 514 if (_stepTimeout !== null) { |
515 Env.cancelRequestAnimationFrame(_stepTimeout); | 515 Env.cancelRequestAnimationFrame(_stepTimeout); |
516 _stepTimeout = null; | 516 _stepTimeout = null; |
517 } | 517 } |
518 } | 518 } |
519 | 519 |
520 Coordinate stop() { | 520 Coordinate stop() { |
521 final wasDecelerating = _decelerating; | 521 final wasDecelerating = _decelerating; |
522 _decelerating = false; | 522 _decelerating = false; |
523 Coordinate velocity; | 523 Coordinate velocity; |
524 if (!_moves.isEmpty()) { | 524 if (!_moves.isEmpty) { |
525 final move = _moves.first(); | 525 final move = _moves.first(); |
526 // This is a workaround for the ugly hacks that get applied when a user | 526 // This is a workaround for the ugly hacks that get applied when a user |
527 // passed a velocity in to this Momentum implementation. | 527 // passed a velocity in to this Momentum implementation. |
528 num velocityScale = SingleDimensionPhysics._MS_PER_FRAME * | 528 num velocityScale = SingleDimensionPhysics._MS_PER_FRAME * |
529 SingleDimensionPhysics._INITIAL_VELOCITY_BOOST_FACTOR; | 529 SingleDimensionPhysics._INITIAL_VELOCITY_BOOST_FACTOR; |
530 velocity = new Coordinate( | 530 velocity = new Coordinate( |
531 move.vx / velocityScale, move.vy / velocityScale); | 531 move.vx / velocityScale, move.vy / velocityScale); |
532 } else { | 532 } else { |
533 velocity = new Coordinate(0, 0); | 533 velocity = new Coordinate(0, 0); |
534 } | 534 } |
535 _moves.clear(); | 535 _moves.clear(); |
536 if (_stepTimeout !== null) { | 536 if (_stepTimeout !== null) { |
537 Env.cancelRequestAnimationFrame(_stepTimeout); | 537 Env.cancelRequestAnimationFrame(_stepTimeout); |
538 _stepTimeout = null; | 538 _stepTimeout = null; |
539 } | 539 } |
540 if (wasDecelerating) { | 540 if (wasDecelerating) { |
541 _delegate.onDecelerationEnd(); | 541 _delegate.onDecelerationEnd(); |
542 } | 542 } |
543 return velocity; | 543 return velocity; |
544 } | 544 } |
545 | 545 |
546 Coordinate get destination { | 546 Coordinate get destination { |
547 if (!_moves.isEmpty()) { | 547 if (!_moves.isEmpty) { |
548 final lastMove = _moves.last(); | 548 final lastMove = _moves.last(); |
549 return new Coordinate(lastMove.x, lastMove.y); | 549 return new Coordinate(lastMove.x, lastMove.y); |
550 } else { | 550 } else { |
551 return null; | 551 return null; |
552 } | 552 } |
553 } | 553 } |
554 } | 554 } |
OLD | NEW |