OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "app/multi_animation.h" | |
6 | |
7 #include "app/animation_delegate.h" | |
8 #include "base/logging.h" | |
9 | |
10 // Default interval, in ms. | |
11 static const int kDefaultInterval = 20; | |
12 | |
13 static int TotalTime(const MultiAnimation::Parts& parts) { | |
14 int time_ms = 0; | |
15 for (size_t i = 0; i < parts.size(); ++i) { | |
16 DCHECK(parts[i].end_time_ms - parts[i].start_time_ms >= parts[i].time_ms); | |
17 time_ms += parts[i].time_ms; | |
18 } | |
19 return time_ms; | |
20 } | |
21 | |
22 MultiAnimation::MultiAnimation(const Parts& parts) | |
23 : Animation(base::TimeDelta::FromMilliseconds(kDefaultInterval)), | |
24 parts_(parts), | |
25 cycle_time_ms_(TotalTime(parts)), | |
26 current_value_(0), | |
27 current_part_index_(0), | |
28 continuous_(true) { | |
29 DCHECK(!parts_.empty()); | |
30 } | |
31 | |
32 MultiAnimation::~MultiAnimation() {} | |
33 | |
34 double MultiAnimation::GetCurrentValue() const { | |
35 return current_value_; | |
36 } | |
37 | |
38 void MultiAnimation::Step(base::TimeTicks time_now) { | |
39 double last_value = current_value_; | |
40 size_t last_index = current_part_index_; | |
41 | |
42 int delta = static_cast<int>((time_now - start_time()).InMilliseconds()); | |
43 if (delta >= cycle_time_ms_ && !continuous_) { | |
44 current_part_index_ = parts_.size() - 1; | |
45 current_value_ = Tween::CalculateValue(parts_[current_part_index_].type, 1); | |
46 Stop(); | |
47 return; | |
48 } | |
49 delta %= cycle_time_ms_; | |
50 const Part& part = GetPart(&delta, ¤t_part_index_); | |
51 double percent = static_cast<double>(delta + part.start_time_ms) / | |
52 static_cast<double>(part.end_time_ms); | |
53 DCHECK(percent <= 1); | |
54 current_value_ = Tween::CalculateValue(part.type, percent); | |
55 | |
56 if ((current_value_ != last_value || current_part_index_ != last_index) && | |
57 delegate()) { | |
58 delegate()->AnimationProgressed(this); | |
59 } | |
60 } | |
61 | |
62 void MultiAnimation::SetStartTime(base::TimeTicks start_time) { | |
63 Animation::SetStartTime(start_time); | |
64 current_value_ = 0; | |
65 current_part_index_ = 0; | |
66 } | |
67 | |
68 const MultiAnimation::Part& MultiAnimation::GetPart(int* time_ms, | |
69 size_t* part_index) { | |
70 DCHECK(*time_ms < cycle_time_ms_); | |
71 | |
72 for (size_t i = 0; i < parts_.size(); ++i) { | |
73 if (*time_ms < parts_[i].time_ms) { | |
74 *part_index = i; | |
75 return parts_[i]; | |
76 } | |
77 | |
78 *time_ms -= parts_[i].time_ms; | |
79 } | |
80 NOTREACHED(); | |
81 *time_ms = 0; | |
82 *part_index = 0; | |
83 return parts_[0]; | |
84 } | |
OLD | NEW |