OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // Inspired by NSAnimation | 4 // Inspired by NSAnimation |
5 | 5 |
6 #ifndef APP_ANIMATION_H_ | 6 #ifndef APP_ANIMATION_H_ |
7 #define APP_ANIMATION_H_ | 7 #define APP_ANIMATION_H_ |
8 | 8 |
| 9 #include "base/ref_counted.h" |
9 #include "base/time.h" | 10 #include "base/time.h" |
10 #include "base/timer.h" | |
11 | 11 |
12 class Animation; | 12 class Animation; |
| 13 class AnimationContainer; |
13 | 14 |
14 namespace gfx { | 15 namespace gfx { |
15 class Rect; | 16 class Rect; |
16 } | 17 } |
17 | 18 |
18 // AnimationDelegate | 19 // AnimationDelegate |
19 // | 20 // |
20 // Implement this interface when you want to receive notifications about the | 21 // Implement this interface when you want to receive notifications about the |
21 // state of an animation. | 22 // state of an animation. |
22 // | 23 // |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 // Caller must make sure to call SetDuration() if they use this | 63 // Caller must make sure to call SetDuration() if they use this |
63 // constructor; it is preferable to use the full one, but sometimes | 64 // constructor; it is preferable to use the full one, but sometimes |
64 // duration can change between calls to Start() and we need to | 65 // duration can change between calls to Start() and we need to |
65 // expose this interface. | 66 // expose this interface. |
66 Animation(int frame_rate, AnimationDelegate* delegate); | 67 Animation(int frame_rate, AnimationDelegate* delegate); |
67 | 68 |
68 // Initializes all fields. | 69 // Initializes all fields. |
69 Animation(int duration, int frame_rate, AnimationDelegate* delegate); | 70 Animation(int duration, int frame_rate, AnimationDelegate* delegate); |
70 virtual ~Animation(); | 71 virtual ~Animation(); |
71 | 72 |
72 // Reset state so that the animation can be started again. | |
73 virtual void Reset(); | |
74 | |
75 // Called when the animation progresses. Subclasses override this to | 73 // Called when the animation progresses. Subclasses override this to |
76 // efficiently update their state. | 74 // efficiently update their state. |
77 virtual void AnimateToState(double state) = 0; | 75 virtual void AnimateToState(double state) = 0; |
78 | 76 |
79 // Gets the value for the current state, according to the animation | 77 // Gets the value for the current state, according to the animation |
80 // curve in use. This class provides only for a linear relationship, | 78 // curve in use. This class provides only for a linear relationship, |
81 // however subclasses can override this to provide others. | 79 // however subclasses can override this to provide others. |
82 virtual double GetCurrentValue() const; | 80 virtual double GetCurrentValue() const; |
83 | 81 |
84 // Convenience for returning a value between |start| and |target| based on | 82 // Convenience for returning a value between |start| and |target| based on |
(...skipping 20 matching lines...) Expand all Loading... |
105 void SetDuration(int duration); | 103 void SetDuration(int duration); |
106 | 104 |
107 // Returns true if rich animations should be rendered. | 105 // Returns true if rich animations should be rendered. |
108 // Looks at session type (e.g. remote desktop) and accessibility settings | 106 // Looks at session type (e.g. remote desktop) and accessibility settings |
109 // to give guidance for heavy animations such as "start download" arrow. | 107 // to give guidance for heavy animations such as "start download" arrow. |
110 static bool ShouldRenderRichAnimation(); | 108 static bool ShouldRenderRichAnimation(); |
111 | 109 |
112 // Sets the delegate. | 110 // Sets the delegate. |
113 void set_delegate(AnimationDelegate* delegate) { delegate_ = delegate; } | 111 void set_delegate(AnimationDelegate* delegate) { delegate_ = delegate; } |
114 | 112 |
| 113 // Sets the container used to manage the timer. A value of NULL results in |
| 114 // creating a new AnimationContainer. |
| 115 void SetContainer(AnimationContainer* container); |
| 116 |
| 117 base::TimeDelta timer_interval() const { return timer_interval_; } |
| 118 |
115 protected: | 119 protected: |
116 // Overriddable, called by Run. | 120 // Invoked by the AnimationContainer when the animation is running to advance |
117 virtual void Step(); | 121 // the animation. Use |time_now| rather than Time::Now to avoid multiple |
| 122 // animations running at the same time diverging. |
| 123 virtual void Step(base::TimeTicks time_now); |
118 | 124 |
119 // Calculates the timer interval from the constructor list. | 125 // Calculates the timer interval from the constructor list. |
120 base::TimeDelta CalculateInterval(int frame_rate); | 126 base::TimeDelta CalculateInterval(int frame_rate); |
121 | 127 |
| 128 private: |
| 129 friend class AnimationContainer; |
| 130 |
| 131 // Invoked from AnimationContainer when started. |
| 132 void set_start_time(base::TimeTicks start_time) { start_time_ = start_time; } |
| 133 |
122 // Whether or not we are currently animating. | 134 // Whether or not we are currently animating. |
123 bool animating_; | 135 bool animating_; |
124 | 136 |
125 int frame_rate_; | 137 int frame_rate_; |
126 base::TimeDelta timer_interval_; | 138 base::TimeDelta timer_interval_; |
127 base::TimeDelta duration_; | 139 base::TimeDelta duration_; |
128 | 140 |
129 // Current state, on a scale from 0.0 to 1.0. | 141 // Current state, on a scale from 0.0 to 1.0. |
130 double state_; | 142 double state_; |
131 | 143 |
132 base::Time start_time_; | 144 base::TimeTicks start_time_; |
133 | 145 |
134 AnimationDelegate* delegate_; | 146 AnimationDelegate* delegate_; |
135 | 147 |
136 base::RepeatingTimer<Animation> timer_; | 148 scoped_refptr<AnimationContainer> container_; |
137 | |
138 private: | |
139 // Called when the animation's timer expires, calls Step. | |
140 void Run(); | |
141 | 149 |
142 DISALLOW_COPY_AND_ASSIGN(Animation); | 150 DISALLOW_COPY_AND_ASSIGN(Animation); |
143 }; | 151 }; |
144 | 152 |
145 #endif // APP_ANIMATION_H_ | 153 #endif // APP_ANIMATION_H_ |
OLD | NEW |