| 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 "config.h" | |
| 6 | |
| 7 #include "CCKeyframedAnimationCurve.h" | |
| 8 | |
| 9 using WebKit::WebTransformationMatrix; | |
| 10 | |
| 11 namespace cc { | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 template <class Keyframe> | |
| 16 void insertKeyframe(scoped_ptr<Keyframe> keyframe, ScopedPtrVector<Keyframe>& ke
yframes) | |
| 17 { | |
| 18 // Usually, the keyframes will be added in order, so this loop would be unne
cessary and | |
| 19 // we should skip it if possible. | |
| 20 if (!keyframes.isEmpty() && keyframe->time() < keyframes.last()->time()) { | |
| 21 for (size_t i = 0; i < keyframes.size(); ++i) { | |
| 22 if (keyframe->time() < keyframes[i]->time()) { | |
| 23 keyframes.insert(i, keyframe.Pass()); | |
| 24 return; | |
| 25 } | |
| 26 } | |
| 27 } | |
| 28 | |
| 29 keyframes.append(keyframe.Pass()); | |
| 30 } | |
| 31 | |
| 32 scoped_ptr<CCTimingFunction> cloneTimingFunction(const CCTimingFunction* timingF
unction) | |
| 33 { | |
| 34 ASSERT(timingFunction); | |
| 35 scoped_ptr<CCAnimationCurve> curve(timingFunction->clone()); | |
| 36 return scoped_ptr<CCTimingFunction>(static_cast<CCTimingFunction*>(curve.rel
ease())); | |
| 37 } | |
| 38 | |
| 39 } // namespace | |
| 40 | |
| 41 CCKeyframe::CCKeyframe(double time, scoped_ptr<CCTimingFunction> timingFunction) | |
| 42 : m_time(time) | |
| 43 , m_timingFunction(timingFunction.Pass()) | |
| 44 { | |
| 45 } | |
| 46 | |
| 47 CCKeyframe::~CCKeyframe() | |
| 48 { | |
| 49 } | |
| 50 | |
| 51 double CCKeyframe::time() const | |
| 52 { | |
| 53 return m_time; | |
| 54 } | |
| 55 | |
| 56 const CCTimingFunction* CCKeyframe::timingFunction() const | |
| 57 { | |
| 58 return m_timingFunction.get(); | |
| 59 } | |
| 60 | |
| 61 scoped_ptr<CCFloatKeyframe> CCFloatKeyframe::create(double time, float value, sc
oped_ptr<CCTimingFunction> timingFunction) | |
| 62 { | |
| 63 return make_scoped_ptr(new CCFloatKeyframe(time, value, timingFunction.Pass(
))); | |
| 64 } | |
| 65 | |
| 66 CCFloatKeyframe::CCFloatKeyframe(double time, float value, scoped_ptr<CCTimingFu
nction> timingFunction) | |
| 67 : CCKeyframe(time, timingFunction.Pass()) | |
| 68 , m_value(value) | |
| 69 { | |
| 70 } | |
| 71 | |
| 72 CCFloatKeyframe::~CCFloatKeyframe() | |
| 73 { | |
| 74 } | |
| 75 | |
| 76 float CCFloatKeyframe::value() const | |
| 77 { | |
| 78 return m_value; | |
| 79 } | |
| 80 | |
| 81 scoped_ptr<CCFloatKeyframe> CCFloatKeyframe::clone() const | |
| 82 { | |
| 83 scoped_ptr<CCTimingFunction> func; | |
| 84 if (timingFunction()) | |
| 85 func = cloneTimingFunction(timingFunction()); | |
| 86 return CCFloatKeyframe::create(time(), value(), func.Pass()); | |
| 87 } | |
| 88 | |
| 89 scoped_ptr<CCTransformKeyframe> CCTransformKeyframe::create(double time, const W
ebKit::WebTransformOperations& value, scoped_ptr<CCTimingFunction> timingFunctio
n) | |
| 90 { | |
| 91 return make_scoped_ptr(new CCTransformKeyframe(time, value, timingFunction.P
ass())); | |
| 92 } | |
| 93 | |
| 94 CCTransformKeyframe::CCTransformKeyframe(double time, const WebKit::WebTransform
Operations& value, scoped_ptr<CCTimingFunction> timingFunction) | |
| 95 : CCKeyframe(time, timingFunction.Pass()) | |
| 96 , m_value(value) | |
| 97 { | |
| 98 } | |
| 99 | |
| 100 CCTransformKeyframe::~CCTransformKeyframe() | |
| 101 { | |
| 102 } | |
| 103 | |
| 104 const WebKit::WebTransformOperations& CCTransformKeyframe::value() const | |
| 105 { | |
| 106 return m_value; | |
| 107 } | |
| 108 | |
| 109 scoped_ptr<CCTransformKeyframe> CCTransformKeyframe::clone() const | |
| 110 { | |
| 111 scoped_ptr<CCTimingFunction> func; | |
| 112 if (timingFunction()) | |
| 113 func = cloneTimingFunction(timingFunction()); | |
| 114 return CCTransformKeyframe::create(time(), value(), func.Pass()); | |
| 115 } | |
| 116 | |
| 117 scoped_ptr<CCKeyframedFloatAnimationCurve> CCKeyframedFloatAnimationCurve::creat
e() | |
| 118 { | |
| 119 return make_scoped_ptr(new CCKeyframedFloatAnimationCurve); | |
| 120 } | |
| 121 | |
| 122 CCKeyframedFloatAnimationCurve::CCKeyframedFloatAnimationCurve() | |
| 123 { | |
| 124 } | |
| 125 | |
| 126 CCKeyframedFloatAnimationCurve::~CCKeyframedFloatAnimationCurve() | |
| 127 { | |
| 128 } | |
| 129 | |
| 130 void CCKeyframedFloatAnimationCurve::addKeyframe(scoped_ptr<CCFloatKeyframe> key
frame) | |
| 131 { | |
| 132 insertKeyframe(keyframe.Pass(), m_keyframes); | |
| 133 } | |
| 134 | |
| 135 double CCKeyframedFloatAnimationCurve::duration() const | |
| 136 { | |
| 137 return m_keyframes.last()->time() - m_keyframes.first()->time(); | |
| 138 } | |
| 139 | |
| 140 scoped_ptr<CCAnimationCurve> CCKeyframedFloatAnimationCurve::clone() const | |
| 141 { | |
| 142 scoped_ptr<CCKeyframedFloatAnimationCurve> toReturn(CCKeyframedFloatAnimatio
nCurve::create()); | |
| 143 for (size_t i = 0; i < m_keyframes.size(); ++i) | |
| 144 toReturn->addKeyframe(m_keyframes[i]->clone()); | |
| 145 return toReturn.PassAs<CCAnimationCurve>(); | |
| 146 } | |
| 147 | |
| 148 float CCKeyframedFloatAnimationCurve::getValue(double t) const | |
| 149 { | |
| 150 if (t <= m_keyframes.first()->time()) | |
| 151 return m_keyframes.first()->value(); | |
| 152 | |
| 153 if (t >= m_keyframes.last()->time()) | |
| 154 return m_keyframes.last()->value(); | |
| 155 | |
| 156 size_t i = 0; | |
| 157 for (; i < m_keyframes.size() - 1; ++i) { | |
| 158 if (t < m_keyframes[i+1]->time()) | |
| 159 break; | |
| 160 } | |
| 161 | |
| 162 float progress = static_cast<float>((t - m_keyframes[i]->time()) / (m_keyfra
mes[i+1]->time() - m_keyframes[i]->time())); | |
| 163 | |
| 164 if (m_keyframes[i]->timingFunction()) | |
| 165 progress = m_keyframes[i]->timingFunction()->getValue(progress); | |
| 166 | |
| 167 return m_keyframes[i]->value() + (m_keyframes[i+1]->value() - m_keyframes[i]
->value()) * progress; | |
| 168 } | |
| 169 | |
| 170 scoped_ptr<CCKeyframedTransformAnimationCurve> CCKeyframedTransformAnimationCurv
e::create() | |
| 171 { | |
| 172 return make_scoped_ptr(new CCKeyframedTransformAnimationCurve); | |
| 173 } | |
| 174 | |
| 175 CCKeyframedTransformAnimationCurve::CCKeyframedTransformAnimationCurve() | |
| 176 { | |
| 177 } | |
| 178 | |
| 179 CCKeyframedTransformAnimationCurve::~CCKeyframedTransformAnimationCurve() | |
| 180 { | |
| 181 } | |
| 182 | |
| 183 void CCKeyframedTransformAnimationCurve::addKeyframe(scoped_ptr<CCTransformKeyfr
ame> keyframe) | |
| 184 { | |
| 185 insertKeyframe(keyframe.Pass(), m_keyframes); | |
| 186 } | |
| 187 | |
| 188 double CCKeyframedTransformAnimationCurve::duration() const | |
| 189 { | |
| 190 return m_keyframes.last()->time() - m_keyframes.first()->time(); | |
| 191 } | |
| 192 | |
| 193 scoped_ptr<CCAnimationCurve> CCKeyframedTransformAnimationCurve::clone() const | |
| 194 { | |
| 195 scoped_ptr<CCKeyframedTransformAnimationCurve> toReturn(CCKeyframedTransform
AnimationCurve::create()); | |
| 196 for (size_t i = 0; i < m_keyframes.size(); ++i) | |
| 197 toReturn->addKeyframe(m_keyframes[i]->clone()); | |
| 198 return toReturn.PassAs<CCAnimationCurve>(); | |
| 199 } | |
| 200 | |
| 201 WebTransformationMatrix CCKeyframedTransformAnimationCurve::getValue(double t) c
onst | |
| 202 { | |
| 203 if (t <= m_keyframes.first()->time()) | |
| 204 return m_keyframes.first()->value().apply(); | |
| 205 | |
| 206 if (t >= m_keyframes.last()->time()) | |
| 207 return m_keyframes.last()->value().apply(); | |
| 208 | |
| 209 size_t i = 0; | |
| 210 for (; i < m_keyframes.size() - 1; ++i) { | |
| 211 if (t < m_keyframes[i+1]->time()) | |
| 212 break; | |
| 213 } | |
| 214 | |
| 215 double progress = (t - m_keyframes[i]->time()) / (m_keyframes[i+1]->time() -
m_keyframes[i]->time()); | |
| 216 | |
| 217 if (m_keyframes[i]->timingFunction()) | |
| 218 progress = m_keyframes[i]->timingFunction()->getValue(progress); | |
| 219 | |
| 220 return m_keyframes[i+1]->value().blend(m_keyframes[i]->value(), progress); | |
| 221 } | |
| 222 | |
| 223 } // namespace cc | |
| OLD | NEW |