| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "ui/events/gestures/fling_curve.h" | 5 #include "ui/events/gestures/fling_curve.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cmath> | 8 #include <cmath> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 inline double GetVelocityAtTime(double t) { | 23 inline double GetVelocityAtTime(double t) { |
| 24 return -kDefaultAlpha * kDefaultGamma * exp(-kDefaultGamma * t) - | 24 return -kDefaultAlpha * kDefaultGamma * exp(-kDefaultGamma * t) - |
| 25 kDefaultBeta; | 25 kDefaultBeta; |
| 26 } | 26 } |
| 27 | 27 |
| 28 inline double GetTimeAtVelocity(double v) { | 28 inline double GetTimeAtVelocity(double v) { |
| 29 return -log((v + kDefaultBeta) / (-kDefaultAlpha * kDefaultGamma)) / | 29 return -log((v + kDefaultBeta) / (-kDefaultAlpha * kDefaultGamma)) / |
| 30 kDefaultGamma; | 30 kDefaultGamma; |
| 31 } | 31 } |
| 32 | 32 |
| 33 inline double GetTimeAtPosition(double p) { |
| 34 // TODO(sunyunjia): Set a range for the time for faster interpolation. |
| 35 double tl = 0; |
| 36 double tr = GetTimeAtVelocity(0); |
| 37 p = GetPositionAtTime(tr) - p; |
| 38 double tm = (tl + tr) / 2; |
| 39 double pm = GetPositionAtTime(tm); |
| 40 while (fabs(pm - p) > 0.001) { |
| 41 if (pm < p) |
| 42 tl = tm; |
| 43 else |
| 44 tr = tm; |
| 45 tm = (tl + tr) / 2; |
| 46 pm = GetPositionAtTime(tm); |
| 47 } |
| 48 return tm; |
| 49 } |
| 50 |
| 33 } // namespace | 51 } // namespace |
| 34 | 52 |
| 35 namespace ui { | 53 namespace ui { |
| 36 | 54 |
| 37 FlingCurve::FlingCurve(const gfx::Vector2dF& velocity, | 55 FlingCurve::FlingCurve(const gfx::Vector2dF& velocity, |
| 38 base::TimeTicks start_timestamp) | 56 base::TimeTicks start_timestamp) |
| 39 : curve_duration_(GetTimeAtVelocity(0)), | 57 : curve_duration_(GetTimeAtVelocity(0)), |
| 40 start_timestamp_(start_timestamp), | 58 start_timestamp_(start_timestamp), |
| 41 previous_timestamp_(start_timestamp_), | 59 previous_timestamp_(start_timestamp_), |
| 42 time_offset_(0), | 60 time_offset_(0), |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 previous_timestamp_ = current; | 115 previous_timestamp_ = current; |
| 98 | 116 |
| 99 gfx::Vector2dF offset, velocity; | 117 gfx::Vector2dF offset, velocity; |
| 100 bool still_active = ComputeScrollOffset(current, &offset, &velocity); | 118 bool still_active = ComputeScrollOffset(current, &offset, &velocity); |
| 101 | 119 |
| 102 *delta = offset - cumulative_scroll_; | 120 *delta = offset - cumulative_scroll_; |
| 103 cumulative_scroll_ = offset; | 121 cumulative_scroll_ = offset; |
| 104 return still_active; | 122 return still_active; |
| 105 } | 123 } |
| 106 | 124 |
| 125 void FlingCurve::ComputeTotalScrollOffset(gfx::Vector2dF& offset) { |
| 126 float scalar_offset = GetPositionAtTime(curve_duration_) - position_offset_; |
| 127 offset = gfx::ScaleVector2d(displacement_ratio_, scalar_offset); |
| 128 } |
| 129 |
| 130 bool FlingCurve::ResetCurveBySnappedOffset(const gfx::Vector2dF& offset) { |
| 131 float max_offset = std::max(fabs(offset.x()), fabs(offset.y())); |
| 132 if (max_offset > GetPositionAtTime(0)) { |
| 133 return false; |
| 134 } |
| 135 displacement_ratio_ = |
| 136 gfx::Vector2dF(offset.x() / max_offset, offset.y() / max_offset); |
| 137 |
| 138 time_offset_ = GetTimeAtPosition(max_offset); |
| 139 position_offset_ = GetPositionAtTime(time_offset_); |
| 140 return true; |
| 141 } |
| 142 |
| 107 } // namespace ui | 143 } // namespace ui |
| OLD | NEW |