Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(197)

Side by Side Diff: cc/keyframed_animation_curve.cc

Issue 12517003: cc: Chromify the Animation and LayerAnimationController classes (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « cc/keyframed_animation_curve.h ('k') | cc/keyframed_animation_curve_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 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 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/keyframed_animation_curve.h" 5 #include "cc/keyframed_animation_curve.h"
6 6
7 namespace cc { 7 namespace cc {
8 8
9 namespace { 9 namespace {
10 10
11 template <class Keyframe> 11 template <class Keyframe>
12 void insertKeyframe(scoped_ptr<Keyframe> keyframe, ScopedPtrVector<Keyframe>& ke yframes) 12 void InsertKeyframe(scoped_ptr<Keyframe> keyframe,
13 { 13 ScopedPtrVector<Keyframe>& keyframes) {
14 // Usually, the keyframes will be added in order, so this loop would be unne cessary and 14 // Usually, the keyframes will be added in order, so this loop would be
15 // we should skip it if possible. 15 // unnecessary and we should skip it if possible.
16 if (!keyframes.empty() && keyframe->time() < keyframes.back()->time()) { 16 if (!keyframes.empty() && keyframe->Time() < keyframes.back()->Time()) {
17 for (size_t i = 0; i < keyframes.size(); ++i) { 17 for (size_t i = 0; i < keyframes.size(); ++i) {
18 if (keyframe->time() < keyframes[i]->time()) { 18 if (keyframe->Time() < keyframes[i]->Time()) {
19 keyframes.insert(keyframes.begin() + i, keyframe.Pass()); 19 keyframes.insert(keyframes.begin() + i, keyframe.Pass());
20 return; 20 return;
21 } 21 }
22 }
23 } 22 }
23 }
24 24
25 keyframes.push_back(keyframe.Pass()); 25 keyframes.push_back(keyframe.Pass());
26 } 26 }
27 27
28 scoped_ptr<TimingFunction> cloneTimingFunction(const TimingFunction* timingFunct ion) 28 scoped_ptr<TimingFunction> CloneTimingFunction(
29 { 29 const TimingFunction* timing_function) {
30 DCHECK(timingFunction); 30 DCHECK(timing_function);
31 scoped_ptr<AnimationCurve> curve(timingFunction->clone()); 31 scoped_ptr<AnimationCurve> curve(timing_function->Clone());
32 return scoped_ptr<TimingFunction>(static_cast<TimingFunction*>(curve.release ())); 32 return scoped_ptr<TimingFunction>(
33 static_cast<TimingFunction*>(curve.release()));
33 } 34 }
34 35
35 } // namespace 36 } // namespace
36 37
37 Keyframe::Keyframe(double time, scoped_ptr<TimingFunction> timingFunction) 38 Keyframe::Keyframe(double time, scoped_ptr<TimingFunction> timing_function)
38 : m_time(time) 39 : time_(time),
39 , m_timingFunction(timingFunction.Pass()) 40 timing_function_(timing_function.Pass()) {}
40 { 41
42 Keyframe::~Keyframe() {}
43
44 double Keyframe::Time() const {
45 return time_;
41 } 46 }
42 47
43 Keyframe::~Keyframe() 48 scoped_ptr<FloatKeyframe> FloatKeyframe::Create(
44 { 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()));
45 } 54 }
46 55
47 double Keyframe::time() const 56 FloatKeyframe::FloatKeyframe(double time,
48 { 57 float value,
49 return m_time; 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_;
50 } 66 }
51 67
52 const TimingFunction* Keyframe::timingFunction() const 68 scoped_ptr<FloatKeyframe> FloatKeyframe::Clone() const {
53 { 69 scoped_ptr<TimingFunction> func;
54 return m_timingFunction.get(); 70 if (timing_function())
71 func = CloneTimingFunction(timing_function());
72 return FloatKeyframe::Create(Time(), Value(), func.Pass());
55 } 73 }
56 74
57 scoped_ptr<FloatKeyframe> FloatKeyframe::create(double time, float value, scoped _ptr<TimingFunction> timingFunction) 75 scoped_ptr<TransformKeyframe> TransformKeyframe::Create(
58 { 76 double time,
59 return make_scoped_ptr(new FloatKeyframe(time, value, timingFunction.Pass()) ); 77 const TransformOperations& value,
78 scoped_ptr<TimingFunction> timing_function) {
79 return make_scoped_ptr(
80 new TransformKeyframe(time, value, timing_function.Pass()));
60 } 81 }
61 82
62 FloatKeyframe::FloatKeyframe(double time, float value, scoped_ptr<TimingFunction > timingFunction) 83 TransformKeyframe::TransformKeyframe(double time,
63 : Keyframe(time, timingFunction.Pass()) 84 const TransformOperations& value,
64 , m_value(value) 85 scoped_ptr<TimingFunction> timing_function)
65 { 86 : Keyframe(time, timing_function.Pass()),
87 value_(value) {}
88
89 TransformKeyframe::~TransformKeyframe() {}
90
91 const TransformOperations& TransformKeyframe::Value() const {
92 return value_;
66 } 93 }
67 94
68 FloatKeyframe::~FloatKeyframe() 95 scoped_ptr<TransformKeyframe> TransformKeyframe::Clone() const {
69 { 96 scoped_ptr<TimingFunction> func;
97 if (timing_function())
98 func = CloneTimingFunction(timing_function());
99 return TransformKeyframe::Create(Time(), Value(), func.Pass());
70 } 100 }
71 101
72 float FloatKeyframe::value() const 102 scoped_ptr<KeyframedFloatAnimationCurve> KeyframedFloatAnimationCurve::
73 { 103 Create() {
74 return m_value; 104 return make_scoped_ptr(new KeyframedFloatAnimationCurve);
75 } 105 }
76 106
77 scoped_ptr<FloatKeyframe> FloatKeyframe::clone() const 107 KeyframedFloatAnimationCurve::KeyframedFloatAnimationCurve() {}
78 { 108
79 scoped_ptr<TimingFunction> func; 109 KeyframedFloatAnimationCurve::~KeyframedFloatAnimationCurve() {}
80 if (timingFunction()) 110
81 func = cloneTimingFunction(timingFunction()); 111 void KeyframedFloatAnimationCurve::AddKeyframe(
82 return FloatKeyframe::create(time(), value(), func.Pass()); 112 scoped_ptr<FloatKeyframe> keyframe) {
113 InsertKeyframe(keyframe.Pass(), keyframes_);
83 } 114 }
84 115
85 scoped_ptr<TransformKeyframe> TransformKeyframe::create(double time, const Trans formOperations& value, scoped_ptr<TimingFunction> timingFunction) 116 double KeyframedFloatAnimationCurve::Duration() const {
86 { 117 return keyframes_.back()->Time() - keyframes_.front()->Time();
87 return make_scoped_ptr(new TransformKeyframe(time, value, timingFunction.Pas s()));
88 } 118 }
89 119
90 TransformKeyframe::TransformKeyframe(double time, const TransformOperations& val ue, scoped_ptr<TimingFunction> timingFunction) 120 scoped_ptr<AnimationCurve> KeyframedFloatAnimationCurve::Clone() const {
91 : Keyframe(time, timingFunction.Pass()) 121 scoped_ptr<KeyframedFloatAnimationCurve> to_return(
92 , m_value(value) 122 KeyframedFloatAnimationCurve::Create());
93 { 123 for (size_t i = 0; i < keyframes_.size(); ++i)
124 to_return->AddKeyframe(keyframes_[i]->Clone());
125 return to_return.PassAs<AnimationCurve>();
94 } 126 }
95 127
96 TransformKeyframe::~TransformKeyframe() 128 float KeyframedFloatAnimationCurve::GetValue(double t) const {
97 { 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;
98 } 150 }
99 151
100 const TransformOperations& TransformKeyframe::value() const 152 scoped_ptr<KeyframedTransformAnimationCurve> KeyframedTransformAnimationCurve::
101 { 153 Create() {
102 return m_value; 154 return make_scoped_ptr(new KeyframedTransformAnimationCurve);
103 } 155 }
104 156
105 scoped_ptr<TransformKeyframe> TransformKeyframe::clone() const 157 KeyframedTransformAnimationCurve::KeyframedTransformAnimationCurve() {}
106 { 158
107 scoped_ptr<TimingFunction> func; 159 KeyframedTransformAnimationCurve::~KeyframedTransformAnimationCurve() {}
108 if (timingFunction()) 160
109 func = cloneTimingFunction(timingFunction()); 161 void KeyframedTransformAnimationCurve::AddKeyframe(
110 return TransformKeyframe::create(time(), value(), func.Pass()); 162 scoped_ptr<TransformKeyframe> keyframe) {
163 InsertKeyframe(keyframe.Pass(), keyframes_);
111 } 164 }
112 165
113 scoped_ptr<KeyframedFloatAnimationCurve> KeyframedFloatAnimationCurve::create() 166 double KeyframedTransformAnimationCurve::Duration() const {
114 { 167 return keyframes_.back()->Time() - keyframes_.front()->Time();
115 return make_scoped_ptr(new KeyframedFloatAnimationCurve);
116 } 168 }
117 169
118 KeyframedFloatAnimationCurve::KeyframedFloatAnimationCurve() 170 scoped_ptr<AnimationCurve> KeyframedTransformAnimationCurve::Clone() const {
119 { 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>();
120 } 176 }
121 177
122 KeyframedFloatAnimationCurve::~KeyframedFloatAnimationCurve() 178 gfx::Transform KeyframedTransformAnimationCurve::GetValue(double t) const {
123 { 179 if (t <= keyframes_.front()->Time())
124 } 180 return keyframes_.front()->Value().Apply();
125 181
126 void KeyframedFloatAnimationCurve::addKeyframe(scoped_ptr<FloatKeyframe> keyfram e) 182 if (t >= keyframes_.back()->Time())
127 { 183 return keyframes_.back()->Value().Apply();
128 insertKeyframe(keyframe.Pass(), m_keyframes);
129 }
130 184
131 double KeyframedFloatAnimationCurve::duration() const 185 size_t i = 0;
132 { 186 for (; i < keyframes_.size() - 1; ++i) {
133 return m_keyframes.back()->time() - m_keyframes.front()->time(); 187 if (t < keyframes_[i+1]->Time())
134 } 188 break;
189 }
135 190
136 scoped_ptr<AnimationCurve> KeyframedFloatAnimationCurve::clone() const 191 double progress = (t - keyframes_[i]->Time()) /
137 { 192 (keyframes_[i+1]->Time() - keyframes_[i]->Time());
138 scoped_ptr<KeyframedFloatAnimationCurve> toReturn(KeyframedFloatAnimationCur ve::create());
139 for (size_t i = 0; i < m_keyframes.size(); ++i)
140 toReturn->addKeyframe(m_keyframes[i]->clone());
141 return toReturn.PassAs<AnimationCurve>();
142 }
143 193
144 float KeyframedFloatAnimationCurve::getValue(double t) const 194 if (keyframes_[i]->timing_function())
145 { 195 progress = keyframes_[i]->timing_function()->GetValue(progress);
146 if (t <= m_keyframes.front()->time())
147 return m_keyframes.front()->value();
148 196
149 if (t >= m_keyframes.back()->time()) 197 return keyframes_[i+1]->Value().Blend(keyframes_[i]->Value(), progress);
150 return m_keyframes.back()->value();
151
152 size_t i = 0;
153 for (; i < m_keyframes.size() - 1; ++i) {
154 if (t < m_keyframes[i+1]->time())
155 break;
156 }
157
158 float progress = static_cast<float>((t - m_keyframes[i]->time()) / (m_keyfra mes[i+1]->time() - m_keyframes[i]->time()));
159
160 if (m_keyframes[i]->timingFunction())
161 progress = m_keyframes[i]->timingFunction()->getValue(progress);
162
163 return m_keyframes[i]->value() + (m_keyframes[i+1]->value() - m_keyframes[i] ->value()) * progress;
164 }
165
166 scoped_ptr<KeyframedTransformAnimationCurve> KeyframedTransformAnimationCurve::c reate()
167 {
168 return make_scoped_ptr(new KeyframedTransformAnimationCurve);
169 }
170
171 KeyframedTransformAnimationCurve::KeyframedTransformAnimationCurve()
172 {
173 }
174
175 KeyframedTransformAnimationCurve::~KeyframedTransformAnimationCurve()
176 {
177 }
178
179 void KeyframedTransformAnimationCurve::addKeyframe(scoped_ptr<TransformKeyframe> keyframe)
180 {
181 insertKeyframe(keyframe.Pass(), m_keyframes);
182 }
183
184 double KeyframedTransformAnimationCurve::duration() const
185 {
186 return m_keyframes.back()->time() - m_keyframes.front()->time();
187 }
188
189 scoped_ptr<AnimationCurve> KeyframedTransformAnimationCurve::clone() const
190 {
191 scoped_ptr<KeyframedTransformAnimationCurve> toReturn(KeyframedTransformAnim ationCurve::create());
192 for (size_t i = 0; i < m_keyframes.size(); ++i)
193 toReturn->addKeyframe(m_keyframes[i]->clone());
194 return toReturn.PassAs<AnimationCurve>();
195 }
196
197 gfx::Transform KeyframedTransformAnimationCurve::getValue(double t) const
198 {
199 if (t <= m_keyframes.front()->time())
200 return m_keyframes.front()->value().Apply();
201
202 if (t >= m_keyframes.back()->time())
203 return m_keyframes.back()->value().Apply();
204
205 size_t i = 0;
206 for (; i < m_keyframes.size() - 1; ++i) {
207 if (t < m_keyframes[i+1]->time())
208 break;
209 }
210
211 double progress = (t - m_keyframes[i]->time()) / (m_keyframes[i+1]->time() - m_keyframes[i]->time());
212
213 if (m_keyframes[i]->timingFunction())
214 progress = m_keyframes[i]->timingFunction()->getValue(progress);
215
216 return m_keyframes[i+1]->value().Blend(m_keyframes[i]->value(), progress);
217 } 198 }
218 199
219 } // namespace cc 200 } // namespace cc
OLDNEW
« no previous file with comments | « cc/keyframed_animation_curve.h ('k') | cc/keyframed_animation_curve_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698