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 'simulation.dart'; | 7 import 'generators.dart'; |
8 | 8 |
9 const double _kSlope = 0.01; | 9 const double _kSlope = 0.01; |
10 | 10 |
11 abstract class ScrollCurve { | 11 abstract class ScrollBehavior { |
12 Simulation release(Particle particle) => null; | 12 Simulation release(Particle particle) => null; |
13 | 13 |
14 // Returns the new scroll offset. | 14 // Returns the new scroll offset. |
15 double apply(double scrollOffset, double scrollDelta); | 15 double applyCurve(double scrollOffset, double scrollDelta); |
16 } | 16 } |
17 | 17 |
18 class BoundedScrollCurve extends ScrollCurve { | 18 class BoundedScrollBehavior extends ScrollBehavior { |
19 double minOffset; | 19 double minOffset; |
20 double maxOffset; | 20 double maxOffset; |
21 | 21 |
22 BoundedScrollCurve({this.minOffset: 0.0, this.maxOffset}); | 22 BoundedScrollBehavior({this.minOffset: 0.0, this.maxOffset}); |
23 | 23 |
24 double apply(double scrollOffset, double scrollDelta) { | 24 double applyCurve(double scrollOffset, double scrollDelta) { |
25 double newScrollOffset = scrollOffset + scrollDelta; | 25 double newScrollOffset = scrollOffset + scrollDelta; |
26 if (minOffset != null) | 26 if (minOffset != null) |
27 newScrollOffset = math.max(minOffset, newScrollOffset); | 27 newScrollOffset = math.max(minOffset, newScrollOffset); |
28 if (maxOffset != null) | 28 if (maxOffset != null) |
29 newScrollOffset = math.min(maxOffset, newScrollOffset); | 29 newScrollOffset = math.min(maxOffset, newScrollOffset); |
30 return newScrollOffset; | 30 return newScrollOffset; |
31 } | 31 } |
32 } | 32 } |
33 | 33 |
34 class OverscrollCurve extends ScrollCurve { | 34 class OverscrollBehavior extends ScrollBehavior { |
35 Simulation release(Particle particle) { | 35 Simulation release(Particle particle) { |
36 if (particle.position >= 0.0) | 36 if (particle.position >= 0.0) |
37 return null; | 37 return null; |
38 System system = new ParticleClimbingRamp( | 38 System system = new ParticleClimbingRamp( |
39 particle: particle, | 39 particle: particle, |
40 box: new Box(max: 0.0), | 40 box: new Box(max: 0.0), |
41 slope: _kSlope, | 41 slope: _kSlope, |
42 targetPosition: 0.0); | 42 targetPosition: 0.0); |
43 return new Simulation(system, | 43 return new Simulation(system, |
44 terminationCondition: () => particle.position == 0.0); | 44 terminationCondition: () => particle.position == 0.0); |
45 } | 45 } |
46 | 46 |
47 double apply(double scrollOffset, double scrollDelta) { | 47 double applyCurve(double scrollOffset, double scrollDelta) { |
48 double newScrollOffset = scrollOffset + scrollDelta; | 48 double newScrollOffset = scrollOffset + scrollDelta; |
49 if (newScrollOffset < 0.0) { | 49 if (newScrollOffset < 0.0) { |
50 // If we're overscrolling, we want move the scroll offset 2x slower than | 50 // If we're overscrolling, we want move the scroll offset 2x slower than |
51 // we would otherwise. Therefore, we "rewind" the newScrollOffset by half | 51 // we would otherwise. Therefore, we "rewind" the newScrollOffset by half |
52 // the amount that we moved it above. Notice that we clap the "old" value | 52 // the amount that we moved it above. Notice that we clap the "old" value |
53 // to 0.0 so that we only reduce the portion of scrollDelta that's applied | 53 // to 0.0 so that we only reduce the portion of scrollDelta that's applied |
54 // beyond 0.0. | 54 // beyond 0.0. |
55 newScrollOffset -= (newScrollOffset - math.min(0.0, scrollOffset)) / 2.0; | 55 newScrollOffset -= (newScrollOffset - math.min(0.0, scrollOffset)) / 2.0; |
56 } | 56 } |
57 return newScrollOffset; | 57 return newScrollOffset; |
58 } | 58 } |
59 } | 59 } |
OLD | NEW |