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 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 * constant used when guessing at the velocity required to throw to a specific | 233 * constant used when guessing at the velocity required to throw to a specific |
234 * location. Chosen arbitrarily. All that really matters is that the velocity | 234 * location. Chosen arbitrarily. All that really matters is that the velocity |
235 * is large enough that a throw gesture will occur. | 235 * is large enough that a throw gesture will occur. |
236 */ | 236 */ |
237 static final _VELOCITY_GUESS = 20; | 237 static final _VELOCITY_GUESS = 20; |
238 | 238 |
239 SingleDimensionPhysics() : _bouncingState = BouncingState.NOT_BOUNCING { | 239 SingleDimensionPhysics() : _bouncingState = BouncingState.NOT_BOUNCING { |
240 } | 240 } |
241 | 241 |
242 void configure(num minCoord, num maxCoord, | 242 void configure(num minCoord, num maxCoord, |
243 num initialOffset, num customDecelerationFactor, | 243 num initialOffset, num customDecelerationFactor_, |
244 num velocity) { | 244 num velocity_) { |
245 _bouncingState = BouncingState.NOT_BOUNCING; | 245 _bouncingState = BouncingState.NOT_BOUNCING; |
246 _minCoord = minCoord; | 246 _minCoord = minCoord; |
247 _maxCoord = maxCoord; | 247 _maxCoord = maxCoord; |
248 _currentOffset = initialOffset; | 248 _currentOffset = initialOffset; |
249 this.customDecelerationFactor = customDecelerationFactor; | 249 this.customDecelerationFactor = customDecelerationFactor_; |
250 _adjustInitialVelocityAndBouncingState(velocity); | 250 _adjustInitialVelocityAndBouncingState(velocity_); |
251 } | 251 } |
252 | 252 |
253 num solve(num initialOffset, num targetOffset, | 253 num solve(num initialOffset, num targetOffset, |
254 num customDecelerationFactor) { | 254 num customDecelerationFactor_) { |
255 initialOffset = initialOffset.round(); | 255 initialOffset = initialOffset.round(); |
256 targetOffset = targetOffset.round(); | 256 targetOffset = targetOffset.round(); |
257 if (initialOffset == targetOffset) { | 257 if (initialOffset == targetOffset) { |
258 return 0; | 258 return 0; |
259 } | 259 } |
260 return Solver.solve((num velocity) { | 260 return Solver.solve((num velocity_) { |
261 // Don't specify min and max coordinates as we don't need to bother | 261 // Don't specify min and max coordinates as we don't need to bother |
262 // with the simulating bouncing off the edges. | 262 // with the simulating bouncing off the edges. |
263 configure(null, null, initialOffset.round(), | 263 configure(null, null, initialOffset.round(), |
264 customDecelerationFactor, velocity); | 264 customDecelerationFactor_, velocity_); |
265 stepAll(); | 265 stepAll(); |
266 return _currentOffset; | 266 return _currentOffset; |
267 }, | 267 }, |
268 targetOffset, | 268 targetOffset, |
269 targetOffset > initialOffset ? _VELOCITY_GUESS : -_VELOCITY_GUESS); | 269 targetOffset > initialOffset ? _VELOCITY_GUESS : -_VELOCITY_GUESS); |
270 } | 270 } |
271 | 271 |
272 /** | 272 /** |
273 * Helper method to calculate initial velocity. | 273 * Helper method to calculate initial velocity. |
274 * The [velocity] passed here should be in terms of number of | 274 * The [velocity] passed here should be in terms of number of |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 return _nextY != _previousOffset.y || _nextX != _previousOffset.x; | 419 return _nextY != _previousOffset.y || _nextX != _previousOffset.x; |
420 } | 420 } |
421 | 421 |
422 /** | 422 /** |
423 * The [TouchHandler] requires this function but we don't need to do | 423 * The [TouchHandler] requires this function but we don't need to do |
424 * anything here. | 424 * anything here. |
425 */ | 425 */ |
426 void onTransitionEnd() { | 426 void onTransitionEnd() { |
427 } | 427 } |
428 | 428 |
429 Coordinate calculateVelocity(Coordinate start, Coordinate target, | 429 Coordinate calculateVelocity(Coordinate start_, Coordinate target, |
430 [num decelerationFactor = null]) { | 430 [num decelerationFactor = null]) { |
431 return new Coordinate( | 431 return new Coordinate( |
432 physicsX.solve(start.x, target.x, decelerationFactor), | 432 physicsX.solve(start_.x, target.x, decelerationFactor), |
433 physicsY.solve(start.y, target.y, decelerationFactor)); | 433 physicsY.solve(start_.y, target.y, decelerationFactor)); |
434 } | 434 } |
435 | 435 |
436 bool start(Coordinate velocity, Coordinate minCoord, Coordinate maxCoord, | 436 bool start(Coordinate velocity, Coordinate minCoord, Coordinate maxCoord, |
437 Coordinate initialOffset, [num decelerationFactor = null]) { | 437 Coordinate initialOffset, [num decelerationFactor = null]) { |
438 _customDecelerationFactor = _defaultDecelerationFactor; | 438 _customDecelerationFactor = _defaultDecelerationFactor; |
439 if (decelerationFactor !== null) { | 439 if (decelerationFactor !== null) { |
440 _customDecelerationFactor = decelerationFactor; | 440 _customDecelerationFactor = decelerationFactor; |
441 } | 441 } |
442 | 442 |
443 if (_stepTimeout !== null) { | 443 if (_stepTimeout !== null) { |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 |