diff options
Diffstat (limited to 'app/animation.cc')
-rw-r--r-- | app/animation.cc | 36 |
1 files changed, 15 insertions, 21 deletions
diff --git a/app/animation.cc b/app/animation.cc index 1ef905d..94c99c2 100644 --- a/app/animation.cc +++ b/app/animation.cc @@ -10,6 +10,7 @@ #include "base/win_util.h" #endif +using base::Time; using base::TimeDelta; Animation::Animation(int frame_rate, @@ -17,9 +18,6 @@ Animation::Animation(int frame_rate, : animating_(false), frame_rate_(frame_rate), timer_interval_(CalculateInterval(frame_rate)), - duration_(0), - iteration_count_(0), - current_iteration_(0), state_(0.0), delegate_(delegate) { } @@ -30,9 +28,7 @@ Animation::Animation(int duration, : animating_(false), frame_rate_(frame_rate), timer_interval_(CalculateInterval(frame_rate)), - duration_(0), - iteration_count_(0), - current_iteration_(0), + duration_(TimeDelta::FromMilliseconds(duration)), state_(0.0), delegate_(delegate) { @@ -43,7 +39,7 @@ Animation::~Animation() { } void Animation::Reset() { - current_iteration_ = 0; + start_time_ = Time::Now(); } double Animation::GetCurrentValue() const { @@ -53,8 +49,8 @@ double Animation::GetCurrentValue() const { void Animation::Start() { if (!animating_) { - timer_.Start(TimeDelta::FromMilliseconds(timer_interval_), this, - &Animation::Run); + start_time_ = Time::Now(); + timer_.Start(timer_interval_, this, &Animation::Run); animating_ = true; if (delegate_) @@ -92,18 +88,16 @@ bool Animation::IsAnimating() const { } void Animation::SetDuration(int duration) { - duration_ = duration; + duration_ = TimeDelta::FromMilliseconds(duration); if (duration_ < timer_interval_) duration_ = timer_interval_; - iteration_count_ = duration_ / timer_interval_; - - // Changing the number of iterations forces us to reset the - // animation to the first iteration. - current_iteration_ = 0; + start_time_ = Time::Now(); } void Animation::Step() { - state_ = static_cast<double>(++current_iteration_) / iteration_count_; + TimeDelta elapsed_time = Time::Now() - start_time_; + state_ = static_cast<double>(elapsed_time.InMicroseconds()) / + static_cast<double>(duration_.InMicroseconds()); if (state_ >= 1.0) state_ = 1.0; @@ -120,11 +114,11 @@ void Animation::Run() { Step(); } -int Animation::CalculateInterval(int frame_rate) { - int timer_interval = 1000 / frame_rate; - if (timer_interval < 10) - timer_interval = 10; - return timer_interval; +TimeDelta Animation::CalculateInterval(int frame_rate) { + int timer_interval = 1000000 / frame_rate; + if (timer_interval < 10000) + timer_interval = 10000; + return TimeDelta::FromMicroseconds(timer_interval); } // static |