| 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 | 6 |
| 7 import 'mechanics.dart'; | 7 import 'mechanics.dart'; |
| 8 import 'generators.dart'; | 8 import 'generators.dart'; |
| 9 | 9 |
| 10 const double _kScrollFriction = 0.005; | 10 const double _kScrollFriction = 0.005; |
| 11 const double _kOverscrollFriction = 0.075; | 11 const double _kOverscrollFriction = 0.075; |
| 12 const double _kBounceSlopeAngle = math.PI / 512.0; // radians | 12 const double _kBounceSlopeAngle = math.PI / 512.0; // radians |
| 13 | 13 |
| 14 abstract class ScrollBehavior { | 14 abstract class ScrollBehavior { |
| 15 Simulation release(Particle particle) => null; | 15 Simulation release(Particle particle) => null; |
| 16 | 16 |
| 17 // Returns the new scroll offset. | 17 // Returns the new scroll offset. |
| 18 double applyCurve(double scrollOffset, double scrollDelta); | 18 double applyCurve(double scrollOffset, double scrollDelta); |
| 19 } | 19 } |
| 20 | 20 |
| 21 class BoundedScrollBehavior extends ScrollBehavior { | 21 class BoundedScrollBehavior extends ScrollBehavior { |
| 22 double minOffset; | 22 double minOffset; |
| 23 double maxOffset; | 23 double maxOffset; |
| 24 | 24 |
| 25 BoundedScrollBehavior({this.minOffset: 0.0, this.maxOffset}); | 25 BoundedScrollBehavior({this.minOffset: 0.0, this.maxOffset}); |
| 26 | 26 |
| 27 @override |
| 27 double applyCurve(double scrollOffset, double scrollDelta) { | 28 double applyCurve(double scrollOffset, double scrollDelta) { |
| 28 double newScrollOffset = scrollOffset + scrollDelta; | 29 double newScrollOffset = scrollOffset + scrollDelta; |
| 29 if (minOffset != null) | 30 if (minOffset != null) |
| 30 newScrollOffset = math.max(minOffset, newScrollOffset); | 31 newScrollOffset = math.max(minOffset, newScrollOffset); |
| 31 if (maxOffset != null) | 32 if (maxOffset != null) |
| 32 newScrollOffset = math.min(maxOffset, newScrollOffset); | 33 newScrollOffset = math.min(maxOffset, newScrollOffset); |
| 33 return newScrollOffset; | 34 return newScrollOffset; |
| 34 } | 35 } |
| 35 } | 36 } |
| 36 | 37 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 53 // TODO(ianh) now what? what if we have a simulation ongoing? | 54 // TODO(ianh) now what? what if we have a simulation ongoing? |
| 54 } | 55 } |
| 55 } | 56 } |
| 56 | 57 |
| 57 OverscrollBehavior({double contentsHeight: 0.0, double containerHeight: 0.0}) | 58 OverscrollBehavior({double contentsHeight: 0.0, double containerHeight: 0.0}) |
| 58 : _contentsHeight = contentsHeight, | 59 : _contentsHeight = contentsHeight, |
| 59 _containerHeight = containerHeight; | 60 _containerHeight = containerHeight; |
| 60 | 61 |
| 61 double get maxScroll => math.max(0.0, _contentsHeight - _containerHeight); | 62 double get maxScroll => math.max(0.0, _contentsHeight - _containerHeight); |
| 62 | 63 |
| 64 @override |
| 63 Simulation release(Particle particle) { | 65 Simulation release(Particle particle) { |
| 64 System system; | 66 System system; |
| 65 if ((particle.position >= 0.0) && (particle.position < maxScroll)) { | 67 if ((particle.position >= 0.0) && (particle.position < maxScroll)) { |
| 66 if (particle.velocity == 0.0) | 68 if (particle.velocity == 0.0) |
| 67 return null; | 69 return null; |
| 68 System slowdownSystem = new ParticleInBoxWithFriction( | 70 System slowdownSystem = new ParticleInBoxWithFriction( |
| 69 particle: particle, | 71 particle: particle, |
| 70 friction: _kScrollFriction, | 72 friction: _kScrollFriction, |
| 71 box: new GeofenceBox(min: 0.0, max: maxScroll, onEscape: () { | 73 box: new GeofenceBox(min: 0.0, max: maxScroll, onEscape: () { |
| 72 (system as Multisystem).transitionToSystem(new ParticleInBoxWithFricti
on( | 74 (system as Multisystem).transitionToSystem(new ParticleInBoxWithFricti
on( |
| (...skipping 17 matching lines...) Expand all Loading... |
| 90 box: new ClosedBox(max: 0.0), | 92 box: new ClosedBox(max: 0.0), |
| 91 theta: _kBounceSlopeAngle, | 93 theta: _kBounceSlopeAngle, |
| 92 targetPosition: 0.0); | 94 targetPosition: 0.0); |
| 93 return new ParticleClimbingRamp( | 95 return new ParticleClimbingRamp( |
| 94 particle: particle, | 96 particle: particle, |
| 95 box: new ClosedBox(min: maxScroll), | 97 box: new ClosedBox(min: maxScroll), |
| 96 theta: _kBounceSlopeAngle, | 98 theta: _kBounceSlopeAngle, |
| 97 targetPosition: maxScroll); | 99 targetPosition: maxScroll); |
| 98 } | 100 } |
| 99 | 101 |
| 102 @override |
| 100 double applyCurve(double scrollOffset, double scrollDelta) { | 103 double applyCurve(double scrollOffset, double scrollDelta) { |
| 101 double newScrollOffset = scrollOffset + scrollDelta; | 104 double newScrollOffset = scrollOffset + scrollDelta; |
| 102 // If we're overscrolling, we want move the scroll offset 2x | 105 // If we're overscrolling, we want move the scroll offset 2x |
| 103 // slower than we would otherwise. Therefore, we "rewind" the | 106 // slower than we would otherwise. Therefore, we "rewind" the |
| 104 // newScrollOffset by half the amount that we moved it above. | 107 // newScrollOffset by half the amount that we moved it above. |
| 105 // Notice that we clap the "old" value to 0.0 so that we only | 108 // Notice that we clap the "old" value to 0.0 so that we only |
| 106 // reduce the portion of scrollDelta that's applied beyond 0.0. We | 109 // reduce the portion of scrollDelta that's applied beyond 0.0. We |
| 107 // do similar things for overscroll in the other direction. | 110 // do similar things for overscroll in the other direction. |
| 108 if (newScrollOffset < 0.0) { | 111 if (newScrollOffset < 0.0) { |
| 109 newScrollOffset -= (newScrollOffset - math.min(0.0, scrollOffset)) / 2.0; | 112 newScrollOffset -= (newScrollOffset - math.min(0.0, scrollOffset)) / 2.0; |
| 110 } else if (newScrollOffset > maxScroll) { | 113 } else if (newScrollOffset > maxScroll) { |
| 111 newScrollOffset -= (newScrollOffset - math.max(maxScroll, scrollOffset)) /
2.0; | 114 newScrollOffset -= (newScrollOffset - math.max(maxScroll, scrollOffset)) /
2.0; |
| 112 } | 115 } |
| 113 return newScrollOffset; | 116 return newScrollOffset; |
| 114 } | 117 } |
| 115 } | 118 } |
| OLD | NEW |