| 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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 int animation_id, | 58 int animation_id, |
| 59 int group_id, | 59 int group_id, |
| 60 TargetProperty target_property) | 60 TargetProperty target_property) |
| 61 : curve_(curve.Pass()), | 61 : curve_(curve.Pass()), |
| 62 id_(animation_id), | 62 id_(animation_id), |
| 63 group_(group_id), | 63 group_(group_id), |
| 64 target_property_(target_property), | 64 target_property_(target_property), |
| 65 run_state_(WaitingForTargetAvailability), | 65 run_state_(WaitingForTargetAvailability), |
| 66 iterations_(1), | 66 iterations_(1), |
| 67 start_time_(0), | 67 start_time_(0), |
| 68 alternates_direction_(false), | 68 direction_(Normal), |
| 69 time_offset_(0), | 69 time_offset_(0), |
| 70 needs_synchronized_start_time_(false), | 70 needs_synchronized_start_time_(false), |
| 71 received_finished_event_(false), | 71 received_finished_event_(false), |
| 72 suspended_(false), | 72 suspended_(false), |
| 73 pause_time_(0), | 73 pause_time_(0), |
| 74 total_paused_time_(0), | 74 total_paused_time_(0), |
| 75 is_controlling_instance_(false), | 75 is_controlling_instance_(false), |
| 76 is_impl_only_(false) {} | 76 is_impl_only_(false) {} |
| 77 | 77 |
| 78 Animation::~Animation() { | 78 Animation::~Animation() { |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 // Returned time should always be relative to the start time and should | 166 // Returned time should always be relative to the start time and should |
| 167 // subtract all time spent paused. | 167 // subtract all time spent paused. |
| 168 trimmed -= start_time_ + total_paused_time_; | 168 trimmed -= start_time_ + total_paused_time_; |
| 169 | 169 |
| 170 // If we're just starting or we're waiting on receiving a start time, | 170 // If we're just starting or we're waiting on receiving a start time, |
| 171 // time is 'stuck' at the initial state. | 171 // time is 'stuck' at the initial state. |
| 172 if ((run_state_ == Starting && !has_set_start_time()) || | 172 if ((run_state_ == Starting && !has_set_start_time()) || |
| 173 needs_synchronized_start_time()) | 173 needs_synchronized_start_time()) |
| 174 trimmed = time_offset_; | 174 trimmed = time_offset_; |
| 175 | 175 |
| 176 // Zero is always the start of the animation. | 176 // Return 0 if we are before the start of the animation |
| 177 if (trimmed <= 0) | 177 if (trimmed < 0) |
| 178 return 0; | 178 return 0; |
| 179 | 179 |
| 180 // Always return zero if we have no iterations. | 180 // Always return zero if we have no iterations. |
| 181 if (!iterations_) | 181 if (!iterations_) |
| 182 return 0; | 182 return 0; |
| 183 | 183 |
| 184 // Don't attempt to trim if we have no duration. | 184 // Don't attempt to trim if we have no duration. |
| 185 if (curve_->Duration() <= 0) | 185 if (curve_->Duration() <= 0) |
| 186 return 0; | 186 return 0; |
| 187 | 187 |
| 188 // If less than an iteration duration, just return trimmed. | 188 // check if we are past active interval |
| 189 if (trimmed < curve_->Duration()) | 189 bool is_past_total_duration = |
| 190 return trimmed; | 190 (iterations_ > 0 && trimmed >= curve_->Duration() * iterations_); |
| 191 | 191 |
| 192 // If greater than or equal to the total duration, return iteration duration. | 192 // We need to know the current iteration if we're alternating. |
| 193 if (iterations_ >= 0 && trimmed >= curve_->Duration() * iterations_) { | 193 int iteration = 0; |
| 194 if (alternates_direction_ && !(iterations_ % 2)) | 194 |
| 195 return 0; | 195 // If we are past the active interval, return iteration duration. |
| 196 return curve_->Duration(); | 196 if (is_past_total_duration) { |
| 197 iteration = iterations_; |
| 198 trimmed = curve_->Duration(); |
| 199 } else { |
| 200 iteration = static_cast<int>(trimmed / curve_->Duration()); |
| 201 // Calculate x where trimmed = x + n * curve_->Duration() for some positive |
| 202 // integer n. |
| 203 trimmed = fmod(trimmed, curve_->Duration()); |
| 197 } | 204 } |
| 198 | 205 |
| 199 // We need to know the current iteration if we're alternating. | 206 // check if we are running the animation in reverse direction for the current |
| 200 int iteration = static_cast<int>(trimmed / curve_->Duration()); | 207 // iteration |
| 208 bool reverse = (direction_ == Reverse) || |
| 209 (direction_ == Alternate && iteration % 2 == 1) || |
| 210 (direction_ == AlternateReverse && iteration % 2 == 0); |
| 201 | 211 |
| 202 // Calculate x where trimmed = x + n * curve_->Duration() for some positive | 212 // if we are running the animation in reverse direction, reverse the result |
| 203 // integer n. | 213 if (reverse) |
| 204 trimmed = fmod(trimmed, curve_->Duration()); | |
| 205 | |
| 206 // If we're alternating and on an odd iteration, reverse the direction. | |
| 207 if (alternates_direction_ && iteration % 2 == 1) | |
| 208 return curve_->Duration() - trimmed; | 214 return curve_->Duration() - trimmed; |
| 209 | 215 |
| 210 return trimmed; | 216 return trimmed; |
| 211 } | 217 } |
| 212 | 218 |
| 213 scoped_ptr<Animation> Animation::Clone() const { | 219 scoped_ptr<Animation> Animation::Clone() const { |
| 214 return CloneAndInitialize(run_state_, start_time_); | 220 return CloneAndInitialize(run_state_, start_time_); |
| 215 } | 221 } |
| 216 | 222 |
| 217 scoped_ptr<Animation> Animation::CloneAndInitialize(RunState initial_run_state, | 223 scoped_ptr<Animation> Animation::CloneAndInitialize(RunState initial_run_state, |
| 218 double start_time) const { | 224 double start_time) const { |
| 219 scoped_ptr<Animation> to_return( | 225 scoped_ptr<Animation> to_return( |
| 220 new Animation(curve_->Clone(), id_, group_, target_property_)); | 226 new Animation(curve_->Clone(), id_, group_, target_property_)); |
| 221 to_return->run_state_ = initial_run_state; | 227 to_return->run_state_ = initial_run_state; |
| 222 to_return->iterations_ = iterations_; | 228 to_return->iterations_ = iterations_; |
| 223 to_return->start_time_ = start_time; | 229 to_return->start_time_ = start_time; |
| 224 to_return->pause_time_ = pause_time_; | 230 to_return->pause_time_ = pause_time_; |
| 225 to_return->total_paused_time_ = total_paused_time_; | 231 to_return->total_paused_time_ = total_paused_time_; |
| 226 to_return->time_offset_ = time_offset_; | 232 to_return->time_offset_ = time_offset_; |
| 227 to_return->alternates_direction_ = alternates_direction_; | 233 to_return->direction_ = direction_; |
| 228 DCHECK(!to_return->is_controlling_instance_); | 234 DCHECK(!to_return->is_controlling_instance_); |
| 229 to_return->is_controlling_instance_ = true; | 235 to_return->is_controlling_instance_ = true; |
| 230 return to_return.Pass(); | 236 return to_return.Pass(); |
| 231 } | 237 } |
| 232 | 238 |
| 233 void Animation::PushPropertiesTo(Animation* other) const { | 239 void Animation::PushPropertiesTo(Animation* other) const { |
| 234 // Currently, we only push changes due to pausing and resuming animations on | 240 // Currently, we only push changes due to pausing and resuming animations on |
| 235 // the main thread. | 241 // the main thread. |
| 236 if (run_state_ == Animation::Paused || | 242 if (run_state_ == Animation::Paused || |
| 237 other->run_state_ == Animation::Paused) { | 243 other->run_state_ == Animation::Paused) { |
| 238 other->run_state_ = run_state_; | 244 other->run_state_ = run_state_; |
| 239 other->pause_time_ = pause_time_; | 245 other->pause_time_ = pause_time_; |
| 240 other->total_paused_time_ = total_paused_time_; | 246 other->total_paused_time_ = total_paused_time_; |
| 241 } | 247 } |
| 242 } | 248 } |
| 243 | 249 |
| 244 } // namespace cc | 250 } // namespace cc |
| OLD | NEW |