From dbba0c455f8b3f139fbfd9634d0bfba268085a33 Mon Sep 17 00:00:00 2001 From: "sky@chromium.org" Date: Sun, 7 Feb 2010 17:23:14 +0000 Subject: Adds Animation::CurrentValueBetween and gets rid of a bunch of duplicate code. BUG=none TEST=none Review URL: http://codereview.chromium.org/570055 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@38330 0039d316-1c4b-4281-b951-d872f2087c98 --- app/animation.cc | 20 ++++++++++++++++++++ app/animation.h | 14 ++++++++++++++ 2 files changed, 34 insertions(+) (limited to 'app') diff --git a/app/animation.cc b/app/animation.cc index 94c99c2..b341d2a 100644 --- a/app/animation.cc +++ b/app/animation.cc @@ -4,6 +4,7 @@ #include "app/animation.h" +#include "base/gfx/rect.h" #include "base/message_loop.h" #if defined(OS_WIN) @@ -47,6 +48,25 @@ double Animation::GetCurrentValue() const { return state_; } +double Animation::CurrentValueBetween(double start, double target) const { + return start + (target - start) * GetCurrentValue(); +} + +int Animation::CurrentValueBetween(int start, int target) const { + return static_cast(CurrentValueBetween(static_cast(start), + static_cast(target))); +} + +gfx::Rect Animation::CurrentValueBetween(const gfx::Rect& start_bounds, + const gfx::Rect& target_bounds) const { + return gfx::Rect(CurrentValueBetween(start_bounds.x(), target_bounds.x()), + CurrentValueBetween(start_bounds.y(), target_bounds.y()), + CurrentValueBetween(start_bounds.width(), + target_bounds.width()), + CurrentValueBetween(start_bounds.height(), + target_bounds.height())); +} + void Animation::Start() { if (!animating_) { start_time_ = Time::Now(); diff --git a/app/animation.h b/app/animation.h index bd38fb4..bfa7fd0 100644 --- a/app/animation.h +++ b/app/animation.h @@ -11,6 +11,10 @@ class Animation; +namespace gfx { +class Rect; +} + // AnimationDelegate // // Implement this interface when you want to receive notifications about the @@ -77,6 +81,13 @@ class Animation { // however subclasses can override this to provide others. virtual double GetCurrentValue() const; + // Convenience for returning a value between |start| and |target| based on + // the current value. This is (target - start) * GetCurrentValue() + start. + double CurrentValueBetween(double start, double target) const; + int CurrentValueBetween(int start, int target) const; + gfx::Rect CurrentValueBetween(const gfx::Rect& start_bounds, + const gfx::Rect& target_bounds) const; + // Start the animation. void Start(); @@ -98,6 +109,9 @@ class Animation { // to give guidance for heavy animations such as "start download" arrow. static bool ShouldRenderRichAnimation(); + // Sets the delegate. + void set_delegate(AnimationDelegate* delegate) { delegate_ = delegate; } + protected: // Overriddable, called by Run. virtual void Step(); -- cgit v1.1