| OLD | NEW |
| (Empty) |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "cc/keyframed_animation_curve.h" | |
| 6 | |
| 7 namespace cc { | |
| 8 | |
| 9 namespace { | |
| 10 | |
| 11 template <class Keyframe> | |
| 12 void InsertKeyframe(scoped_ptr<Keyframe> keyframe, | |
| 13 ScopedPtrVector<Keyframe>& keyframes) { | |
| 14 // Usually, the keyframes will be added in order, so this loop would be | |
| 15 // unnecessary and we should skip it if possible. | |
| 16 if (!keyframes.empty() && keyframe->Time() < keyframes.back()->Time()) { | |
| 17 for (size_t i = 0; i < keyframes.size(); ++i) { | |
| 18 if (keyframe->Time() < keyframes[i]->Time()) { | |
| 19 keyframes.insert(keyframes.begin() + i, keyframe.Pass()); | |
| 20 return; | |
| 21 } | |
| 22 } | |
| 23 } | |
| 24 | |
| 25 keyframes.push_back(keyframe.Pass()); | |
| 26 } | |
| 27 | |
| 28 scoped_ptr<TimingFunction> CloneTimingFunction( | |
| 29 const TimingFunction* timing_function) { | |
| 30 DCHECK(timing_function); | |
| 31 scoped_ptr<AnimationCurve> curve(timing_function->Clone()); | |
| 32 return scoped_ptr<TimingFunction>( | |
| 33 static_cast<TimingFunction*>(curve.release())); | |
| 34 } | |
| 35 | |
| 36 } // namespace | |
| 37 | |
| 38 Keyframe::Keyframe(double time, scoped_ptr<TimingFunction> timing_function) | |
| 39 : time_(time), | |
| 40 timing_function_(timing_function.Pass()) {} | |
| 41 | |
| 42 Keyframe::~Keyframe() {} | |
| 43 | |
| 44 double Keyframe::Time() const { | |
| 45 return time_; | |
| 46 } | |
| 47 | |
| 48 scoped_ptr<FloatKeyframe> FloatKeyframe::Create( | |
| 49 double time, | |
| 50 float value, | |
| 51 scoped_ptr<TimingFunction> timing_function) { | |
| 52 return make_scoped_ptr( | |
| 53 new FloatKeyframe(time, value, timing_function.Pass())); | |
| 54 } | |
| 55 | |
| 56 FloatKeyframe::FloatKeyframe(double time, | |
| 57 float value, | |
| 58 scoped_ptr<TimingFunction> timing_function) | |
| 59 : Keyframe(time, timing_function.Pass()), | |
| 60 value_(value) {} | |
| 61 | |
| 62 FloatKeyframe::~FloatKeyframe() {} | |
| 63 | |
| 64 float FloatKeyframe::Value() const { | |
| 65 return value_; | |
| 66 } | |
| 67 | |
| 68 scoped_ptr<FloatKeyframe> FloatKeyframe::Clone() const { | |
| 69 scoped_ptr<TimingFunction> func; | |
| 70 if (timing_function()) | |
| 71 func = CloneTimingFunction(timing_function()); | |
| 72 return FloatKeyframe::Create(Time(), Value(), func.Pass()); | |
| 73 } | |
| 74 | |
| 75 scoped_ptr<TransformKeyframe> TransformKeyframe::Create( | |
| 76 double time, | |
| 77 const TransformOperations& value, | |
| 78 scoped_ptr<TimingFunction> timing_function) { | |
| 79 return make_scoped_ptr( | |
| 80 new TransformKeyframe(time, value, timing_function.Pass())); | |
| 81 } | |
| 82 | |
| 83 TransformKeyframe::TransformKeyframe(double time, | |
| 84 const TransformOperations& value, | |
| 85 scoped_ptr<TimingFunction> timing_function) | |
| 86 : Keyframe(time, timing_function.Pass()), | |
| 87 value_(value) {} | |
| 88 | |
| 89 TransformKeyframe::~TransformKeyframe() {} | |
| 90 | |
| 91 const TransformOperations& TransformKeyframe::Value() const { | |
| 92 return value_; | |
| 93 } | |
| 94 | |
| 95 scoped_ptr<TransformKeyframe> TransformKeyframe::Clone() const { | |
| 96 scoped_ptr<TimingFunction> func; | |
| 97 if (timing_function()) | |
| 98 func = CloneTimingFunction(timing_function()); | |
| 99 return TransformKeyframe::Create(Time(), Value(), func.Pass()); | |
| 100 } | |
| 101 | |
| 102 scoped_ptr<KeyframedFloatAnimationCurve> KeyframedFloatAnimationCurve:: | |
| 103 Create() { | |
| 104 return make_scoped_ptr(new KeyframedFloatAnimationCurve); | |
| 105 } | |
| 106 | |
| 107 KeyframedFloatAnimationCurve::KeyframedFloatAnimationCurve() {} | |
| 108 | |
| 109 KeyframedFloatAnimationCurve::~KeyframedFloatAnimationCurve() {} | |
| 110 | |
| 111 void KeyframedFloatAnimationCurve::AddKeyframe( | |
| 112 scoped_ptr<FloatKeyframe> keyframe) { | |
| 113 InsertKeyframe(keyframe.Pass(), keyframes_); | |
| 114 } | |
| 115 | |
| 116 double KeyframedFloatAnimationCurve::Duration() const { | |
| 117 return keyframes_.back()->Time() - keyframes_.front()->Time(); | |
| 118 } | |
| 119 | |
| 120 scoped_ptr<AnimationCurve> KeyframedFloatAnimationCurve::Clone() const { | |
| 121 scoped_ptr<KeyframedFloatAnimationCurve> to_return( | |
| 122 KeyframedFloatAnimationCurve::Create()); | |
| 123 for (size_t i = 0; i < keyframes_.size(); ++i) | |
| 124 to_return->AddKeyframe(keyframes_[i]->Clone()); | |
| 125 return to_return.PassAs<AnimationCurve>(); | |
| 126 } | |
| 127 | |
| 128 float KeyframedFloatAnimationCurve::GetValue(double t) const { | |
| 129 if (t <= keyframes_.front()->Time()) | |
| 130 return keyframes_.front()->Value(); | |
| 131 | |
| 132 if (t >= keyframes_.back()->Time()) | |
| 133 return keyframes_.back()->Value(); | |
| 134 | |
| 135 size_t i = 0; | |
| 136 for (; i < keyframes_.size() - 1; ++i) { | |
| 137 if (t < keyframes_[i+1]->Time()) | |
| 138 break; | |
| 139 } | |
| 140 | |
| 141 float progress = | |
| 142 static_cast<float>((t - keyframes_[i]->Time()) / | |
| 143 (keyframes_[i+1]->Time() - keyframes_[i]->Time())); | |
| 144 | |
| 145 if (keyframes_[i]->timing_function()) | |
| 146 progress = keyframes_[i]->timing_function()->GetValue(progress); | |
| 147 | |
| 148 return keyframes_[i]->Value() + | |
| 149 (keyframes_[i+1]->Value() - keyframes_[i]->Value()) * progress; | |
| 150 } | |
| 151 | |
| 152 scoped_ptr<KeyframedTransformAnimationCurve> KeyframedTransformAnimationCurve:: | |
| 153 Create() { | |
| 154 return make_scoped_ptr(new KeyframedTransformAnimationCurve); | |
| 155 } | |
| 156 | |
| 157 KeyframedTransformAnimationCurve::KeyframedTransformAnimationCurve() {} | |
| 158 | |
| 159 KeyframedTransformAnimationCurve::~KeyframedTransformAnimationCurve() {} | |
| 160 | |
| 161 void KeyframedTransformAnimationCurve::AddKeyframe( | |
| 162 scoped_ptr<TransformKeyframe> keyframe) { | |
| 163 InsertKeyframe(keyframe.Pass(), keyframes_); | |
| 164 } | |
| 165 | |
| 166 double KeyframedTransformAnimationCurve::Duration() const { | |
| 167 return keyframes_.back()->Time() - keyframes_.front()->Time(); | |
| 168 } | |
| 169 | |
| 170 scoped_ptr<AnimationCurve> KeyframedTransformAnimationCurve::Clone() const { | |
| 171 scoped_ptr<KeyframedTransformAnimationCurve> to_return( | |
| 172 KeyframedTransformAnimationCurve::Create()); | |
| 173 for (size_t i = 0; i < keyframes_.size(); ++i) | |
| 174 to_return->AddKeyframe(keyframes_[i]->Clone()); | |
| 175 return to_return.PassAs<AnimationCurve>(); | |
| 176 } | |
| 177 | |
| 178 gfx::Transform KeyframedTransformAnimationCurve::GetValue(double t) const { | |
| 179 if (t <= keyframes_.front()->Time()) | |
| 180 return keyframes_.front()->Value().Apply(); | |
| 181 | |
| 182 if (t >= keyframes_.back()->Time()) | |
| 183 return keyframes_.back()->Value().Apply(); | |
| 184 | |
| 185 size_t i = 0; | |
| 186 for (; i < keyframes_.size() - 1; ++i) { | |
| 187 if (t < keyframes_[i+1]->Time()) | |
| 188 break; | |
| 189 } | |
| 190 | |
| 191 double progress = (t - keyframes_[i]->Time()) / | |
| 192 (keyframes_[i+1]->Time() - keyframes_[i]->Time()); | |
| 193 | |
| 194 if (keyframes_[i]->timing_function()) | |
| 195 progress = keyframes_[i]->timing_function()->GetValue(progress); | |
| 196 | |
| 197 return keyframes_[i+1]->Value().Blend(keyframes_[i]->Value(), progress); | |
| 198 } | |
| 199 | |
| 200 } // namespace cc | |
| OLD | NEW |