OLD | NEW |
1 // Copyright (c) 2006-2008 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.h" | 5 #include "app/animation.h" |
6 | 6 |
7 #include "base/message_loop.h" | 7 #include "app/animation_container.h" |
8 #include "gfx/rect.h" | 8 #include "gfx/rect.h" |
9 | 9 |
10 #if defined(OS_WIN) | 10 #if defined(OS_WIN) |
11 #include "base/win_util.h" | 11 #include "base/win_util.h" |
12 #endif | 12 #endif |
13 | 13 |
14 using base::Time; | 14 using base::Time; |
15 using base::TimeDelta; | 15 using base::TimeDelta; |
16 | 16 |
17 Animation::Animation(int frame_rate, | 17 Animation::Animation(int frame_rate, |
(...skipping 12 matching lines...) Expand all Loading... |
30 frame_rate_(frame_rate), | 30 frame_rate_(frame_rate), |
31 timer_interval_(CalculateInterval(frame_rate)), | 31 timer_interval_(CalculateInterval(frame_rate)), |
32 duration_(TimeDelta::FromMilliseconds(duration)), | 32 duration_(TimeDelta::FromMilliseconds(duration)), |
33 state_(0.0), | 33 state_(0.0), |
34 delegate_(delegate) { | 34 delegate_(delegate) { |
35 | 35 |
36 SetDuration(duration); | 36 SetDuration(duration); |
37 } | 37 } |
38 | 38 |
39 Animation::~Animation() { | 39 Animation::~Animation() { |
40 } | 40 if (animating_) |
41 | 41 container_->Stop(this); |
42 void Animation::Reset() { | |
43 start_time_ = Time::Now(); | |
44 } | 42 } |
45 | 43 |
46 double Animation::GetCurrentValue() const { | 44 double Animation::GetCurrentValue() const { |
47 // Default is linear relationship, subclass to adapt. | 45 // Default is linear relationship, subclass to adapt. |
48 return state_; | 46 return state_; |
49 } | 47 } |
50 | 48 |
51 double Animation::CurrentValueBetween(double start, double target) const { | 49 double Animation::CurrentValueBetween(double start, double target) const { |
52 return start + (target - start) * GetCurrentValue(); | 50 return start + (target - start) * GetCurrentValue(); |
53 } | 51 } |
54 | 52 |
55 int Animation::CurrentValueBetween(int start, int target) const { | 53 int Animation::CurrentValueBetween(int start, int target) const { |
56 return static_cast<int>(CurrentValueBetween(static_cast<double>(start), | 54 return static_cast<int>(CurrentValueBetween(static_cast<double>(start), |
57 static_cast<double>(target))); | 55 static_cast<double>(target))); |
58 } | 56 } |
59 | 57 |
60 gfx::Rect Animation::CurrentValueBetween(const gfx::Rect& start_bounds, | 58 gfx::Rect Animation::CurrentValueBetween(const gfx::Rect& start_bounds, |
61 const gfx::Rect& target_bounds) const { | 59 const gfx::Rect& target_bounds) const { |
62 return gfx::Rect(CurrentValueBetween(start_bounds.x(), target_bounds.x()), | 60 return gfx::Rect(CurrentValueBetween(start_bounds.x(), target_bounds.x()), |
63 CurrentValueBetween(start_bounds.y(), target_bounds.y()), | 61 CurrentValueBetween(start_bounds.y(), target_bounds.y()), |
64 CurrentValueBetween(start_bounds.width(), | 62 CurrentValueBetween(start_bounds.width(), |
65 target_bounds.width()), | 63 target_bounds.width()), |
66 CurrentValueBetween(start_bounds.height(), | 64 CurrentValueBetween(start_bounds.height(), |
67 target_bounds.height())); | 65 target_bounds.height())); |
68 } | 66 } |
69 | 67 |
70 void Animation::Start() { | 68 void Animation::Start() { |
71 if (!animating_) { | 69 if (!animating_) { |
72 start_time_ = Time::Now(); | 70 if (!container_.get()) |
73 timer_.Start(timer_interval_, this, &Animation::Run); | 71 container_ = new AnimationContainer(); |
74 | 72 |
75 animating_ = true; | 73 animating_ = true; |
| 74 |
| 75 container_->Start(this); |
| 76 |
76 if (delegate_) | 77 if (delegate_) |
77 delegate_->AnimationStarted(this); | 78 delegate_->AnimationStarted(this); |
78 } | 79 } |
79 } | 80 } |
80 | 81 |
81 void Animation::Stop() { | 82 void Animation::Stop() { |
82 if (animating_) { | 83 if (animating_) { |
83 timer_.Stop(); | 84 animating_ = false; |
84 | 85 |
85 animating_ = false; | 86 // Notify the container first as the delegate may delete us. |
| 87 container_->Stop(this); |
| 88 |
86 if (delegate_) { | 89 if (delegate_) { |
87 if (state_ >= 1.0) | 90 if (state_ >= 1.0) |
88 delegate_->AnimationEnded(this); | 91 delegate_->AnimationEnded(this); |
89 else | 92 else |
90 delegate_->AnimationCanceled(this); | 93 delegate_->AnimationCanceled(this); |
91 } | 94 } |
92 } | 95 } |
93 } | 96 } |
94 | 97 |
95 void Animation::End() { | 98 void Animation::End() { |
96 if (animating_) { | 99 if (animating_) { |
97 timer_.Stop(); | 100 animating_ = false; |
98 | 101 |
99 animating_ = false; | 102 // Notify the container first as the delegate may delete us. |
| 103 container_->Stop(this); |
| 104 |
100 AnimateToState(1.0); | 105 AnimateToState(1.0); |
101 if (delegate_) | 106 if (delegate_) |
102 delegate_->AnimationEnded(this); | 107 delegate_->AnimationEnded(this); |
103 } | 108 } |
104 } | 109 } |
105 | 110 |
106 bool Animation::IsAnimating() const { | 111 bool Animation::IsAnimating() const { |
107 return animating_; | 112 return animating_; |
108 } | 113 } |
109 | 114 |
110 void Animation::SetDuration(int duration) { | 115 void Animation::SetDuration(int duration) { |
111 duration_ = TimeDelta::FromMilliseconds(duration); | 116 duration_ = TimeDelta::FromMilliseconds(duration); |
112 if (duration_ < timer_interval_) | 117 if (duration_ < timer_interval_) |
113 duration_ = timer_interval_; | 118 duration_ = timer_interval_; |
114 start_time_ = Time::Now(); | 119 if (animating_) |
115 } | 120 start_time_ = container_->last_tick_time(); |
116 | |
117 void Animation::Step() { | |
118 TimeDelta elapsed_time = Time::Now() - start_time_; | |
119 state_ = static_cast<double>(elapsed_time.InMicroseconds()) / | |
120 static_cast<double>(duration_.InMicroseconds()); | |
121 | |
122 if (state_ >= 1.0) | |
123 state_ = 1.0; | |
124 | |
125 AnimateToState(state_); | |
126 if (delegate_) | |
127 delegate_->AnimationProgressed(this); | |
128 | |
129 if (state_ == 1.0) | |
130 Stop(); | |
131 } | |
132 | |
133 void Animation::Run() { | |
134 Step(); | |
135 } | |
136 | |
137 TimeDelta Animation::CalculateInterval(int frame_rate) { | |
138 int timer_interval = 1000000 / frame_rate; | |
139 if (timer_interval < 10000) | |
140 timer_interval = 10000; | |
141 return TimeDelta::FromMicroseconds(timer_interval); | |
142 } | 121 } |
143 | 122 |
144 // static | 123 // static |
145 bool Animation::ShouldRenderRichAnimation() { | 124 bool Animation::ShouldRenderRichAnimation() { |
146 #if defined(OS_WIN) | 125 #if defined(OS_WIN) |
147 if (win_util::GetWinVersion() >= win_util::WINVERSION_VISTA) { | 126 if (win_util::GetWinVersion() >= win_util::WINVERSION_VISTA) { |
148 BOOL result; | 127 BOOL result; |
149 // Get "Turn off all unnecessary animations" value. | 128 // Get "Turn off all unnecessary animations" value. |
150 if (::SystemParametersInfo(SPI_GETCLIENTAREAANIMATION, 0, &result, 0)) { | 129 if (::SystemParametersInfo(SPI_GETCLIENTAREAANIMATION, 0, &result, 0)) { |
151 // There seems to be a typo in the MSDN document (as of May 2009): | 130 // There seems to be a typo in the MSDN document (as of May 2009): |
152 // http://msdn.microsoft.com/en-us/library/ms724947(VS.85).aspx | 131 // http://msdn.microsoft.com/en-us/library/ms724947(VS.85).aspx |
153 // The document states that the result is TRUE when animations are | 132 // The document states that the result is TRUE when animations are |
154 // _disabled_, but in fact, it is TRUE when they are _enabled_. | 133 // _disabled_, but in fact, it is TRUE when they are _enabled_. |
155 return !!result; | 134 return !!result; |
156 } | 135 } |
157 } | 136 } |
158 return !::GetSystemMetrics(SM_REMOTESESSION); | 137 return !::GetSystemMetrics(SM_REMOTESESSION); |
159 #endif | 138 #endif |
160 return true; | 139 return true; |
161 } | 140 } |
162 | 141 |
| 142 void Animation::SetContainer(AnimationContainer* container) { |
| 143 if (container == container_.get()) |
| 144 return; |
| 145 |
| 146 if (animating_) |
| 147 container_->Stop(this); |
| 148 |
| 149 if (container) |
| 150 container_ = container; |
| 151 else |
| 152 container_ = new AnimationContainer(); |
| 153 |
| 154 if (animating_) |
| 155 container_->Start(this); |
| 156 } |
| 157 |
| 158 void Animation::Step(base::TimeTicks time_now) { |
| 159 TimeDelta elapsed_time = time_now - start_time_; |
| 160 state_ = static_cast<double>(elapsed_time.InMicroseconds()) / |
| 161 static_cast<double>(duration_.InMicroseconds()); |
| 162 if (state_ >= 1.0) |
| 163 state_ = 1.0; |
| 164 |
| 165 AnimateToState(state_); |
| 166 |
| 167 if (delegate_) |
| 168 delegate_->AnimationProgressed(this); |
| 169 |
| 170 if (state_ == 1.0) |
| 171 Stop(); |
| 172 } |
| 173 |
| 174 TimeDelta Animation::CalculateInterval(int frame_rate) { |
| 175 int timer_interval = 1000000 / frame_rate; |
| 176 if (timer_interval < 10000) |
| 177 timer_interval = 10000; |
| 178 return TimeDelta::FromMicroseconds(timer_interval); |
| 179 } |
OLD | NEW |