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/animation/animation.h" | 5 #include "cc/animation/animation.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 | 8 |
9 #include "base/debug/trace_event.h" | 9 #include "base/debug/trace_event.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 // Returned time should always be relative to the start time and should | 172 // Returned time should always be relative to the start time and should |
173 // subtract all time spent paused. | 173 // subtract all time spent paused. |
174 trimmed -= (start_time_ - base::TimeTicks()) + total_paused_time_; | 174 trimmed -= (start_time_ - base::TimeTicks()) + total_paused_time_; |
175 | 175 |
176 // If we're just starting or we're waiting on receiving a start time, | 176 // If we're just starting or we're waiting on receiving a start time, |
177 // time is 'stuck' at the initial state. | 177 // time is 'stuck' at the initial state. |
178 if ((run_state_ == Starting && !has_set_start_time()) || | 178 if ((run_state_ == Starting && !has_set_start_time()) || |
179 needs_synchronized_start_time()) | 179 needs_synchronized_start_time()) |
180 trimmed = base::TimeTicks() + time_offset_; | 180 trimmed = base::TimeTicks() + time_offset_; |
181 | 181 |
182 double trimmed_in_seconds = (trimmed - base::TimeTicks()).InSecondsF(); | 182 double active_time = (trimmed - base::TimeTicks()).InSecondsF(); |
183 | 183 |
184 // Return 0 if we are before the start of the animation | 184 // Return 0 if we are before the start of the animation |
185 if (trimmed_in_seconds < 0) | 185 if (active_time < 0) |
186 return 0; | 186 return 0; |
187 | 187 |
188 // Always return zero if we have no iterations. | 188 // Always return zero if we have no iterations. |
189 if (!iterations_) | 189 if (!iterations_) |
190 return 0; | 190 return 0; |
191 | 191 |
192 // Don't attempt to trim if we have no duration. | 192 // Don't attempt to trim if we have no duration. |
193 if (curve_->Duration() <= 0) | 193 if (curve_->Duration() <= 0) |
194 return 0; | 194 return 0; |
195 | 195 |
196 // Calculate the active time | 196 double repeated_duration = iterations_ * curve_->Duration(); |
197 trimmed_in_seconds *= std::abs(playback_rate_); | 197 double active_duration = repeated_duration / std::abs(playback_rate_); |
198 | 198 |
199 // check if we are past active interval | 199 // Check if we are past active duration |
200 bool is_past_total_duration = | 200 bool is_past_active_duration = |
201 (iterations_ > 0 && | 201 iterations_ > 0 && active_time >= active_duration; |
202 trimmed_in_seconds >= curve_->Duration() * iterations_); | |
203 | 202 |
204 // We need to know the current iteration if we're alternating. | 203 // Calculate the scaled active time |
205 int iteration = 0; | 204 double scaled_active_time; |
| 205 if (playback_rate_ < 0) |
| 206 scaled_active_time = (active_time - active_duration) * playback_rate_; |
| 207 else |
| 208 scaled_active_time = active_time * playback_rate_; |
206 | 209 |
207 // If we are past the active interval, return iteration duration of last | 210 // Calculate the iteration time |
208 // iteration | 211 double iteration_time; |
209 if (is_past_total_duration) { | 212 if (is_past_active_duration) { |
210 iteration = iterations_ - 1; | 213 // If we are past the active interval, return iteration duration of last |
211 double frac = fmod(curve_->Duration() * iterations_, curve_->Duration()); | 214 // iteration |
212 trimmed_in_seconds = frac == 0 ? curve_->Duration() : frac; | 215 double frac = fmod(repeated_duration, curve_->Duration()); |
| 216 iteration_time = |
| 217 frac == 0 && scaled_active_time > 0 ? curve_->Duration() : frac; |
| 218 } else if (scaled_active_time == repeated_duration) { |
| 219 iteration_time = curve_->Duration(); |
213 } else { | 220 } else { |
214 iteration = static_cast<int>(trimmed_in_seconds / curve_->Duration()); | 221 iteration_time = fmod(scaled_active_time, curve_->Duration()); |
215 // Calculate x where trimmed = x + n * curve_->Duration() for some positive | |
216 // integer n. | |
217 trimmed_in_seconds = fmod(trimmed_in_seconds, curve_->Duration()); | |
218 } | 222 } |
219 | 223 |
220 // check if we are running the animation in reverse direction for the current | 224 // Calculate the current iteration |
| 225 int iteration; |
| 226 if (scaled_active_time <= 0) { |
| 227 iteration = 0; |
| 228 } else if (is_past_active_duration || iteration_time == curve_->Duration()) { |
| 229 iteration = iterations_ - 1; |
| 230 } else { |
| 231 iteration = static_cast<int>(scaled_active_time / curve_->Duration()); |
| 232 } |
| 233 |
| 234 // Check if we are running the animation in reverse direction for the current |
221 // iteration | 235 // iteration |
222 bool reverse = (direction_ == Reverse) || | 236 bool reverse = (direction_ == Reverse) || |
223 (direction_ == Alternate && iteration % 2 == 1) || | 237 (direction_ == Alternate && iteration % 2 == 1) || |
224 (direction_ == AlternateReverse && iteration % 2 == 0); | 238 (direction_ == AlternateReverse && iteration % 2 == 0); |
225 | 239 |
226 // check if playback rate is negative and if the playback rate is negative and | 240 // If we are running the animation in reverse direction, reverse the result |
227 // the animation is in reverse direction, run the animation normally | 241 if (reverse) |
228 if (playback_rate_ < 0) | 242 return curve_->Duration() - iteration_time; |
229 reverse = !reverse; | |
230 | 243 |
231 // if we are running the animation in reverse direction, reverse the result | 244 return iteration_time; |
232 if (reverse) | |
233 return curve_->Duration() - trimmed_in_seconds; | |
234 | |
235 return trimmed_in_seconds; | |
236 } | 245 } |
237 | 246 |
238 scoped_ptr<Animation> Animation::CloneAndInitialize( | 247 scoped_ptr<Animation> Animation::CloneAndInitialize( |
239 RunState initial_run_state) const { | 248 RunState initial_run_state) const { |
240 scoped_ptr<Animation> to_return( | 249 scoped_ptr<Animation> to_return( |
241 new Animation(curve_->Clone(), id_, group_, target_property_)); | 250 new Animation(curve_->Clone(), id_, group_, target_property_)); |
242 to_return->run_state_ = initial_run_state; | 251 to_return->run_state_ = initial_run_state; |
243 to_return->iterations_ = iterations_; | 252 to_return->iterations_ = iterations_; |
244 to_return->start_time_ = start_time_; | 253 to_return->start_time_ = start_time_; |
245 to_return->pause_time_ = pause_time_; | 254 to_return->pause_time_ = pause_time_; |
(...skipping 11 matching lines...) Expand all Loading... |
257 // the main thread. | 266 // the main thread. |
258 if (run_state_ == Animation::Paused || | 267 if (run_state_ == Animation::Paused || |
259 other->run_state_ == Animation::Paused) { | 268 other->run_state_ == Animation::Paused) { |
260 other->run_state_ = run_state_; | 269 other->run_state_ = run_state_; |
261 other->pause_time_ = pause_time_; | 270 other->pause_time_ = pause_time_; |
262 other->total_paused_time_ = total_paused_time_; | 271 other->total_paused_time_ = total_paused_time_; |
263 } | 272 } |
264 } | 273 } |
265 | 274 |
266 } // namespace cc | 275 } // namespace cc |
OLD | NEW |