| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 import 'dart:math' as math; | 5 import 'dart:math' as math; |
| 6 import 'mechanics.dart'; | 6 import 'mechanics.dart'; |
| 7 import 'generators.dart'; | 7 import 'generators.dart'; |
| 8 | 8 |
| 9 const double _kSlope = 0.02; | 9 const double _kScrollFriction = 0.005; |
| 10 const double _kFriction = 0.004; | 10 const double _kOverscrollFriction = 0.075; |
| 11 const double _kBounceSlopeAngle = math.PI / 512.0; // radians |
| 11 | 12 |
| 12 abstract class ScrollBehavior { | 13 abstract class ScrollBehavior { |
| 13 Simulation release(Particle particle) => null; | 14 Simulation release(Particle particle) => null; |
| 14 | 15 |
| 15 // Returns the new scroll offset. | 16 // Returns the new scroll offset. |
| 16 double applyCurve(double scrollOffset, double scrollDelta); | 17 double applyCurve(double scrollOffset, double scrollDelta); |
| 17 } | 18 } |
| 18 | 19 |
| 19 class BoundedScrollBehavior extends ScrollBehavior { | 20 class BoundedScrollBehavior extends ScrollBehavior { |
| 20 double minOffset; | 21 double minOffset; |
| 21 double maxOffset; | 22 double maxOffset; |
| 22 | 23 |
| 23 BoundedScrollBehavior({this.minOffset: 0.0, this.maxOffset}); | 24 BoundedScrollBehavior({this.minOffset: 0.0, this.maxOffset}); |
| 24 | 25 |
| 25 double applyCurve(double scrollOffset, double scrollDelta) { | 26 double applyCurve(double scrollOffset, double scrollDelta) { |
| 26 double newScrollOffset = scrollOffset + scrollDelta; | 27 double newScrollOffset = scrollOffset + scrollDelta; |
| 27 if (minOffset != null) | 28 if (minOffset != null) |
| 28 newScrollOffset = math.max(minOffset, newScrollOffset); | 29 newScrollOffset = math.max(minOffset, newScrollOffset); |
| 29 if (maxOffset != null) | 30 if (maxOffset != null) |
| 30 newScrollOffset = math.min(maxOffset, newScrollOffset); | 31 newScrollOffset = math.min(maxOffset, newScrollOffset); |
| 31 return newScrollOffset; | 32 return newScrollOffset; |
| 32 } | 33 } |
| 33 } | 34 } |
| 34 | 35 |
| 35 class OverscrollBehavior extends ScrollBehavior { | 36 class OverscrollBehavior extends ScrollBehavior { |
| 36 Simulation release(Particle particle) { | 37 Simulation release(Particle particle) { |
| 38 System system; |
| 37 if (particle.position >= 0.0) { | 39 if (particle.position >= 0.0) { |
| 38 if (particle.velocity == 0.0) | 40 if (particle.velocity == 0.0) |
| 39 return null; | 41 return null; |
| 40 System system = new ParticleInBoxWithFriction( | 42 System slowdownSystem = new ParticleInBoxWithFriction( |
| 41 particle: particle, | 43 particle: particle, |
| 42 box: new Box(min: 0.0), | 44 friction: _kScrollFriction, |
| 43 friction: _kFriction); | 45 box: new GeofenceBox(min: 0.0, onEscape: () { |
| 44 return new Simulation(system, | 46 (system as Multisystem).transitionToSystem(new ParticleInBoxWithFricti
on( |
| 45 terminationCondition: () => particle.velocity == 0.0); | 47 particle: particle, |
| 48 friction: _kOverscrollFriction, |
| 49 box: new ClosedBox(), |
| 50 onStop: () => (system as Multisystem).transitionToSystem(getBounceBa
ckSystem(particle)) |
| 51 )); |
| 52 })); |
| 53 system = new Multisystem(particle: particle, system: slowdownSystem); |
| 54 } else { |
| 55 system = getBounceBackSystem(particle); |
| 46 } | 56 } |
| 47 System system = new ParticleClimbingRamp( | 57 return new Simulation(system, terminationCondition: () => particle.position
== 0.0); |
| 58 } |
| 59 |
| 60 System getBounceBackSystem(Particle particle) { |
| 61 return new ParticleClimbingRamp( |
| 48 particle: particle, | 62 particle: particle, |
| 49 box: new Box(max: 0.0), | 63 box: new ClosedBox(max: 0.0), |
| 50 slope: _kSlope, | 64 theta: _kBounceSlopeAngle, |
| 51 targetPosition: 0.0); | 65 targetPosition: 0.0); |
| 52 return new Simulation(system, | |
| 53 terminationCondition: () => particle.position == 0.0); | |
| 54 } | 66 } |
| 55 | 67 |
| 56 double applyCurve(double scrollOffset, double scrollDelta) { | 68 double applyCurve(double scrollOffset, double scrollDelta) { |
| 57 double newScrollOffset = scrollOffset + scrollDelta; | 69 double newScrollOffset = scrollOffset + scrollDelta; |
| 58 if (newScrollOffset < 0.0) { | 70 if (newScrollOffset < 0.0) { |
| 59 // If we're overscrolling, we want move the scroll offset 2x slower than | 71 // If we're overscrolling, we want move the scroll offset 2x slower than |
| 60 // we would otherwise. Therefore, we "rewind" the newScrollOffset by half | 72 // we would otherwise. Therefore, we "rewind" the newScrollOffset by half |
| 61 // the amount that we moved it above. Notice that we clap the "old" value | 73 // the amount that we moved it above. Notice that we clap the "old" value |
| 62 // to 0.0 so that we only reduce the portion of scrollDelta that's applied | 74 // to 0.0 so that we only reduce the portion of scrollDelta that's applied |
| 63 // beyond 0.0. | 75 // beyond 0.0. |
| 64 newScrollOffset -= (newScrollOffset - math.min(0.0, scrollOffset)) / 2.0; | 76 newScrollOffset -= (newScrollOffset - math.min(0.0, scrollOffset)) / 2.0; |
| 65 } | 77 } |
| 66 return newScrollOffset; | 78 return newScrollOffset; |
| 67 } | 79 } |
| 68 } | 80 } |
| OLD | NEW |