| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "app/multi_animation.h" | 5 #include "app/multi_animation.h" |
| 6 | 6 |
| 7 // Default interval, in ms. | 7 // Default interval, in ms. |
| 8 static const int kDefaultInterval = 20; | 8 static const int kDefaultInterval = 20; |
| 9 | 9 |
| 10 static int TotalTime(const MultiAnimation::Parts& parts) { | 10 static int TotalTime(const MultiAnimation::Parts& parts) { |
| 11 int time_ms = 0; | 11 int time_ms = 0; |
| 12 for (size_t i = 0; i < parts.size(); ++i) | 12 for (size_t i = 0; i < parts.size(); ++i) { |
| 13 DCHECK(parts[i].end_time_ms - parts[i].start_time_ms >= parts[i].time_ms); |
| 13 time_ms += parts[i].time_ms; | 14 time_ms += parts[i].time_ms; |
| 15 } |
| 14 return time_ms; | 16 return time_ms; |
| 15 } | 17 } |
| 16 | 18 |
| 17 MultiAnimation::MultiAnimation(const Parts& parts) | 19 MultiAnimation::MultiAnimation(const Parts& parts) |
| 18 : Animation(base::TimeDelta::FromMilliseconds(kDefaultInterval)), | 20 : Animation(base::TimeDelta::FromMilliseconds(kDefaultInterval)), |
| 19 parts_(parts), | 21 parts_(parts), |
| 20 cycle_time_ms_(TotalTime(parts)), | 22 cycle_time_ms_(TotalTime(parts)), |
| 21 current_value_(0), | 23 current_value_(0), |
| 22 current_part_index_(0) { | 24 current_part_index_(0) { |
| 23 DCHECK(!parts_.empty()); | 25 DCHECK(!parts_.empty()); |
| 24 } | 26 } |
| 25 | 27 |
| 26 void MultiAnimation::Step(base::TimeTicks time_now) { | 28 void MultiAnimation::Step(base::TimeTicks time_now) { |
| 27 double last_value = current_value_; | 29 double last_value = current_value_; |
| 28 size_t last_index = current_part_index_; | 30 size_t last_index = current_part_index_; |
| 29 | 31 |
| 30 int delta = static_cast<int>((time_now - start_time()).InMilliseconds() % | 32 int delta = static_cast<int>((time_now - start_time()).InMilliseconds() % |
| 31 cycle_time_ms_); | 33 cycle_time_ms_); |
| 32 const Part& part = GetPart(&delta, ¤t_part_index_); | 34 const Part& part = GetPart(&delta, ¤t_part_index_); |
| 33 double percent = static_cast<double>(delta) / | 35 double percent = static_cast<double>(delta + part.start_time_ms) / |
| 34 static_cast<double>(part.time_ms); | 36 static_cast<double>(part.end_time_ms); |
| 37 DCHECK(percent <= 1); |
| 35 current_value_ = Tween::CalculateValue(part.type, percent); | 38 current_value_ = Tween::CalculateValue(part.type, percent); |
| 36 | 39 |
| 37 if ((current_value_ != last_value || current_part_index_ != last_index) && | 40 if ((current_value_ != last_value || current_part_index_ != last_index) && |
| 38 delegate()) { | 41 delegate()) { |
| 39 delegate()->AnimationProgressed(this); | 42 delegate()->AnimationProgressed(this); |
| 40 } | 43 } |
| 41 } | 44 } |
| 42 | 45 |
| 43 const MultiAnimation::Part& MultiAnimation::GetPart(int* time_ms, | 46 const MultiAnimation::Part& MultiAnimation::GetPart(int* time_ms, |
| 44 size_t* part_index) { | 47 size_t* part_index) { |
| 45 DCHECK(*time_ms < cycle_time_ms_); | 48 DCHECK(*time_ms < cycle_time_ms_); |
| 46 | 49 |
| 47 for (size_t i = 0; i < parts_.size(); ++i) { | 50 for (size_t i = 0; i < parts_.size(); ++i) { |
| 48 if (*time_ms < parts_[i].time_ms) { | 51 if (*time_ms < parts_[i].time_ms) { |
| 49 *part_index = i; | 52 *part_index = i; |
| 50 return parts_[i]; | 53 return parts_[i]; |
| 51 } | 54 } |
| 52 | 55 |
| 53 *time_ms -= parts_[i].time_ms; | 56 *time_ms -= parts_[i].time_ms; |
| 54 } | 57 } |
| 55 NOTREACHED(); | 58 NOTREACHED(); |
| 56 *time_ms = 0; | 59 *time_ms = 0; |
| 57 *part_index = 0; | 60 *part_index = 0; |
| 58 return parts_[0]; | 61 return parts_[0]; |
| 59 } | 62 } |
| OLD | NEW |