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