Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / gfx / animation / multi_animation.cc
blob4ecc73c805a7059a189ddba4840393c9720c2a4c
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"
10 namespace gfx {
12 // Default interval, in ms.
13 static const int kDefaultTimerInterval = 20;
15 static int TotalTime(const MultiAnimation::Parts& parts) {
16 int time_ms = 0;
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;
21 return time_ms;
24 MultiAnimation::MultiAnimation(const Parts& parts,
25 base::TimeDelta timer_interval)
26 : Animation(timer_interval),
27 parts_(parts),
28 cycle_time_ms_(TotalTime(parts)),
29 current_value_(0),
30 current_part_index_(0),
31 continuous_(true) {
32 DCHECK(!parts_.empty());
35 MultiAnimation::~MultiAnimation() {}
37 // static.
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);
54 Stop();
55 return;
57 delta %= cycle_time_ms_;
58 const Part& part = GetPart(&delta, &current_part_index_);
59 double percent = static_cast<double>(delta + part.start_time_ms) /
60 static_cast<double>(part.end_time_ms);
61 DCHECK(percent <= 1);
62 current_value_ = Tween::CalculateValue(part.type, percent);
64 if ((current_value_ != last_value || current_part_index_ != last_index) &&
65 delegate()) {
66 delegate()->AnimationProgressed(this);
70 void MultiAnimation::SetStartTime(base::TimeTicks start_time) {
71 Animation::SetStartTime(start_time);
72 current_value_ = 0;
73 current_part_index_ = 0;
76 const MultiAnimation::Part& MultiAnimation::GetPart(int* time_ms,
77 size_t* part_index) {
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) {
82 *part_index = i;
83 return parts_[i];
86 *time_ms -= parts_[i].time_ms;
88 NOTREACHED();
89 *time_ms = 0;
90 *part_index = 0;
91 return parts_[0];
94 } // namespace gfx