15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2011 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 5d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#ifndef UI_GFX_ANIMATION_ANIMATION_H_ 6d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#define UI_GFX_ANIMATION_ANIMATION_H_ 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/compiler_specific.h" 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/ref_counted.h" 10eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/time/time.h" 11d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "ui/gfx/animation/animation_container_element.h" 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace gfx { 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Rect; 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 17d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)namespace gfx { 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class AnimationContainer; 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class AnimationDelegate; 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Base class used in implementing animations. You only need use this class if 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// you're implementing a new animation type, otherwise you'll likely want one of 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// LinearAnimation, SlideAnimation, ThrobAnimation or MultiAnimation. 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// To subclass override Step, which is invoked as the animation progresses and 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// GetCurrentValue() to return the value appropriate to the animation. 28d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)class GFX_EXPORT Animation : public AnimationContainerElement { 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) explicit Animation(base::TimeDelta timer_interval); 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual ~Animation(); 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Starts the animation. Does nothing if the animation is already running. 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void Start(); 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Stops the animation. Does nothing if the animation is not running. 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void Stop(); 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Gets the value for the current state, according to the animation 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // curve in use. 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual double GetCurrentValue() const = 0; 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Convenience for returning a value between |start| and |target| based on 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // the current value. This is (target - start) * GetCurrentValue() + start. 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) double CurrentValueBetween(double start, double target) const; 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int CurrentValueBetween(int start, int target) const; 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) gfx::Rect CurrentValueBetween(const gfx::Rect& start_bounds, 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const gfx::Rect& target_bounds) const; 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Sets the delegate. 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void set_delegate(AnimationDelegate* delegate) { delegate_ = delegate; } 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Sets the container used to manage the timer. A value of NULL results in 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // creating a new AnimationContainer. 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void SetContainer(AnimationContainer* container); 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool is_animating() const { return is_animating_; } 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::TimeDelta timer_interval() const { return timer_interval_; } 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns true if rich animations should be rendered. 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Looks at session type (e.g. remote desktop) and accessibility settings 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // to give guidance for heavy animations such as "start download" arrow. 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static bool ShouldRenderRichAnimation(); 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected: 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Invoked from Start to allow subclasses to prepare for the animation. 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void AnimationStarted() {} 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Invoked from Stop after we're removed from the container but before the 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // delegate has been invoked. 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void AnimationStopped() {} 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Invoked from stop to determine if cancel should be invoked. If this returns 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // true the delegate is notified the animation was canceled, otherwise the 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // delegate is notified the animation stopped. 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool ShouldSendCanceledFromStop(); 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AnimationContainer* container() { return container_.get(); } 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::TimeTicks start_time() const { return start_time_; } 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AnimationDelegate* delegate() { return delegate_; } 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // AnimationContainer::Element overrides 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void SetStartTime(base::TimeTicks start_time) OVERRIDE; 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void Step(base::TimeTicks time_now) = 0; 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual base::TimeDelta GetTimerInterval() const OVERRIDE; 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Interval for the animation. 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const base::TimeDelta timer_interval_; 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // If true we're running. 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool is_animating_; 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Our delegate; may be null. 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AnimationDelegate* delegate_; 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Container we're in. If non-null we're animating. 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_refptr<AnimationContainer> container_; 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Time we started at. 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::TimeTicks start_time_; 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(Animation); 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 107d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)} // namespace gfx 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 109d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#endif // UI_GFX_ANIMATION_ANIMATION_H_ 110