| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "cc/animation/scroll_offset_animation_curve.h" | 5 #include "cc/animation/scroll_offset_animation_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" |
| 11 #include "cc/animation/timing_function.h" | 11 #include "cc/animation/timing_function.h" |
| 12 #include "cc/base/time_util.h" |
| 12 #include "ui/gfx/animation/tween.h" | 13 #include "ui/gfx/animation/tween.h" |
| 13 | 14 |
| 14 const double kDurationDivisor = 60.0; | 15 const double kDurationDivisor = 60.0; |
| 15 | 16 |
| 16 namespace cc { | 17 namespace cc { |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 static float MaximumDimension(gfx::Vector2dF delta) { | 21 static float MaximumDimension(gfx::Vector2dF delta) { |
| 21 return std::max(std::abs(delta.x()), std::abs(delta.y())); | 22 return std::max(std::abs(delta.x()), std::abs(delta.y())); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 | 59 |
| 59 ScrollOffsetAnimationCurve::~ScrollOffsetAnimationCurve() {} | 60 ScrollOffsetAnimationCurve::~ScrollOffsetAnimationCurve() {} |
| 60 | 61 |
| 61 void ScrollOffsetAnimationCurve::SetInitialValue( | 62 void ScrollOffsetAnimationCurve::SetInitialValue( |
| 62 const gfx::ScrollOffset& initial_value) { | 63 const gfx::ScrollOffset& initial_value) { |
| 63 initial_value_ = initial_value; | 64 initial_value_ = initial_value; |
| 64 total_animation_duration_ = DurationFromDelta( | 65 total_animation_duration_ = DurationFromDelta( |
| 65 target_value_.DeltaFrom(initial_value_)); | 66 target_value_.DeltaFrom(initial_value_)); |
| 66 } | 67 } |
| 67 | 68 |
| 68 gfx::ScrollOffset ScrollOffsetAnimationCurve::GetValue(double t) const { | 69 gfx::ScrollOffset ScrollOffsetAnimationCurve::GetValue( |
| 69 double duration = (total_animation_duration_ - last_retarget_).InSecondsF(); | 70 base::TimeDelta t) const { |
| 70 t -= last_retarget_.InSecondsF(); | 71 base::TimeDelta duration = total_animation_duration_ - last_retarget_; |
| 72 t -= last_retarget_; |
| 71 | 73 |
| 72 if (t <= 0) | 74 if (t <= base::TimeDelta()) |
| 73 return initial_value_; | 75 return initial_value_; |
| 74 | 76 |
| 75 if (t >= duration) | 77 if (t >= duration) |
| 76 return target_value_; | 78 return target_value_; |
| 77 | 79 |
| 78 double progress = (timing_function_->GetValue(t / duration)); | 80 double progress = timing_function_->GetValue(TimeUtil::Divide(t, duration)); |
| 79 return gfx::ScrollOffset( | 81 return gfx::ScrollOffset( |
| 80 gfx::Tween::FloatValueBetween( | 82 gfx::Tween::FloatValueBetween( |
| 81 progress, initial_value_.x(), target_value_.x()), | 83 progress, initial_value_.x(), target_value_.x()), |
| 82 gfx::Tween::FloatValueBetween( | 84 gfx::Tween::FloatValueBetween( |
| 83 progress, initial_value_.y(), target_value_.y())); | 85 progress, initial_value_.y(), target_value_.y())); |
| 84 } | 86 } |
| 85 | 87 |
| 86 base::TimeDelta ScrollOffsetAnimationCurve::Duration() const { | 88 base::TimeDelta ScrollOffsetAnimationCurve::Duration() const { |
| 87 return total_animation_duration_; | 89 return total_animation_duration_; |
| 88 } | 90 } |
| 89 | 91 |
| 90 AnimationCurve::CurveType ScrollOffsetAnimationCurve::Type() const { | 92 AnimationCurve::CurveType ScrollOffsetAnimationCurve::Type() const { |
| 91 return ScrollOffset; | 93 return ScrollOffset; |
| 92 } | 94 } |
| 93 | 95 |
| 94 scoped_ptr<AnimationCurve> ScrollOffsetAnimationCurve::Clone() const { | 96 scoped_ptr<AnimationCurve> ScrollOffsetAnimationCurve::Clone() const { |
| 95 scoped_ptr<TimingFunction> timing_function( | 97 scoped_ptr<TimingFunction> timing_function( |
| 96 static_cast<TimingFunction*>(timing_function_->Clone().release())); | 98 static_cast<TimingFunction*>(timing_function_->Clone().release())); |
| 97 scoped_ptr<ScrollOffsetAnimationCurve> curve_clone = | 99 scoped_ptr<ScrollOffsetAnimationCurve> curve_clone = |
| 98 Create(target_value_, timing_function.Pass()); | 100 Create(target_value_, timing_function.Pass()); |
| 99 curve_clone->initial_value_ = initial_value_; | 101 curve_clone->initial_value_ = initial_value_; |
| 100 curve_clone->total_animation_duration_ = total_animation_duration_; | 102 curve_clone->total_animation_duration_ = total_animation_duration_; |
| 101 curve_clone->last_retarget_ = last_retarget_; | 103 curve_clone->last_retarget_ = last_retarget_; |
| 102 return curve_clone.Pass(); | 104 return curve_clone.Pass(); |
| 103 } | 105 } |
| 104 | 106 |
| 105 void ScrollOffsetAnimationCurve::UpdateTarget( | 107 void ScrollOffsetAnimationCurve::UpdateTarget( |
| 106 double t, | 108 double t, |
| 107 const gfx::ScrollOffset& new_target) { | 109 const gfx::ScrollOffset& new_target) { |
| 108 gfx::ScrollOffset current_position = GetValue(t); | 110 gfx::ScrollOffset current_position = |
| 111 GetValue(base::TimeDelta::FromSecondsD(t)); |
| 109 gfx::Vector2dF old_delta = target_value_.DeltaFrom(initial_value_); | 112 gfx::Vector2dF old_delta = target_value_.DeltaFrom(initial_value_); |
| 110 gfx::Vector2dF new_delta = new_target.DeltaFrom(current_position); | 113 gfx::Vector2dF new_delta = new_target.DeltaFrom(current_position); |
| 111 | 114 |
| 112 double old_duration = | 115 double old_duration = |
| 113 (total_animation_duration_ - last_retarget_).InSecondsF(); | 116 (total_animation_duration_ - last_retarget_).InSecondsF(); |
| 114 double new_duration = DurationFromDelta(new_delta).InSecondsF(); | 117 double new_duration = DurationFromDelta(new_delta).InSecondsF(); |
| 115 | 118 |
| 116 double old_velocity = timing_function_->Velocity( | 119 double old_velocity = timing_function_->Velocity( |
| 117 (t - last_retarget_.InSecondsF()) / old_duration); | 120 (t - last_retarget_.InSecondsF()) / old_duration); |
| 118 | 121 |
| 119 // TimingFunction::Velocity gives the slope of the curve from 0 to 1. | 122 // TimingFunction::Velocity gives the slope of the curve from 0 to 1. |
| 120 // To match the "true" velocity in px/sec we must adjust this slope for | 123 // To match the "true" velocity in px/sec we must adjust this slope for |
| 121 // differences in duration and scroll delta between old and new curves. | 124 // differences in duration and scroll delta between old and new curves. |
| 122 double new_velocity = | 125 double new_velocity = |
| 123 old_velocity * (new_duration / old_duration) * | 126 old_velocity * (new_duration / old_duration) * |
| 124 (MaximumDimension(old_delta) / MaximumDimension(new_delta)); | 127 (MaximumDimension(old_delta) / MaximumDimension(new_delta)); |
| 125 | 128 |
| 126 initial_value_ = current_position; | 129 initial_value_ = current_position; |
| 127 target_value_ = new_target; | 130 target_value_ = new_target; |
| 128 total_animation_duration_ = base::TimeDelta::FromSecondsD(t + new_duration); | 131 total_animation_duration_ = base::TimeDelta::FromSecondsD(t + new_duration); |
| 129 last_retarget_ = base::TimeDelta::FromSecondsD(t); | 132 last_retarget_ = base::TimeDelta::FromSecondsD(t); |
| 130 timing_function_ = EaseOutWithInitialVelocity(new_velocity); | 133 timing_function_ = EaseOutWithInitialVelocity(new_velocity); |
| 131 } | 134 } |
| 132 | 135 |
| 133 } // namespace cc | 136 } // namespace cc |
| OLD | NEW |