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_LINEAR_ANIMATION_H_ 6d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#define UI_GFX_ANIMATION_LINEAR_ANIMATION_H_ 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 8eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/time/time.h" 9d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "ui/gfx/animation/animation.h" 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 11d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)namespace gfx { 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class AnimationDelegate; 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Linear time bounded animation. As the animation progresses AnimateToState is 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// invoked. 17d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)class GFX_EXPORT LinearAnimation : public Animation { 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Initializes everything except the duration. 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Caller must make sure to call SetDuration() if they use this 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // constructor; it is preferable to use the full one, but sometimes 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // duration can change between calls to Start() and we need to 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // expose this interface. 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) LinearAnimation(int frame_rate, AnimationDelegate* delegate); 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Initializes all fields. 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) LinearAnimation(int duration, int frame_rate, AnimationDelegate* delegate); 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Gets the value for the current state, according to the animation curve in 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // use. This class provides only for a linear relationship, however subclasses 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // can override this to provide others. 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual double GetCurrentValue() const OVERRIDE; 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Change the current state of the animation to |new_value|. 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void SetCurrentValue(double new_value); 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Skip to the end of the current animation. 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void End(); 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Changes the length of the animation. This resets the current 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // state of the animation to the beginning. 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void SetDuration(int duration); 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected: 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Called when the animation progresses. Subclasses override this to 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // efficiently update their state. 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void AnimateToState(double state) {} 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Invoked by the AnimationContainer when the animation is running to advance 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // the animation. Use |time_now| rather than Time::Now to avoid multiple 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // animations running at the same time diverging. 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void Step(base::TimeTicks time_now) OVERRIDE; 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Overriden to initialize state. 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void AnimationStarted() OVERRIDE; 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Overriden to advance to the end (if End was invoked). 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void AnimationStopped() OVERRIDE; 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Overriden to return true if state is not 1. 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool ShouldSendCanceledFromStop() OVERRIDE; 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::TimeDelta duration_; 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Current state, on a scale from 0.0 to 1.0. 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) double state_; 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // If true, we're in end. This is used to determine if the animation should 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // be advanced to the end from AnimationStopped. 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool in_end_; 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(LinearAnimation); 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 77d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)} // namespace gfx 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // APP_LINEAR_ANIMATION_H_ 80