1 // Copyright (c) 2011 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.
5 #include "ui/gfx/animation/multi_animation.h"
7 #include "base/logging.h"
8 #include "ui/gfx/animation/animation_delegate.h"
12 // Default interval, in ms.
13 static const int kDefaultTimerInterval
= 20;
15 static int TotalTime(const MultiAnimation::Parts
& parts
) {
17 for (size_t i
= 0; i
< parts
.size(); ++i
) {
18 DCHECK(parts
[i
].end_time_ms
- parts
[i
].start_time_ms
>= parts
[i
].time_ms
);
19 time_ms
+= parts
[i
].time_ms
;
24 MultiAnimation::MultiAnimation(const Parts
& parts
,
25 base::TimeDelta timer_interval
)
26 : Animation(timer_interval
),
28 cycle_time_ms_(TotalTime(parts
)),
30 current_part_index_(0),
32 DCHECK(!parts_
.empty());
35 MultiAnimation::~MultiAnimation() {}
38 base::TimeDelta
MultiAnimation::GetDefaultTimerInterval() {
39 return base::TimeDelta::FromMilliseconds(kDefaultTimerInterval
);
42 double MultiAnimation::GetCurrentValue() const {
43 return current_value_
;
46 void MultiAnimation::Step(base::TimeTicks time_now
) {
47 double last_value
= current_value_
;
48 size_t last_index
= current_part_index_
;
50 int delta
= static_cast<int>((time_now
- start_time()).InMilliseconds());
51 if (delta
>= cycle_time_ms_
&& !continuous_
) {
52 current_part_index_
= parts_
.size() - 1;
53 current_value_
= Tween::CalculateValue(parts_
[current_part_index_
].type
, 1);
57 delta
%= cycle_time_ms_
;
58 const Part
& part
= GetPart(&delta
, ¤t_part_index_
);
59 double percent
= static_cast<double>(delta
+ part
.start_time_ms
) /
60 static_cast<double>(part
.end_time_ms
);
62 current_value_
= Tween::CalculateValue(part
.type
, percent
);
64 if ((current_value_
!= last_value
|| current_part_index_
!= last_index
) &&
66 delegate()->AnimationProgressed(this);
70 void MultiAnimation::SetStartTime(base::TimeTicks start_time
) {
71 Animation::SetStartTime(start_time
);
73 current_part_index_
= 0;
76 const MultiAnimation::Part
& MultiAnimation::GetPart(int* time_ms
,
78 DCHECK(*time_ms
< cycle_time_ms_
);
80 for (size_t i
= 0; i
< parts_
.size(); ++i
) {
81 if (*time_ms
< parts_
[i
].time_ms
) {
86 *time_ms
-= parts_
[i
].time_ms
;