Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(35)

Side by Side Diff: app/animation.h

Issue 1961001: Refactors animation to allow for cleaner subclassing. I'm doing this (Closed)
Patch Set: Incorporated review feedback Created 10 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | app/animation.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 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
5 4
6 #ifndef APP_ANIMATION_H_ 5 #ifndef APP_ANIMATION_H_
7 #define APP_ANIMATION_H_ 6 #define APP_ANIMATION_H_
8 7
8 #include "app/animation_container.h"
9 #include "base/ref_counted.h" 9 #include "base/ref_counted.h"
10 #include "base/time.h" 10 #include "base/time.h"
11 11
12 class Animation;
13 class AnimationContainer;
14
15 namespace gfx { 12 namespace gfx {
16 class Rect; 13 class Rect;
17 } 14 }
18 15
16 class Animation;
17
19 // AnimationDelegate 18 // AnimationDelegate
20 // 19 //
21 // Implement this interface when you want to receive notifications about the 20 // Implement this interface when you want to receive notifications about the
22 // state of an animation. 21 // state of an animation.
23 //
24 class AnimationDelegate { 22 class AnimationDelegate {
25 public: 23 public:
26 // Called when an animation has started.
27 virtual void AnimationStarted(const Animation* animation) {
28 }
29
30 // Called when an animation has completed. 24 // Called when an animation has completed.
31 virtual void AnimationEnded(const Animation* animation) { 25 virtual void AnimationEnded(const Animation* animation) {
32 } 26 }
33 27
34 // Called when an animation has progressed. 28 // Called when an animation has progressed.
35 virtual void AnimationProgressed(const Animation* animation) { 29 virtual void AnimationProgressed(const Animation* animation) {
36 } 30 }
37 31
38 // Called when an animation has been canceled. 32 // Called when an animation has been canceled.
39 virtual void AnimationCanceled(const Animation* animation) { 33 virtual void AnimationCanceled(const Animation* animation) {
40 } 34 }
41 35
42 protected: 36 protected:
43 virtual ~AnimationDelegate() {} 37 virtual ~AnimationDelegate() {}
44 }; 38 };
45 39
46 // Animation 40 // Base class used in implementing animations. You only need use this class if
41 // you're implementing a new animation type, otherwise you'll likely want one of
42 // LinearAnimation, SlideAnimation, ThrobAnimation or MultiAnimation.
47 // 43 //
48 // This class provides a basic implementation of an object that uses a timer 44 // To subclass override Step, which is invoked as the animation progresses and
49 // to increment its state over the specified time and frame-rate. To 45 // GetCurrentValue() to return the value appropriate to the animation.
50 // actually do something useful with this you need to subclass it and override 46 class Animation : public AnimationContainer::Element {
51 // AnimateToState and optionally GetCurrentValue to update your state.
52 //
53 // The Animation notifies a delegate when events of interest occur.
54 //
55 // The practice is to instantiate a subclass and call Init and any other
56 // initialization specific to the subclass, and then call |Start|. The
57 // animation uses the current thread's message loop.
58 //
59 class Animation {
60 public: 47 public:
61 // Initializes everything except the duration. 48 explicit Animation(base::TimeDelta timer_interval);
62 //
63 // Caller must make sure to call SetDuration() if they use this
64 // constructor; it is preferable to use the full one, but sometimes
65 // duration can change between calls to Start() and we need to
66 // expose this interface.
67 Animation(int frame_rate, AnimationDelegate* delegate);
68
69 // Initializes all fields.
70 Animation(int duration, int frame_rate, AnimationDelegate* delegate);
71 virtual ~Animation(); 49 virtual ~Animation();
72 50
73 // Called when the animation progresses. Subclasses override this to 51 // Starts the animation. Does nothing if the animation is already running.
74 // efficiently update their state. 52 void Start();
75 virtual void AnimateToState(double state) = 0; 53
54 // Stops the animation. Does nothing if the animation is not running.
55 void Stop();
76 56
77 // Gets the value for the current state, according to the animation 57 // Gets the value for the current state, according to the animation
78 // curve in use. This class provides only for a linear relationship, 58 // curve in use.
79 // however subclasses can override this to provide others. 59 virtual double GetCurrentValue() const = 0;
80 virtual double GetCurrentValue() const;
81 60
82 // Convenience for returning a value between |start| and |target| based on 61 // Convenience for returning a value between |start| and |target| based on
83 // the current value. This is (target - start) * GetCurrentValue() + start. 62 // the current value. This is (target - start) * GetCurrentValue() + start.
84 double CurrentValueBetween(double start, double target) const; 63 double CurrentValueBetween(double start, double target) const;
85 int CurrentValueBetween(int start, int target) const; 64 int CurrentValueBetween(int start, int target) const;
86 gfx::Rect CurrentValueBetween(const gfx::Rect& start_bounds, 65 gfx::Rect CurrentValueBetween(const gfx::Rect& start_bounds,
87 const gfx::Rect& target_bounds) const; 66 const gfx::Rect& target_bounds) const;
88 67
89 // Start the animation.
90 void Start();
91
92 // Stop the animation.
93 void Stop();
94
95 // Skip to the end of the current animation.
96 void End();
97
98 // Return whether this animation is animating.
99 bool IsAnimating() const;
100
101 // Changes the length of the animation. This resets the current
102 // state of the animation to the beginning.
103 void SetDuration(int duration);
104
105 // Returns true if rich animations should be rendered.
106 // Looks at session type (e.g. remote desktop) and accessibility settings
107 // to give guidance for heavy animations such as "start download" arrow.
108 static bool ShouldRenderRichAnimation();
109
110 // Sets the delegate. 68 // Sets the delegate.
111 void set_delegate(AnimationDelegate* delegate) { delegate_ = delegate; } 69 void set_delegate(AnimationDelegate* delegate) { delegate_ = delegate; }
112 70
113 // Sets the container used to manage the timer. A value of NULL results in 71 // Sets the container used to manage the timer. A value of NULL results in
114 // creating a new AnimationContainer. 72 // creating a new AnimationContainer.
115 void SetContainer(AnimationContainer* container); 73 void SetContainer(AnimationContainer* container);
116 74
75 bool is_animating() const { return is_animating_; }
76
117 base::TimeDelta timer_interval() const { return timer_interval_; } 77 base::TimeDelta timer_interval() const { return timer_interval_; }
118 78
79 // Returns true if rich animations should be rendered.
80 // Looks at session type (e.g. remote desktop) and accessibility settings
81 // to give guidance for heavy animations such as "start download" arrow.
82 static bool ShouldRenderRichAnimation();
83
119 protected: 84 protected:
120 // Invoked by the AnimationContainer when the animation is running to advance 85 // Invoked from Start to allow subclasses to prepare for the animation.
121 // the animation. Use |time_now| rather than Time::Now to avoid multiple 86 virtual void AnimationStarted() {}
122 // animations running at the same time diverging.
123 virtual void Step(base::TimeTicks time_now);
124 87
125 // Calculates the timer interval from the constructor list. 88 // Invoked from Stop after we're removed from the container but before the
126 base::TimeDelta CalculateInterval(int frame_rate); 89 // delegate has been invoked.
90 virtual void AnimationStopped() {}
91
92 // Invoked from stop to determine if cancel should be invoked. If this returns
93 // true the delegate is notified the animation was canceled, otherwise the
94 // delegate is notified the animation stopped.
95 virtual bool ShouldSendCanceledFromStop() { return false; }
96
97 AnimationContainer* container() { return container_.get(); }
98 base::TimeTicks start_time() const { return start_time_; }
99 AnimationDelegate* delegate() { return delegate_; }
100
101 // AnimationContainer::Element overrides
102 virtual void SetStartTime(base::TimeTicks start_time);
103 virtual void Step(base::TimeTicks time_now) = 0;
104 virtual base::TimeDelta GetTimerInterval() const { return timer_interval_; }
127 105
128 private: 106 private:
129 friend class AnimationContainer; 107 // Interval for the animation.
108 const base::TimeDelta timer_interval_;
130 109
131 // Invoked from AnimationContainer when started. 110 // If true we're running.
132 void set_start_time(base::TimeTicks start_time) { start_time_ = start_time; } 111 bool is_animating_;
133 112
134 // Whether or not we are currently animating. 113 // Our delegate; may be null.
135 bool animating_;
136
137 int frame_rate_;
138 base::TimeDelta timer_interval_;
139 base::TimeDelta duration_;
140
141 // Current state, on a scale from 0.0 to 1.0.
142 double state_;
143
144 base::TimeTicks start_time_;
145
146 AnimationDelegate* delegate_; 114 AnimationDelegate* delegate_;
147 115
116 // Container we're in. If non-null we're animating.
148 scoped_refptr<AnimationContainer> container_; 117 scoped_refptr<AnimationContainer> container_;
149 118
119 // Time we started at.
120 base::TimeTicks start_time_;
121
150 DISALLOW_COPY_AND_ASSIGN(Animation); 122 DISALLOW_COPY_AND_ASSIGN(Animation);
151 }; 123 };
152 124
153 #endif // APP_ANIMATION_H_ 125 #endif // APP_ANIMATION_H_
OLDNEW
« no previous file with comments | « no previous file | app/animation.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698