OLD | NEW |
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/test/animation_test_common.h" | 5 #include "cc/test/animation_test_common.h" |
6 | 6 |
7 #include "cc/animation/keyframed_animation_curve.h" | 7 #include "cc/animation/keyframed_animation_curve.h" |
8 #include "cc/animation/layer_animation_controller.h" | 8 #include "cc/animation/layer_animation_controller.h" |
9 #include "cc/animation/transform_operations.h" | 9 #include "cc/animation/transform_operations.h" |
10 #include "cc/layers/layer.h" | 10 #include "cc/layers/layer.h" |
11 #include "cc/layers/layer_impl.h" | 11 #include "cc/layers/layer_impl.h" |
12 | 12 |
13 using cc::Animation; | 13 using cc::Animation; |
14 using cc::AnimationCurve; | 14 using cc::AnimationCurve; |
15 using cc::EaseTimingFunction; | 15 using cc::EaseTimingFunction; |
16 using cc::FloatKeyframe; | 16 using cc::FloatKeyframe; |
17 using cc::KeyframedFloatAnimationCurve; | 17 using cc::KeyframedFloatAnimationCurve; |
18 using cc::KeyframedTransformAnimationCurve; | 18 using cc::KeyframedTransformAnimationCurve; |
19 using cc::TimingFunction; | 19 using cc::TimingFunction; |
20 using cc::TransformKeyframe; | 20 using cc::TransformKeyframe; |
21 | 21 |
22 namespace cc { | 22 namespace cc { |
23 | 23 |
24 static int nextAnimationId = 0; | 24 static int nextAnimationId = 0; |
25 | 25 |
26 template <class Target> | 26 template <class Target> |
27 int addOpacityTransition(Target& target, double duration, float startOpacity, fl
oat endOpacity, bool useTimingFunction) | 27 int AddOpacityTransition(Target* target, |
28 { | 28 double duration, |
29 scoped_ptr<KeyframedFloatAnimationCurve> curve(KeyframedFloatAnimationCurve:
:Create()); | 29 float startOpacity, |
30 | 30 float endOpacity, |
31 scoped_ptr<TimingFunction> func; | 31 bool useTimingFunction) { |
32 if (!useTimingFunction) | 32 scoped_ptr<KeyframedFloatAnimationCurve> |
33 func = EaseTimingFunction::create(); | 33 curve(KeyframedFloatAnimationCurve::Create()); |
34 if (duration > 0) | 34 |
35 curve->AddKeyframe(FloatKeyframe::Create(0, startOpacity, func.Pass())); | 35 scoped_ptr<TimingFunction> func; |
36 curve->AddKeyframe(FloatKeyframe::Create(duration, endOpacity, scoped_ptr<cc
::TimingFunction>())); | 36 if (!useTimingFunction) |
37 | 37 func = EaseTimingFunction::create(); |
38 int id = nextAnimationId++; | 38 if (duration > 0.0) |
39 | 39 curve->AddKeyframe(FloatKeyframe::Create(0.0, startOpacity, func.Pass())); |
40 scoped_ptr<Animation> animation(Animation::Create(curve.PassAs<AnimationCurv
e>(), id, 0, Animation::Opacity)); | 40 curve->AddKeyframe(FloatKeyframe::Create(duration, |
41 animation->set_needs_synchronized_start_time(true); | 41 endOpacity, |
42 | 42 scoped_ptr<cc::TimingFunction>())); |
43 target.AddAnimation(animation.Pass()); | 43 |
44 return id; | 44 int id = nextAnimationId++; |
| 45 |
| 46 scoped_ptr<Animation> animation(Animation::Create( |
| 47 curve.PassAs<AnimationCurve>(), |
| 48 id, |
| 49 0, |
| 50 Animation::Opacity)); |
| 51 animation->set_needs_synchronized_start_time(true); |
| 52 |
| 53 target->AddAnimation(animation.Pass()); |
| 54 return id; |
45 } | 55 } |
46 | 56 |
47 template <class Target> | 57 template <class Target> |
48 int addAnimatedTransform(Target& target, double duration, int deltaX, int deltaY
) | 58 int AddAnimatedTransform(Target* target, |
49 { | 59 double duration, |
50 scoped_ptr<KeyframedTransformAnimationCurve> curve(KeyframedTransformAnimati
onCurve::Create()); | 60 int deltaX, |
51 | 61 int deltaY) { |
52 if (duration > 0) { | 62 scoped_ptr<KeyframedTransformAnimationCurve> |
53 TransformOperations startOperations; | 63 curve(KeyframedTransformAnimationCurve::Create()); |
54 startOperations.AppendTranslate(deltaX, deltaY, 0); | 64 |
55 curve->AddKeyframe(TransformKeyframe::Create(0, startOperations, scoped_
ptr<cc::TimingFunction>())); | 65 if (duration > 0.0) { |
56 } | 66 TransformOperations startOperations; |
57 | 67 startOperations.AppendTranslate(deltaX, deltaY, 0.0); |
58 TransformOperations operations; | 68 curve->AddKeyframe(TransformKeyframe::Create( |
59 operations.AppendTranslate(deltaX, deltaY, 0); | 69 0.0, |
60 curve->AddKeyframe(TransformKeyframe::Create(duration, operations, scoped_pt
r<cc::TimingFunction>())); | 70 startOperations, |
61 | 71 scoped_ptr<cc::TimingFunction>())); |
62 int id = nextAnimationId++; | 72 } |
63 | 73 |
64 scoped_ptr<Animation> animation(Animation::Create(curve.PassAs<AnimationCurv
e>(), id, 0, Animation::Transform)); | 74 TransformOperations operations; |
65 animation->set_needs_synchronized_start_time(true); | 75 operations.AppendTranslate(deltaX, deltaY, 0.0); |
66 | 76 curve->AddKeyframe(TransformKeyframe::Create( |
67 target.AddAnimation(animation.Pass()); | 77 duration, |
68 return id; | 78 operations, |
| 79 scoped_ptr<cc::TimingFunction>())); |
| 80 |
| 81 int id = nextAnimationId++; |
| 82 |
| 83 scoped_ptr<Animation> animation(Animation::Create( |
| 84 curve.PassAs<AnimationCurve>(), |
| 85 id, 0, Animation::Transform)); |
| 86 animation->set_needs_synchronized_start_time(true); |
| 87 |
| 88 target->AddAnimation(animation.Pass()); |
| 89 return id; |
69 } | 90 } |
70 | 91 |
71 FakeFloatAnimationCurve::FakeFloatAnimationCurve() | 92 FakeFloatAnimationCurve::FakeFloatAnimationCurve() |
72 : m_duration(1) | 93 : duration_(1.0) |
73 { | 94 { |
74 } | 95 } |
75 | 96 |
76 FakeFloatAnimationCurve::FakeFloatAnimationCurve(double duration) | 97 FakeFloatAnimationCurve::FakeFloatAnimationCurve(double duration) |
77 : m_duration(duration) | 98 : duration_(duration) |
78 { | 99 { |
79 } | 100 } |
80 | 101 |
81 FakeFloatAnimationCurve::~FakeFloatAnimationCurve() | 102 FakeFloatAnimationCurve::~FakeFloatAnimationCurve() { |
82 { | 103 } |
83 } | 104 |
84 | 105 double FakeFloatAnimationCurve::Duration() const { |
85 double FakeFloatAnimationCurve::Duration() const | 106 return duration_; |
86 { | 107 } |
87 return m_duration; | 108 |
88 } | 109 float FakeFloatAnimationCurve::GetValue(double now) const { |
89 | 110 return 0.0f; |
90 float FakeFloatAnimationCurve::GetValue(double now) const | 111 } |
91 { | 112 |
92 return 0; | 113 scoped_ptr<cc::AnimationCurve> FakeFloatAnimationCurve::Clone() const { |
93 } | 114 return make_scoped_ptr( |
94 | 115 new FakeFloatAnimationCurve).PassAs<cc::AnimationCurve>(); |
95 scoped_ptr<cc::AnimationCurve> FakeFloatAnimationCurve::Clone() const | |
96 { | |
97 return make_scoped_ptr(new FakeFloatAnimationCurve).PassAs<cc::AnimationCurv
e>(); | |
98 } | 116 } |
99 | 117 |
100 FakeTransformTransition::FakeTransformTransition(double duration) | 118 FakeTransformTransition::FakeTransformTransition(double duration) |
101 : m_duration(duration) | 119 : duration_(duration) |
102 { | 120 { |
103 } | 121 } |
104 | 122 |
105 FakeTransformTransition::~FakeTransformTransition() | 123 FakeTransformTransition::~FakeTransformTransition() { |
106 { | 124 } |
107 } | 125 |
108 | 126 double FakeTransformTransition::Duration() const { |
109 double FakeTransformTransition::Duration() const | 127 return duration_; |
110 { | 128 } |
111 return m_duration; | 129 |
112 } | 130 gfx::Transform FakeTransformTransition::GetValue(double time) const { |
113 | 131 return gfx::Transform(); |
114 gfx::Transform FakeTransformTransition::GetValue(double time) const | 132 } |
115 { | 133 |
116 return gfx::Transform(); | 134 scoped_ptr<cc::AnimationCurve> FakeTransformTransition::Clone() const { |
117 } | 135 return make_scoped_ptr( |
118 | 136 new FakeTransformTransition(*this)).PassAs<cc::AnimationCurve>(); |
119 scoped_ptr<cc::AnimationCurve> FakeTransformTransition::Clone() const | |
120 { | |
121 return make_scoped_ptr(new FakeTransformTransition(*this)).PassAs<cc::Animat
ionCurve>(); | |
122 } | 137 } |
123 | 138 |
124 | 139 |
125 FakeFloatTransition::FakeFloatTransition(double duration, float from, float to) | 140 FakeFloatTransition::FakeFloatTransition(double duration, float from, float to) |
126 : m_duration(duration) | 141 : duration_(duration) |
127 , m_from(from) | 142 , from_(from) |
128 , m_to(to) | 143 , to_(to) |
129 { | 144 { |
130 } | 145 } |
131 | 146 |
132 FakeFloatTransition::~FakeFloatTransition() | 147 FakeFloatTransition::~FakeFloatTransition() { |
133 { | 148 } |
134 } | 149 |
135 | 150 double FakeFloatTransition::Duration() const { |
136 double FakeFloatTransition::Duration() const | 151 return duration_; |
137 { | 152 } |
138 return m_duration; | 153 |
139 } | 154 float FakeFloatTransition::GetValue(double time) const { |
140 | 155 time /= duration_; |
141 float FakeFloatTransition::GetValue(double time) const | 156 if (time >= 1.0) |
142 { | 157 time = 1.0; |
143 time /= m_duration; | 158 return (1.0 - time) * from_ + time * to_; |
144 if (time >= 1) | |
145 time = 1; | |
146 return (1 - time) * m_from + time * m_to; | |
147 } | 159 } |
148 | 160 |
149 FakeLayerAnimationValueObserver::FakeLayerAnimationValueObserver() | 161 FakeLayerAnimationValueObserver::FakeLayerAnimationValueObserver() |
150 : m_opacity(0) | 162 : opacity_(0.0f) |
151 { | 163 { |
152 } | 164 } |
153 | 165 |
154 FakeLayerAnimationValueObserver::~FakeLayerAnimationValueObserver() | 166 FakeLayerAnimationValueObserver::~FakeLayerAnimationValueObserver() { |
155 { | 167 } |
156 } | 168 |
157 | 169 void FakeLayerAnimationValueObserver::OnOpacityAnimated(float opacity) { |
158 void FakeLayerAnimationValueObserver::OnOpacityAnimated(float opacity) | 170 opacity_ = opacity; |
159 { | 171 } |
160 m_opacity = opacity; | 172 |
161 } | 173 void FakeLayerAnimationValueObserver::OnTransformAnimated( |
162 | 174 const gfx::Transform& transform) { |
163 void FakeLayerAnimationValueObserver::OnTransformAnimated(const gfx::Transform&
transform) | 175 transform_ = transform; |
164 { | 176 } |
165 m_transform = transform; | 177 |
166 } | 178 bool FakeLayerAnimationValueObserver::IsActive() const { |
167 | 179 return true; |
168 bool FakeLayerAnimationValueObserver::IsActive() const | 180 } |
169 { | 181 |
170 return true; | 182 scoped_ptr<cc::AnimationCurve> FakeFloatTransition::Clone() const { |
171 } | 183 return make_scoped_ptr( |
172 | 184 new FakeFloatTransition(*this)).PassAs<cc::AnimationCurve>(); |
173 scoped_ptr<cc::AnimationCurve> FakeFloatTransition::Clone() const | 185 } |
174 { | 186 |
175 return make_scoped_ptr(new FakeFloatTransition(*this)).PassAs<cc::AnimationC
urve>(); | 187 int AddOpacityTransitionToController(cc::LayerAnimationController* controller, |
176 } | 188 double duration, |
177 | 189 float start_opacity, |
178 int addOpacityTransitionToController(cc::LayerAnimationController& controller, d
ouble duration, float startOpacity, float endOpacity, bool useTimingFunction) | 190 float end_opacity, |
179 { | 191 bool use_timing_function) { |
180 return addOpacityTransition(controller, duration, startOpacity, endOpacity,
useTimingFunction); | 192 return AddOpacityTransition(controller, |
181 } | 193 duration, |
182 | 194 start_opacity, |
183 int addAnimatedTransformToController(cc::LayerAnimationController& controller, d
ouble duration, int deltaX, int deltaY) | 195 end_opacity, |
184 { | 196 use_timing_function); |
185 return addAnimatedTransform(controller, duration, deltaX, deltaY); | 197 } |
186 } | 198 |
187 | 199 int AddAnimatedTransformToController(cc::LayerAnimationController* controller, |
188 int addOpacityTransitionToLayer(cc::Layer& layer, double duration, float startOp
acity, float endOpacity, bool useTimingFunction) | 200 double duration, |
189 { | 201 int delta_x, |
190 return addOpacityTransition(layer, duration, startOpacity, endOpacity, useTi
mingFunction); | 202 int delta_y) { |
191 } | 203 return AddAnimatedTransform(controller, |
192 | 204 duration, |
193 int addOpacityTransitionToLayer(cc::LayerImpl& layer, double duration, float sta
rtOpacity, float endOpacity, bool useTimingFunction) | 205 delta_x, |
194 { | 206 delta_y); |
195 return addOpacityTransition(*layer.layer_animation_controller(), duration, s
tartOpacity, endOpacity, useTimingFunction); | 207 } |
196 } | 208 |
197 | 209 int AddOpacityTransitionToLayer(cc::Layer* layer, |
198 int addAnimatedTransformToLayer(cc::Layer& layer, double duration, int deltaX, i
nt deltaY) | 210 double duration, |
199 { | 211 float start_opacity, |
200 return addAnimatedTransform(layer, duration, deltaX, deltaY); | 212 float end_opacity, |
201 } | 213 bool use_timing_function) { |
202 | 214 return AddOpacityTransition(layer, |
203 int addAnimatedTransformToLayer(cc::LayerImpl& layer, double duration, int delta
X, int deltaY) | 215 duration, |
204 { | 216 start_opacity, |
205 return addAnimatedTransform(*layer.layer_animation_controller(), duration, d
eltaX, deltaY); | 217 end_opacity, |
| 218 use_timing_function); |
| 219 } |
| 220 |
| 221 int AddOpacityTransitionToLayer(cc::LayerImpl* layer, |
| 222 double duration, |
| 223 float start_opacity, |
| 224 float end_opacity, |
| 225 bool use_timing_function) { |
| 226 return AddOpacityTransition(layer->layer_animation_controller(), |
| 227 duration, |
| 228 start_opacity, |
| 229 end_opacity, |
| 230 use_timing_function); |
| 231 } |
| 232 |
| 233 int AddAnimatedTransformToLayer(cc::Layer* layer, |
| 234 double duration, |
| 235 int delta_x, |
| 236 int delta_y) { |
| 237 return AddAnimatedTransform(layer, duration, delta_x, delta_y); |
| 238 } |
| 239 |
| 240 int AddAnimatedTransformToLayer(cc::LayerImpl* layer, |
| 241 double duration, |
| 242 int delta_x, |
| 243 int delta_y) { |
| 244 return AddAnimatedTransform(layer->layer_animation_controller(), |
| 245 duration, |
| 246 delta_x, |
| 247 delta_y); |
206 } | 248 } |
207 | 249 |
208 } // namespace cc | 250 } // namespace cc |
OLD | NEW |