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/animation_container.h" |
| 6 |
| 7 #include "app/animation.h" |
| 8 |
| 9 using base::TimeDelta; |
| 10 using base::TimeTicks; |
| 11 |
| 12 AnimationContainer::AnimationContainer() |
| 13 : last_tick_time_(TimeTicks::Now()), |
| 14 observer_(NULL) { |
| 15 } |
| 16 |
| 17 AnimationContainer::~AnimationContainer() { |
| 18 // The animations own us and stop themselves before being deleted. If |
| 19 // animations_ is not empty, something is wrong. |
| 20 DCHECK(animations_.empty()); |
| 21 } |
| 22 |
| 23 void AnimationContainer::Start(Animation* animation) { |
| 24 DCHECK(animations_.count(animation) == 0); // Start should only be invoked |
| 25 // if the animation isn't running. |
| 26 |
| 27 if (animations_.empty()) { |
| 28 last_tick_time_ = TimeTicks::Now(); |
| 29 SetMinTimerInterval(animation->timer_interval()); |
| 30 } else if (animation->timer_interval() < min_timer_interval_) { |
| 31 SetMinTimerInterval(animation->timer_interval()); |
| 32 } |
| 33 |
| 34 animation->set_start_time(last_tick_time_); |
| 35 animations_.insert(animation); |
| 36 } |
| 37 |
| 38 void AnimationContainer::Stop(Animation* animation) { |
| 39 DCHECK(animations_.count(animation) > 0); // The animation must be running. |
| 40 |
| 41 animations_.erase(animation); |
| 42 |
| 43 if (animations_.empty()) { |
| 44 timer_.Stop(); |
| 45 if (observer_) |
| 46 observer_->AnimationContainerEmpty(this); |
| 47 } else { |
| 48 TimeDelta min_timer_interval = GetMinInterval(); |
| 49 if (min_timer_interval > min_timer_interval_) |
| 50 SetMinTimerInterval(min_timer_interval); |
| 51 } |
| 52 } |
| 53 |
| 54 void AnimationContainer::Run() { |
| 55 TimeTicks current_time = TimeTicks::Now(); |
| 56 |
| 57 last_tick_time_ = current_time; |
| 58 |
| 59 // Make a copy of the animations to iterate over so that if any animations |
| 60 // are removed as part of invoking Step there aren't any problems. |
| 61 Animations animations = animations_; |
| 62 |
| 63 for (Animations::const_iterator i = animations.begin(); |
| 64 i != animations.end(); ++i) { |
| 65 // Make sure the animation is still valid. |
| 66 if (animations_.find(*i) != animations_.end()) |
| 67 (*i)->Step(current_time); |
| 68 } |
| 69 |
| 70 if (observer_) |
| 71 observer_->AnimationContainerProgressed(this); |
| 72 } |
| 73 |
| 74 void AnimationContainer::SetMinTimerInterval(base::TimeDelta delta) { |
| 75 // This doesn't take into account how far along current animation is, but that |
| 76 // shouldn't be a problem for uses of Animation/AnimationContainer. |
| 77 timer_.Stop(); |
| 78 min_timer_interval_ = delta; |
| 79 timer_.Start(min_timer_interval_, this, &AnimationContainer::Run); |
| 80 } |
| 81 |
| 82 TimeDelta AnimationContainer::GetMinInterval() { |
| 83 DCHECK(!animations_.empty()); |
| 84 |
| 85 TimeDelta min; |
| 86 Animations::const_iterator i = animations_.begin(); |
| 87 min = (*i)->timer_interval(); |
| 88 for (++i; i != animations_.end(); ++i) { |
| 89 if ((*i)->timer_interval() < min) |
| 90 min = (*i)->timer_interval(); |
| 91 } |
| 92 return min; |
| 93 } |
OLD | NEW |