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/linear_animation.h" | |
6 | |
7 #include <math.h> | |
8 | |
9 #include "app/animation_container.h" | |
10 #include "app/animation_delegate.h" | |
11 | |
12 using base::Time; | |
13 using base::TimeDelta; | |
14 | |
15 static TimeDelta CalculateInterval(int frame_rate) { | |
16 int timer_interval = 1000000 / frame_rate; | |
17 if (timer_interval < 10000) | |
18 timer_interval = 10000; | |
19 return TimeDelta::FromMicroseconds(timer_interval); | |
20 } | |
21 | |
22 LinearAnimation::LinearAnimation(int frame_rate, | |
23 AnimationDelegate* delegate) | |
24 : Animation(CalculateInterval(frame_rate)), | |
25 state_(0.0), | |
26 in_end_(false) { | |
27 set_delegate(delegate); | |
28 } | |
29 | |
30 LinearAnimation::LinearAnimation(int duration, | |
31 int frame_rate, | |
32 AnimationDelegate* delegate) | |
33 : Animation(CalculateInterval(frame_rate)), | |
34 duration_(TimeDelta::FromMilliseconds(duration)), | |
35 state_(0.0), | |
36 in_end_(false) { | |
37 set_delegate(delegate); | |
38 SetDuration(duration); | |
39 } | |
40 | |
41 double LinearAnimation::GetCurrentValue() const { | |
42 // Default is linear relationship, subclass to adapt. | |
43 return state_; | |
44 } | |
45 | |
46 void LinearAnimation::End() { | |
47 if (!is_animating()) | |
48 return; | |
49 | |
50 // NOTE: We don't use AutoReset here as Stop may end up deleting us (by way | |
51 // of the delegate). | |
52 in_end_ = true; | |
53 Stop(); | |
54 } | |
55 | |
56 void LinearAnimation::SetDuration(int duration) { | |
57 duration_ = TimeDelta::FromMilliseconds(duration); | |
58 if (duration_ < timer_interval()) | |
59 duration_ = timer_interval(); | |
60 if (is_animating()) | |
61 SetStartTime(container()->last_tick_time()); | |
62 } | |
63 | |
64 void LinearAnimation::Step(base::TimeTicks time_now) { | |
65 TimeDelta elapsed_time = time_now - start_time(); | |
66 state_ = static_cast<double>(elapsed_time.InMicroseconds()) / | |
67 static_cast<double>(duration_.InMicroseconds()); | |
68 if (state_ >= 1.0) | |
69 state_ = 1.0; | |
70 | |
71 AnimateToState(state_); | |
72 | |
73 if (delegate()) | |
74 delegate()->AnimationProgressed(this); | |
75 | |
76 if (state_ == 1.0) | |
77 Stop(); | |
78 } | |
79 | |
80 void LinearAnimation::AnimationStopped() { | |
81 if (!in_end_) | |
82 return; | |
83 | |
84 in_end_ = false; | |
85 // Set state_ to ensure we send ended to delegate and not canceled. | |
86 state_ = 1; | |
87 AnimateToState(1.0); | |
88 } | |
89 | |
90 bool LinearAnimation::ShouldSendCanceledFromStop() { | |
91 return state_ != 1; | |
92 } | |
OLD | NEW |