OLD | NEW |
---|---|
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 | 4 |
5 #ifndef VIEWS_ANIMATION_BOUNDS_ANIMATOR_H_ | 5 #ifndef VIEWS_ANIMATION_BOUNDS_ANIMATOR_H_ |
6 #define VIEWS_ANIMATION_BOUNDS_ANIMATOR_H_ | 6 #define VIEWS_ANIMATION_BOUNDS_ANIMATOR_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 | 9 |
10 #include "app/animation.h" | 10 #include "app/animation.h" |
11 #include "base/scoped_ptr.h" | 11 #include "app/animation_container.h" |
12 #include "gfx/rect.h" | 12 #include "gfx/rect.h" |
13 | 13 |
14 class SlideAnimation; | |
15 | |
14 namespace views { | 16 namespace views { |
15 | 17 |
18 class BoundsAnimator; | |
16 class View; | 19 class View; |
17 | 20 |
21 class BoundsAnimatorObserver { | |
22 public: | |
23 // Invoked when all animations are complete. | |
24 virtual void OnBoundsAnimatorDone(BoundsAnimator* animator) = 0; | |
25 }; | |
26 | |
18 // Bounds animator is responsible for animating the bounds of a view from the | 27 // Bounds animator is responsible for animating the bounds of a view from the |
19 // the views current location and size to a target position and size. To use | 28 // the views current location and size to a target position and size. To use |
20 // BoundsAnimator invoke AnimateViewTo for the set of views you want to | 29 // BoundsAnimator invoke AnimateViewTo for the set of views you want to |
21 // animate, followed by Start to start the animation. | 30 // animate. |
22 class BoundsAnimator : public AnimationDelegate { | 31 // |
32 // BoundsAnimator internally creates an animation for each view. If you need | |
33 // a specific animation invoke SetAnimationForView after invoking AnimateViewTo. | |
34 // You can attach an AnimationDelegate to the individual animation for a view | |
35 // by way of SetAnimationDelegate. Additionally you can attach an observer to | |
36 // the BoundsAnimator that is notified when all animations are complete. | |
37 class BoundsAnimator : public AnimationDelegate, | |
38 public AnimationContainer::Observer { | |
23 public: | 39 public: |
24 BoundsAnimator(); | 40 // If |delete_when_done| is set to true in |SetAnimationDelegate| the |
41 // |AnimationDelegate| must subclass this class. | |
42 class OwnedAnimationDelegate : public AnimationDelegate { | |
43 public: | |
44 virtual ~OwnedAnimationDelegate() {} | |
45 }; | |
46 | |
47 explicit BoundsAnimator(View* view); | |
25 ~BoundsAnimator(); | 48 ~BoundsAnimator(); |
26 | 49 |
27 // Schedules |view| to animate from it's current bounds to |target|. If | 50 // Starts animating |view| from its current bounds to |target|. If |
28 // |delete_when_done| is true the view is deleted when the animation | 51 // |delete_when_done| is true the view is deleted when the animation |
29 // completes. Invoke Start to start the animation. | 52 // completes. If there is already an animation running for the view its |
Jay Civelli
2010/04/01 21:32:20
I think this one should remain 'it's'
| |
53 // stopped and a new one started. | |
30 void AnimateViewTo(View* view, | 54 void AnimateViewTo(View* view, |
31 const gfx::Rect& target, | 55 const gfx::Rect& target, |
32 bool delete_when_done); | 56 bool delete_when_done); |
33 | 57 |
58 // Sets the animation for the specified view. BoundsAnimator takes ownership | |
59 // of the specified animation. | |
60 void SetAnimationForView(View* view, SlideAnimation* animation); | |
61 | |
62 // Returns the animation for the specified view. BoundsAnimator owns the | |
63 // returned Animation. | |
64 const SlideAnimation* GetAnimationForView(View* view); | |
65 | |
66 // Stops animating the specified view. If the view was scheduled for deletion | |
67 // it is deleted. | |
68 void StopAnimatingView(View* view); | |
69 | |
70 // Sets the delegate for the animation created for the specified view. If | |
71 // |delete_when_done| is true the |delegate| is deleted when done and | |
72 // |delegate| must subclass OwnedAnimationDelegate. | |
73 void SetAnimationDelegate(View* view, | |
74 AnimationDelegate* delegate, | |
75 bool delete_when_done); | |
76 | |
34 // Returns true if BoundsAnimator is animating the bounds of |view|. | 77 // Returns true if BoundsAnimator is animating the bounds of |view|. |
35 bool IsAnimating(View* view) const; | 78 bool IsAnimating(View* view) const; |
36 | 79 |
37 // Starts the animation. | 80 // Returns true if BoundsAnimator is animating any view. |
38 void Start(); | 81 bool IsAnimating() const; |
39 | 82 |
40 // Stops the animation. | 83 // Cancels all animations, leaving the views at their current location and |
41 void Stop(); | 84 // size. Any views marked for deletion are deleted. |
85 void Cancel(); | |
42 | 86 |
43 // Sets the animation to use when animating changes. BoundsAnimator takes | 87 void set_observer(BoundsAnimatorObserver* observer) { |
44 // ownership of |animation|. Set |is_slide| to true if |animation| is a | 88 observer_ = observer; |
45 // SlideAnimation. | 89 } |
46 void SetAnimation(Animation* animation, bool is_slide); | 90 |
91 private: | |
92 // Tracks data about the view being animated. | |
93 struct Data { | |
94 Data() | |
95 : delete_when_done(false), | |
96 delete_delegate_when_done(false), | |
97 animation(NULL), | |
98 delegate(NULL) {} | |
99 | |
100 // Should the view be deleted when done? | |
101 bool delete_when_done; | |
102 | |
103 // If true the delegate is deleted when done. | |
104 bool delete_delegate_when_done; | |
105 | |
106 // The initial bounds. | |
107 gfx::Rect start_bounds; | |
108 | |
109 // Target bounds. | |
110 gfx::Rect target_bounds; | |
111 | |
112 // The animation. We own this. | |
113 SlideAnimation* animation; | |
114 | |
115 // Additional delegate for the animation, may be null. | |
116 AnimationDelegate* delegate; | |
117 }; | |
118 | |
119 typedef std::map<View*, Data> ViewToDataMap; | |
120 | |
121 typedef std::map<const Animation*, View*> AnimationToViewMap; | |
122 | |
123 // Creates the animation to use for animating views. | |
124 SlideAnimation* CreateAnimation(); | |
125 | |
126 // Removes references to |view| and its animation as well as deleting |view| | |
127 // (if necessary). This does NOT delete the animation or delegate. | |
128 void RemoveFromMapsAndDelete(View* view); | |
129 | |
130 // Does the necessary cleanup for |data|. | |
131 void CleanupData(Data* data); | |
132 | |
133 // Used when changing the animation for a view. This resets the maps for | |
134 // the animation used by view and returns the current animation. Ownership | |
135 // of the returned animation passes to the caller. | |
136 Animation* ResetAnimationForView(View* view); | |
47 | 137 |
48 // AnimationDelegate overrides. | 138 // AnimationDelegate overrides. |
49 virtual void AnimationProgressed(const Animation* animation); | 139 virtual void AnimationProgressed(const Animation* animation); |
50 virtual void AnimationEnded(const Animation* animation); | 140 virtual void AnimationEnded(const Animation* animation); |
51 virtual void AnimationCanceled(const Animation* animation); | 141 virtual void AnimationCanceled(const Animation* animation); |
52 | 142 |
53 private: | 143 // AnimationContainer::Observer overrides. |
54 // Tracks data about the view being animated. | 144 virtual void AnimationContainerProgressed(AnimationContainer* container); |
55 struct Data { | 145 virtual void AnimationContainerEmpty(AnimationContainer* container); |
56 Data() : delete_when_done(false) {} | |
57 | 146 |
58 // Should the view be deleted when done? | 147 // Parent of all views being animated. |
59 bool delete_when_done; | 148 View* parent_; |
60 | 149 |
61 // The initial bounds. | 150 BoundsAnimatorObserver* observer_; |
62 gfx::Rect start_bounds; | |
63 | 151 |
64 // Target bounds. | 152 // All animations we create up with the same container. |
65 gfx::Rect target_bounds; | 153 scoped_refptr<AnimationContainer> container_; |
66 }; | |
67 | 154 |
68 typedef std::map<View*, Data> ViewToDataMap; | 155 // Maps from view being animated to info about the view. |
69 | |
70 // Empties data_, deleting any views that have been marked as needing to be | |
71 // deleted. | |
72 void DeleteViews(); | |
73 | |
74 // Mapes from view being animated to info about the view. | |
75 ViewToDataMap data_; | 156 ViewToDataMap data_; |
76 | 157 |
77 // The animation. | 158 // Makes from animation to view. |
78 scoped_ptr<Animation> animation_; | 159 AnimationToViewMap animation_to_view_; |
79 | 160 |
80 // Is |animation_| a SlideAnimation? | 161 // As the animations we created update (AnimationProgressed is invoked) this |
81 bool is_slide_; | 162 // is updated. When all the animations have completed for a given tick of |
163 // the timer (AnimationContainerProgressed is invoked) the parent_ is asked | |
164 // to repaint these bounds. | |
165 gfx::Rect repaint_bounds_; | |
82 | 166 |
83 DISALLOW_COPY_AND_ASSIGN(BoundsAnimator); | 167 DISALLOW_COPY_AND_ASSIGN(BoundsAnimator); |
84 }; | 168 }; |
85 | 169 |
86 } // namespace views | 170 } // namespace views |
87 | 171 |
88 #endif // VIEWS_ANIMATION_BOUNDS_ANIMATOR_H_ | 172 #endif // VIEWS_ANIMATION_BOUNDS_ANIMATOR_H_ |
OLD | NEW |