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