1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef UI_GFX_ANIMATION_LINEAR_ANIMATION_H_
6 #define UI_GFX_ANIMATION_LINEAR_ANIMATION_H_
7 
8 #include "base/macros.h"
9 #include "base/time/time.h"
10 #include "ui/gfx/animation/animation.h"
11 
12 namespace gfx {
13 
14 class AnimationDelegate;
15 
16 // Linear time bounded animation. As the animation progresses AnimateToState is
17 // invoked.
18 class ANIMATION_EXPORT LinearAnimation : public Animation {
19  public:
20   // Default frame rate (hz).
21   static const int kDefaultFrameRate;
22 
23   // Initializes everything except the duration.
24   //
25   // Caller must make sure to call SetDuration() if they use this
26   // constructor; it is preferable to use the full one, but sometimes
27   // duration can change between calls to Start() and we need to
28   // expose this interface.
29   explicit LinearAnimation(AnimationDelegate* delegate,
30                            int frame_rate = kDefaultFrameRate);
31 
32   // Initializes all fields.
33   LinearAnimation(base::TimeDelta duration,
34                   int frame_rate,
35                   AnimationDelegate* delegate);
36 
37   // Gets the value for the current state, according to the animation curve in
38   // use. This class provides only for a linear relationship, however subclasses
39   // can override this to provide others.
40   double GetCurrentValue() const override;
41 
42   // Change the current state of the animation to |new_value|.
43   void SetCurrentValue(double new_value);
44 
45   // Skip to the end of the current animation.
46   void End();
47 
48   // Changes the length of the animation. This resets the current
49   // state of the animation to the beginning. This value will be multiplied by
50   // the currently set scale factor.
51   void SetDuration(base::TimeDelta duration);
52 
53  protected:
54   // Called when the animation progresses. Subclasses override this to
55   // efficiently update their state.
AnimateToState(double state)56   virtual void AnimateToState(double state) {}
57 
58   // Invoked by the AnimationContainer when the animation is running to advance
59   // the animation. Use |time_now| rather than Time::Now to avoid multiple
60   // animations running at the same time diverging.
61   void Step(base::TimeTicks time_now) override;
62 
63   // Overriden to initialize state.
64   void AnimationStarted() override;
65 
66   // Overriden to advance to the end (if End was invoked).
67   void AnimationStopped() override;
68 
69   // Overriden to return true if state is not 1.
70   bool ShouldSendCanceledFromStop() override;
71 
duration()72   base::TimeDelta duration() const { return duration_; }
73 
74  private:
75   base::TimeDelta duration_;
76 
77   // Current state, on a scale from 0.0 to 1.0.
78   double state_;
79 
80   // If true, we're in end. This is used to determine if the animation should
81   // be advanced to the end from AnimationStopped.
82   bool in_end_;
83 
84   DISALLOW_COPY_AND_ASSIGN(LinearAnimation);
85 };
86 
87 }  // namespace gfx
88 
89 #endif  // APP_LINEAR_ANIMATION_H_
90