// This should match the RunState enum.
static const char* const s_runStateNames[] = {
- "WaitingForNextTick",
"WaitingForTargetAvailability",
- "WaitingForStartTime",
"WaitingForDeletion",
"Starting",
"Running",
"Transform",
"Opacity",
"Filter",
+ "ScrollOffset",
"BackgroundColor"
};
target_property_(target_property),
run_state_(WaitingForTargetAvailability),
iterations_(1),
- start_time_(0),
- alternates_direction_(false),
- time_offset_(0),
+ iteration_start_(0),
+ direction_(Normal),
+ playback_rate_(1),
+ fill_mode_(FillModeBoth),
needs_synchronized_start_time_(false),
received_finished_event_(false),
suspended_(false),
- pause_time_(0),
- total_paused_time_(0),
is_controlling_instance_(false),
- is_impl_only_(false) {}
+ is_impl_only_(false),
+ affects_active_observers_(true),
+ affects_pending_observers_(true) {
+}
Animation::~Animation() {
if (run_state_ == Running || run_state_ == Paused)
- SetRunState(Aborted, 0);
+ SetRunState(Aborted, base::TimeTicks());
}
-void Animation::SetRunState(RunState run_state, double monotonic_time) {
+void Animation::SetRunState(RunState run_state,
+ base::TimeTicks monotonic_time) {
if (suspended_)
return;
group_,
is_controlling_instance_ ? "(impl)" : "");
- bool is_waiting_to_start = run_state_ == WaitingForNextTick ||
- run_state_ == WaitingForTargetAvailability ||
- run_state_ == WaitingForStartTime ||
+ bool is_waiting_to_start = run_state_ == WaitingForTargetAvailability ||
run_state_ == Starting;
if (is_waiting_to_start && run_state == Running) {
const char* old_run_state_name = s_runStateNames[run_state_];
if (run_state == Running && run_state_ == Paused)
- total_paused_time_ += monotonic_time - pause_time_;
+ total_paused_time_ += (monotonic_time - pause_time_);
else if (run_state == Paused)
pause_time_ = monotonic_time;
run_state_ = run_state;
TRACE_STR_COPY(state_buffer));
}
-void Animation::Suspend(double monotonic_time) {
+void Animation::Suspend(base::TimeTicks monotonic_time) {
SetRunState(Paused, monotonic_time);
suspended_ = true;
}
-void Animation::Resume(double monotonic_time) {
+void Animation::Resume(base::TimeTicks monotonic_time) {
suspended_ = false;
SetRunState(Running, monotonic_time);
}
-bool Animation::IsFinishedAt(double monotonic_time) const {
+bool Animation::IsFinishedAt(base::TimeTicks monotonic_time) const {
if (is_finished())
return true;
if (needs_synchronized_start_time_)
return false;
- return run_state_ == Running &&
- iterations_ >= 0 &&
- iterations_ * curve_->Duration() <= (monotonic_time -
- start_time() -
- total_paused_time_);
+ if (playback_rate_ == 0)
+ return false;
+
+ return run_state_ == Running && iterations_ >= 0 &&
+ iterations_ * curve_->Duration() / std::abs(playback_rate_) <=
+ (monotonic_time + time_offset_ - start_time_ - total_paused_time_)
+ .InSecondsF();
+}
+
+bool Animation::InEffect(base::TimeTicks monotonic_time) const {
+ return ConvertToActiveTime(monotonic_time) >= 0 ||
+ (fill_mode_ == FillModeBoth || fill_mode_ == FillModeBackwards);
}
-double Animation::TrimTimeToCurrentIteration(double monotonic_time) const {
- double trimmed = monotonic_time + time_offset_;
+double Animation::ConvertToActiveTime(base::TimeTicks monotonic_time) const {
+ base::TimeTicks trimmed = monotonic_time + time_offset_;
// If we're paused, time is 'stuck' at the pause time.
if (run_state_ == Paused)
// Returned time should always be relative to the start time and should
// subtract all time spent paused.
- trimmed -= start_time_ + total_paused_time_;
+ trimmed -= (start_time_ - base::TimeTicks()) + total_paused_time_;
// If we're just starting or we're waiting on receiving a start time,
// time is 'stuck' at the initial state.
if ((run_state_ == Starting && !has_set_start_time()) ||
needs_synchronized_start_time())
- trimmed = time_offset_;
+ trimmed = base::TimeTicks() + time_offset_;
+
+ return (trimmed - base::TimeTicks()).InSecondsF();
+}
+
+double Animation::TrimTimeToCurrentIteration(
+ base::TimeTicks monotonic_time) const {
+ // Check for valid parameters
+ DCHECK(playback_rate_);
+ DCHECK_GE(iteration_start_, 0);
+
+ double active_time = ConvertToActiveTime(monotonic_time);
- // Zero is always the start of the animation.
- if (trimmed <= 0)
+ // Return 0 if we are before the start of the animation
+ if (active_time < 0)
return 0;
// Always return zero if we have no iterations.
if (curve_->Duration() <= 0)
return 0;
- // If less than an iteration duration, just return trimmed.
- if (trimmed < curve_->Duration())
- return trimmed;
-
- // If greater than or equal to the total duration, return iteration duration.
- if (iterations_ >= 0 && trimmed >= curve_->Duration() * iterations_) {
- if (alternates_direction_ && !(iterations_ % 2))
- return 0;
- return curve_->Duration();
- }
-
- // We need to know the current iteration if we're alternating.
- int iteration = static_cast<int>(trimmed / curve_->Duration());
-
- // Calculate x where trimmed = x + n * curve_->Duration() for some positive
- // integer n.
- trimmed = fmod(trimmed, curve_->Duration());
-
- // If we're alternating and on an odd iteration, reverse the direction.
- if (alternates_direction_ && iteration % 2 == 1)
- return curve_->Duration() - trimmed;
-
- return trimmed;
-}
-
-scoped_ptr<Animation> Animation::Clone() const {
- return CloneAndInitialize(run_state_, start_time_);
+ double repeated_duration = iterations_ * curve_->Duration();
+ double active_duration = repeated_duration / std::abs(playback_rate_);
+ double start_offset = iteration_start_ * curve_->Duration();
+
+ // Check if we are past active duration
+ if (iterations_ > 0 && active_time >= active_duration)
+ active_time = active_duration;
+
+ // Calculate the scaled active time
+ double scaled_active_time;
+ if (playback_rate_ < 0)
+ scaled_active_time =
+ (active_time - active_duration) * playback_rate_ + start_offset;
+ else
+ scaled_active_time = active_time * playback_rate_ + start_offset;
+
+ // Calculate the iteration time
+ double iteration_time;
+ if (scaled_active_time - start_offset == repeated_duration &&
+ fmod(iterations_ + iteration_start_, 1) == 0)
+ iteration_time = curve_->Duration();
+ else
+ iteration_time = fmod(scaled_active_time, curve_->Duration());
+
+ // Calculate the current iteration
+ int iteration;
+ if (scaled_active_time <= 0)
+ iteration = 0;
+ else if (iteration_time == curve_->Duration())
+ iteration = ceil(iteration_start_ + iterations_ - 1);
+ else
+ iteration = static_cast<int>(scaled_active_time / curve_->Duration());
+
+ // Check if we are running the animation in reverse direction for the current
+ // iteration
+ bool reverse = (direction_ == Reverse) ||
+ (direction_ == Alternate && iteration % 2 == 1) ||
+ (direction_ == AlternateReverse && iteration % 2 == 0);
+
+ // If we are running the animation in reverse direction, reverse the result
+ if (reverse)
+ iteration_time = curve_->Duration() - iteration_time;
+
+ return iteration_time;
}
-scoped_ptr<Animation> Animation::CloneAndInitialize(RunState initial_run_state,
- double start_time) const {
+scoped_ptr<Animation> Animation::CloneAndInitialize(
+ RunState initial_run_state) const {
scoped_ptr<Animation> to_return(
new Animation(curve_->Clone(), id_, group_, target_property_));
to_return->run_state_ = initial_run_state;
to_return->iterations_ = iterations_;
- to_return->start_time_ = start_time;
+ to_return->iteration_start_ = iteration_start_;
+ to_return->start_time_ = start_time_;
to_return->pause_time_ = pause_time_;
to_return->total_paused_time_ = total_paused_time_;
to_return->time_offset_ = time_offset_;
- to_return->alternates_direction_ = alternates_direction_;
+ to_return->direction_ = direction_;
+ to_return->playback_rate_ = playback_rate_;
+ to_return->fill_mode_ = fill_mode_;
DCHECK(!to_return->is_controlling_instance_);
to_return->is_controlling_instance_ = true;
return to_return.Pass();