diff options
-rw-r--r-- | app/animation.cc | 36 | ||||
-rw-r--r-- | app/animation.h | 13 |
2 files changed, 22 insertions, 27 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 diff --git a/app/animation.h b/app/animation.h index 811c038..e6877b6 100644 --- a/app/animation.h +++ b/app/animation.h @@ -6,6 +6,7 @@ #ifndef APP_ANIMATION_H_ #define APP_ANIMATION_H_ +#include "base/time.h" #include "base/timer.h" class Animation; @@ -99,20 +100,20 @@ class Animation { virtual void Step(); // Calculates the timer interval from the constructor list. - int CalculateInterval(int frame_rate); + base::TimeDelta CalculateInterval(int frame_rate); // Whether or not we are currently animating. bool animating_; int frame_rate_; - int timer_interval_; - int duration_; + base::TimeDelta timer_interval_; + base::TimeDelta duration_; - // For determining state. - int iteration_count_; - int current_iteration_; + // Current state, on a scale from 0.0 to 1.0. double state_; + base::Time start_time_; + AnimationDelegate* delegate_; base::RepeatingTimer<Animation> timer_; |